3
0

mkdir.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "libbb.h"
  19. /* This is a NOFORK applet. Be very careful! */
  20. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  21. static const char mkdir_longopts[] ALIGN1 =
  22. "mode\0" Required_argument "m"
  23. "parents\0" No_argument "p"
  24. #if ENABLE_SELINUX
  25. "context\0" Required_argument "Z"
  26. #endif
  27. ;
  28. #endif
  29. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int mkdir_main(int argc UNUSED_PARAM, char **argv)
  31. {
  32. mode_t mode = (mode_t)(-1);
  33. int status = EXIT_SUCCESS;
  34. int flags = 0;
  35. unsigned opt;
  36. char *smode;
  37. #if ENABLE_SELINUX
  38. security_context_t scontext;
  39. #endif
  40. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  41. applet_long_options = mkdir_longopts;
  42. #endif
  43. opt = getopt32(argv, "m:p" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
  44. if (opt & 1) {
  45. mode = 0777;
  46. if (!bb_parse_mode(smode, &mode)) {
  47. bb_error_msg_and_die("invalid mode '%s'", smode);
  48. }
  49. }
  50. if (opt & 2)
  51. flags |= FILEUTILS_RECUR;
  52. #if ENABLE_SELINUX
  53. if (opt & 4) {
  54. selinux_or_die();
  55. setfscreatecon_or_die(scontext);
  56. }
  57. #endif
  58. argv += optind;
  59. if (!argv[0])
  60. bb_show_usage();
  61. do {
  62. if (bb_make_directory(*argv, mode, flags)) {
  63. status = EXIT_FAILURE;
  64. }
  65. } while (*++argv);
  66. return status;
  67. }