scrut(1): use libfileis

This commit is contained in:
dtb 2024-07-15 14:13:53 -06:00
parent 2c4349872c
commit b9c4b49603
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B
4 changed files with 32 additions and 42 deletions

View File

@ -29,7 +29,7 @@ RUSTC ?= rustc
RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \
--extern sysexits=build/o/libsysexits.rlib \
--extern strerror=build/o/libstrerror.rlib
CFLAGS += -I$(SYSEXITS)
CFLAGS += -I$(SYSEXITS) -Iinclude
.PHONY: all
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
@ -87,6 +87,11 @@ build/o/libsysexits.rlib: build/include/sysexits.h
build/include/sysexits.h: build $(SYSEXITS)sysexits.h
printf '\043define EXIT_FAILURE 1\n' | cat - $(SYSEXITS)sysexits.h > $@
.PHONY: libfileis
libfileis: build/o/libfileis.o
build/o/libfileis.o: build src/libfileis.c
$(CC) $(CFLAGS) -c -o $@ src/libfileis.c
.PHONY: dj
dj: build/bin/dj
build/bin/dj: src/dj.c build
@ -129,8 +134,8 @@ build/bin/rpn: src/rpn.rs build rustlibs
.PHONY: scrut
scrut: build/bin/scrut
build/bin/scrut: src/scrut.c build
$(CC) $(CFLAGS) -o $@ src/scrut.c
build/bin/scrut: src/scrut.c build libfileis
$(CC) $(CFLAGS) -o $@ src/scrut.c build/o/libfileis.o
.PHONY: str
str: build/bin/str

View File

@ -7,7 +7,7 @@ static struct stat s;
int
fileis_exists(char *fn){
if (fn == NULL) { return ofn != NULL; }
if (fn == NULL || fn == ofn) { return ofn != NULL; }
if (lstat(fn, &s) == -1) { return 0; }
ofn = fn; return 1;
}

View File

@ -22,21 +22,19 @@
#include <string.h> /* memset(3), strchr(3) */
#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,
* S_ISUID, S_ISVTX */
#include <libfileis.h>
static char args[] = "bcdefgkprsuwxLS";
static char ops[(sizeof args) / (sizeof *args)];
static char *program_name = "scrut";
char *program_name = "scrut";
int usage(char *s){
fprintf(stderr, "Usage: %s [-%s] file...", s, args);
fprintf(stderr, "Usage: %s [-%s] file...\n", s, args);
return EX_USAGE;
}
int main(int argc, char *argv[]){
struct stat buf;
int c;
size_t i;
char *p;
@ -62,40 +60,27 @@ int main(int argc, char *argv[]){
ops[i] = '\0';
}
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1)
do{
if (!fileis_exists(*argv))
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] == '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 EXIT_FAILURE;
else if ((ops[i] == 'b' && !fileis_block(*argv))
|| (ops[i] == 'c' && !fileis_char(*argv))
|| (ops[i] == 'd' && !fileis_dir(*argv))
|| (ops[i] == 'f' && !fileis_regular(*argv))
|| (ops[i] == 'g' && !fileis_setgid(*argv))
|| (ops[i] == 'k' && !fileis_vtx(*argv))
|| (ops[i] == 'p' && !fileis_fifo(*argv))
|| (ops[i] == 'r' && access(*argv, R_OK) != 0)
|| (ops[i] == 'u' && !fileis_setuid(*argv))
|| (ops[i] == 'w' && access(*argv, W_OK) != 0)
|| (ops[i] == 'x' && access(*argv, X_OK) != 0)
|| (ops[i] == 'L' && !fileis_link(*argv))
|| (ops[i] == 'S' && !fileis_socket(*argv))
) { return EXIT_FAILURE; }
} while (*++argv != NULL);
return EXIT_SUCCESS;