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 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;

36
chown.c
View File

@ -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);
}