3
0

mkdir.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #if ENABLE_FEATURE_VERBOSE
  45. "verbose\0" No_argument "v"
  46. #endif
  47. ;
  48. #endif
  49. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  50. int mkdir_main(int argc UNUSED_PARAM, char **argv)
  51. {
  52. long mode = -1;
  53. int status = EXIT_SUCCESS;
  54. int flags = 0;
  55. unsigned opt;
  56. char *smode;
  57. #if ENABLE_SELINUX
  58. security_context_t scontext;
  59. #endif
  60. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  61. applet_long_options = mkdir_longopts;
  62. #endif
  63. opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
  64. if (opt & 1) {
  65. mode_t mmode = 0777;
  66. if (!bb_parse_mode(smode, &mmode)) {
  67. bb_error_msg_and_die("invalid mode '%s'", smode);
  68. }
  69. mode = mmode;
  70. }
  71. if (opt & 2)
  72. flags |= FILEUTILS_RECUR;
  73. if ((opt & 4) && FILEUTILS_VERBOSE)
  74. flags |= FILEUTILS_VERBOSE;
  75. #if ENABLE_SELINUX
  76. if (opt & 8) {
  77. selinux_or_die();
  78. setfscreatecon_or_die(scontext);
  79. }
  80. #endif
  81. argv += optind;
  82. if (!argv[0])
  83. bb_show_usage();
  84. do {
  85. if (bb_make_directory(*argv, mode, flags)) {
  86. status = EXIT_FAILURE;
  87. }
  88. } while (*++argv);
  89. return status;
  90. }