scrut(1)
This commit is contained in:
parent
6b8ce7d5b1
commit
697695e6e4
@ -1,5 +1,5 @@
|
|||||||
scrut: scrut.o libfileis.o
|
scrut: scrut.o libfileis.o
|
||||||
$(CC) $(CFLAGS) -o scrut scrut.o scrut.o
|
$(CC) $(CFLAGS) -o scrut libfileis.o scrut.o
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) scrut *.o
|
$(RM) scrut *.o
|
||||||
|
@ -1,29 +1,69 @@
|
|||||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
#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>
|
# include <sysexits.h>
|
||||||
#endif
|
#endif
|
||||||
#include <unistd.h> /* getopt(3) */
|
#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[]){
|
int main(int argc, char *argv[]){
|
||||||
|
char args[(sizeof argsdict) / (sizeof *argsdict) + 1];
|
||||||
int c;
|
int c;
|
||||||
|
int (*f[(sizeof argsdict) / (sizeof *argsdict)])(char *p);
|
||||||
|
size_t fi;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
if(argc < 2)
|
if(argc < 2)
|
||||||
goto usage;
|
goto usage;
|
||||||
|
|
||||||
while((c = getopt(argc, argv, "erwx")) != -1)
|
for(i = 0; i < (sizeof argsdict) / (sizeof *argsdict); ++i)
|
||||||
switch(c){
|
args[i] = argsdict[i].c;
|
||||||
default: goto usage;
|
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){
|
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]);
|
argv[0] == NULL ? program_name : argv[0]);
|
||||||
return EX_USAGE;
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user