Compare commits

...

5 Commits

Author SHA1 Message Date
DTB
0f12dcc552
scrut(1): fix ugly opt parsing 2024-07-15 14:28:00 -06:00
DTB
b9c4b49603
scrut(1): use libfileis 2024-07-15 14:13:53 -06:00
DTB
2c4349872c
scrut(1): banish gotos 2024-07-15 13:55:01 -06:00
DTB
59fa3ed3d2
scrut(1): get rid of -h 2024-07-15 13:47:55 -06:00
DTB
71655a8559
libfileis(3): a library for scrutinizing files 2024-07-15 13:47:04 -06:00
4 changed files with 97 additions and 74 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

14
include/libfileis.h Normal file
View File

@ -0,0 +1,14 @@
int fileis_block (char *fn);
int fileis_char (char *fn);
int fileis_dir (char *fn);
/* fileis_exec (char *fn); */
int fileis_exists (char *fn);
int fileis_fifo (char *fn);
int fileis_setgid (char *fn);
int fileis_link (char *fn);
/* fileis_read (char *fn); */
int fileis_regular(char *fn);
int fileis_socket (char *fn);
int fileis_setuid (char *fn);
int fileis_vtx (char *fn);
/* fileis_write (char *fn); */

27
src/libfileis.c Normal file
View File

@ -0,0 +1,27 @@
#include <stddef.h> /* NULL */
#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 *ofn = NULL;
static struct stat s;
int
fileis_exists(char *fn){
if (fn == NULL || fn == ofn) { return ofn != NULL; }
if (lstat(fn, &s) == -1) { return 0; }
ofn = fn; return 1;
}
int fileis_block(char *fn) { return fileis_exists(fn) && S_ISBLK(s.st_mode); }
int fileis_char(char *fn) { return fileis_exists(fn) && S_ISCHR(s.st_mode); }
int fileis_dir(char *fn) { return fileis_exists(fn) && S_ISDIR(s.st_mode); }
/* fileis_exec(char *fn) { return fileis_exists(fn) && } */
int fileis_fifo(char *fn) { return fileis_exists(fn) && S_ISFIFO(s.st_mode); }
int fileis_setgid(char *fn) { return fileis_exists(fn) && (s.st_mode & S_ISGID); }
int fileis_link(char *fn) { return fileis_exists(fn) && S_ISLNK(s.st_mode); }
/* fileis_read(char *fn) { return fileis_exists(fn) && } */
int fileis_regular(char *fn){ return fileis_exists(fn) && S_ISREG(s.st_mode); }
int fileis_socket(char *fn) { return fileis_exists(fn) && S_ISSOCK(s.st_mode); }
int fileis_setuid(char *fn) { return fileis_exists(fn) && (s.st_mode & S_ISUID); }
int fileis_vtx(char *fn) { return fileis_exists(fn) && (s.st_mode & S_ISVTX); }
/* fileis_write(char *fn) { return fileis_exists(fn) && } */

View File

@ -17,90 +17,67 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
#include <assert.h> /* assert(3) */
#include <stdio.h> /* fprintf(3), stderr, NULL */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <string.h> /* memset(3), strchr(3) */
#ifndef EX_USAGE
# include <sysexits.h>
#endif
#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 <sysexits.h> /* EX_USAGE */
#include <unistd.h> /* access(3), getopt(3), R_OK, W_OK, X_OK */
#include <libfileis.h>
static char args[] = "bcdefghkprsuwxLS";
static char ops[(sizeof args) / (sizeof *args)];
static char *program_name = "scrut";
char *program_name = "scrut";
static char opts[] = "bcdefgkprsuwxLS";
int main(int argc, char *argv[]){
struct stat buf;
int c;
size_t i;
char *p;
if(argc < 2)
goto usage;
memset(ops, '\0', sizeof ops);
while((c = getopt(argc, argv, args)) != -1)
if((p = strchr(args, c)) == NULL)
goto usage;
else
ops[p - args] = c;
/* straighten out ops */
for(i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i)
if(ops[i] != '\0'){
*p = ops[i];
if(&ops[i] != p++)
ops[i] = '\0';
static int usage(char *s){
fprintf(stderr, "Usage: %s [-%s] file...\n", s, opts);
return EX_USAGE;
}
if(optind == argc)
goto usage;
int main(int argc, char *argv[]){
char sel[(sizeof opts) / (sizeof *opts)];
if (argc > 0) { program_name = argv[0]; }
if (argc < 2) { return usage(program_name); }
memset(sel, '\0', sizeof sel);
{
int c;
char *p = sel;
while ((c = getopt(argc, argv, opts)) != -1) {
if ((strchr(opts, c)) == NULL) { return usage(program_name); }
else if ((strchr(sel, c)) == NULL) { *p++ = c; }
assert(p - sel < (sizeof opts) / (sizeof *opts));
}
}
if (optind == argc) { return usage(program_name); }
argv += optind;
do{ if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1)
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] == '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)))
do{
if (!fileis_exists(*argv))
return EXIT_FAILURE;
for (size_t i = 0; sel[i] != '\0'; ++i) {
if ((sel[i] == 'b' && !fileis_block(*argv))
|| (sel[i] == 'c' && !fileis_char(*argv))
|| (sel[i] == 'd' && !fileis_dir(*argv))
|| (sel[i] != 'e')
|| (sel[i] == 'f' && !fileis_regular(*argv))
|| (sel[i] == 'g' && !fileis_setgid(*argv))
|| (sel[i] == 'k' && !fileis_vtx(*argv))
|| (sel[i] == 'p' && !fileis_fifo(*argv))
|| (sel[i] == 'r' && access(*argv, R_OK) != 0)
|| (sel[i] == 'u' && !fileis_setuid(*argv))
|| (sel[i] == 'w' && access(*argv, W_OK) != 0)
|| (sel[i] == 'x' && access(*argv, X_OK) != 0)
|| (sel[i] == 'L' && !fileis_link(*argv))
|| (sel[i] == 'S' && !fileis_socket(*argv))
) { return EXIT_FAILURE; }
}
} while (*++argv != NULL);
return EXIT_SUCCESS;