X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FFlock.c;h=90e76614f23b92f2c109d136ea10b837b253d83e;hp=67a4209010d765968105ce7bef7af3e95e999407;hb=26f7a8bb19c6cb198c213757a97b6ac79e40db4b;hpb=32564231c62ef917086f71a223a1847c859edf0e diff --git a/sources/Flock.c b/sources/Flock.c index 67a4209..90e7661 100644 --- a/sources/Flock.c +++ b/sources/Flock.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/**************************************************************** +/***************************************************************************** * * Program Flock.c: * Program to test file locking @@ -26,9 +26,7 @@ * * Usage: flock -h give all info's * - * $Id: Flock.c,v 1.1 2002/11/16 14:11:55 piccardi Exp $ - * - ****************************************************************/ + *****************************************************************************/ /* * Include needed headers */ @@ -36,11 +34,12 @@ #include /* C standard library */ #include /* unix standard library */ #include /* standard I/O library */ -#include /* string functions */ -#include /* string functions */ - +#include /* C strings library */ +#include /* file control functions */ +#include -#include "macros.h" +/* user defined header */ +#include "macros.h" /* some useful macros */ /* Help printing routine */ void usage(void); @@ -50,18 +49,19 @@ int main(int argc, char *argv[]) /* * Variables definition */ - int type = F_UNLCK; - off_t start; - off_t len; - int fd, res, i; - int cmd = F_SETLK ; - struct flock lock; + 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 */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hs:l:wrb")) != -1) { + while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) { switch (i) { /* * Handling options @@ -71,25 +71,28 @@ int main(int argc, char *argv[]) usage(); return -1; break; - case 's': /* take wait time for childen */ - 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 wait time for childen */ - len = strtol(optarg, NULL, 10); /* convert input */ + case 'l': /* take length of the lock */ + len = strtol(optarg, NULL, 10); /* convert input */ break; - case 'w': + case 'w': /* set type to write lock */ type = F_WRLCK; break; - case 'r': + case 'r': /* set type to read lock */ type = F_RDLCK; break; - case 'b': + case 'b': /* set lock to blocking */ cmd = F_SETLKW; break; - case '?': /* unrecognized options */ + case 'f': /* enable BSD semantic */ + bsd = 1; + break; + case '?': /* unrecognized options */ printf("Unrecognized options -%c\n",optopt); usage(); - default: /* should not reached */ + default: /* should not reached */ usage(); } } @@ -100,32 +103,46 @@ int main(int argc, char *argv[]) * Main code beginning * * ***********************************************************/ - /* There must be remaing parameters */ - if (type == F_UNLCK) { - printf("You should set a read or a write lock\n"); - usage(); - } - if ((argc - optind) != 1) { + if ((argc - optind) != 1) { /* There must be remaing parameters */ printf("Wrong number of arguments %d\n", argc - optind); usage(); } - fd = open(argv[optind], O_RDWR); - if (fd < 0) { + 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 */ perror("Wrong filename"); exit(1); } - /* setting lock structure */ - lock.l_type = type; - lock.l_whence = SEEK_SET; - lock.l_start = start; - lock.l_len = len; /* do lock */ - res = fcntl(fd, cmd, &lock); - if (res) { + if (bsd) { /* BSD locking */ + /* rewrite cmd for suitables flock operation values */ + 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 { /* POSIX locking */ + /* setting flock structure */ + 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 */ + } + /* check lock results */ + if (res) { /* on error exit */ perror("Failed lock"); exit(1); + } else { /* else write message */ + printf("Lock acquired\n"); } - pause(); + pause(); /* stop the process, use a signal to exit */ return 0; } /* @@ -141,6 +158,6 @@ void usage(void) { printf(" -w write lock\n"); printf(" -r read lock\n"); printf(" -b block when locking impossible\n"); - + printf(" -f enable BSD semantic\n"); exit(1); }