Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / Flock.c
index fb88d1719546f3abbcbc4bde34f35884e36d6c89..90e76614f23b92f2c109d136ea10b837b253d83e 100644 (file)
@@ -26,8 +26,6 @@
  *
  * Usage: flock -h give all info's
  *
- * $Id: Flock.c,v 1.5 2003/05/02 09:55:13 piccardi Exp $
- *
  *****************************************************************************/
 /*
  * Include needed headers
 #include <stdlib.h>      /* C standard library */
 #include <unistd.h>      /* unix standard library */
 #include <stdio.h>      /* standard I/O library */
-#include <string.h>      /* string functions */
-#include <fcntl.h>       /* string functions */
+#include <string.h>      /* C strings library */
+#include <fcntl.h>       /* file control functions */
+#include <sys/file.h>
+
 /* user defined header */
 #include "macros.h"      /* some useful macros */
 
@@ -49,13 +49,13 @@ int main(int argc, char *argv[])
 /* 
  * Variables definition  
  */
-    int type = F_UNLCK;            /* lock type: default to unlock (invalid) */
-    off_t start = 0;             /* start of the locked region: default to 0 */
-    off_t len = 0;              /* length of the locked region: default to 0 */
-    int fd, res, i;                                    /* internal variables */
-    int bsd = 0;                          /* semantic type: default to POSIX */
-    int cmd = F_SETLK;              /* lock command: default to non-blocking */
-    struct flock lock;                                /* file lock structure */
+    int type = F_UNLCK;  /* lock type: default to unlock (invalid) */
+    off_t start = 0;     /* start of the locked region: default to 0 */
+    off_t len = 0;       /* length of the locked region: default to 0 */
+    int fd, res, i;      /* internal variables */
+    int bsd = 0;         /* semantic type: default to POSIX */
+    int cmd = F_SETLK;   /* lock command: default to non-blocking */
+    struct flock lock;   /* file lock structure */
     /*
      * Input section: decode command line parameters 
      * Use getopt function
@@ -66,33 +66,33 @@ int main(int argc, char *argv[])
        /* 
         * Handling options 
         */ 
-       case 'h':                                            /* help option */
+       case 'h':   /* help option */
            printf("Wrong -h option use\n");
            usage();
            return -1;
            break;
-       case 's':                            /* take start point of the lock */
-           start = strtol(optarg, NULL, 10);               /* convert input */
+       case 's':   /* take start point of the lock */
+           start = strtol(optarg, NULL, 10);  /* convert input */
            break;
-       case 'l':                                 /* take length of the lock */
-           len = strtol(optarg, NULL, 10);                 /* convert input */
+       case 'l':   /* take length of the lock */
+           len = strtol(optarg, NULL, 10);    /* convert input */
            break;
-       case 'w':                                  /* set type to write lock */
+       case 'w':   /* set type to write lock */
            type = F_WRLCK;
            break;
-       case 'r':                                   /* set type to read lock */
+       case 'r':   /* set type to read lock */
            type = F_RDLCK;
            break;
-       case 'b':                                    /* set lock to blocking */
+       case 'b':  /* set lock to blocking */
            cmd = F_SETLKW;
            break;
-       case 'f':                                     /* enable BSD semantic */
+       case 'f':  /* enable BSD semantic */
            bsd = 1;
            break;
-       case '?':                                    /* unrecognized options */
+       case '?':  /* unrecognized options */
            printf("Unrecognized options -%c\n",optopt);
            usage();
-       default:                                       /* should not reached */
+       default:   /* should not reached */
            usage();
        }
     }
@@ -103,46 +103,46 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
-    if ((argc - optind) != 1) {          /* There must be remaing parameters */
+    if ((argc - optind) != 1) {  /* There must be remaing parameters */
        printf("Wrong number of arguments %d\n", argc - optind);
         usage();
     }
-    if (type == F_UNLCK) {            /* There must be a -w or -r option set */
+    if (type == F_UNLCK) {       /* There must be a -w or -r option set */
        printf("You should set a read or a write lock\n");
        usage();
     }
-    fd = open(argv[optind], O_RDONLY);         /* open the file to be locked */
-    if (fd < 0) {                                           /* on error exit */
+    fd = open(argv[optind], O_RDONLY);  /* open the file to be locked */
+    if (fd < 0) {                       /* on error exit */
        perror("Wrong filename");
        exit(1);
     }
     /* do lock */
-    if (bsd) {                                             /* if BSD locking */
+    if (bsd) {  /* BSD locking */
        /* rewrite cmd for suitables flock operation values */ 
-       if (cmd == F_SETLK) {                              /* if no-blocking */
-           cmd = LOCK_NB;              /* set the value for flock operation */
-       } else {                                                     /* else */
-           cmd = 0;                                      /* default is null */
+       if (cmd == F_SETLK) {  /* if no-blocking set for flock operation */
+           cmd = LOCK_NB;
+       } else {               /* else default is null */
+           cmd = 0;
        }
-       if (type == F_RDLCK) cmd |= LOCK_SH;          /* set for shared lock */
-       if (type == F_WRLCK) cmd |= LOCK_EX;       /* set for exclusive lock */
-       res = flock(fd, cmd);                                /* esecute lock */
-    } else {                                             /* if POSIX locking */
+       if (type == F_RDLCK) cmd |= LOCK_SH;   /* set for shared lock */
+       if (type == F_WRLCK) cmd |= LOCK_EX;   /* set for exclusive lock */
+       res = flock(fd, cmd);                  /* esecute lock */
+    } else {  /* POSIX locking */
        /* setting flock structure */
-       lock.l_type = type;                       /* set type: read or write */
+       lock.l_type = type;          /* set type: read or write */
        lock.l_whence = SEEK_SET;    /* start from the beginning of the file */
-       lock.l_start = start;          /* set the start of the locked region */
-       lock.l_len = len;             /* set the length of the locked region */
-       res = fcntl(fd, cmd, &lock);                              /* do lock */
+       lock.l_start = start;        /* set the start of the locked region */
+       lock.l_len = len;            /* set the length of the locked region */
+       res = fcntl(fd, cmd, &lock); /* do lock */
     }
     /* check lock results */
-    if (res) {                                              /* on error exit */
+    if (res) {   /* on error exit */
        perror("Failed lock");
        exit(1);
-    } else {                                           /* else write message */
+    } else {     /* else write message */
        printf("Lock acquired\n");
     }
-    pause();                       /* stop the process, use a signal to exit */
+    pause();     /* stop the process, use a signal to exit */
     return 0;
 }
 /*