blob: 1774ce296323589a6fda6e97b0453381ba4d6d7d [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbed30e91999-10-18 19:02:32 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/rm.html */
12
13/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Size reduction.
16 */
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Denis Vlasenko99912ca2007-04-10 15:43:37 +000020/* This is a NOFORK applet. Be very careful! */
21
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000022int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000023int rm_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersenbed30e91999-10-18 19:02:32 +000024{
Matt Kraai8810bdb2001-04-24 20:04:18 +000025 int status = 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +000026 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000027 unsigned opt;
Matt Kraai8810bdb2001-04-24 20:04:18 +000028
Denis Vlasenko67b23e62006-10-03 21:00:06 +000029 opt_complementary = "f-i:i-f";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000030 opt = getopt32(argv, "fiRr");
Denis Vlasenko99912ca2007-04-10 15:43:37 +000031 argv += optind;
Denis Vlasenko51742f42007-04-12 00:32:05 +000032 if (opt & 1)
Denis Vlasenko99912ca2007-04-10 15:43:37 +000033 flags |= FILEUTILS_FORCE;
Denis Vlasenko51742f42007-04-12 00:32:05 +000034 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000035 flags |= FILEUTILS_INTERACTIVE;
Denis Vlasenko51742f42007-04-12 00:32:05 +000036 if (opt & 12)
Eric Andersen8876fb22003-06-20 09:01:58 +000037 flags |= FILEUTILS_RECUR;
Matt Kraai8810bdb2001-04-24 20:04:18 +000038
Denis Vlasenko99912ca2007-04-10 15:43:37 +000039 if (*argv != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 do {
Denis Vlasenko818322b2007-09-24 18:27:04 +000041 const char *base = bb_get_last_path_component_strip(*argv);
Matt Kraai8810bdb2001-04-24 20:04:18 +000042
Denis Vlasenko16abcd92007-04-13 23:59:52 +000043 if (DOT_OR_DOTDOT(base)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000044 bb_error_msg("cannot remove '.' or '..'");
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 } else if (remove_file(*argv, flags) >= 0) {
46 continue;
47 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000048 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 } while (*++argv);
50 } else if (!(flags & FILEUTILS_FORCE)) {
51 bb_show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +000052 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000053
Matt Kraaid27753a2000-12-05 05:11:41 +000054 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000055}