mkdir.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <getopt.h> /* struct option */
  19. #include "busybox.h"
  20. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  21. static const struct option mkdir_long_options[] = {
  22. { "mode", 1, NULL, 'm' },
  23. { "parents", 0, NULL, 'p' },
  24. { 0, 0, 0, 0 }
  25. };
  26. #endif
  27. int mkdir_main(int argc, char **argv)
  28. {
  29. mode_t mode = (mode_t)(-1);
  30. int status = EXIT_SUCCESS;
  31. int flags = 0;
  32. unsigned opt;
  33. char *smode;
  34. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  35. applet_long_options = mkdir_long_options;
  36. #endif
  37. opt = getopt32(argc, argv, "m:p", &smode);
  38. if (opt & 1) {
  39. mode = 0777;
  40. if (!bb_parse_mode(smode, &mode)) {
  41. bb_error_msg_and_die("invalid mode '%s'", smode);
  42. }
  43. }
  44. if (opt & 2)
  45. flags |= FILEUTILS_RECUR;
  46. if (optind == argc) {
  47. bb_show_usage();
  48. }
  49. argv += optind;
  50. do {
  51. if (bb_make_directory(*argv, mode, flags)) {
  52. status = EXIT_FAILURE;
  53. }
  54. } while (*++argv);
  55. return status;
  56. }