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