mknod.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* $OpenBSD: mknod.c,v 1.13 2003/06/02 20:06:15 millert Exp $ */
  2. /* $NetBSD: mknod.c,v 1.8 1995/08/11 00:08:18 jtc Exp $ */
  3. /*
  4. * Copyright (c) 1989, 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Kevin Fall.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <sys/cdefs.h>
  35. __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
  36. The Regents of the University of California. All rights reserved.\n");
  37. __SCCSID("@(#)mknod.c 8.1 (Berkeley) 6/5/93");
  38. __RCSID("$MirOS: src/sbin/mknod/mknod.c,v 1.3 2016/01/02 21:33:04 tg Exp $");
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <locale.h>
  46. #include <err.h>
  47. extern char *__progname;
  48. int domknod(int, char **, mode_t);
  49. int domkfifo(int, char **, mode_t);
  50. void usage(int);
  51. extern mode_t getmode(const void *, mode_t);
  52. extern void *setmode(const char *);
  53. int
  54. main(int argc, char *argv[])
  55. {
  56. int ch, ismkfifo = 0;
  57. void *set = NULL;
  58. mode_t mode = 0;
  59. if (strcmp(__progname, "mkfifo") == 0)
  60. ismkfifo = 1;
  61. while ((ch = getopt(argc, argv, "m:")) != -1)
  62. switch(ch) {
  63. case 'm':
  64. if (!(set = setmode(optarg))) {
  65. errx(1, "invalid file mode.");
  66. /* NOTREACHED */
  67. }
  68. /*
  69. * In symbolic mode strings, the + and - operators are
  70. * interpreted relative to an assumed initial mode of
  71. * a=rw.
  72. */
  73. mode = getmode(set, DEFFILEMODE);
  74. free(set);
  75. break;
  76. case '?':
  77. default:
  78. usage(ismkfifo);
  79. }
  80. argc -= optind;
  81. argv += optind;
  82. if (argv[0] == NULL)
  83. usage(ismkfifo);
  84. if (!ismkfifo) {
  85. if (argc == 2 && argv[1][0] == 'p') {
  86. ismkfifo = 2;
  87. argc--;
  88. argv[1] = NULL;
  89. } else if (argc != 4) {
  90. usage(ismkfifo);
  91. /* NOTREACHED */
  92. }
  93. }
  94. /*
  95. * If the user specified a mode via `-m', don't allow the umask
  96. * to modified it. If no `-m' flag was specified, the default
  97. * mode is the value of the bitwise inclusive or of S_IRUSR,
  98. * S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH as modified by
  99. * the umask.
  100. */
  101. if (set)
  102. (void)umask(0);
  103. else
  104. mode = DEFFILEMODE;
  105. if (ismkfifo)
  106. exit(domkfifo(argc, argv, mode));
  107. else
  108. exit(domknod(argc, argv, mode));
  109. }
  110. int
  111. domknod(int argc, char **argv, mode_t mode)
  112. {
  113. dev_t dev;
  114. char *endp;
  115. u_int major, minor;
  116. if (argv[1][0] == 'c')
  117. mode |= S_IFCHR;
  118. else if (argv[1][0] == 'b')
  119. mode |= S_IFBLK;
  120. else {
  121. errx(1, "node must be type 'b' or 'c'.");
  122. /* NOTREACHED */
  123. }
  124. major = (long)strtoul(argv[2], &endp, 0);
  125. if (endp == argv[2] || *endp != '\0') {
  126. errx(1, "non-numeric major number.");
  127. /* NOTREACHED */
  128. }
  129. minor = (long)strtoul(argv[3], &endp, 0);
  130. if (endp == argv[3] || *endp != '\0') {
  131. errx(1, "non-numeric minor number.");
  132. /* NOTREACHED */
  133. }
  134. dev = makedev(major, minor);
  135. if (major(dev) != major || minor(dev) != minor) {
  136. errx(1, "major or minor number too large");
  137. /* NOTREACHED */
  138. }
  139. if (mknod(argv[0], mode, dev) < 0) {
  140. err(1, "%s", argv[0]);
  141. /* NOTREACHED */
  142. }
  143. return(0);
  144. }
  145. int
  146. domkfifo(int argc, char **argv, mode_t mode)
  147. {
  148. int rv;
  149. for (rv = 0; *argv; ++argv) {
  150. if (mkfifo(*argv, mode) < 0) {
  151. warn("%s", *argv);
  152. rv = 1;
  153. }
  154. }
  155. return(rv);
  156. }
  157. void
  158. usage(int ismkfifo)
  159. {
  160. if (ismkfifo == 1)
  161. (void)fprintf(stderr, "usage: %s [-m mode] fifoname ...\n",
  162. __progname);
  163. else {
  164. (void)fprintf(stderr, "usage: %s [-m mode] name [b | c] major minor\n",
  165. __progname);
  166. (void)fprintf(stderr, "usage: %s [-m mode] name p\n",
  167. __progname);
  168. }
  169. exit(1);
  170. }