Un po' di materiale su {{{splice}}} e inizio della ripulitura degli
[gapil.git] / sources / Flock.c
1 /* Flock.c
2  * 
3  * Copyright (C) 2001 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /*****************************************************************************
20  *
21  * Program Flock.c: 
22  * Program to test file locking
23  *
24  * Author: Simone Piccardi
25  * Nov. 2002
26  *
27  * Usage: flock -h give all info's
28  *
29  * $Id: Flock.c,v 1.5 2003/05/02 09:55:13 piccardi Exp $
30  *
31  *****************************************************************************/
32 /*
33  * Include needed headers
34  */
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>      /* C strings library */
40 #include <fcntl.h>       /* file control functions */
41
42
43 /* user defined header */
44 #include "macros.h"      /* some useful macros */
45
46 /* Help printing routine */
47 void usage(void);
48
49 int main(int argc, char *argv[])
50 {
51 /* 
52  * Variables definition  
53  */
54     int type = F_UNLCK;            /* lock type: default to unlock (invalid) */
55     off_t start = 0;             /* start of the locked region: default to 0 */
56     off_t len = 0;              /* length of the locked region: default to 0 */
57     int fd, res, i;                                    /* internal variables */
58     int bsd = 0;                          /* semantic type: default to POSIX */
59     int cmd = F_SETLK;              /* lock command: default to non-blocking */
60     struct flock lock;                                /* file lock structure */
61     /*
62      * Input section: decode command line parameters 
63      * Use getopt function
64      */
65     opterr = 0;  /* don't want writing to stderr */
66     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
67         switch (i) {
68         /* 
69          * Handling options 
70          */ 
71         case 'h':                                            /* help option */
72             printf("Wrong -h option use\n");
73             usage();
74             return -1;
75             break;
76         case 's':                            /* take start point of the lock */
77             start = strtol(optarg, NULL, 10);               /* convert input */
78             break;
79         case 'l':                                 /* take length of the lock */
80             len = strtol(optarg, NULL, 10);                 /* convert input */
81             break;
82         case 'w':                                  /* set type to write lock */
83             type = F_WRLCK;
84             break;
85         case 'r':                                   /* set type to read lock */
86             type = F_RDLCK;
87             break;
88         case 'b':                                    /* set lock to blocking */
89             cmd = F_SETLKW;
90             break;
91         case 'f':                                     /* enable BSD semantic */
92             bsd = 1;
93             break;
94         case '?':                                    /* unrecognized options */
95             printf("Unrecognized options -%c\n",optopt);
96             usage();
97         default:                                       /* should not reached */
98             usage();
99         }
100     }
101     /* ***********************************************************
102      * 
103      *           Options processing completed
104      *
105      *                Main code beginning
106      * 
107      * ***********************************************************/
108     if ((argc - optind) != 1) {          /* There must be remaing parameters */
109         printf("Wrong number of arguments %d\n", argc - optind);
110         usage();
111     }
112     if (type == F_UNLCK) {            /* There must be a -w or -r option set */
113         printf("You should set a read or a write lock\n");
114         usage();
115     }
116     fd = open(argv[optind], O_RDONLY);         /* open the file to be locked */
117     if (fd < 0) {                                           /* on error exit */
118         perror("Wrong filename");
119         exit(1);
120     }
121     /* do lock */
122     if (bsd) {                                             /* if BSD locking */
123         /* rewrite cmd for suitables flock operation values */ 
124         if (cmd == F_SETLK) {                              /* if no-blocking */
125             cmd = LOCK_NB;              /* set the value for flock operation */
126         } else {                                                     /* else */
127             cmd = 0;                                      /* default is null */
128         }
129         if (type == F_RDLCK) cmd |= LOCK_SH;          /* set for shared lock */
130         if (type == F_WRLCK) cmd |= LOCK_EX;       /* set for exclusive lock */
131         res = flock(fd, cmd);                                /* esecute lock */
132     } else {                                             /* if POSIX locking */
133         /* setting flock structure */
134         lock.l_type = type;                       /* set type: read or write */
135         lock.l_whence = SEEK_SET;    /* start from the beginning of the file */
136         lock.l_start = start;          /* set the start of the locked region */
137         lock.l_len = len;             /* set the length of the locked region */
138         res = fcntl(fd, cmd, &lock);                              /* do lock */
139     }
140     /* check lock results */
141     if (res) {                                              /* on error exit */
142         perror("Failed lock");
143         exit(1);
144     } else {                                           /* else write message */
145         printf("Lock acquired\n");
146     }
147     pause();                       /* stop the process, use a signal to exit */
148     return 0;
149 }
150 /*
151  * routine to print usage info and exit
152  */
153 void usage(void) {
154     printf("Program flock: lock a region of a file \n");
155     printf("Usage:\n");
156     printf("  forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
157     printf("  -h           print this help\n");
158     printf("  -s start     region starting byte\n");
159     printf("  -l len       region length (0 means all file)\n");
160     printf("  -w           write lock\n");
161     printf("  -r           read lock\n");
162     printf("  -b           block when locking impossible\n");
163     printf("  -f           enable BSD semantic\n");
164     exit(1);
165 }