[hackers] [sbase] Add h-flag to chown(1) and chgrp(1) || FRIGN

From: <git_AT_suckless.org>
Date: Fri, 13 Feb 2015 12:22:59 +0100 (CET)

commit 53ab9167a8b022475a61052d2b10db0b2206fdf8
Author: FRIGN <dev_AT_frign.de>
Date: Thu Feb 12 21:56:06 2015 +0100

    Add h-flag to chown(1) and chgrp(1)

diff --git a/chgrp.c b/chgrp.c
index 52ad1f0..75e22d2 100644
--- a/chgrp.c
+++ b/chgrp.c
_AT_@ -14,30 +14,36 @@ static int gid;
 static int status;
 static int rflag;
 static struct stat st;
-
-static void
-usage(void)
-{
- eprintf("usage: chgrp [-R] groupname file...\n");
-}
+static char *chown_f_name = "chown";
+static int (*chown_f)(const char *, uid_t, gid_t) = chown;
 
 static void
 chgrp(const char *path)
 {
- if (chown(path, st.st_uid, gid) < 0) {
- weprintf("chown %s:", path);
+ if (chown_f(path, st.st_uid, gid) < 0) {
+ weprintf("%s %s:", chown_f_name, path);
                 status = 1;
         }
         if (rflag)
                 recurse(path, chgrp);
 }
 
+static void
+usage(void)
+{
+ eprintf("usage: chgrp [-hR] groupname file...\n");
+}
+
 int
 main(int argc, char *argv[])
 {
         struct group *gr;
 
         ARGBEGIN {
+ case 'h':
+ chown_f_name = "lchown";
+ chown_f = lchown;
+ break;
         case 'R':
                 rflag = 1;
                 break;
diff --git a/chown.c b/chown.c
index 5b6178b..f965b95 100644
--- a/chown.c
+++ b/chown.c
_AT_@ -8,17 +8,27 @@
 
 #include "util.h"
 
-static void chownpwgr(const char *);
+static int rflag = 0;
+static uid_t uid = -1;
+static gid_t gid = -1;
+static int ret = 0;
+static int (*chown_func)(const char *, uid_t, gid_t) = chown;
 
-static int rflag = 0;
-static uid_t uid = -1;
-static gid_t gid = -1;
-static int ret = 0;
+static void
+chownpwgr(const char *path)
+{
+ if (chown_func(path, uid, gid) < 0) {
+ weprintf("chown %s:", path);
+ ret = 1;
+ }
+ if (rflag)
+ recurse(path, chownpwgr);
+}
 
 static void
 usage(void)
 {
- eprintf("usage: %s [-Rr] [owner][:[group]] file...\n", argv0);
+ eprintf("usage: %s [-hRr] [owner][:[group]] file...\n", argv0);
 }
 
 int
_AT_@ -29,6 +39,9 @@ main(int argc, char *argv[])
         struct group *gr;
 
         ARGBEGIN {
+ case 'h':
+ chown_func = lchown;
+ break;
         case 'R':
         case 'r':
                 rflag = 1;
_AT_@ -77,14 +90,3 @@ main(int argc, char *argv[])
 
         return ret;
 }
-
-void
-chownpwgr(const char *path)
-{
- if (chown(path, uid, gid) < 0) {
- weprintf("chown %s:", path);
- ret = 1;
- }
- if (rflag)
- recurse(path, chownpwgr);
-}
Received on Fri Feb 13 2015 - 12:22:59 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 13 2015 - 12:24:09 CET