Refactor enmasse() and recurse() to reflect depth

The HLP-changes to sbase have been a great addition of functionality,
but they kind of "polluted" the enmasse() and recurse() prototypes.
As this will come in handy in the future, knowing at which "depth"
you are inside a recursing function is an important functionality.

Instead of having a special HLP-flag passed to enmasse, each sub-
function needs to provide it on its own and can calculate results
based on the current depth (for instance, 'H' implies 'P' at
depth > 0).
A special case is recurse(), because it actually depends on the
follow-type. A new flag "recurse_follow" brings consistency into
what used to be spread across different naming conventions (fflag,
HLP_flag, ...).

This also fixes numerous bugs with the behaviour of HLP in the
tools using it.
This commit is contained in:
FRIGN
2015-03-02 21:43:56 +01:00
parent 274e86e1aa
commit 8dc92fbd6c
13 changed files with 71 additions and 59 deletions

27
chgrp.c
View File

@@ -10,20 +10,19 @@
static int gid;
static int status;
static int Rflag;
static int fflag = 'P';
static struct stat st;
static char *chown_f_name = "chown";
static int (*chown_f)(const char *, uid_t, gid_t) = chown;
static char *chownf_name = "chown";
static int (*chownf)(const char *, uid_t, gid_t) = chown;
static void
chgrp(const char *path, int fflag)
chgrp(const char *path, int depth)
{
if (chown_f(path, st.st_uid, gid) < 0) {
weprintf("%s %s:", chown_f_name, path);
if (chownf(path, st.st_uid, gid) < 0) {
weprintf("%s %s:", chownf_name, path);
status = 1;
}
if (Rflag)
recurse(path, chgrp, fflag);
recurse(path, chgrp, depth);
}
static void
@@ -39,8 +38,8 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'h':
chown_f_name = "lchown";
chown_f = lchown;
chownf_name = "lchown";
chownf = lchown;
break;
case 'R':
Rflag = 1;
@@ -48,14 +47,18 @@ main(int argc, char *argv[])
case 'H':
case 'L':
case 'P':
fflag = ARGC();
recurse_follow = ARGC();
break;
default:
usage();
} ARGEND;
if (argc < 2 || (chown_f == lchown && Rflag))
if (argc < 2)
usage();
if (recurse_follow == 'P') {
chownf_name = "lchown";
chownf = lchown;
}
errno = 0;
gr = getgrnam(argv[0]);
@@ -73,7 +76,7 @@ main(int argc, char *argv[])
status = 1;
continue;
}
chgrp(*argv, fflag);
chgrp(*argv, 0);
}
return status;
}