1
0

minimum program

This commit is contained in:
dtb 2023-12-18 00:09:33 -07:00
parent dc2936c36d
commit 18704ce890
4 changed files with 52 additions and 0 deletions

7
fileis/Makefile Normal file
View File

@ -0,0 +1,7 @@
fileis: fileis.o libfileis.o
$(CC) $(CFLAGS) -o fileis fileis.o libfileis.o
clean:
$(RM) fileis fileis.o libfileis.o
.PHONY: clean

34
fileis/fileis.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h> /* fprintf(3), stderr, NULL */
#if !defined EX_OK || !defined EX_USAGE
# include <sysexits.h>
#endif
#include <unistd.h> /* getopt(3) */
#include "libfileis.h" /* f_executable(3), f_exists(3), f_readable(3),
* f_writeable(3) */
static char *program_name = "fileis";
int main(int argc, char *argv[]){
int c;
int (*f)(char *path);
if(argc < 2)
goto usage;
while((c = getopt(argc, argv, "erwx")) != -1)
switch(c){
case 'e': f = f_exists; break;
case 'r': f = f_readable; break;
case 'w': f = f_writeable; break;
case 'x': f = f_executable; break;
default: goto usage;
}
if(optind == argc){
usage: fprintf(stderr, "Usage: %s (-ehrwx) [file]\n",
argv[0] == NULL ? program_name : argv[0]);
return EX_USAGE;
}
return !f(argv[optind]);
}

7
fileis/libfileis.c Normal file
View File

@ -0,0 +1,7 @@
#include "libfileis.h"
#include <unistd.h> /* access(3), F_OK, R_OK, W_OK, X_OK */
int f_executable(char *path){ return access(path, X_OK) == 0; }
int f_exists(char *path){ return access(path, F_OK) == 0; }
int f_readable(char *path){ return access(path, R_OK) == 0; }
int f_writeable(char *path){ return access(path, W_OK) == 0; }

4
fileis/libfileis.h Normal file
View File

@ -0,0 +1,4 @@
int f_executable(char *path);
int f_exists(char *path);
int f_readable(char *path);
int f_writeable(char *path);