bonsix/src/libfileis.c

28 lines
1.4 KiB
C

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