mkdir.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 source tree.
  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. //usage:#define mkdir_trivial_usage
  19. //usage: "[OPTIONS] DIRECTORY..."
  20. //usage:#define mkdir_full_usage "\n\n"
  21. //usage: "Create DIRECTORY\n"
  22. //usage: "\n -m MODE Mode"
  23. //usage: "\n -p No error if exists; make parent directories as needed"
  24. //usage: IF_SELINUX(
  25. //usage: "\n -Z Set security context"
  26. //usage: )
  27. //usage:
  28. //usage:#define mkdir_example_usage
  29. //usage: "$ mkdir /tmp/foo\n"
  30. //usage: "$ mkdir /tmp/foo\n"
  31. //usage: "/tmp/foo: File exists\n"
  32. //usage: "$ mkdir /tmp/foo/bar/baz\n"
  33. //usage: "/tmp/foo/bar/baz: No such file or directory\n"
  34. //usage: "$ mkdir -p /tmp/foo/bar/baz\n"
  35. #include "libbb.h"
  36. /* This is a NOFORK applet. Be very careful! */
  37. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  38. static const char mkdir_longopts[] ALIGN1 =
  39. "mode\0" Required_argument "m"
  40. "parents\0" No_argument "p"
  41. #if ENABLE_SELINUX
  42. "context\0" Required_argument "Z"
  43. #endif
  44. "verbose\0" No_argument "v"
  45. ;
  46. #endif
  47. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  48. int mkdir_main(int argc UNUSED_PARAM, char **argv)
  49. {
  50. long mode = -1;
  51. int status = EXIT_SUCCESS;
  52. int flags = 0;
  53. unsigned opt;
  54. char *smode;
  55. #if ENABLE_SELINUX
  56. security_context_t scontext;
  57. #endif
  58. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  59. applet_long_options = mkdir_longopts;
  60. #endif
  61. opt = getopt32(argv, "m:p" IF_SELINUX("Z:") "v", &smode IF_SELINUX(,&scontext));
  62. if (opt & 1) {
  63. mode_t mmode = 0777;
  64. if (!bb_parse_mode(smode, &mmode)) {
  65. bb_error_msg_and_die("invalid mode '%s'", smode);
  66. }
  67. mode = mmode;
  68. }
  69. if (opt & 2)
  70. flags |= FILEUTILS_RECUR;
  71. #if ENABLE_SELINUX
  72. if (opt & 4) {
  73. selinux_or_die();
  74. setfscreatecon_or_die(scontext);
  75. }
  76. #endif
  77. argv += optind;
  78. if (!argv[0])
  79. bb_show_usage();
  80. do {
  81. if (bb_make_directory(*argv, mode, flags)) {
  82. status = EXIT_FAILURE;
  83. }
  84. } while (*++argv);
  85. return status;
  86. }