From 5d2872d05046fca6d326c5ae3552dac208ee7787 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 12 Jul 2024 16:04:07 -0600 Subject: [PATCH] scrut(1): formatting, removes gotos --- src/scrut.c | 62 +++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/scrut.c b/src/scrut.c index d85d243..f708eb5 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -20,9 +20,7 @@ #include /* fprintf(3), stderr, NULL */ #include /* EXIT_FAILURE, EXIT_SUCCESS */ #include /* memset(3), strchr(3) */ -#ifndef EX_USAGE -# include -#endif +#include #include /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */ #include /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR, * 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 *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; int c; size_t i; char *p; - if(argc < 2) - goto usage; + 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) - goto usage; - else + while ((c = getopt(argc, argv, args)) != -1) { + if ((p = strchr(args, c)) == NULL) { + return usage(argv[0]); + } else { ops[p - args] = c; + } + } + /* 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'){ *p = ops[i]; if(&ops[i] != p++) ops[i] = '\0'; } + } - if(optind == argc) - goto usage; + if (optind == argc) { return usage(argv[0]); } 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 */ + } - for(i = 0; ops[i] != '\0'; ++i) - if(ops[i] == 'e') + for (i = 0; ops[i] != '\0'; ++i) + if (ops[i] == 'e') { continue; - else if(ops[i] == 'h'){ -usage: fprintf(stderr, "Usage: %s [-%s] file...\n", - argv[0] == NULL - ? program_name - : argv[0], - args); - - return EX_USAGE; - }else if( + } else if (ops[i] == 'h') { + return usage(argv[0]); + } else if ( (ops[i] == 'b' && !S_ISBLK(buf.st_mode)) || (ops[i] == 'c' @@ -99,9 +105,9 @@ usage: fprintf(stderr, "Usage: %s [-%s] file...\n", || (ops[i] == 'L' && !S_ISLNK(buf.st_mode)) || (ops[i] == 'S' - && !S_ISSOCK(buf.st_mode))) - return EXIT_FAILURE; - }while(*++argv != NULL); + && !S_ISSOCK(buf.st_mode)) + ) { return EXIT_FAILURE; } + } while(*++argv != NULL); return EXIT_SUCCESS; }