scrut(1): replace do/while loop
This commit is contained in:
parent
9086bf0d08
commit
19eee6b4e5
100
src/scrut.c
100
src/scrut.c
@ -26,87 +26,65 @@
|
||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||
* S_ISUID, S_ISVTX */
|
||||
|
||||
static char args[] = "bcdefghkprsuwxLS";
|
||||
static char ops[(sizeof args) / (sizeof *args)];
|
||||
static char *program_name = "scrut";
|
||||
char *program_name = "scrut";
|
||||
static char args[] = "bcdefgkprsuwxLS";
|
||||
|
||||
int usage(char *s) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"Usage: %s [-%s] file...\n",
|
||||
s == NULL ? program_name : s, args
|
||||
);
|
||||
|
||||
fprintf(stderr, "Usage: %s [-%s] file...\n", s, args);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct stat buf;
|
||||
int c;
|
||||
size_t i;
|
||||
char sel[(sizeof args) / (sizeof *args)];
|
||||
|
||||
if (argc < 2) { return usage(argv[0] == NULL ? program_name : argv[0]); }
|
||||
|
||||
{ /* option parsing */
|
||||
char *p;
|
||||
|
||||
if (argc < 2) { return usage(argv[0]); }
|
||||
|
||||
memset(ops, '\0', sizeof ops);
|
||||
while ((c = getopt(argc, argv, args)) != -1) {
|
||||
if ((p = strchr(args, c)) == NULL) {
|
||||
return usage(argv[0]);
|
||||
} else {
|
||||
ops[p - args] = c;
|
||||
}
|
||||
memset(sel, '\0', sizeof sel);
|
||||
for (int c; (c = getopt(argc, argv, args)) != -1;) {
|
||||
if ((p = strchr(args, c)) == NULL) { return usage(argv[0]); }
|
||||
else { sel[p - args] = c; }
|
||||
}
|
||||
|
||||
/* straighten out ops */
|
||||
for (i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i) {
|
||||
if (ops[i] != '\0') {
|
||||
*p = ops[i];
|
||||
if (&ops[i] != p++) { ops[i] = '\0'; }
|
||||
/* straighten out selections */
|
||||
for (size_t i = 0, p = sel; i < (sizeof sel) / (sizeof *sel); ++i) {
|
||||
if (sel[i] != '\0') {
|
||||
*p = sel[i];
|
||||
if (&sel[i] != p++) { sel[i] = '\0'; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (optind == argc) { return usage(argv[0]); }
|
||||
|
||||
argv += optind;
|
||||
do { /* while(*++argv != NULL); */
|
||||
for (argv += optind ; *argv != NULL; ++argv) {
|
||||
struct stat buf;
|
||||
|
||||
if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) {
|
||||
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
|
||||
}
|
||||
|
||||
for (i = 0; ops[i] != '\0'; ++i)
|
||||
if (ops[i] == 'e') {
|
||||
continue;
|
||||
} else if (ops[i] == 'h') {
|
||||
return usage(argv[0]);
|
||||
} else if (
|
||||
(ops[i] == 'b'
|
||||
&& !S_ISBLK(buf.st_mode))
|
||||
|| (ops[i] == 'c'
|
||||
&& !S_ISCHR(buf.st_mode))
|
||||
|| (ops[i] == 'd'
|
||||
&& !S_ISDIR(buf.st_mode))
|
||||
|| (ops[i] == 'f'
|
||||
&& !S_ISREG(buf.st_mode))
|
||||
|| (ops[i] == 'g'
|
||||
&& !(buf.st_mode & S_ISGID))
|
||||
|| (ops[i] == 'k'
|
||||
&& !(buf.st_mode & S_ISVTX))
|
||||
|| (ops[i] == 'p'
|
||||
&& !S_ISFIFO(buf.st_mode))
|
||||
|| (ops[i] == 'r'
|
||||
&& access(*argv, R_OK) != 0)
|
||||
|| (ops[i] == 'u'
|
||||
&& !(buf.st_mode & S_ISUID))
|
||||
|| (ops[i] == 'w'
|
||||
&& access(*argv, W_OK) != 0)
|
||||
|| (ops[i] == 'x'
|
||||
&& access(*argv, X_OK) != 0)
|
||||
|| (ops[i] == 'L'
|
||||
&& !S_ISLNK(buf.st_mode))
|
||||
|| (ops[i] == 'S'
|
||||
&& !S_ISSOCK(buf.st_mode))
|
||||
for (size_t i = 0; sel[i] != '\0'; ++i) {
|
||||
if (
|
||||
(sel[i] == 'b' && !S_ISBLK(buf.st_mode))
|
||||
|| (sel[i] == 'c' && !S_ISCHR(buf.st_mode))
|
||||
|| (sel[i] == 'd' && !S_ISDIR(buf.st_mode))
|
||||
|| (sel[i] == 'e' && 0)
|
||||
|| (sel[i] == 'f' && !S_ISREG(buf.st_mode))
|
||||
|| (sel[i] == 'g' && !(buf.st_mode & S_ISGID))
|
||||
|| (sel[i] == 'k' && !(buf.st_mode & S_ISVTX))
|
||||
|| (sel[i] == 'p' && !S_ISFIFO(buf.st_mode))
|
||||
|| (sel[i] == 'r' && access(*argv, R_OK) != 0)
|
||||
|| (sel[i] == 'u' && !(buf.st_mode & S_ISUID))
|
||||
|| (sel[i] == 'w' && access(*argv, W_OK) != 0)
|
||||
|| (sel[i] == 'x' && access(*argv, X_OK) != 0)
|
||||
|| (sel[i] == 'L' && !S_ISLNK(buf.st_mode))
|
||||
|| (sel[i] == 'S' && !S_ISSOCK(buf.st_mode))
|
||||
) { return EXIT_FAILURE; }
|
||||
} while(*++argv != NULL);
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user