1
0
This commit is contained in:
dtb 2023-12-21 15:28:41 -07:00
parent 6b8ce7d5b1
commit 697695e6e4
2 changed files with 51 additions and 11 deletions

View File

@ -1,5 +1,5 @@
scrut: scrut.o libfileis.o
$(CC) $(CFLAGS) -o scrut scrut.o scrut.o
$(CC) $(CFLAGS) -o scrut libfileis.o scrut.o
clean:
$(RM) scrut *.o

View File

@ -1,29 +1,69 @@
#include <stdio.h> /* fprintf(3), stderr, NULL */
#if !defined EX_OK || !defined EX_USAGE
#include <string.h> /* memset(3) */
#ifndef EX_USAGE
# include <sysexits.h>
#endif
#include <unistd.h> /* getopt(3) */
#include "libfileis.h" /* f_executable(3), f_exists(3), f_readable(3),
* f_writeable(3) */
static char *program_name = "fileis";
static char *program_name = "scrut";
#include "libfileis.h"
struct {
char c;
int (*f)(char *p);
} argsdict[] = {
{ 'b', f_blockspecial },
{ 'c', f_charspecial },
{ 'd', f_directory },
{ 'e', f_exists },
{ 'f', f_regular },
{ 'g', f_gid },
{ 'p', f_fifospecial },
{ 'r', f_readable },
{ 'x', f_executable },
{ 'k', f_sticky },
{ 'u', f_uid },
{ 'w', f_writeable },
{ 'L', f_symlink },
{ 'S', f_socket },
{ '\0', NULL }
};
int main(int argc, char *argv[]){
char args[(sizeof argsdict) / (sizeof *argsdict) + 1];
int c;
int (*f[(sizeof argsdict) / (sizeof *argsdict)])(char *p);
size_t fi;
size_t i;
if(argc < 2)
goto usage;
while((c = getopt(argc, argv, "erwx")) != -1)
switch(c){
default: goto usage;
}
for(i = 0; i < (sizeof argsdict) / (sizeof *argsdict); ++i)
args[i] = argsdict[i].c;
args[(sizeof argsdict) / (sizeof *argsdict)] = '\0';
fi = 0;
while((c = getopt(argc, argv, args)) != -1)
for(i = 0; i < (sizeof argsdict) / (sizeof *argsdict); ++i)
if(argsdict[i].c == '\0')
goto usage;
else if(argsdict[i].c == c){
f[fi++] = argsdict[i].f;
break;
}
if(optind == argc){
usage: fprintf(stderr, "Usage: %s (-ehrwx) [file]\n",
usage: fprintf(stderr, "Usage: %s (-bcdefghkprsuwxLS) [file...]\n",
argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE;
}
return !f(argv[optind]);
argv += optind;
do for(i = 0; i < fi; ++i)
if(!f[i](*argv))
return 1;
while(*++argv != NULL);
return 0;
}