scrut(1): banish gotos
This commit is contained in:
parent
59fa3ed3d2
commit
2c4349872c
41
src/scrut.c
41
src/scrut.c
@ -20,9 +20,7 @@
|
||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||
#include <string.h> /* memset(3), strchr(3) */
|
||||
#ifndef EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <sysexits.h> /* EX_USAGE */
|
||||
#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,
|
||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||
@ -32,21 +30,30 @@ static char args[] = "bcdefgkprsuwxLS";
|
||||
static char ops[(sizeof args) / (sizeof *args)];
|
||||
static char *program_name = "scrut";
|
||||
|
||||
int usage(char *s){
|
||||
fprintf(stderr, "Usage: %s [-%s] file...", 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 > 0) { program_name = argv[0]; }
|
||||
if (argc < 2) { return usage(program_name); }
|
||||
|
||||
memset(ops, '\0', sizeof ops);
|
||||
while((c = getopt(argc, argv, args)) != -1)
|
||||
if((p = strchr(args, c)) == NULL)
|
||||
goto usage;
|
||||
else
|
||||
ops[p - args] = c;
|
||||
|
||||
while ((c = getopt(argc, argv, args)) != -1)
|
||||
if ((p = strchr(args, c)) == NULL) { return usage(program_name); }
|
||||
else { ops[p - args] = c; }
|
||||
|
||||
if (optind == argc) { return usage(program_name); }
|
||||
|
||||
argv += optind;
|
||||
|
||||
/* straighten out ops */
|
||||
for(i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i)
|
||||
if(ops[i] != '\0'){
|
||||
@ -55,25 +62,13 @@ int main(int argc, char *argv[]){
|
||||
ops[i] = '\0';
|
||||
}
|
||||
|
||||
if(optind == argc)
|
||||
goto usage;
|
||||
|
||||
argv += optind;
|
||||
do{ 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'){
|
||||
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] == 'b'
|
||||
&& !S_ISBLK(buf.st_mode))
|
||||
|| (ops[i] == 'c'
|
||||
|
Loading…
Reference in New Issue
Block a user