rm.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /* -v (verbose) is ignored */
  35. opt = getopt32(argv, "fiRrv");
  36. argv += optind;
  37. if (opt & 1)
  38. flags |= FILEUTILS_FORCE;
  39. if (opt & 2)
  40. flags |= FILEUTILS_INTERACTIVE;
  41. if (opt & (8|4))
  42. flags |= FILEUTILS_RECUR;
  43. if (*argv != NULL) {
  44. do {
  45. const char *base = bb_get_last_path_component_strip(*argv);
  46. if (DOT_OR_DOTDOT(base)) {
  47. bb_error_msg("can't remove '.' or '..'");
  48. } else if (remove_file(*argv, flags) >= 0) {
  49. continue;
  50. }
  51. status = 1;
  52. } while (*++argv);
  53. } else if (!(flags & FILEUTILS_FORCE)) {
  54. bb_show_usage();
  55. }
  56. return status;
  57. }