• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Program for testing file locking. The original data file consists of
3  * characters from 'A' to 'Z'. The data file after running this program
4  * would consist of lines with 1's in even lines and 0's in odd lines.
5  */
6 
7 #include "nfs_flock.h"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 #define BYTES 64
14 #define LINES 16384
15 
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
18 	int i, fd, mac;
19 	int offset = 0;
20 	char buf[BUFSIZ];
21 
22 	if (argc != 3) {
23 		fprintf(stderr, "Usage: %s <mac num> <file name>\n", argv[0]);
24 		exit(2);
25 	}
26 
27 	if ((fd = open(argv[2], O_RDWR)) < 0) {
28 		perror("opening a file");
29 		exit(1);
30 	}
31 
32 	mac = atoi(argv[1]);
33 
34 	/*
35 	 * Replace a line of characters by 1's if it is process one
36 	 * else with 0's. Number of charcters in any line are BYTES-1,
37 	 * the last character being a newline character.
38 	 */
39 	for (i = 0; i < BYTES - 1; i++) {
40 		if (mac == 1)
41 			buf[i] = '1';
42 		else
43 			buf[i] = '0';
44 	}
45 	buf[BYTES - 1] = '\n';
46 
47 	for (i = 0; i < LINES; i++) {
48 		if (mac == 1) {	/* Set the offset to even lines */
49 			if ((i % 2) == 0) {
50 				if (i == 0)
51 					offset = 0;
52 				else
53 					offset += 2 * BYTES;
54 			} else
55 				continue;
56 		} else {	/* Set the offset to odd lines */
57 			if ((i % 2) == 1) {
58 				if (i == 1)
59 					offset = BYTES;
60 				else
61 					offset += 2 * BYTES;
62 			} else
63 				continue;
64 		}
65 
66 		if (writeb_lock(fd, offset, SEEK_SET, BYTES) < 0)
67 			printf("failed in writeb_lock, Errno = %d", errno);
68 
69 		lseek(fd, offset, SEEK_SET);
70 
71 		/* write to the test file */
72 		write(fd, buf, BYTES);
73 
74 		if (unb_lock(fd, offset, SEEK_SET, BYTES) < 0)
75 			printf("failed in unb_lock, Errno = %d", errno);
76 	}
77 	exit(0);
78 }
79