• 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(argc,argv)16 int main(argc, argv)
17 int argc;
18 char **argv;
19 {
20 	int i, fd, mac;
21 	int offset = 0;
22 	char buf[BUFSIZ];
23 
24 	if (argc != 3) {
25 		fprintf(stderr, "Usage: %s <mac num> <file name>\n", argv[0]);
26 		exit(2);
27 	}
28 
29 	if ((fd = open(argv[2], O_RDWR)) < 0) {
30 		perror("opening a file");
31 		exit(1);
32 	}
33 
34 	mac = atoi(argv[1]);
35 
36 	/*
37 	 * Replace a line of characters by 1's if it is process one
38 	 * else with 0's. Number of charcters in any line are BYTES-1,
39 	 * the last character being a newline character.
40 	 */
41 	for (i = 0; i < BYTES - 1; i++) {
42 		if (mac == 1)
43 			buf[i] = '1';
44 		else
45 			buf[i] = '0';
46 	}
47 	buf[BYTES - 1] = '\n';
48 
49 	for (i = 0; i < LINES; i++) {
50 		if (mac == 1) {	/* Set the offset to even lines */
51 			if ((i % 2) == 0) {
52 				if (i == 0)
53 					offset = 0;
54 				else
55 					offset += 2 * BYTES;
56 			} else
57 				continue;
58 		} else {	/* Set the offset to odd lines */
59 			if ((i % 2) == 1) {
60 				if (i == 1)
61 					offset = BYTES;
62 				else
63 					offset += 2 * BYTES;
64 			} else
65 				continue;
66 		}
67 
68 		if (writeb_lock(fd, offset, SEEK_SET, BYTES) < 0)
69 			printf("failed in writeb_lock, Errno = %d", errno);
70 
71 		lseek(fd, offset, SEEK_SET);
72 
73 		/* write to the test file */
74 		write(fd, buf, BYTES);
75 
76 		if (unb_lock(fd, offset, SEEK_SET, BYTES) < 0)
77 			printf("failed in unb_lock, Errno = %d", errno);
78 	}
79 	exit(0);
80 }
81