diff options
author | Calvin Morrison <mutantturkey@gmail.com> | 2014-04-10 11:16:07 -0400 |
---|---|---|
committer | Calvin Morrison <mutantturkey@gmail.com> | 2014-04-10 11:16:07 -0400 |
commit | 01dbecb2e93aa782685c87021a1bc6912b810c4d (patch) | |
tree | 753691167f8be6c93e073a57d7ad34289a32bf5b /lock.c | |
parent | 15b51380182f13ca401d7ab9c39ab3ef5435de1b (diff) |
c version of lock
Diffstat (limited to 'lock.c')
-rw-r--r-- | lock.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -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;
+}
|