Compare commits

...

3 Commits

Author SHA1 Message Date
DTB
f96ed9c1f3
scrut(1): fix syntax error 2024-07-19 19:34:37 -06:00
DTB
19eee6b4e5
scrut(1): replace do/while loop 2024-07-19 19:31:34 -06:00
DTB
9086bf0d08
dj(1): remove do/while statement in read loop 2024-07-19 19:18:04 -06:00
2 changed files with 49 additions and 67 deletions

View File

@ -158,7 +158,7 @@ usage(char *s) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int align; /* low 8b used, negative if no alignment is being done */ int align; /* low 8b used, negative if no alignment is being done */
int count; /* 0 if dj(1) runs until no more reads are possible */ int count; /* -1 if dj(1) runs until no more reads are possible */
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */ char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
size_t i; /* side of io being modified */ size_t i; /* side of io being modified */
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
@ -166,7 +166,7 @@ int main(int argc, char *argv[]) {
/* Set defaults. */ /* Set defaults. */
align = -1; align = -1;
count = 0; count = -1;
fmt = fmt_asv; fmt = fmt_asv;
noerror = 0; noerror = 0;
for (i = 0; i < (sizeof io) / (sizeof *io); ++i) { for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
@ -284,7 +284,10 @@ int main(int argc, char *argv[]) {
return oserr(io[1].fn, errno); return oserr(io[1].fn, errno);
} }
do { /* while(count == 0 || --count > 0); */ for ( ;
count == -1 || count > 0;
count -= (count != -1) /* decrement if counting */
) {
assert(io[0].bufuse == 0); assert(io[0].bufuse == 0);
{ /* read */ { /* read */
@ -303,7 +306,7 @@ int main(int argc, char *argv[]) {
assert(io[0].bufuse >= t); assert(io[0].bufuse >= t);
if (io[0].bufuse == t) /* that's all she wrote */ { break; } if (io[0].bufuse == t) { break; } /* that's all she wrote */
if (/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs) { if (/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs) {
fprintf(stderr, "%s: Partial read:\n\t", program_name); fprintf(stderr, "%s: Partial read:\n\t", program_name);
@ -326,7 +329,7 @@ int main(int argc, char *argv[]) {
if (skipping > 0) { if (skipping > 0) {
io[0].seek -= skipping; io[0].seek -= skipping;
io[0].bufuse = 0; io[0].bufuse = 0;
count += (count != 0); count += (count != -1); /* increment if counting */
continue; continue;
} }
} }
@ -393,7 +396,7 @@ int main(int argc, char *argv[]) {
if(!noerror) { count = 1; } if(!noerror) { count = 1; }
} }
} }
} while(count == 0 || --count > 0); }
fprintio(stderr, fmt, io); fprintio(stderr, fmt, io);

View File

@ -26,87 +26,66 @@
* 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; permute out nulls */
for (i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i) { p = sel;
if (ops[i] != '\0') { for (size_t i = 0; i < (sizeof sel) / (sizeof *sel); ++i) {
*p = ops[i]; if (sel[i] != '\0') {
if (&ops[i] != p++) { ops[i] = '\0'; } *p = sel[i];
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;
} }