make independent of libfileis and faster, too
This commit is contained in:
parent
8516533533
commit
e3d27c7963
105
scrut/scrut.c
105
scrut/scrut.c
@ -1,69 +1,80 @@
|
||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||
#include <string.h> /* memset(3) */
|
||||
#include <string.h> /* strchr(3) */
|
||||
#ifndef EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <unistd.h> /* getopt(3) */
|
||||
#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,
|
||||
* S_ISUID, S_ISVTX */
|
||||
|
||||
static char args[] = "bcdefghkprsuwxLS";
|
||||
static char ops[(sizeof args) / (sizeof *args)];
|
||||
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];
|
||||
struct stat buf;
|
||||
int c;
|
||||
int (*f[(sizeof argsdict) / (sizeof *argsdict)])(char *p);
|
||||
size_t fi;
|
||||
size_t i;
|
||||
|
||||
if(argc < 2)
|
||||
goto usage;
|
||||
|
||||
for(i = 0; i < (sizeof argsdict) / (sizeof *argsdict); ++i)
|
||||
args[i] = argsdict[i].c;
|
||||
args[(sizeof argsdict) / (sizeof *argsdict)] = '\0';
|
||||
|
||||
fi = 0;
|
||||
i = 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(strchr(args, c) == NULL)
|
||||
goto usage;
|
||||
else
|
||||
ops[i++] = c;
|
||||
ops[i] = '\0';
|
||||
|
||||
if(optind == argc){
|
||||
usage: fprintf(stderr, "Usage: %s (-bcdefghkprsuwxLS) [file...]\n",
|
||||
argv[0] == NULL ? program_name : argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
if(optind == argc)
|
||||
goto usage;
|
||||
|
||||
argv += optind;
|
||||
do for(i = 0; i < fi; ++i)
|
||||
if(!f[i](*argv))
|
||||
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1)
|
||||
return 1; /* 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(
|
||||
(ops[i] == 'b'
|
||||
&& !S_ISBLK(buf.st_mode))
|
||||
|| (ops[i] == 'c'
|
||||
&& !S_ISCHR(buf.st_mode))
|
||||
|| (ops[i] == 'd'
|
||||
&& !S_ISDIR(buf.st_mode))
|
||||
|| (ops[i] == 'f'
|
||||
&& !S_ISREG(buf.st_mode))
|
||||
|| (ops[i] == 'g'
|
||||
&& !(buf.st_mode & S_ISGID))
|
||||
|| (ops[i] == 'k'
|
||||
&& !(buf.st_mode & S_ISVTX))
|
||||
|| (ops[i] == 'p'
|
||||
&& !S_ISFIFO(buf.st_mode))
|
||||
|| (ops[i] == 'r'
|
||||
&& access(*argv, R_OK) != 0)
|
||||
|| (ops[i] == 'u'
|
||||
&& !(buf.st_mode & S_ISUID))
|
||||
|| (ops[i] == 'w'
|
||||
&& access(*argv, W_OK) != 0)
|
||||
|| (ops[i] == 'x'
|
||||
&& access(*argv, X_OK) != 0)
|
||||
|| (ops[i] == 'L'
|
||||
&& !S_ISLNK(buf.st_mode))
|
||||
|| (ops[i] == 'S'
|
||||
&& !S_ISSOCK(buf.st_mode)))
|
||||
return 1;
|
||||
while(*++argv != NULL);
|
||||
}while(*++argv != NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user