2013-10-07 16:03:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2013-10-07 23:45:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2013-06-09 13:20:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
2013-10-07 23:45:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-06-09 13:20:55 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-10-07 15:12:09 +00:00
|
|
|
static int gid;
|
|
|
|
static int failures = 0;
|
2013-10-07 23:45:25 +00:00
|
|
|
static int rflag = 0;
|
|
|
|
static struct stat st;
|
2013-06-09 13:20:55 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: chgrp [-R] groupname file...\n");
|
|
|
|
}
|
|
|
|
|
2013-10-07 23:45:25 +00:00
|
|
|
static void
|
|
|
|
chgrp(const char *path)
|
2013-06-09 13:20:55 +00:00
|
|
|
{
|
2013-10-07 23:45:25 +00:00
|
|
|
if(chown(path, st.st_uid, gid) == -1) {
|
2013-06-09 13:20:55 +00:00
|
|
|
fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno));
|
|
|
|
failures++;
|
|
|
|
}
|
2013-10-07 23:45:25 +00:00
|
|
|
if (rflag)
|
|
|
|
recurse(path, chgrp);
|
2013-06-09 13:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'R':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-10-07 23:45:25 +00:00
|
|
|
|
|
|
|
if(argc < 2)
|
2013-06-09 13:20:55 +00:00
|
|
|
usage();
|
|
|
|
|
2013-10-07 23:45:25 +00:00
|
|
|
errno = 0;
|
2013-06-09 13:20:55 +00:00
|
|
|
gr = getgrnam(argv[0]);
|
2013-10-07 23:45:25 +00:00
|
|
|
if (errno)
|
|
|
|
eprintf("getgrnam %s:");
|
|
|
|
else if(!gr)
|
2013-06-09 13:20:55 +00:00
|
|
|
eprintf("chgrp: '%s': No such group\n", argv[0]);
|
|
|
|
gid = gr->gr_gid;
|
|
|
|
|
|
|
|
while(*++argv) {
|
|
|
|
if(stat(*argv, &st) == -1) {
|
|
|
|
fprintf(stderr, "chgrp: '%s': %s\n", *argv,
|
|
|
|
strerror(errno));
|
|
|
|
failures++;
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-07 23:45:25 +00:00
|
|
|
chgrp(*argv);
|
2013-06-09 13:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
|