X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=listati%2FFlock.c;h=59bc48a55f769e7ca0abb4948cdedd70516b97cf;hp=cf85d3bcac7723dd2dcab34c06ecfae18a4b3863;hb=d47f15496fa85c8ec22edcde608f2665ec5b95ae;hpb=b324b7a09e071b2f84a1849d109d4d14f27f44cd diff --git a/listati/Flock.c b/listati/Flock.c index cf85d3b..59bc48a 100644 --- a/listati/Flock.c +++ b/listati/Flock.c @@ -1,52 +1,52 @@ int main(int argc, char *argv[]) { - 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 */ ... - 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_RDWR); /* open the file to be locked */ - if (fd < 0) { /* on error exit */ + fd = open(argv[optind], O_RDWR); /* 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) { /* if BSD locking */ /* rewrite cmd for suitables flock operation values */ - if (cmd == F_SETLKW) { /* if no-blocking */ - cmd = LOCK_NB; /* set the value for flock operation */ - } else { /* else */ - cmd = 0; /* default is null */ + if (cmd == F_SETLKW) { /* if no-blocking */ + cmd = LOCK_NB; /* set the value for flock operation */ + } else { /* else */ + cmd = 0; /* default is null */ } - 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 { /* if 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; }