sbase/chgrp.c
sin af8e38f5fa Fix some error messages
There's many more to go.
2014-11-17 16:22:01 +00:00

71 lines
1.1 KiB
C

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