From 9eb15ff2326a5114644521cb406e3729bddb94d8 Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 8 Oct 2013 00:45:25 +0100 Subject: [PATCH] Simplify code and don't use ftw() for chgrp(1) Fix issue with uninitialized struct stat buffer as well. --- chgrp.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/chgrp.c b/chgrp.c index 2035a07..5658f21 100644 --- a/chgrp.c +++ b/chgrp.c @@ -1,16 +1,18 @@ /* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include #include "util.h" static int gid; static int failures = 0; +static int rflag = 0; +static struct stat st; static void usage(void) @@ -18,25 +20,21 @@ usage(void) eprintf("usage: chgrp [-R] groupname file...\n"); } -static int -chgrp(const char *path, const struct stat *st, int f) +static void +chgrp(const char *path) { - (void)f; - - if(chown(path, st->st_uid, gid) == -1) { + if(chown(path, st.st_uid, gid) == -1) { fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno)); failures++; } - - return 0; + if (rflag) + recurse(path, chgrp); } int main(int argc, char **argv) { - int rflag = 0; struct group *gr; - struct stat st; ARGBEGIN { case 'R': @@ -45,20 +43,18 @@ main(int argc, char **argv) default: usage(); } ARGEND; - if(argc<2) + + if(argc < 2) usage(); + errno = 0; gr = getgrnam(argv[0]); - if(!gr) + if (errno) + eprintf("getgrnam %s:"); + else if(!gr) eprintf("chgrp: '%s': No such group\n", argv[0]); gid = gr->gr_gid; - if(rflag) { - while(*++argv) - ftw(*argv, chgrp, FOPEN_MAX); - - return EXIT_SUCCESS; - } while(*++argv) { if(stat(*argv, &st) == -1) { fprintf(stderr, "chgrp: '%s': %s\n", *argv, @@ -66,7 +62,7 @@ main(int argc, char **argv) failures++; continue; } - chgrp(*argv, &st, 0); + chgrp(*argv); } return failures;