3
0

rmdir.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <libgen.h>
  12. #include "libbb.h"
  13. /* This is a NOFORK applet. Be very careful! */
  14. int rmdir_main(int argc, char **argv);
  15. int rmdir_main(int argc, char **argv)
  16. {
  17. int status = EXIT_SUCCESS;
  18. int flags;
  19. int do_dot;
  20. char *path;
  21. flags = getopt32(argv, "p");
  22. argv += optind;
  23. if (!*argv) {
  24. bb_show_usage();
  25. }
  26. do {
  27. path = *argv;
  28. /* Record if the first char was a '.' so we can use dirname later. */
  29. do_dot = (*path == '.');
  30. while (1) {
  31. if (rmdir(path) < 0) {
  32. bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
  33. status = EXIT_FAILURE;
  34. } else if (flags) {
  35. /* Note: path was not empty or null since rmdir succeeded. */
  36. path = dirname(path);
  37. /* Path is now just the parent component. Note that dirname
  38. * returns "." if there are no parents. We must distinguish
  39. * this from the case of the original path starting with '.'.
  40. */
  41. if (do_dot || (*path != '.') || path[1]) {
  42. continue;
  43. }
  44. }
  45. break;
  46. }
  47. } while (*++argv);
  48. return status;
  49. }