logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

mknod.c (3102B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #define _XOPEN_SOURCE 700 // mknod is in XSI
  6. #include "../lib/mode.h"
  7. #include <errno.h>
  8. #include <stdio.h> // fprintf
  9. #include <stdlib.h> // abort, exit
  10. #include <string.h> // strerror, strrchr
  11. #include <sys/stat.h> // mknod
  12. #include <sys/sysmacros.h> // makedev
  13. #include <unistd.h> // getopt
  14. mode_t filemask;
  15. static void
  16. usage(void)
  17. {
  18. fprintf(stderr, "Usage: mknod [-m mode] file <b|c|u|p> [major minor]\n");
  19. }
  20. static unsigned int
  21. strtodev(char *arg)
  22. {
  23. char *endptr = NULL;
  24. long dev = strtol(arg, &endptr, 0);
  25. if(errno != 0)
  26. {
  27. fprintf(stderr, "mknod: error: Failed parsing '%s' into a number: %s\n", arg, strerror(errno));
  28. exit(1);
  29. }
  30. if(endptr != NULL && endptr[0] != 0)
  31. {
  32. fprintf(stderr,
  33. "mknod: error: Extraneous characters in '%s' while parsing it as a number: '%s'\n",
  34. arg,
  35. endptr);
  36. exit(1);
  37. }
  38. return (unsigned int)dev;
  39. }
  40. int
  41. main(int argc, char *argv[])
  42. {
  43. mode_t mode = 0666;
  44. const char *errstr = NULL;
  45. for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;)
  46. {
  47. switch(c)
  48. {
  49. case 'm':
  50. mode = new_mode(optarg, 0666, &errstr);
  51. if(errstr != NULL)
  52. {
  53. fprintf(stderr, "mknod: error: Failed parsing mode '%s': %s\n", optarg, errstr);
  54. return 1;
  55. }
  56. break;
  57. case ':':
  58. fprintf(stderr, "mknod: error: Missing operand for option: '-%c'\n", optopt);
  59. usage();
  60. return 1;
  61. case '?':
  62. fprintf(stderr, "mknod: error: Unrecognised option: '-%c'\n", optopt);
  63. usage();
  64. return 1;
  65. default:
  66. abort();
  67. }
  68. }
  69. argc -= optind;
  70. argv += optind;
  71. if(argc < 2)
  72. {
  73. fprintf(stderr, "mknod: error: Missing operands\n");
  74. usage();
  75. return 1;
  76. }
  77. char *file = argv[0];
  78. char *type = argv[1];
  79. if(type[0] == 0 || type[1] != 0)
  80. {
  81. fprintf(stderr, "mknod: error: Invalid type '%s'\n", type);
  82. usage();
  83. return 1;
  84. }
  85. if(type[0] == 'p')
  86. {
  87. if(mknod(file, mode | S_IFIFO, 0) != 0)
  88. {
  89. fprintf(stderr, "mknod: error: Failed creating FIFO at '%s': %s\n", file, strerror(errno));
  90. return 1;
  91. }
  92. }
  93. else
  94. {
  95. if(argc != 4)
  96. {
  97. fprintf(stderr,
  98. "mknod: error: non-FIFO device types requires major minor arguments to be given\n");
  99. return 1;
  100. }
  101. unsigned int maj = strtodev(argv[2]);
  102. unsigned int min = strtodev(argv[3]);
  103. const char *type_name;
  104. switch(type[0])
  105. {
  106. case 'b':
  107. mode |= S_IFBLK;
  108. type_name = "block-special";
  109. break;
  110. case 'c':
  111. mode |= S_IFCHR;
  112. type_name = "character-special";
  113. break;
  114. case 'u':
  115. mode |= S_IFCHR;
  116. type_name = "character-special";
  117. break;
  118. default:
  119. fprintf(stderr, "mknod: error: Invalid type '%c'\n", type[0]);
  120. usage();
  121. return 1;
  122. }
  123. if(mknod(file, mode, makedev(maj, min)) != 0)
  124. {
  125. fprintf(stderr,
  126. "mknod: error: Failed creating %s at '%s': %s\n",
  127. type_name,
  128. file,
  129. strerror(errno));
  130. return 1;
  131. }
  132. }
  133. return 0;
  134. }