blob: 1883feed8d1f679af6d2ab8b3350f37781c53d3c [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
Eric Andersened3ef502001-01-27 08:24:39 +000018#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000019#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Denis Vlasenko06af2162007-02-03 17:28:39 +000021int rm_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000022int rm_main(int argc, char **argv)
Eric Andersenbed30e91999-10-18 19:02:32 +000023{
Matt Kraai8810bdb2001-04-24 20:04:18 +000024 int status = 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +000025 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000026 unsigned opt;
Matt Kraai8810bdb2001-04-24 20:04:18 +000027
Denis Vlasenko67b23e62006-10-03 21:00:06 +000028 opt_complementary = "f-i:i-f";
29 opt = getopt32(argc, argv, "fiRr");
Eric Andersen8876fb22003-06-20 09:01:58 +000030 if(opt & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 flags |= FILEUTILS_FORCE;
Eric Andersen8876fb22003-06-20 09:01:58 +000032 if(opt & 2)
33 flags |= FILEUTILS_INTERACTIVE;
34 if(opt & 12)
35 flags |= FILEUTILS_RECUR;
Matt Kraai8810bdb2001-04-24 20:04:18 +000036
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 if (*(argv += optind) != NULL) {
38 do {
39 const char *base = bb_get_last_path_component(*argv);
Matt Kraai8810bdb2001-04-24 20:04:18 +000040
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 if ((base[0] == '.') && (!base[1] || ((base[1] == '.') && !base[2]))) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000042 bb_error_msg("cannot remove '.' or '..'");
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 } else if (remove_file(*argv, flags) >= 0) {
44 continue;
45 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000046 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 } while (*++argv);
48 } else if (!(flags & FILEUTILS_FORCE)) {
49 bb_show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +000050 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000051
Matt Kraaid27753a2000-12-05 05:11:41 +000052 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000053}