X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FFlock.c;fp=sources%2FFlock.c;h=67a4209010d765968105ce7bef7af3e95e999407;hp=0000000000000000000000000000000000000000;hb=32564231c62ef917086f71a223a1847c859edf0e;hpb=8dfb3e378ecbbd8d2c84ef46da9c9002465ef4dd diff --git a/sources/Flock.c b/sources/Flock.c new file mode 100644 index 0000000..67a4209 --- /dev/null +++ b/sources/Flock.c @@ -0,0 +1,146 @@ +/* Flock.c + * + * Copyright (C) 2001 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * 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 + * + * Author: Simone Piccardi + * Nov. 2002 + * + * Usage: flock -h give all info's + * + * $Id: Flock.c,v 1.1 2002/11/16 14:11:55 piccardi Exp $ + * + ****************************************************************/ +/* + * Include needed headers + */ +#include /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* string functions */ +#include /* string functions */ + + +#include "macros.h" + +/* Help printing routine */ +void usage(void); + +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; + /* + * 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) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 's': /* take wait time for childen */ + start = strtol(optarg, NULL, 10); /* convert input */ + break; + case 'l': /* take wait time for childen */ + len = strtol(optarg, NULL, 10); /* convert input */ + break; + case 'w': + type = F_WRLCK; + break; + case 'r': + type = F_RDLCK; + break; + case 'b': + cmd = F_SETLKW; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * 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) { + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + fd = open(argv[optind], O_RDWR); + if (fd < 0) { + 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) { + perror("Failed lock"); + exit(1); + } + pause(); + return 0; +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program flock: lock a region of a file \n"); + printf("Usage:\n"); + printf(" forktest [-h] [-s start] [-l len] [-w|-r] filename \n"); + printf(" -h print this help\n"); + printf(" -s start region starting byte\n"); + printf(" -l len region length (0 means all file)\n"); + printf(" -w write lock\n"); + printf(" -r read lock\n"); + printf(" -b block when locking impossible\n"); + + exit(1); +}