3
0

rm.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini rm implementation for busybox
  4. *
  5. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
  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/rm.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Size reduction.
  14. */
  15. //usage:#define rm_trivial_usage
  16. //usage: "[-irf] FILE..."
  17. //usage:#define rm_full_usage "\n\n"
  18. //usage: "Remove (unlink) FILEs\n"
  19. //usage: "\n -i Always prompt before removing"
  20. //usage: "\n -f Never prompt"
  21. //usage: "\n -R,-r Recurse"
  22. //usage:
  23. //usage:#define rm_example_usage
  24. //usage: "$ rm -rf /tmp/foo\n"
  25. #include "libbb.h"
  26. /* This is a NOFORK applet. Be very careful! */
  27. int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  28. int rm_main(int argc UNUSED_PARAM, char **argv)
  29. {
  30. int status = 0;
  31. int flags = 0;
  32. unsigned opt;
  33. opt_complementary = "f-i:i-f";
  34. opt = getopt32(argv, "fiRrv");
  35. argv += optind;
  36. if (opt & 1)
  37. flags |= FILEUTILS_FORCE;
  38. if (opt & 2)
  39. flags |= FILEUTILS_INTERACTIVE;
  40. if (opt & (8|4))
  41. flags |= FILEUTILS_RECUR;
  42. if ((opt & 16) && FILEUTILS_VERBOSE)
  43. flags |= FILEUTILS_VERBOSE;
  44. if (*argv != NULL) {
  45. do {
  46. const char *base = bb_get_last_path_component_strip(*argv);
  47. if (DOT_OR_DOTDOT(base)) {
  48. bb_error_msg("can't remove '.' or '..'");
  49. } else if (remove_file(*argv, flags) >= 0) {
  50. continue;
  51. }
  52. status = 1;
  53. } while (*++argv);
  54. } else if (!(flags & FILEUTILS_FORCE)) {
  55. bb_show_usage();
  56. }
  57. return status;
  58. }