Compare commits
5 Commits
9addfc9284
...
0f12dcc552
Author | SHA1 | Date | |
---|---|---|---|
0f12dcc552 | |||
b9c4b49603 | |||
2c4349872c | |||
59fa3ed3d2 | |||
71655a8559 |
11
Makefile
11
Makefile
@ -29,7 +29,7 @@ RUSTC ?= rustc
|
|||||||
RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \
|
RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \
|
||||||
--extern sysexits=build/o/libsysexits.rlib \
|
--extern sysexits=build/o/libsysexits.rlib \
|
||||||
--extern strerror=build/o/libstrerror.rlib
|
--extern strerror=build/o/libstrerror.rlib
|
||||||
CFLAGS += -I$(SYSEXITS)
|
CFLAGS += -I$(SYSEXITS) -Iinclude
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
|
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
|
build/include/sysexits.h: build $(SYSEXITS)sysexits.h
|
||||||
printf '\043define EXIT_FAILURE 1\n' | cat - $(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
|
.PHONY: dj
|
||||||
dj: build/bin/dj
|
dj: build/bin/dj
|
||||||
build/bin/dj: src/dj.c build
|
build/bin/dj: src/dj.c build
|
||||||
@ -129,8 +134,8 @@ build/bin/rpn: src/rpn.rs build rustlibs
|
|||||||
|
|
||||||
.PHONY: scrut
|
.PHONY: scrut
|
||||||
scrut: build/bin/scrut
|
scrut: build/bin/scrut
|
||||||
build/bin/scrut: src/scrut.c build
|
build/bin/scrut: src/scrut.c build libfileis
|
||||||
$(CC) $(CFLAGS) -o $@ src/scrut.c
|
$(CC) $(CFLAGS) -o $@ src/scrut.c build/o/libfileis.o
|
||||||
|
|
||||||
.PHONY: str
|
.PHONY: str
|
||||||
str: build/bin/str
|
str: build/bin/str
|
||||||
|
14
include/libfileis.h
Normal file
14
include/libfileis.h
Normal 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
27
src/libfileis.c
Normal 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) && } */
|
117
src/scrut.c
117
src/scrut.c
@ -17,91 +17,68 @@
|
|||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
* 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 <stdio.h> /* fprintf(3), stderr, NULL */
|
||||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||||
#include <string.h> /* memset(3), strchr(3) */
|
#include <string.h> /* memset(3), strchr(3) */
|
||||||
#ifndef EX_USAGE
|
#include <sysexits.h> /* EX_USAGE */
|
||||||
# include <sysexits.h>
|
#include <unistd.h> /* access(3), getopt(3), R_OK, W_OK, X_OK */
|
||||||
#endif
|
#include <libfileis.h>
|
||||||
#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";
|
char *program_name = "scrut";
|
||||||
static char ops[(sizeof args) / (sizeof *args)];
|
static char opts[] = "bcdefgkprsuwxLS";
|
||||||
static char *program_name = "scrut";
|
|
||||||
|
static int usage(char *s){
|
||||||
|
fprintf(stderr, "Usage: %s [-%s] file...\n", s, opts);
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
struct stat buf;
|
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;
|
int c;
|
||||||
size_t i;
|
char *p = sel;
|
||||||
char *p;
|
|
||||||
|
|
||||||
if(argc < 2)
|
while ((c = getopt(argc, argv, opts)) != -1) {
|
||||||
goto usage;
|
if ((strchr(opts, c)) == NULL) { return usage(program_name); }
|
||||||
|
else if ((strchr(sel, c)) == NULL) { *p++ = c; }
|
||||||
|
|
||||||
memset(ops, '\0', sizeof ops);
|
assert(p - sel < (sizeof opts) / (sizeof *opts));
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(optind == argc)
|
if (optind == argc) { return usage(program_name); }
|
||||||
goto usage;
|
|
||||||
|
|
||||||
argv += optind;
|
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)
|
do{
|
||||||
if(ops[i] == 'e')
|
if (!fileis_exists(*argv))
|
||||||
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 EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}while(*++argv != NULL);
|
|
||||||
|
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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user