aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE32
-rw-r--r--Makefile16
-rw-r--r--lock.c54
-rwxr-xr-xlock.sh (renamed from lock)0
4 files changed, 102 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..15c098a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,32 @@
+Copyright (c) 2014, Calvin Morrison, Laslo Hunhold
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+notice, this list of conditions and the following disclaimer in the
+documentation and/or other materials provided with the distribution.
+
+* Neither the name of the <organization> nor the
+names of its contributors may be used to endorse or promote products
+derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED.
+
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+
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