mkdir.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Fixed broken permission setting when -p was used; especially in
  12. * conjunction with -m.
  13. */
  14. /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
  15. */
  16. //config:config MKDIR
  17. //config: bool "mkdir (4.4 kb)"
  18. //config: default y
  19. //config: help
  20. //config: mkdir is used to create directories with the specified names.
  21. //applet:IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir))
  22. //kbuild:lib-$(CONFIG_MKDIR) += mkdir.o
  23. /* BB_AUDIT SUSv3 compliant */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
  25. //usage:#define mkdir_trivial_usage
  26. //usage: "[OPTIONS] DIRECTORY..."
  27. //usage:#define mkdir_full_usage "\n\n"
  28. //usage: "Create DIRECTORY\n"
  29. //usage: "\n -m MODE Mode"
  30. //usage: "\n -p No error if exists; make parent directories as needed"
  31. //usage: IF_SELINUX(
  32. //usage: "\n -Z Set security context"
  33. //usage: )
  34. //usage:
  35. //usage:#define mkdir_example_usage
  36. //usage: "$ mkdir /tmp/foo\n"
  37. //usage: "$ mkdir /tmp/foo\n"
  38. //usage: "/tmp/foo: File exists\n"
  39. //usage: "$ mkdir /tmp/foo/bar/baz\n"
  40. //usage: "/tmp/foo/bar/baz: No such file or directory\n"
  41. //usage: "$ mkdir -p /tmp/foo/bar/baz\n"
  42. #include "libbb.h"
  43. /* This is a NOFORK applet. Be very careful! */
  44. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  45. int mkdir_main(int argc UNUSED_PARAM, char **argv)
  46. {
  47. long mode = -1;
  48. int status = EXIT_SUCCESS;
  49. int flags = 0;
  50. unsigned opt;
  51. char *smode;
  52. #if ENABLE_SELINUX
  53. security_context_t scontext;
  54. #endif
  55. opt = getopt32long(argv, "m:pv" IF_SELINUX("Z:"),
  56. "mode\0" Required_argument "m"
  57. "parents\0" No_argument "p"
  58. # if ENABLE_SELINUX
  59. "context\0" Required_argument "Z"
  60. # endif
  61. # if ENABLE_FEATURE_VERBOSE
  62. "verbose\0" No_argument "v"
  63. # endif
  64. , &smode IF_SELINUX(,&scontext)
  65. );
  66. if (opt & 1) {
  67. mode_t mmode = bb_parse_mode(smode, 0777);
  68. if (mmode == (mode_t)-1) {
  69. bb_error_msg_and_die("invalid mode '%s'", smode);
  70. }
  71. mode = mmode;
  72. }
  73. if (opt & 2)
  74. flags |= FILEUTILS_RECUR;
  75. if ((opt & 4) && FILEUTILS_VERBOSE)
  76. flags |= FILEUTILS_VERBOSE;
  77. #if ENABLE_SELINUX
  78. if (opt & 8) {
  79. selinux_or_die();
  80. setfscreatecon_or_die(scontext);
  81. }
  82. #endif
  83. argv += optind;
  84. if (!argv[0])
  85. bb_show_usage();
  86. do {
  87. if (bb_make_directory(*argv, mode, flags)) {
  88. status = EXIT_FAILURE;
  89. }
  90. } while (*++argv);
  91. return status;
  92. }