rmdir.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
  11. #include "libbb.h"
  12. /* This is a NOFORK applet. Be very careful! */
  13. #define PARENTS 0x01
  14. #define IGNORE_NON_EMPTY 0x02
  15. int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  16. int rmdir_main(int argc ATTRIBUTE_UNUSED, char **argv)
  17. {
  18. int status = EXIT_SUCCESS;
  19. int flags;
  20. char *path;
  21. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  22. static const char rmdir_longopts[] ALIGN1 =
  23. "parents\0" No_argument "p"
  24. /* Debian etch: many packages fail to be purged or installed
  25. * because they desperately want this option: */
  26. "ignore-fail-on-non-empty\0" No_argument "\xff"
  27. ;
  28. applet_long_options = rmdir_longopts;
  29. #endif
  30. flags = getopt32(argv, "p");
  31. argv += optind;
  32. if (!*argv) {
  33. bb_show_usage();
  34. }
  35. do {
  36. path = *argv;
  37. while (1) {
  38. if (rmdir(path) < 0) {
  39. #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  40. if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
  41. break;
  42. #endif
  43. bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
  44. status = EXIT_FAILURE;
  45. } else if (flags & PARENTS) {
  46. /* Note: path was not "" since rmdir succeeded. */
  47. path = dirname(path);
  48. /* Path is now just the parent component. Dirname
  49. * returns "." if there are no parents.
  50. */
  51. if (NOT_LONE_CHAR(path, '.')) {
  52. continue;
  53. }
  54. }
  55. break;
  56. }
  57. } while (*++argv);
  58. return status;
  59. }