1*7dc8df11Sschwarze /* $OpenBSD: makeinodes.c,v 1.2 2021/07/18 11:25:47 schwarze Exp $ */
2264ca280Sschwarze /*
3*7dc8df11Sschwarze * Copyright (c) 2016, 2021 Ingo Schwarze <schwarze@openbsd.org>
4264ca280Sschwarze *
5264ca280Sschwarze * Permission to use, copy, modify, and distribute this software for any
6264ca280Sschwarze * purpose with or without fee is hereby granted, provided that the above
7264ca280Sschwarze * copyright notice and this permission notice appear in all copies.
8264ca280Sschwarze *
9264ca280Sschwarze * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10264ca280Sschwarze * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11264ca280Sschwarze * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12264ca280Sschwarze * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13264ca280Sschwarze * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14264ca280Sschwarze * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15264ca280Sschwarze * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16264ca280Sschwarze */
17264ca280Sschwarze #include <sys/stat.h>
18264ca280Sschwarze #include <err.h>
19264ca280Sschwarze #include <fcntl.h>
20264ca280Sschwarze #include <stdio.h>
21*7dc8df11Sschwarze #include <string.h>
22264ca280Sschwarze #include <unistd.h>
23264ca280Sschwarze
24264ca280Sschwarze #define HSIZE 64
25264ca280Sschwarze
26264ca280Sschwarze int
main(int argc,char * argv[])27*7dc8df11Sschwarze main(int argc, char *argv[])
28264ca280Sschwarze {
29264ca280Sschwarze struct stat sb1, sb2;
30264ca280Sschwarze long long diff;
31*7dc8df11Sschwarze int fd, rev;
32264ca280Sschwarze
33*7dc8df11Sschwarze if (argc > 1 && strcmp(argv[1], "-r") == 0) {
34*7dc8df11Sschwarze rev = 1;
35*7dc8df11Sschwarze argc--;
36*7dc8df11Sschwarze argv++;
37*7dc8df11Sschwarze } else
38*7dc8df11Sschwarze rev = 0;
39*7dc8df11Sschwarze
40*7dc8df11Sschwarze if (argc != 2) {
41*7dc8df11Sschwarze fputs("usage: makeinodes [-r] dirname\n", stderr);
42*7dc8df11Sschwarze return 1;
43*7dc8df11Sschwarze }
44*7dc8df11Sschwarze if (mkdir(argv[1], 0755) == -1)
45*7dc8df11Sschwarze err(1, "mkdir(%s)", argv[1]);
46*7dc8df11Sschwarze if (chdir(argv[1]) == -1)
47*7dc8df11Sschwarze err(1, "chdir(%s)", argv[1]);
48264ca280Sschwarze if (mkdir("man1", 0755) == -1)
49264ca280Sschwarze err(1, "mkdir(man1)");
50264ca280Sschwarze if (chdir("man1") == -1)
51264ca280Sschwarze err(1, "chdir(man1)");
52264ca280Sschwarze if ((fd = open("1", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
53264ca280Sschwarze err(1, "open(1)");
54264ca280Sschwarze if (fstat(fd, &sb1) == -1)
55264ca280Sschwarze err(1, "fstat(1)");
56264ca280Sschwarze if (close(fd) == -1)
57264ca280Sschwarze err(1, "close(1)");
58264ca280Sschwarze if ((fd = open("2", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
59264ca280Sschwarze err(1, "open(2)");
60264ca280Sschwarze if (fstat(fd, &sb2) == -1)
61264ca280Sschwarze err(1, "fstat(2)");
62264ca280Sschwarze if (close(fd) == -1)
63264ca280Sschwarze err(1, "close(2)");
64264ca280Sschwarze while ((diff = sb2.st_ino % HSIZE - sb1.st_ino % HSIZE) == 0) {
65264ca280Sschwarze if ((fd = open("3", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1)
66264ca280Sschwarze err(1, "open(3)");
67264ca280Sschwarze if (fstat(fd, &sb2) == -1)
68264ca280Sschwarze err(1, "fstat(3)");
69264ca280Sschwarze if (close(fd) == -1)
70264ca280Sschwarze err(1, "close(3)");
71264ca280Sschwarze if (rename("3", "2") == -1)
72264ca280Sschwarze err(1, "rename(3, 2)");
73264ca280Sschwarze }
74*7dc8df11Sschwarze if ((diff < 0) == rev) {
75*7dc8df11Sschwarze if (rename("1", "one.1") == -1)
76*7dc8df11Sschwarze err(1, "rename(1, one)");
77*7dc8df11Sschwarze if (rename("2", "two.1") == -1)
78*7dc8df11Sschwarze err(1, "rename(2, two)");
79*7dc8df11Sschwarze } else {
80*7dc8df11Sschwarze if (rename("2", "one.1") == -1)
81*7dc8df11Sschwarze err(1, "rename(2, one)");
82*7dc8df11Sschwarze if (rename("1", "two.1") == -1)
83*7dc8df11Sschwarze err(1, "rename(1, two)");
84264ca280Sschwarze }
85264ca280Sschwarze return 0;
86264ca280Sschwarze }
87