scrut(1): use libfileis
This commit is contained in:
		
							parent
							
								
									2c4349872c
								
							
						
					
					
						commit
						b9c4b49603
					
				
							
								
								
									
										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
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@ static struct stat s;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
fileis_exists(char *fn){
 | 
					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; }
 | 
						if (lstat(fn, &s) == -1) { return 0; }
 | 
				
			||||||
	ofn = fn; return 1;
 | 
						ofn = fn; return 1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										61
									
								
								src/scrut.c
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								src/scrut.c
									
									
									
									
									
								
							@ -22,21 +22,19 @@
 | 
				
			|||||||
#include <string.h> /* memset(3), strchr(3) */
 | 
					#include <string.h> /* memset(3), strchr(3) */
 | 
				
			||||||
#include <sysexits.h> /* EX_USAGE */
 | 
					#include <sysexits.h> /* EX_USAGE */
 | 
				
			||||||
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
 | 
					#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,
 | 
					#include <libfileis.h>
 | 
				
			||||||
                       * S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
 | 
					 | 
				
			||||||
                       * S_ISUID, S_ISVTX */
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static char args[] = "bcdefgkprsuwxLS";
 | 
					static char args[] = "bcdefgkprsuwxLS";
 | 
				
			||||||
static char ops[(sizeof args) / (sizeof *args)];
 | 
					static char ops[(sizeof args) / (sizeof *args)];
 | 
				
			||||||
static char *program_name = "scrut";
 | 
					
 | 
				
			||||||
 | 
					char *program_name = "scrut";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int usage(char *s){
 | 
					int usage(char *s){
 | 
				
			||||||
	fprintf(stderr, "Usage: %s [-%s] file...", s, args);
 | 
						fprintf(stderr, "Usage: %s [-%s] file...\n", s, args);
 | 
				
			||||||
	return EX_USAGE;
 | 
						return EX_USAGE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[]){
 | 
					int main(int argc, char *argv[]){
 | 
				
			||||||
	struct stat buf;
 | 
					 | 
				
			||||||
	int c;
 | 
						int c;
 | 
				
			||||||
	size_t i;
 | 
						size_t i;
 | 
				
			||||||
	char *p;
 | 
						char *p;
 | 
				
			||||||
@ -62,41 +60,28 @@ int main(int argc, char *argv[]){
 | 
				
			|||||||
				ops[i] = '\0';
 | 
									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 */
 | 
								return EXIT_FAILURE; /* doesn't exist or isn't stattable */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for(i = 0; ops[i] != '\0'; ++i)
 | 
							for (i = 0; ops[i] != '\0'; ++i)
 | 
				
			||||||
			if(ops[i] == 'e')
 | 
								if (ops[i] == 'e')
 | 
				
			||||||
				continue;
 | 
									continue;
 | 
				
			||||||
			else if(
 | 
								else if ((ops[i] == 'b' && !fileis_block(*argv))
 | 
				
			||||||
					(ops[i] == 'b'
 | 
										|| (ops[i] == 'c' && !fileis_char(*argv))
 | 
				
			||||||
						&& !S_ISBLK(buf.st_mode))
 | 
										|| (ops[i] == 'd' && !fileis_dir(*argv))
 | 
				
			||||||
					|| (ops[i] == 'c'
 | 
										|| (ops[i] == 'f' && !fileis_regular(*argv))
 | 
				
			||||||
						&& !S_ISCHR(buf.st_mode))
 | 
										|| (ops[i] == 'g' && !fileis_setgid(*argv))
 | 
				
			||||||
					|| (ops[i] == 'd'
 | 
										|| (ops[i] == 'k' && !fileis_vtx(*argv))
 | 
				
			||||||
						&& !S_ISDIR(buf.st_mode))
 | 
										|| (ops[i] == 'p' && !fileis_fifo(*argv))
 | 
				
			||||||
					|| (ops[i] == 'f'
 | 
										|| (ops[i] == 'r' && access(*argv, R_OK) != 0)
 | 
				
			||||||
						&& !S_ISREG(buf.st_mode))
 | 
										|| (ops[i] == 'u' && !fileis_setuid(*argv))
 | 
				
			||||||
					|| (ops[i] == 'g'
 | 
										|| (ops[i] == 'w' && access(*argv, W_OK) != 0)
 | 
				
			||||||
						&& !(buf.st_mode & S_ISGID))
 | 
										|| (ops[i] == 'x' && access(*argv, X_OK) != 0)
 | 
				
			||||||
					|| (ops[i] == 'k'
 | 
										|| (ops[i] == 'L' && !fileis_link(*argv))
 | 
				
			||||||
						&& !(buf.st_mode & S_ISVTX))
 | 
										|| (ops[i] == 'S' && !fileis_socket(*argv))
 | 
				
			||||||
					|| (ops[i] == 'p'
 | 
								) { return EXIT_FAILURE; }
 | 
				
			||||||
						&& !S_ISFIFO(buf.st_mode))
 | 
						} while (*++argv != NULL);
 | 
				
			||||||
					|| (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;
 | 
					 | 
				
			||||||
	}while(*++argv != NULL);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return EXIT_SUCCESS;
 | 
						return EXIT_SUCCESS;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user