fixed instructions
[tridge/junkcode.git] / lockrun.c
1 #include <unistd.h>
2 #include <fcntl.h>
3
4 /* lock a byte range in a open file */
5 int lock_range(int fd, int offset, int len)
6 {
7         struct flock lock;
8
9         lock.l_type = F_WRLCK;
10         lock.l_whence = SEEK_SET;
11         lock.l_start = offset;
12         lock.l_len = len;
13         lock.l_pid = 0;
14         
15         return fcntl(fd,F_SETLK,&lock) == 0;
16 }
17
18 int main(int argc, char *argv[])
19 {
20         char *cmd, *lockf;
21         int fd;
22         struct flock lock;
23
24         if (argc < 3) {
25                 printf("lockrun <lockfile> <cmd>\n");
26                 exit(1);
27         }
28
29         lockf = argv[1];
30         cmd = argv[2];
31
32         fd = open(lockf, O_CREAT|O_TRUNC|O_RDWR, 0600);
33         if (fd == -1) exit(1);
34
35         lock.l_type = F_WRLCK;
36         lock.l_whence = SEEK_SET;
37         lock.l_start = 0;
38         lock.l_len = 1;
39         lock.l_pid = 0;
40
41         if (fcntl(fd,F_SETLKW,&lock) == 0) {
42                 system(cmd);
43         }
44         return 0;
45 }