rm.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Size reduction.
  12. */
  13. //config:config RM
  14. //config: bool "rm (5.4 kb)"
  15. //config: default y
  16. //config: help
  17. //config: rm is used to remove files or directories.
  18. //applet:IF_RM(APPLET_NOEXEC(rm, rm, BB_DIR_BIN, BB_SUID_DROP, rm))
  19. /* was NOFORK, but then "rm -i FILE" can't be ^C'ed if run by hush */
  20. //kbuild:lib-$(CONFIG_RM) += rm.o
  21. /* BB_AUDIT SUSv3 compliant */
  22. /* http://www.opengroup.org/onlinepubs/007904975/utilities/rm.html */
  23. //usage:#define rm_trivial_usage
  24. //usage: "[-irf] FILE..."
  25. //usage:#define rm_full_usage "\n\n"
  26. //usage: "Remove (unlink) FILEs\n"
  27. //usage: "\n -i Always prompt before removing"
  28. //usage: "\n -f Never prompt"
  29. //usage: "\n -R,-r Recurse"
  30. //usage:
  31. //usage:#define rm_example_usage
  32. //usage: "$ rm -rf /tmp/foo\n"
  33. #include "libbb.h"
  34. /* This is a NOEXEC applet. Be very careful! */
  35. int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int rm_main(int argc UNUSED_PARAM, char **argv)
  37. {
  38. int status = 0;
  39. int flags = 0;
  40. unsigned opt;
  41. opt = getopt32(argv, "^" "fiRrv" "\0" "f-i:i-f");
  42. argv += optind;
  43. if (opt & 1)
  44. flags |= FILEUTILS_FORCE;
  45. if (opt & 2)
  46. flags |= FILEUTILS_INTERACTIVE;
  47. if (opt & (8|4))
  48. flags |= FILEUTILS_RECUR;
  49. if ((opt & 16) && FILEUTILS_VERBOSE)
  50. flags |= FILEUTILS_VERBOSE;
  51. if (*argv != NULL) {
  52. do {
  53. const char *base = bb_get_last_path_component_strip(*argv);
  54. if (DOT_OR_DOTDOT(base)) {
  55. bb_simple_error_msg("can't remove '.' or '..'");
  56. } else if (remove_file(*argv, flags) >= 0) {
  57. continue;
  58. }
  59. status = 1;
  60. } while (*++argv);
  61. } else if (!(flags & FILEUTILS_FORCE)) {
  62. bb_show_usage();
  63. }
  64. return status;
  65. }