diff options
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | lock.c | 54 | ||||
-rwxr-xr-x | lock.sh (renamed from lock) | 0 |
3 files changed, 70 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0c3dbd8 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +VERSION=\"0.0.5\" +CC = gcc +CFLAGS = -Os -mtune=native -Wall -Wextra -pedantic +DESTDIR = /usr/local/ + +all: lock + +lock: + $(CC) $(CFLAGS) lock.c -o lock + + +install: all + @install lock $(DESTDIR)/bin/ + +clean: + rm lock @@ -0,0 +1,54 @@ +/* See LICENSE for licence details. */
+
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv){
+ size_t pathlen, argvlen;
+ char *path;
+
+ if(argc != 2){
+ fprintf(stderr, "Please supply one argument: the lock directory.\n");
+ return EXIT_FAILURE;
+ }
+
+ if(access(argv[1], F_OK)){
+ /* create lock */
+ if(mkdir(argv[1], S_IRUSR | S_IXUSR)){
+ fprintf(stderr, "Failed to create lock %s.\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+ }
+
+ /* prepare path */
+ argvlen = strlen(argv[1]);
+ pathlen = argvlen + sizeof("/..") + 1;
+ path = malloc(pathlen);
+ if(path == NULL) {
+ fprintf(stderr, "Error: %s\n", strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ memcpy(path, argv[1], argvlen);
+ memcpy(path + argvlen, "/..\0", sizeof("/..") + 1);
+
+ if(access(path, F_OK)){
+ /* invalid lock */
+ fprintf(stderr, "%s is not a lock directory.\n", argv[1]);
+ free(path);
+ return EXIT_FAILURE;
+ }
+
+ /* lock exists */
+ for(; !access(path, F_OK) ;){
+ sleep(1);
+ }
+
+ free(path);
+ return EXIT_SUCCESS;
+}
|