commit cb5a9f0a856c5ea438c3c5210de82ad37362bbcc Author: Pin Date: Fri Oct 22 15:31:44 2021 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0354010 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..94b4cdf --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +OUTPUT_DIR = ./bin +OUTPUT = -o $(OUTPUT_DIR)/PROG +SOURCES = *.c + +build: output_dir + gcc -Wall ${SOURCES} ${OUTPUT:PROG=upgrade} + +output_dir: + mkdir -p ${OUTPUT_DIR} diff --git a/main.c b/main.c new file mode 100644 index 0000000..815654e --- /dev/null +++ b/main.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + struct flock fl; + int c; + int sleep_time; + int fd; + + static const struct option long_options[] = { + {"file", required_argument, NULL, 'f'}, + {"sleep", required_argument, NULL, 's'}, + {0, 0, 0, 0} + }; + + while(1) { + int option_index = 0; + c = getopt_long(argc, argv, "f:s:", long_options, &option_index); + + if (c == -1) { + break; + } + + // No input sanitation + switch(c) { + case 'f': + fd=open(optarg, O_RDONLY); + break; + case 's': + sleep_time = atoi(optarg); + break; + } + } + + fl.l_type=F_RDLCK; + fl.l_whence=SEEK_SET; + fl.l_start=0; + fl.l_len=0; + if(fcntl(fd,F_SETLK,&fl) == -1) { + printf("ERROR\n"); + return 1; + } + do { + sleep(sleep_time); + break; + } while(1); + + return 0; +}