scrut(1): formatting, removes gotos

This commit is contained in:
Emma Tebibyte 2024-07-12 16:04:07 -06:00
parent 6e1e3db6c8
commit 5d2872d050
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -20,9 +20,7 @@
#include <stdio.h> /* fprintf(3), stderr, NULL */ #include <stdio.h> /* fprintf(3), stderr, NULL */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */ #include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <string.h> /* memset(3), strchr(3) */ #include <string.h> /* memset(3), strchr(3) */
#ifndef EX_USAGE #include <sysexits.h>
# include <sysexits.h>
#endif
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */ #include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR, #include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK, * S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
@ -32,48 +30,56 @@ static char args[] = "bcdefghkprsuwxLS";
static char ops[(sizeof args) / (sizeof *args)]; static char ops[(sizeof args) / (sizeof *args)];
static char *program_name = "scrut"; static char *program_name = "scrut";
int main(int argc, char *argv[]){ int usage(char *s) {
fprintf(
stderr,
"Usage: %s [-%s] file...\n",
s == NULL ? program_name : s, args
);
return EX_USAGE;
}
int main(int argc, char *argv[]) {
struct stat buf; struct stat buf;
int c; int c;
size_t i; size_t i;
char *p; char *p;
if(argc < 2) if (argc < 2) { return usage(argv[0]); }
goto usage;
memset(ops, '\0', sizeof ops); memset(ops, '\0', sizeof ops);
while((c = getopt(argc, argv, args)) != -1) while ((c = getopt(argc, argv, args)) != -1) {
if((p = strchr(args, c)) == NULL) if ((p = strchr(args, c)) == NULL) {
goto usage; return usage(argv[0]);
else } else {
ops[p - args] = c; ops[p - args] = c;
}
}
/* straighten out ops */ /* straighten out ops */
for(i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i) for (i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i) {
if(ops[i] != '\0'){ if(ops[i] != '\0'){
*p = ops[i]; *p = ops[i];
if(&ops[i] != p++) if(&ops[i] != p++)
ops[i] = '\0'; ops[i] = '\0';
} }
}
if(optind == argc) if (optind == argc) { return usage(argv[0]); }
goto usage;
argv += optind; argv += optind;
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) do { /* while(*++argv != NULL); */
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 (i = 0; ops[i] != '\0'; ++i)
if(ops[i] == 'e') if (ops[i] == 'e') {
continue; continue;
else if(ops[i] == 'h'){ } else if (ops[i] == 'h') {
usage: fprintf(stderr, "Usage: %s [-%s] file...\n", return usage(argv[0]);
argv[0] == NULL } else if (
? program_name
: argv[0],
args);
return EX_USAGE;
}else if(
(ops[i] == 'b' (ops[i] == 'b'
&& !S_ISBLK(buf.st_mode)) && !S_ISBLK(buf.st_mode))
|| (ops[i] == 'c' || (ops[i] == 'c'
@ -99,9 +105,9 @@ usage: fprintf(stderr, "Usage: %s [-%s] file...\n",
|| (ops[i] == 'L' || (ops[i] == 'L'
&& !S_ISLNK(buf.st_mode)) && !S_ISLNK(buf.st_mode))
|| (ops[i] == 'S' || (ops[i] == 'S'
&& !S_ISSOCK(buf.st_mode))) && !S_ISSOCK(buf.st_mode))
return EXIT_FAILURE; ) { return EXIT_FAILURE; }
}while(*++argv != NULL); } while(*++argv != NULL);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }