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:
parent
274e86e1aa
commit
8dc92fbd6c
27
chgrp.c
27
chgrp.c
@ -10,20 +10,19 @@
|
|||||||
static int gid;
|
static int gid;
|
||||||
static int status;
|
static int status;
|
||||||
static int Rflag;
|
static int Rflag;
|
||||||
static int fflag = 'P';
|
|
||||||
static struct stat st;
|
static struct stat st;
|
||||||
static char *chown_f_name = "chown";
|
static char *chownf_name = "chown";
|
||||||
static int (*chown_f)(const char *, uid_t, gid_t) = chown;
|
static int (*chownf)(const char *, uid_t, gid_t) = chown;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
chgrp(const char *path, int fflag)
|
chgrp(const char *path, int depth)
|
||||||
{
|
{
|
||||||
if (chown_f(path, st.st_uid, gid) < 0) {
|
if (chownf(path, st.st_uid, gid) < 0) {
|
||||||
weprintf("%s %s:", chown_f_name, path);
|
weprintf("%s %s:", chownf_name, path);
|
||||||
status = 1;
|
status = 1;
|
||||||
}
|
}
|
||||||
if (Rflag)
|
if (Rflag)
|
||||||
recurse(path, chgrp, fflag);
|
recurse(path, chgrp, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -39,8 +38,8 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'h':
|
case 'h':
|
||||||
chown_f_name = "lchown";
|
chownf_name = "lchown";
|
||||||
chown_f = lchown;
|
chownf = lchown;
|
||||||
break;
|
break;
|
||||||
case 'R':
|
case 'R':
|
||||||
Rflag = 1;
|
Rflag = 1;
|
||||||
@ -48,14 +47,18 @@ main(int argc, char *argv[])
|
|||||||
case 'H':
|
case 'H':
|
||||||
case 'L':
|
case 'L':
|
||||||
case 'P':
|
case 'P':
|
||||||
fflag = ARGC();
|
recurse_follow = ARGC();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc < 2 || (chown_f == lchown && Rflag))
|
if (argc < 2)
|
||||||
usage();
|
usage();
|
||||||
|
if (recurse_follow == 'P') {
|
||||||
|
chownf_name = "lchown";
|
||||||
|
chownf = lchown;
|
||||||
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
gr = getgrnam(argv[0]);
|
gr = getgrnam(argv[0]);
|
||||||
@ -73,7 +76,7 @@ main(int argc, char *argv[])
|
|||||||
status = 1;
|
status = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
chgrp(*argv, fflag);
|
chgrp(*argv, 0);
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
9
chmod.c
9
chmod.c
@ -4,13 +4,12 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int Rflag = 0;
|
static int Rflag = 0;
|
||||||
static int fflag = 'P';
|
|
||||||
static char *modestr = "";
|
static char *modestr = "";
|
||||||
static mode_t mask = 0;
|
static mode_t mask = 0;
|
||||||
static int ret = 0;
|
static int ret = 0;
|
||||||
|
|
||||||
void
|
void
|
||||||
chmodr(const char *path, int fflag)
|
chmodr(const char *path, int depth)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
mode_t m;
|
mode_t m;
|
||||||
@ -27,7 +26,7 @@ chmodr(const char *path, int fflag)
|
|||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
if (Rflag)
|
if (Rflag)
|
||||||
recurse(path, chmodr, fflag);
|
recurse(path, chmodr, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -50,7 +49,7 @@ main(int argc, char *argv[])
|
|||||||
case 'H':
|
case 'H':
|
||||||
case 'L':
|
case 'L':
|
||||||
case 'P':
|
case 'P':
|
||||||
fflag = argv[i][1];
|
recurse_follow = argv[i][1];
|
||||||
break;
|
break;
|
||||||
case 'r': case 'w': case 'x': case 's': case 't':
|
case 'r': case 'w': case 'x': case 's': case 't':
|
||||||
/*
|
/*
|
||||||
@ -71,7 +70,7 @@ done:
|
|||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (++i; i < argc; i++)
|
for (++i; i < argc; i++)
|
||||||
chmodr(argv[i], fflag);
|
chmodr(argv[i], 0);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
25
chown.c
25
chown.c
@ -9,22 +9,21 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int rflag = 0;
|
static int rflag = 0;
|
||||||
static int HLPflag = 'P';
|
|
||||||
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 char *chown_f_name = "chown";
|
static char *chownf_name = "chown";
|
||||||
static int (*chown_f)(const char *, uid_t, gid_t) = chown;
|
static int (*chownf)(const char *, uid_t, gid_t) = chown;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
chownpwgr(const char *path, int HLPflag)
|
chownpwgr(const char *path, int depth)
|
||||||
{
|
{
|
||||||
if (chown_f(path, uid, gid) < 0) {
|
if (chownf(path, uid, gid) < 0) {
|
||||||
weprintf("%s %s:", chown_f_name, path);
|
weprintf("%s %s:", chownf_name, path);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
if (rflag)
|
if (rflag)
|
||||||
recurse(path, chownpwgr, HLPflag);
|
recurse(path, chownpwgr, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -42,8 +41,8 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'h':
|
case 'h':
|
||||||
chown_f_name = "lchown";
|
chownf_name = "lchown";
|
||||||
chown_f = lchown;
|
chownf = lchown;
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
case 'R':
|
case 'R':
|
||||||
@ -52,7 +51,7 @@ main(int argc, char *argv[])
|
|||||||
case 'H':
|
case 'H':
|
||||||
case 'L':
|
case 'L':
|
||||||
case 'P':
|
case 'P':
|
||||||
HLPflag = ARGC();
|
recurse_follow = ARGC();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
@ -60,6 +59,10 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (argc == 0)
|
if (argc == 0)
|
||||||
usage();
|
usage();
|
||||||
|
if (recurse_follow == 'P') {
|
||||||
|
chownf_name = "lchown";
|
||||||
|
chownf = lchown;
|
||||||
|
}
|
||||||
|
|
||||||
owner = argv[0];
|
owner = argv[0];
|
||||||
argv++;
|
argv++;
|
||||||
@ -94,7 +97,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (; argc > 0; argc--, argv++)
|
for (; argc > 0; argc--, argv++)
|
||||||
chownpwgr(argv[0], HLPflag);
|
chownpwgr(argv[0], 0);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
2
cp.c
2
cp.c
@ -51,7 +51,7 @@ main(int argc, char *argv[])
|
|||||||
if (!S_ISDIR(st.st_mode))
|
if (!S_ISDIR(st.st_mode))
|
||||||
eprintf("%s: not a directory\n", argv[argc - 1]);
|
eprintf("%s: not a directory\n", argv[argc - 1]);
|
||||||
}
|
}
|
||||||
enmasse(argc, argv, cp, cp_HLPflag);
|
enmasse(argc, argv, cp);
|
||||||
|
|
||||||
return cp_status;
|
return cp_status;
|
||||||
}
|
}
|
||||||
|
2
fs.h
2
fs.h
@ -11,5 +11,5 @@ extern int rm_fflag;
|
|||||||
extern int rm_rflag;
|
extern int rm_rflag;
|
||||||
extern int rm_status;
|
extern int rm_status;
|
||||||
|
|
||||||
int cp(const char *, const char *, char);
|
int cp(const char *, const char *, int);
|
||||||
void rm(const char *, int);
|
void rm(const char *, int);
|
||||||
|
10
libutil/cp.c
10
libutil/cp.c
@ -24,7 +24,7 @@ int cp_status = 0;
|
|||||||
int cp_HLPflag = 'L';
|
int cp_HLPflag = 'L';
|
||||||
|
|
||||||
int
|
int
|
||||||
cp(const char *s1, const char *s2, char ff)
|
cp(const char *s1, const char *s2, int depth)
|
||||||
{
|
{
|
||||||
FILE *f1, *f2;
|
FILE *f1, *f2;
|
||||||
char *ns1, *ns2;
|
char *ns1, *ns2;
|
||||||
@ -39,9 +39,11 @@ cp(const char *s1, const char *s2, char ff)
|
|||||||
if (cp_vflag)
|
if (cp_vflag)
|
||||||
printf("'%s' -> '%s'\n", s1, s2);
|
printf("'%s' -> '%s'\n", s1, s2);
|
||||||
|
|
||||||
r = ff == 'P' ? lstat(s1, &st) : stat(s1, &st);
|
r = (cp_HLPflag == 'P' || (cp_HLPflag == 'H' && depth > 0)) ?
|
||||||
|
lstat(s1, &st) : stat(s1, &st);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
weprintf("%s %s:", ff == 'P' ? "lstat" : "stat", s1);
|
weprintf("%s %s:", (cp_HLPflag == 'P' ||
|
||||||
|
(cp_HLPflag == 'H' && depth > 0)) ? "lstat" : "stat", s1);
|
||||||
cp_status = 1;
|
cp_status = 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -83,7 +85,7 @@ cp(const char *s1, const char *s2, char ff)
|
|||||||
eprintf("%s/%s: filename too long\n",
|
eprintf("%s/%s: filename too long\n",
|
||||||
s2, d->d_name);
|
s2, d->d_name);
|
||||||
}
|
}
|
||||||
fnck(ns1, ns2, cp, ff == 'H' ? 'P' : ff);
|
fnck(ns1, ns2, cp, depth + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), char ff)
|
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int))
|
||||||
{
|
{
|
||||||
char *buf, *dir;
|
char *buf, *dir;
|
||||||
int i, len;
|
int i, len;
|
||||||
@ -18,7 +18,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), cha
|
|||||||
size_t dlen;
|
size_t dlen;
|
||||||
|
|
||||||
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
|
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
|
||||||
fnck(argv[0], argv[1], fn, ff);
|
fnck(argv[0], argv[1], fn, 0);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
dir = (argc == 1) ? "." : argv[--argc];
|
dir = (argc == 1) ? "." : argv[--argc];
|
||||||
@ -35,7 +35,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), cha
|
|||||||
eprintf("%s/%s: filename too long\n", dir,
|
eprintf("%s/%s: filename too long\n", dir,
|
||||||
basename(argv[i]));
|
basename(argv[i]));
|
||||||
}
|
}
|
||||||
fnck(argv[i], buf, fn, ff);
|
fnck(argv[i], buf, fn, 0);
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
void
|
void
|
||||||
fnck(const char *a, const char *b,
|
fnck(const char *a, const char *b,
|
||||||
int (*fn)(const char *, const char *, char), char ff)
|
int (*fn)(const char *, const char *, int), int depth)
|
||||||
{
|
{
|
||||||
struct stat sta, stb;
|
struct stat sta, stb;
|
||||||
|
|
||||||
@ -16,6 +16,6 @@ fnck(const char *a, const char *b,
|
|||||||
eprintf("%s -> %s: same file\n", a, b);
|
eprintf("%s -> %s: same file\n", a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fn(a, b, ff) < 0)
|
if (fn(a, b, depth) < 0)
|
||||||
eprintf("%s -> %s:", a, b);
|
eprintf("%s -> %s:", a, b);
|
||||||
}
|
}
|
||||||
|
@ -10,25 +10,29 @@
|
|||||||
|
|
||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
|
|
||||||
|
int recurse_follow = 'P';
|
||||||
|
|
||||||
void
|
void
|
||||||
recurse(const char *path, void (*fn)(const char *, int), int follow)
|
recurse(const char *path, void (*fn)(const char *, int), int depth)
|
||||||
{
|
{
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
struct dirent *d;
|
struct dirent *d;
|
||||||
struct stat lst, st;
|
struct stat lst, st;
|
||||||
DIR *dp;
|
DIR *dp;
|
||||||
|
|
||||||
if (lstat(path, &lst) < 0 || stat(path, &st) < 0 ||
|
if (lstat(path, &lst) < 0)
|
||||||
!(S_ISDIR(lst.st_mode) ||
|
eprintf("lstat %s:", path);
|
||||||
(follow != 'P' && S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode))))
|
if (stat(path, &st) < 0)
|
||||||
|
eprintf("stat %s:", path);
|
||||||
|
if (!S_ISDIR(lst.st_mode) && !(S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode) &&
|
||||||
|
!(recurse_follow == 'P' || (recurse_follow == 'H' && depth > 0))))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!(dp = opendir(path)))
|
if (!(dp = opendir(path)))
|
||||||
eprintf("opendir %s:", path);
|
eprintf("opendir %s:", path);
|
||||||
|
|
||||||
while ((d = readdir(dp))) {
|
while ((d = readdir(dp))) {
|
||||||
if (strcmp(d->d_name, ".") == 0 ||
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||||
strcmp(d->d_name, "..") == 0)
|
|
||||||
continue;
|
continue;
|
||||||
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
|
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
@ -37,7 +41,7 @@ recurse(const char *path, void (*fn)(const char *, int), int follow)
|
|||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
|
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
fn(buf, follow == 'H' ? 'P' : follow);
|
fn(buf, depth + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
@ -10,10 +10,10 @@ int rm_rflag = 0;
|
|||||||
int rm_status = 0;
|
int rm_status = 0;
|
||||||
|
|
||||||
void
|
void
|
||||||
rm(const char *path, int flag)
|
rm(const char *path, int depth)
|
||||||
{
|
{
|
||||||
if (rm_rflag)
|
if (rm_rflag)
|
||||||
recurse(path, rm, 'P');
|
recurse(path, rm, depth);
|
||||||
if (remove(path) < 0) {
|
if (remove(path) < 0) {
|
||||||
if (!rm_fflag)
|
if (!rm_fflag)
|
||||||
weprintf("remove %s:", path);
|
weprintf("remove %s:", path);
|
||||||
|
7
mv.c
7
mv.c
@ -10,14 +10,15 @@
|
|||||||
static int mv_status = 0;
|
static int mv_status = 0;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
mv(const char *s1, const char *s2, char ff)
|
mv(const char *s1, const char *s2, int depth)
|
||||||
{
|
{
|
||||||
if (rename(s1, s2) == 0)
|
if (rename(s1, s2) == 0)
|
||||||
return (mv_status = 0);
|
return (mv_status = 0);
|
||||||
if (errno == EXDEV) {
|
if (errno == EXDEV) {
|
||||||
cp_aflag = cp_rflag = cp_pflag = 1;
|
cp_aflag = cp_rflag = cp_pflag = 1;
|
||||||
|
cp_HLPflag = 'P';
|
||||||
rm_rflag = 1;
|
rm_rflag = 1;
|
||||||
cp(s1, s2, ff);
|
cp(s1, s2, depth);
|
||||||
rm(s1, 0);
|
rm(s1, 0);
|
||||||
return (mv_status = cp_status || rm_status);
|
return (mv_status = cp_status || rm_status);
|
||||||
}
|
}
|
||||||
@ -48,7 +49,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
|
if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
|
||||||
eprintf("%s: not a directory\n", argv[argc-1]);
|
eprintf("%s: not a directory\n", argv[argc-1]);
|
||||||
enmasse(argc, &argv[0], mv, 'P');
|
enmasse(argc, argv, mv);
|
||||||
|
|
||||||
return mv_status;
|
return mv_status;
|
||||||
}
|
}
|
||||||
|
9
tar.c
9
tar.c
@ -42,7 +42,6 @@ static ino_t tarinode;
|
|||||||
static dev_t tardev;
|
static dev_t tardev;
|
||||||
|
|
||||||
static int mflag;
|
static int mflag;
|
||||||
static int fflag = 'P';
|
|
||||||
static char filtermode;
|
static char filtermode;
|
||||||
|
|
||||||
static FILE *
|
static FILE *
|
||||||
@ -234,10 +233,10 @@ print(char * fname, int l, char b[BLKSIZ])
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
c(const char * path, int fflag)
|
c(const char *path, int depth)
|
||||||
{
|
{
|
||||||
archive(path);
|
archive(path);
|
||||||
recurse(path, c, fflag);
|
recurse(path, c, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -293,7 +292,7 @@ main(int argc, char *argv[])
|
|||||||
filtermode = ARGC();
|
filtermode = ARGC();
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
fflag = 'L';
|
recurse_follow = 'L';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
@ -316,7 +315,7 @@ main(int argc, char *argv[])
|
|||||||
tarfile = stdout;
|
tarfile = stdout;
|
||||||
}
|
}
|
||||||
chdir(dir);
|
chdir(dir);
|
||||||
c(argv[0], fflag);
|
c(argv[0], 0);
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
case 'x':
|
case 'x':
|
||||||
|
5
util.h
5
util.h
@ -56,12 +56,13 @@ int enregcomp(int, regex_t *, const char *, int);
|
|||||||
int eregcomp(regex_t *, const char *, int);
|
int eregcomp(regex_t *, const char *, int);
|
||||||
|
|
||||||
/* misc */
|
/* misc */
|
||||||
void enmasse(int, char **, int (*)(const char *, const char *, char), char);
|
void enmasse(int, char **, int (*)(const char *, const char *, int));
|
||||||
void fnck(const char *, const char *, int (*)(const char *, const char *, char), char);
|
void fnck(const char *, const char *, int (*)(const char *, const char *, int), int);
|
||||||
mode_t getumask(void);
|
mode_t getumask(void);
|
||||||
char *humansize(double);
|
char *humansize(double);
|
||||||
mode_t parsemode(const char *, mode_t, mode_t);
|
mode_t parsemode(const char *, mode_t, mode_t);
|
||||||
void putword(const char *);
|
void putword(const char *);
|
||||||
|
extern int recurse_follow;
|
||||||
void recurse(const char *, void (*)(const char *, int), int);
|
void recurse(const char *, void (*)(const char *, int), int);
|
||||||
#undef strtonum
|
#undef strtonum
|
||||||
long long strtonum(const char *, long long, long long, const char **);
|
long long strtonum(const char *, long long, long long, const char **);
|
||||||
|
Loading…
Reference in New Issue
Block a user