xref: /openbsd-src/regress/usr.bin/mandoc/db/makeinodes/makeinodes.c (revision 7dc8df11d45673c7ba18477a1132a256e95157ef)
1 /*	$OpenBSD: makeinodes.c,v 1.2 2021/07/18 11:25:47 schwarze Exp $ */
2 /*
3  * Copyright (c) 2016, 2021 Ingo Schwarze <schwarze@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include <sys/stat.h>
18 #include <err.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #define	HSIZE	 64
25 
26 int
main(int argc,char * argv[])27 main(int argc, char *argv[])
28 {
29 	struct stat	 sb1, sb2;
30 	long long	 diff;
31 	int		 fd, rev;
32 
33 	if (argc > 1 && strcmp(argv[1], "-r") == 0) {
34 		rev = 1;
35 		argc--;
36 		argv++;
37 	} else
38 		rev = 0;
39 
40 	if (argc != 2) {
41 		fputs("usage: makeinodes [-r] dirname\n", stderr);
42 		return 1;
43 	}
44 	if (mkdir(argv[1], 0755) == -1)
45 		err(1, "mkdir(%s)", argv[1]);
46 	if (chdir(argv[1]) == -1)
47 		err(1, "chdir(%s)", argv[1]);
48 	if (mkdir("man1", 0755) == -1)
49 		err(1, "mkdir(man1)");
50 	if (chdir("man1") == -1)
51 		err(1, "chdir(man1)");
52 	if ((fd = open("1", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
53 		err(1, "open(1)");
54 	if (fstat(fd, &sb1) == -1)
55 		err(1, "fstat(1)");
56 	if (close(fd) == -1)
57 		err(1, "close(1)");
58 	if ((fd = open("2", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
59 		err(1, "open(2)");
60 	if (fstat(fd, &sb2) == -1)
61 		err(1, "fstat(2)");
62 	if (close(fd) == -1)
63 		err(1, "close(2)");
64 	while ((diff = sb2.st_ino % HSIZE - sb1.st_ino % HSIZE) == 0) {
65 		if ((fd = open("3", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
66 			err(1, "open(3)");
67 		if (fstat(fd, &sb2) == -1)
68 			err(1, "fstat(3)");
69 		if (close(fd) == -1)
70 			err(1, "close(3)");
71 		if (rename("3", "2") == -1)
72 			err(1, "rename(3, 2)");
73 	}
74 	if ((diff < 0) == rev) {
75 		if (rename("1", "one.1") == -1)
76 			err(1, "rename(1, one)");
77 		if (rename("2", "two.1") == -1)
78 			err(1, "rename(2, two)");
79 	} else {
80 		if (rename("2", "one.1") == -1)
81 			err(1, "rename(2, one)");
82 		if (rename("1", "two.1") == -1)
83 			err(1, "rename(1, two)");
84 	}
85 	return 0;
86 }
87