[hackers] [sbase] Simplify code and don't use ftw() for chgrp(1) || sin

From: <git_AT_suckless.org>
Date: Tue, 08 Oct 2013 01:50:29 +0200

commit 9eb15ff2326a5114644521cb406e3729bddb94d8
Author: sin <sin_AT_2f30.org>
Date: Tue Oct 8 00:45:25 2013 +0100

    Simplify code and don't use ftw() for chgrp(1)
    
    Fix issue with uninitialized struct stat buffer as well.

diff --git a/chgrp.c b/chgrp.c
index 2035a07..5658f21 100644
--- a/chgrp.c
+++ b/chgrp.c
_AT_@ -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)
_AT_@ -18,25 +20,21 @@ usage(void)
         eprintf("usage: chgrp [-R] groupname file...
");
 }
 
-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
", 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':
_AT_@ -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
", 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
", *argv,
_AT_@ -66,7 +62,7 @@ main(int argc, char **argv)
                         failures++;
                         continue;
                 }
- chgrp(*argv, &st, 0);
+ chgrp(*argv);
         }
 
         return failures;
Received on Tue Oct 08 2013 - 01:50:29 CEST

This archive was generated by hypermail 2.3.0 : Tue Oct 08 2013 - 02:00:20 CEST