rmdir.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * rmdir implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config RMDIR
  10. //config: bool "rmdir"
  11. //config: default y
  12. //config: help
  13. //config: rmdir is used to remove empty directories.
  14. //config:
  15. //config:config FEATURE_RMDIR_LONG_OPTIONS
  16. //config: bool "Enable long options"
  17. //config: default y
  18. //config: depends on RMDIR && LONG_OPTS
  19. //config: help
  20. //config: Support long options for the rmdir applet, including
  21. //config: --ignore-fail-on-non-empty for compatibility with GNU rmdir.
  22. //applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
  23. //kbuild:lib-$(CONFIG_RMDIR) += rmdir.o
  24. /* BB_AUDIT SUSv3 compliant */
  25. /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
  26. //usage:#define rmdir_trivial_usage
  27. //usage: "[OPTIONS] DIRECTORY..."
  28. //usage:#define rmdir_full_usage "\n\n"
  29. //usage: "Remove DIRECTORY if it is empty\n"
  30. //usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
  31. //usage: "\n -p|--parents Include parents"
  32. //usage: "\n --ignore-fail-on-non-empty"
  33. //usage: )
  34. //usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
  35. //usage: "\n -p Include parents"
  36. //usage: )
  37. //usage:
  38. //usage:#define rmdir_example_usage
  39. //usage: "# rmdir /tmp/foo\n"
  40. #include "libbb.h"
  41. /* This is a NOFORK applet. Be very careful! */
  42. #define PARENTS (1 << 0)
  43. #define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
  44. #define IGNORE_NON_EMPTY (1 << 2)
  45. int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  46. int rmdir_main(int argc UNUSED_PARAM, char **argv)
  47. {
  48. int status = EXIT_SUCCESS;
  49. int flags;
  50. char *path;
  51. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  52. static const char rmdir_longopts[] ALIGN1 =
  53. "parents\0" No_argument "p"
  54. /* Debian etch: many packages fail to be purged or installed
  55. * because they desperately want this option: */
  56. "ignore-fail-on-non-empty\0" No_argument "\xff"
  57. IF_FEATURE_VERBOSE(
  58. "verbose\0" No_argument "v"
  59. )
  60. ;
  61. applet_long_options = rmdir_longopts;
  62. #endif
  63. flags = getopt32(argv, "pv");
  64. argv += optind;
  65. if (!*argv) {
  66. bb_show_usage();
  67. }
  68. do {
  69. path = *argv;
  70. while (1) {
  71. if (flags & VERBOSE) {
  72. printf("rmdir: removing directory, '%s'\n", path);
  73. }
  74. if (rmdir(path) < 0) {
  75. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  76. if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
  77. break;
  78. #endif
  79. bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
  80. status = EXIT_FAILURE;
  81. } else if (flags & PARENTS) {
  82. /* Note: path was not "" since rmdir succeeded. */
  83. path = dirname(path);
  84. /* Path is now just the parent component. Dirname
  85. * returns "." if there are no parents.
  86. */
  87. if (NOT_LONE_CHAR(path, '.')) {
  88. continue;
  89. }
  90. }
  91. break;
  92. }
  93. } while (*++argv);
  94. return status;
  95. }