commit 9eb15ff2326a5114644521cb406e3729bddb94d8
parent 8cd24f052582b3f2ad2b85b06dbd2e9ed21ded83
Author: sin <sin@2f30.org>
Date: Tue, 8 Oct 2013 00:45:25 +0100
Simplify code and don't use ftw() for chgrp(1)
Fix issue with uninitialized struct stat buffer as well.
Diffstat:
M | chgrp.c | | | 42 | +++++++++++++++++++----------------------- |
1 file changed, 19 insertions(+), 23 deletions(-)
diff --git a/chgrp.c b/chgrp.c
@@ -1,16 +1,18 @@
/* See LICENSE file for copyright and license details. */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
-#include <ftw.h>
#include <errno.h>
#include <grp.h>
-#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#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;