3 * Copyright (C) 2001 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Program to test file locking
24 * Author: Simone Piccardi
27 * Usage: flock -h give all info's
29 * $Id: Flock.c,v 1.1 2002/11/16 14:11:55 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
35 #include <errno.h> /* error definitions and routines */
36 #include <stdlib.h> /* C standard library */
37 #include <unistd.h> /* unix standard library */
38 #include <stdio.h> /* standard I/O library */
39 #include <string.h> /* string functions */
40 #include <fcntl.h> /* string functions */
45 /* Help printing routine */
48 int main(int argc, char *argv[])
51 * Variables definition
60 * Input section: decode command line parameters
63 opterr = 0; /* don't want writing to stderr */
64 while ( (i = getopt(argc, argv, "hs:l:wrb")) != -1) {
69 case 'h': /* help option */
70 printf("Wrong -h option use\n");
74 case 's': /* take wait time for childen */
75 start = strtol(optarg, NULL, 10); /* convert input */
77 case 'l': /* take wait time for childen */
78 len = strtol(optarg, NULL, 10); /* convert input */
89 case '?': /* unrecognized options */
90 printf("Unrecognized options -%c\n",optopt);
92 default: /* should not reached */
96 /* ***********************************************************
98 * Options processing completed
100 * Main code beginning
102 * ***********************************************************/
103 /* There must be remaing parameters */
104 if (type == F_UNLCK) {
105 printf("You should set a read or a write lock\n");
108 if ((argc - optind) != 1) {
109 printf("Wrong number of arguments %d\n", argc - optind);
112 fd = open(argv[optind], O_RDWR);
114 perror("Wrong filename");
117 /* setting lock structure */
119 lock.l_whence = SEEK_SET;
120 lock.l_start = start;
123 res = fcntl(fd, cmd, &lock);
125 perror("Failed lock");
132 * routine to print usage info and exit
135 printf("Program flock: lock a region of a file \n");
137 printf(" forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
138 printf(" -h print this help\n");
139 printf(" -s start region starting byte\n");
140 printf(" -l len region length (0 means all file)\n");
141 printf(" -w write lock\n");
142 printf(" -r read lock\n");
143 printf(" -b block when locking impossible\n");