rmdir.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
  11. //usage:#define rmdir_trivial_usage
  12. //usage: "[OPTIONS] DIRECTORY..."
  13. //usage:#define rmdir_full_usage "\n\n"
  14. //usage: "Remove DIRECTORY if it is empty\n"
  15. //usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
  16. //usage: "\n -p|--parents Include parents"
  17. //usage: "\n --ignore-fail-on-non-empty"
  18. //usage: )
  19. //usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
  20. //usage: "\n -p Include parents"
  21. //usage: )
  22. //usage:
  23. //usage:#define rmdir_example_usage
  24. //usage: "# rmdir /tmp/foo\n"
  25. #include "libbb.h"
  26. /* This is a NOFORK applet. Be very careful! */
  27. #define PARENTS (1 << 0)
  28. //efine VERBOSE (1 << 1) //accepted but ignored
  29. #define IGNORE_NON_EMPTY (1 << 2)
  30. int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int rmdir_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. int status = EXIT_SUCCESS;
  34. int flags;
  35. char *path;
  36. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  37. static const char rmdir_longopts[] ALIGN1 =
  38. "parents\0" No_argument "p"
  39. "verbose\0" No_argument "v"
  40. /* Debian etch: many packages fail to be purged or installed
  41. * because they desperately want this option: */
  42. "ignore-fail-on-non-empty\0" No_argument "\xff"
  43. ;
  44. applet_long_options = rmdir_longopts;
  45. #endif
  46. flags = getopt32(argv, "pv");
  47. argv += optind;
  48. if (!*argv) {
  49. bb_show_usage();
  50. }
  51. do {
  52. path = *argv;
  53. while (1) {
  54. if (rmdir(path) < 0) {
  55. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  56. if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
  57. break;
  58. #endif
  59. bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
  60. status = EXIT_FAILURE;
  61. } else if (flags & PARENTS) {
  62. /* Note: path was not "" since rmdir succeeded. */
  63. path = dirname(path);
  64. /* Path is now just the parent component. Dirname
  65. * returns "." if there are no parents.
  66. */
  67. if (NOT_LONE_CHAR(path, '.')) {
  68. continue;
  69. }
  70. }
  71. break;
  72. }
  73. } while (*++argv);
  74. return status;
  75. }