1 /* $OpenBSD: makeinodes.c,v 1.1 2016/07/30 10:56:13 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2016 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 <unistd.h> 22 23 #define HSIZE 64 24 25 int 26 main(void) 27 { 28 struct stat sb1, sb2; 29 long long diff; 30 int fd; 31 32 if (mkdir("man", 0755) == -1) 33 err(1, "mkdir(man)"); 34 if (chdir("man") == -1) 35 err(1, "chdir(man)"); 36 if (mkdir("man1", 0755) == -1) 37 err(1, "mkdir(man1)"); 38 if (chdir("man1") == -1) 39 err(1, "chdir(man1)"); 40 if ((fd = open("1", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) 41 err(1, "open(1)"); 42 if (fstat(fd, &sb1) == -1) 43 err(1, "fstat(1)"); 44 if (close(fd) == -1) 45 err(1, "close(1)"); 46 if ((fd = open("2", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) 47 err(1, "open(2)"); 48 if (fstat(fd, &sb2) == -1) 49 err(1, "fstat(2)"); 50 if (close(fd) == -1) 51 err(1, "close(2)"); 52 while ((diff = sb2.st_ino % HSIZE - sb1.st_ino % HSIZE) == 0) { 53 if ((fd = open("3", O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) 54 err(1, "open(3)"); 55 if (fstat(fd, &sb2) == -1) 56 err(1, "fstat(3)"); 57 if (close(fd) == -1) 58 err(1, "close(3)"); 59 if (rename("3", "2") == -1) 60 err(1, "rename(3, 2)"); 61 } 62 if (diff < 0) { 63 if (rename("1", "3") == -1) 64 err(1, "rename(1, 3)"); 65 if (rename("2", "1") == -1) 66 err(1, "rename(2, 1)"); 67 if (rename("3", "2") == -1) 68 err(1, "rename(3, 2)"); 69 } 70 return 0; 71 } 72