1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2001-2023
4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Program for testing file locking. The original data file consists of
11f08c3bdfSopenharmony_ci * characters from 'A' to 'Z'. The data file after running this program
12f08c3bdfSopenharmony_ci * would consist of lines with 1's in even lines and 0's in odd lines.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * Used in nfslock01.sh.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "nfs_flock.h"
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <stdio.h>
20f08c3bdfSopenharmony_ci#include <stdlib.h>
21f08c3bdfSopenharmony_ci#include <unistd.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ciint main(int argc, char **argv)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	int i, fd, mac, nchars, nlines;
26f08c3bdfSopenharmony_ci	int offset = 0;
27f08c3bdfSopenharmony_ci	char buf[BUFSIZ];
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	if (argc != 5) {
30f08c3bdfSopenharmony_ci		fprintf(stderr, "Usage: %s <mac num> <file name> <nchars> <nlines>\n",
31f08c3bdfSopenharmony_ci				argv[0]);
32f08c3bdfSopenharmony_ci		exit(2);
33f08c3bdfSopenharmony_ci	}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	fd = open(argv[2], O_RDWR);
36f08c3bdfSopenharmony_ci	if (fd < 0) {
37f08c3bdfSopenharmony_ci		perror("opening a file");
38f08c3bdfSopenharmony_ci		exit(1);
39f08c3bdfSopenharmony_ci	}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	mac = atoi(argv[1]);
42f08c3bdfSopenharmony_ci	nchars = atoi(argv[3]);
43f08c3bdfSopenharmony_ci	nlines = atoi(argv[4]);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	if (nchars > BUFSIZ) {
46f08c3bdfSopenharmony_ci		fprintf(stderr, "Exceeded the maximum limit of the buffer (%d)\n", BUFSIZ);
47f08c3bdfSopenharmony_ci		exit(3);
48f08c3bdfSopenharmony_ci	}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (nchars < 1) {
51f08c3bdfSopenharmony_ci		fprintf(stderr, "<char/line> must be > 0\n");
52f08c3bdfSopenharmony_ci		exit(3);
53f08c3bdfSopenharmony_ci	}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	if (nlines < 1) {
56f08c3bdfSopenharmony_ci		fprintf(stderr, "<lines> must be > 0\n");
57f08c3bdfSopenharmony_ci		exit(3);
58f08c3bdfSopenharmony_ci	}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	/*
61f08c3bdfSopenharmony_ci	 * Replace a line of characters by 1's if it is process one
62f08c3bdfSopenharmony_ci	 * else with 0's. Number of charcters in any line are nchars-1,
63f08c3bdfSopenharmony_ci	 * the last character being a newline character.
64f08c3bdfSopenharmony_ci	 */
65f08c3bdfSopenharmony_ci	for (i = 0; i < nchars - 1; i++) {
66f08c3bdfSopenharmony_ci		if (mac == 1)
67f08c3bdfSopenharmony_ci			buf[i] = '1';
68f08c3bdfSopenharmony_ci		else
69f08c3bdfSopenharmony_ci			buf[i] = '0';
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci	buf[nchars - 1] = '\n';
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	for (i = 0; i < nlines; i++) {
74f08c3bdfSopenharmony_ci		if (mac == 1) {	/* Set the offset to even lines */
75f08c3bdfSopenharmony_ci			if ((i % 2) == 0) {
76f08c3bdfSopenharmony_ci				if (i == 0)
77f08c3bdfSopenharmony_ci					offset = 0;
78f08c3bdfSopenharmony_ci				else
79f08c3bdfSopenharmony_ci					offset += 2 * nchars;
80f08c3bdfSopenharmony_ci			} else
81f08c3bdfSopenharmony_ci				continue;
82f08c3bdfSopenharmony_ci		} else {	/* Set the offset to odd lines */
83f08c3bdfSopenharmony_ci			if ((i % 2) == 1) {
84f08c3bdfSopenharmony_ci				if (i == 1)
85f08c3bdfSopenharmony_ci					offset = nchars;
86f08c3bdfSopenharmony_ci				else
87f08c3bdfSopenharmony_ci					offset += 2 * nchars;
88f08c3bdfSopenharmony_ci			} else
89f08c3bdfSopenharmony_ci				continue;
90f08c3bdfSopenharmony_ci		}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci		if (writeb_lock(fd, offset, SEEK_SET, nchars) < 0) {
93f08c3bdfSopenharmony_ci			fprintf(stderr, "failed in writeb_lock, errno = %d\n", errno);
94f08c3bdfSopenharmony_ci			exit(1);
95f08c3bdfSopenharmony_ci		}
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci		lseek(fd, offset, SEEK_SET);
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci		if (write(fd, buf, nchars) < 0) {
100f08c3bdfSopenharmony_ci			fprintf(stderr, "failed to write buffer to test file, errno = %d\n", errno);
101f08c3bdfSopenharmony_ci			exit(1);
102f08c3bdfSopenharmony_ci		}
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci		if (unb_lock(fd, offset, SEEK_SET, nchars) < 0) {
105f08c3bdfSopenharmony_ci			fprintf(stderr, "failed in unb_lock, errno = %d\n", errno);
106f08c3bdfSopenharmony_ci			exit(1);
107f08c3bdfSopenharmony_ci		}
108f08c3bdfSopenharmony_ci	}
109f08c3bdfSopenharmony_ci	exit(0);
110f08c3bdfSopenharmony_ci}
111