aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCalvin Morrison <mutantturkey@gmail.com>2014-04-10 11:16:07 -0400
committerCalvin Morrison <mutantturkey@gmail.com>2014-04-10 11:16:07 -0400
commit01dbecb2e93aa782685c87021a1bc6912b810c4d (patch)
tree753691167f8be6c93e073a57d7ad34289a32bf5b
parent15b51380182f13ca401d7ab9c39ab3ef5435de1b (diff)
c version of lock
-rw-r--r--Makefile16
-rw-r--r--lock.c54
-rwxr-xr-xlock.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
diff --git a/lock.c b/lock.c
new file mode 100644
index 0000000..12d560b
--- /dev/null
+++ b/lock.c
@@ -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;
+}
diff --git a/lock b/lock.sh
index 22191fe..22191fe 100755
--- a/lock
+++ b/lock.sh