mkdir.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, 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" USE_SELINUX("Z:"), &smode USE_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. if (optind == argc) {
  59. bb_show_usage();
  60. }
  61. argv += optind;
  62. do {
  63. if (bb_make_directory(*argv, mode, flags)) {
  64. status = EXIT_FAILURE;
  65. }
  66. } while (*++argv);
  67. return status;
  68. }