scrut(1): use libfileis

This commit is contained in:
dtb
2024-07-15 14:13:53 -06:00
parent 2c4349872c
commit b9c4b49603
4 changed files with 32 additions and 42 deletions

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

@@ -1,14 +0,0 @@
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); */

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,41 +60,28 @@ 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')
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;
}while(*++argv != NULL);
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;
}