mkdir.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mkdir implementation for busybox
  4. *
  5. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Fixed broken permission setting when -p was used; especially in
  14. * conjunction with -m.
  15. */
  16. /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
  17. */
  18. #include <getopt.h> /* struct option */
  19. #include "libbb.h"
  20. /* This is a NOFORK applet. Be very careful! */
  21. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  22. static const char mkdir_longopts[] ALIGN1 =
  23. "mode\0" Required_argument "m"
  24. "parents\0" No_argument "p"
  25. #if ENABLE_SELINUX
  26. "context\0" Required_argument "Z"
  27. #endif
  28. ;
  29. #endif
  30. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int mkdir_main(int argc, char **argv)
  32. {
  33. mode_t mode = (mode_t)(-1);
  34. int status = EXIT_SUCCESS;
  35. int flags = 0;
  36. unsigned opt;
  37. char *smode;
  38. #if ENABLE_SELINUX
  39. security_context_t scontext;
  40. #endif
  41. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  42. applet_long_options = mkdir_longopts;
  43. #endif
  44. opt = getopt32(argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext));
  45. if (opt & 1) {
  46. mode = 0777;
  47. if (!bb_parse_mode(smode, &mode)) {
  48. bb_error_msg_and_die("invalid mode '%s'", smode);
  49. }
  50. }
  51. if (opt & 2)
  52. flags |= FILEUTILS_RECUR;
  53. #if ENABLE_SELINUX
  54. if (opt & 4) {
  55. selinux_or_die();
  56. setfscreatecon_or_die(scontext);
  57. }
  58. #endif
  59. if (optind == argc) {
  60. bb_show_usage();
  61. }
  62. argv += optind;
  63. do {
  64. if (bb_make_directory(*argv, mode, flags)) {
  65. status = EXIT_FAILURE;
  66. }
  67. } while (*++argv);
  68. return status;
  69. }