rmdir.c 1005 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* rmdir.c - remove directory/path
  2. *
  3. * Copyright 2008 Rob Landley <rob@landley.net>
  4. *
  5. * See http://opengroup.org/onlinepubs/9699919799/utilities/rmdir.html
  6. USE_RMDIR(NEWTOY(rmdir, "<1(ignore-fail-on-non-empty)p(parents)", TOYFLAG_BIN))
  7. config RMDIR
  8. bool "rmdir"
  9. default y
  10. help
  11. usage: rmdir [-p] [DIR...]
  12. Remove one or more directories.
  13. -p Remove path
  14. --ignore-fail-on-non-empty Ignore failures caused by non-empty directories
  15. */
  16. #define FOR_rmdir
  17. #include "toys.h"
  18. static void do_rmdir(char *name)
  19. {
  20. char *temp;
  21. do {
  22. if (rmdir(name)) {
  23. if (!FLAG(ignore_fail_on_non_empty) || errno != ENOTEMPTY)
  24. perror_msg_raw(name);
  25. return;
  26. }
  27. // Each -p cycle back up one slash, ignoring trailing and repeated /.
  28. if (!toys.optflags) return;
  29. do {
  30. if (!(temp = strrchr(name, '/'))) return;
  31. *temp = 0;
  32. } while (!temp[1]);
  33. } while (*name);
  34. }
  35. void rmdir_main(void)
  36. {
  37. char **s;
  38. for (s=toys.optargs; *s; s++) do_rmdir(*s);
  39. }