Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rm implementation for busybox |
| 4 | * |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 8 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 9 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 10 | * |
| 11 | * Size reduction. |
| 12 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config:config RM |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame^] | 14 | //config: bool "rm (5.4 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 17 | //config: rm is used to remove files or directories. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 19 | //applet:IF_RM(APPLET_NOEXEC(rm, rm, BB_DIR_BIN, BB_SUID_DROP, rm)) |
| 20 | /* was NOFORK, but then "rm -i FILE" can't be ^C'ed if run by hush */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 21 | |
| 22 | //kbuild:lib-$(CONFIG_RM) += rm.o |
| 23 | |
| 24 | /* BB_AUDIT SUSv3 compliant */ |
| 25 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/rm.html */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 27 | //usage:#define rm_trivial_usage |
| 28 | //usage: "[-irf] FILE..." |
| 29 | //usage:#define rm_full_usage "\n\n" |
| 30 | //usage: "Remove (unlink) FILEs\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 31 | //usage: "\n -i Always prompt before removing" |
| 32 | //usage: "\n -f Never prompt" |
| 33 | //usage: "\n -R,-r Recurse" |
| 34 | //usage: |
| 35 | //usage:#define rm_example_usage |
| 36 | //usage: "$ rm -rf /tmp/foo\n" |
| 37 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 38 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 40 | /* This is a NOEXEC applet. Be very careful! */ |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 42 | int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 43 | int rm_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 44 | { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 45 | int status = 0; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 46 | int flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 47 | unsigned opt; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 49 | opt = getopt32(argv, "^" "fiRrv" "\0" "f-i:i-f"); |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 50 | argv += optind; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 51 | if (opt & 1) |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 52 | flags |= FILEUTILS_FORCE; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 53 | if (opt & 2) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 54 | flags |= FILEUTILS_INTERACTIVE; |
Denis Vlasenko | 4d3a812 | 2009-03-27 17:22:00 +0000 | [diff] [blame] | 55 | if (opt & (8|4)) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 56 | flags |= FILEUTILS_RECUR; |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 57 | if ((opt & 16) && FILEUTILS_VERBOSE) |
| 58 | flags |= FILEUTILS_VERBOSE; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 60 | if (*argv != NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | do { |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 62 | const char *base = bb_get_last_path_component_strip(*argv); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 64 | if (DOT_OR_DOTDOT(base)) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 65 | bb_error_msg("can't remove '.' or '..'"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | } else if (remove_file(*argv, flags) >= 0) { |
| 67 | continue; |
| 68 | } |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 69 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | } while (*++argv); |
| 71 | } else if (!(flags & FILEUTILS_FORCE)) { |
| 72 | bb_show_usage(); |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 73 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | |
Matt Kraai | d27753a | 2000-12-05 05:11:41 +0000 | [diff] [blame] | 75 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | } |