libfileis(3): a library for scrutinizing files

This commit is contained in:
dtb 2024-07-15 13:47:04 -06:00
parent 9addfc9284
commit 71655a8559
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B
2 changed files with 41 additions and 0 deletions

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) { 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) && } */

14
src/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); */