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 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) */
size_t i; /* side of io being modified */
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
@ -166,7 +166,7 @@ int main(int argc, char *argv[]) {
/* Set defaults. */
align = -1;
count = 0;
count = -1;
fmt = fmt_asv;
noerror = 0;
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);
}
do { /* while(count == 0 || --count > 0); */
for ( ;
count == -1 || count > 0;
count -= (count != -1) /* decrement if counting */
) {
assert(io[0].bufuse == 0);
{ /* read */
@ -303,7 +306,7 @@ int main(int argc, char *argv[]) {
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) {
fprintf(stderr, "%s: Partial read:\n\t", program_name);
@ -326,7 +329,7 @@ int main(int argc, char *argv[]) {
if (skipping > 0) {
io[0].seek -= skipping;
io[0].bufuse = 0;
count += (count != 0);
count += (count != -1); /* increment if counting */
continue;
}
}
@ -393,7 +396,7 @@ int main(int argc, char *argv[]) {
if(!noerror) { count = 1; }
}
}
} while(count == 0 || --count > 0);
}
fprintio(stderr, fmt, io);

View File

@ -26,87 +26,66 @@
* 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; permute out nulls */
p = sel;
for (size_t i = 0; 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;
}