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

This commit is contained in:
FRIGN 2015-02-12 21:56:06 +01:00
parent ab9b240dc6
commit c965539b66
2 changed files with 33 additions and 25 deletions

22
chgrp.c
View File

@ -14,30 +14,36 @@ static int gid;
static int status; static int status;
static int rflag; static int rflag;
static struct stat st; static struct stat st;
static char *chown_f_name = "chown";
static void static int (*chown_f)(const char *, uid_t, gid_t) = chown;
usage(void)
{
eprintf("usage: chgrp [-R] groupname file...\n");
}
static void static void
chgrp(const char *path) chgrp(const char *path)
{ {
if (chown(path, st.st_uid, gid) < 0) { if (chown_f(path, st.st_uid, gid) < 0) {
weprintf("chown %s:", path); weprintf("%s %s:", chown_f_name, path);
status = 1; status = 1;
} }
if (rflag) if (rflag)
recurse(path, chgrp); recurse(path, chgrp);
} }
static void
usage(void)
{
eprintf("usage: chgrp [-hR] groupname file...\n");
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct group *gr; struct group *gr;
ARGBEGIN { ARGBEGIN {
case 'h':
chown_f_name = "lchown";
chown_f = lchown;
break;
case 'R': case 'R':
rflag = 1; rflag = 1;
break; break;

30
chown.c
View File

@ -8,17 +8,27 @@
#include "util.h" #include "util.h"
static void chownpwgr(const char *);
static int rflag = 0; static int rflag = 0;
static uid_t uid = -1; static uid_t uid = -1;
static gid_t gid = -1; static gid_t gid = -1;
static int ret = 0; static int ret = 0;
static int (*chown_func)(const char *, uid_t, gid_t) = chown;
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 static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-Rr] [owner][:[group]] file...\n", argv0); eprintf("usage: %s [-hRr] [owner][:[group]] file...\n", argv0);
} }
int int
@ -29,6 +39,9 @@ main(int argc, char *argv[])
struct group *gr; struct group *gr;
ARGBEGIN { ARGBEGIN {
case 'h':
chown_func = lchown;
break;
case 'R': case 'R':
case 'r': case 'r':
rflag = 1; rflag = 1;
@ -77,14 +90,3 @@ main(int argc, char *argv[])
return ret; return ret;
} }
void
chownpwgr(const char *path)
{
if (chown(path, uid, gid) < 0) {
weprintf("chown %s:", path);
ret = 1;
}
if (rflag)
recurse(path, chownpwgr);
}