rm.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 tarball for details.
  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. #include "libbb.h"
  16. /* This is a NOFORK applet. Be very careful! */
  17. int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int rm_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. int status = 0;
  21. int flags = 0;
  22. unsigned opt;
  23. opt_complementary = "f-i:i-f";
  24. /* -v (verbose) is ignored */
  25. opt = getopt32(argv, "fiRrv");
  26. argv += optind;
  27. if (opt & 1)
  28. flags |= FILEUTILS_FORCE;
  29. if (opt & 2)
  30. flags |= FILEUTILS_INTERACTIVE;
  31. if (opt & (8|4))
  32. flags |= FILEUTILS_RECUR;
  33. if (*argv != NULL) {
  34. do {
  35. const char *base = bb_get_last_path_component_strip(*argv);
  36. if (DOT_OR_DOTDOT(base)) {
  37. bb_error_msg("can't remove '.' or '..'");
  38. } else if (remove_file(*argv, flags) >= 0) {
  39. continue;
  40. }
  41. status = 1;
  42. } while (*++argv);
  43. } else if (!(flags & FILEUTILS_FORCE)) {
  44. bb_show_usage();
  45. }
  46. return status;
  47. }