1
0
src/wip/scrut/scrut.c

70 lines
1.4 KiB
C
Raw Normal View History

2023-12-18 00:09:33 -07:00
#include <stdio.h> /* fprintf(3), stderr, NULL */
2023-12-21 15:28:41 -07:00
#include <string.h> /* memset(3) */
#ifndef EX_USAGE
2023-12-18 00:09:33 -07:00
# include <sysexits.h>
#endif
#include <unistd.h> /* getopt(3) */
2023-12-21 15:28:41 -07:00
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 }
};
2023-12-18 00:09:33 -07:00
int main(int argc, char *argv[]){
2023-12-21 15:28:41 -07:00
char args[(sizeof argsdict) / (sizeof *argsdict) + 1];
2023-12-18 00:09:33 -07:00
int c;
2023-12-21 15:28:41 -07:00
int (*f[(sizeof argsdict) / (sizeof *argsdict)])(char *p);
size_t fi;
size_t i;
2023-12-18 00:09:33 -07:00
if(argc < 2)
goto usage;
2023-12-21 15:28:41 -07:00
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;
}
2023-12-18 00:09:33 -07:00
if(optind == argc){
2023-12-21 15:28:41 -07:00
usage: fprintf(stderr, "Usage: %s (-bcdefghkprsuwxLS) [file...]\n",
2023-12-18 00:09:33 -07:00
argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE;
}
2023-12-21 15:28:41 -07:00
argv += optind;
do for(i = 0; i < fi; ++i)
if(!f[i](*argv))
return 1;
while(*++argv != NULL);
return 0;
2023-12-18 00:09:33 -07:00
}