mknod.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mknod implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include "busybox.h"
  31. int mknod_main(int argc, char **argv)
  32. {
  33. char *thisarg;
  34. mode_t mode = 0;
  35. mode_t perm = 0666;
  36. dev_t dev = 0;
  37. argc--;
  38. argv++;
  39. /* Parse any options */
  40. while (argc > 1) {
  41. if (**argv != '-')
  42. break;
  43. thisarg = *argv;
  44. thisarg++;
  45. switch (*thisarg) {
  46. case 'm':
  47. argc--;
  48. argv++;
  49. parse_mode(*argv, &perm);
  50. umask(0);
  51. break;
  52. default:
  53. show_usage();
  54. }
  55. argc--;
  56. argv++;
  57. }
  58. if (argc != 4 && argc != 2) {
  59. show_usage();
  60. }
  61. switch (argv[1][0]) {
  62. case 'c':
  63. case 'u':
  64. mode = S_IFCHR;
  65. break;
  66. case 'b':
  67. mode = S_IFBLK;
  68. break;
  69. case 'p':
  70. mode = S_IFIFO;
  71. if (argc!=2) {
  72. show_usage();
  73. }
  74. break;
  75. default:
  76. show_usage();
  77. }
  78. if (mode == S_IFCHR || mode == S_IFBLK) {
  79. dev = (atoi(argv[2]) << 8) | atoi(argv[3]);
  80. }
  81. mode |= perm;
  82. if (mknod(argv[0], mode, dev) != 0)
  83. perror_msg_and_die("%s", argv[0]);
  84. return EXIT_SUCCESS;
  85. }