3
0

rmdir.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <stdlib.h>
  12. #include <unistd.h>
  13. #include <libgen.h>
  14. #include "busybox.h"
  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(argc, 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. do {
  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. } while (1);
  47. } while (*++argv);
  48. return status;
  49. }