blob: d000129d9e702be3a8d110daeb3119e6a95e87e3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbed30e91999-10-18 19:02:32 +00002/*
3 * Mini rm implementation for busybox
4 *
Matt Kraai8810bdb2001-04-24 20:04:18 +00005 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenbed30e91999-10-18 19:02:32 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenbed30e91999-10-18 19:02:32 +00008 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
10 *
11 * Size reduction.
12 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config:config RM
Denys Vlasenkob097a842018-12-28 03:20:17 +010014//config: bool "rm (5.4 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: rm is used to remove files or directories.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018
Denys Vlasenko7c40ddd2017-08-02 16:37:39 +020019//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 Vlasenkoaf3f4202016-11-23 14:46:56 +010021
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 cad53642003-03-19 09:13:01 +000026
Pere Orga34425382011-03-31 14:43:25 +020027//usage:#define rm_trivial_usage
28//usage: "[-irf] FILE..."
29//usage:#define rm_full_usage "\n\n"
30//usage: "Remove (unlink) FILEs\n"
Pere Orga34425382011-03-31 14:43:25 +020031//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 Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Denys Vlasenko7c40ddd2017-08-02 16:37:39 +020040/* This is a NOEXEC applet. Be very careful! */
Denis Vlasenko99912ca2007-04-10 15:43:37 +000041
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000043int rm_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenbed30e91999-10-18 19:02:32 +000044{
Matt Kraai8810bdb2001-04-24 20:04:18 +000045 int status = 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +000046 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000047 unsigned opt;
Matt Kraai8810bdb2001-04-24 20:04:18 +000048
Denys Vlasenko22542ec2017-08-08 21:55:02 +020049 opt = getopt32(argv, "^" "fiRrv" "\0" "f-i:i-f");
Denis Vlasenko99912ca2007-04-10 15:43:37 +000050 argv += optind;
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 if (opt & 1)
Denis Vlasenko99912ca2007-04-10 15:43:37 +000052 flags |= FILEUTILS_FORCE;
Denis Vlasenko51742f42007-04-12 00:32:05 +000053 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000054 flags |= FILEUTILS_INTERACTIVE;
Denis Vlasenko4d3a8122009-03-27 17:22:00 +000055 if (opt & (8|4))
Eric Andersen8876fb22003-06-20 09:01:58 +000056 flags |= FILEUTILS_RECUR;
Denys Vlasenko17f84182014-05-19 16:23:50 +020057 if ((opt & 16) && FILEUTILS_VERBOSE)
58 flags |= FILEUTILS_VERBOSE;
Matt Kraai8810bdb2001-04-24 20:04:18 +000059
Denis Vlasenko99912ca2007-04-10 15:43:37 +000060 if (*argv != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 do {
Denis Vlasenko818322b2007-09-24 18:27:04 +000062 const char *base = bb_get_last_path_component_strip(*argv);
Matt Kraai8810bdb2001-04-24 20:04:18 +000063
Denis Vlasenko16abcd92007-04-13 23:59:52 +000064 if (DOT_OR_DOTDOT(base)) {
James Byrne69374872019-07-02 11:35:03 +020065 bb_simple_error_msg("can't remove '.' or '..'");
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 } else if (remove_file(*argv, flags) >= 0) {
67 continue;
68 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000069 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 } while (*++argv);
71 } else if (!(flags & FILEUTILS_FORCE)) {
72 bb_show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +000073 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000074
Matt Kraaid27753a2000-12-05 05:11:41 +000075 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000076}