commit c965539b66439c9abea0ab5c617f93156b1a60da
parent ab9b240dc6ea006af397a980dc33cf3d4ed3cd34
Author: FRIGN <dev@frign.de>
Date: Thu, 12 Feb 2015 21:56:06 +0100
Add h-flag to chown(1) and chgrp(1)
Diffstat:
M | chgrp.c | | | 22 | ++++++++++++++-------- |
M | chown.c | | | 36 | +++++++++++++++++++----------------- |
2 files changed, 33 insertions(+), 25 deletions(-)
diff --git a/chgrp.c b/chgrp.c
@@ -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
@@ -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
@@ -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;
@@ -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);
-}