mkdir.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 struct option mkdir_long_options[] = {
  23. { "mode" , 1, NULL, 'm' },
  24. { "parents", 0, NULL, 'p' },
  25. #if ENABLE_SELINUX
  26. { "context", 1, NULL, 'Z' },
  27. #endif
  28. { NULL, 0, NULL, 0 }
  29. };
  30. #endif
  31. int mkdir_main(int argc, char **argv);
  32. int mkdir_main(int argc, char **argv)
  33. {
  34. mode_t mode = (mode_t)(-1);
  35. int status = EXIT_SUCCESS;
  36. int flags = 0;
  37. unsigned opt;
  38. char *smode;
  39. #if ENABLE_SELINUX
  40. security_context_t scontext;
  41. #endif
  42. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  43. applet_long_options = mkdir_long_options;
  44. #endif
  45. opt = getopt32(argc, argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext));
  46. if (opt & 1) {
  47. mode = 0777;
  48. if (!bb_parse_mode(smode, &mode)) {
  49. bb_error_msg_and_die("invalid mode '%s'", smode);
  50. }
  51. }
  52. if (opt & 2)
  53. flags |= FILEUTILS_RECUR;
  54. #if ENABLE_SELINUX
  55. if (opt & 4) {
  56. selinux_or_die();
  57. setfscreatecon_or_die(scontext);
  58. }
  59. #endif
  60. if (optind == argc) {
  61. bb_show_usage();
  62. }
  63. argv += optind;
  64. do {
  65. if (bb_make_directory(*argv, mode, flags)) {
  66. status = EXIT_FAILURE;
  67. }
  68. } while (*++argv);
  69. return status;
  70. }