3
0

rmdir.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 0x01
  28. #define IGNORE_NON_EMPTY 0x02
  29. int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int rmdir_main(int argc UNUSED_PARAM, char **argv)
  31. {
  32. int status = EXIT_SUCCESS;
  33. int flags;
  34. char *path;
  35. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  36. static const char rmdir_longopts[] ALIGN1 =
  37. "parents\0" No_argument "p"
  38. /* Debian etch: many packages fail to be purged or installed
  39. * because they desperately want this option: */
  40. "ignore-fail-on-non-empty\0" No_argument "\xff"
  41. ;
  42. applet_long_options = rmdir_longopts;
  43. #endif
  44. flags = getopt32(argv, "p");
  45. argv += optind;
  46. if (!*argv) {
  47. bb_show_usage();
  48. }
  49. do {
  50. path = *argv;
  51. while (1) {
  52. if (rmdir(path) < 0) {
  53. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  54. if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
  55. break;
  56. #endif
  57. bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
  58. status = EXIT_FAILURE;
  59. } else if (flags & PARENTS) {
  60. /* Note: path was not "" since rmdir succeeded. */
  61. path = dirname(path);
  62. /* Path is now just the parent component. Dirname
  63. * returns "." if there are no parents.
  64. */
  65. if (NOT_LONE_CHAR(path, '.')) {
  66. continue;
  67. }
  68. }
  69. break;
  70. }
  71. } while (*++argv);
  72. return status;
  73. }