3
0

mknod.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mknod implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
  10. #include <sys/sysmacros.h> // For makedev
  11. #include "libbb.h"
  12. #include "libcoreutils/coreutils.h"
  13. /* This is a NOEXEC applet. Be very careful! */
  14. static const char modes_chars[] ALIGN1 = { 'p', 'c', 'u', 'b', 0, 1, 1, 2 };
  15. static const mode_t modes_cubp[] = { S_IFIFO, S_IFCHR, S_IFBLK };
  16. int mknod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  17. int mknod_main(int argc, char **argv)
  18. {
  19. mode_t mode;
  20. dev_t dev;
  21. const char *name;
  22. mode = getopt_mk_fifo_nod(argv);
  23. argv += optind;
  24. argc -= optind;
  25. if (argc >= 2) {
  26. name = strchr(modes_chars, argv[1][0]);
  27. if (name != NULL) {
  28. mode |= modes_cubp[(int)(name[4])];
  29. dev = 0;
  30. if (*name != 'p') {
  31. argc -= 2;
  32. if (argc == 2) {
  33. /* Autodetect what the system supports; these macros should
  34. * optimize out to two constants. */
  35. dev = makedev(xatoul_range(argv[2], 0, major(UINT_MAX)),
  36. xatoul_range(argv[3], 0, minor(UINT_MAX)));
  37. }
  38. }
  39. if (argc == 2) {
  40. name = *argv;
  41. if (mknod(name, mode, dev) == 0) {
  42. return EXIT_SUCCESS;
  43. }
  44. bb_simple_perror_msg_and_die(name);
  45. }
  46. }
  47. }
  48. bb_show_usage();
  49. }