1
0
mirror of git://git.suckless.org/sbase synced 2024-12-25 00:12:42 +00:00
sbase/chgrp.c

71 lines
1.1 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
#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>
#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;
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");
}
static void
chgrp(const char *path)
2013-06-09 13:20:55 +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++;
}
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;
if(argc < 2)
2013-06-09 13:20:55 +00:00
usage();
errno = 0;
2013-06-09 13:20:55 +00:00
gr = getgrnam(argv[0]);
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;
}
chgrp(*argv);
2013-06-09 13:20:55 +00:00
}
return failures;
}