xref: /aosp_15_r20/external/ltp/testcases/network/nfs/nfslock01/nfs_flock_dgen.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2001-2023
4  * Copyright (c) International Business Machines Corp., 2001
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Tool to generate data for testing file locking.
11  * Used in nfslock01.sh.
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 
main(int argc,char ** argv)18 int main(int argc, char **argv)
19 {
20 	int i, j, k, nlines, nchars, ctype;
21 	char c, buf[BUFSIZ];
22 	FILE *fp;
23 
24 	if (argc != 5) {
25 		fprintf(stderr, "usage: <nfs_flock_dgen> <file> <char/line> <lines> <ctype>\n");
26 		exit(2);
27 	}
28 
29 	fp = fopen(argv[1], "w");
30 
31 	nchars = atoi(argv[2]);
32 	nlines = atoi(argv[3]);
33 	ctype = atoi(argv[4]);
34 
35 	if (nchars > BUFSIZ) {
36 		fprintf(stderr, "Exceeded the maximum limit of the buffer (%d)\n", BUFSIZ);
37 		exit(3);
38 	}
39 
40 	if (nchars < 1) {
41 		fprintf(stderr, "<char/line> must be > 0\n");
42 		exit(3);
43 	}
44 
45 	if (nlines < 1) {
46 		fprintf(stderr, "<lines> must be > 0\n");
47 		exit(3);
48 	}
49 
50 	k = 0;
51 	for (i = 1; i <= nlines; i++) {
52 		if (ctype)
53 			c = ((i % 2) ? '1' : '0');
54 		else
55 			c = 'A' + k;
56 
57 		for (j = 0; j < nchars - 1; j++)
58 			buf[j] = c;
59 
60 		fprintf(fp, "%s\n", buf);
61 
62 		if (!ctype) {
63 			if (i != 1 && i % 26 == 0)
64 				k = 0;
65 			else
66 				k++;
67 		}
68 
69 	}
70 
71 	fclose(fp);
72 
73 	return 0;
74 }
75