blob: 5df7d5f65f2fe6ac160897dac1b38d7991b9ad51 [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
Rob Landleydfba7412006-03-06 20:47:33 +000021int rm_main(int argc, char **argv)
Eric Andersenbed30e91999-10-18 19:02:32 +000022{
Matt Kraai8810bdb2001-04-24 20:04:18 +000023 int status = 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +000024 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000025 unsigned opt;
Matt Kraai8810bdb2001-04-24 20:04:18 +000026
Denis Vlasenko67b23e62006-10-03 21:00:06 +000027 opt_complementary = "f-i:i-f";
28 opt = getopt32(argc, argv, "fiRr");
Eric Andersen8876fb22003-06-20 09:01:58 +000029 if(opt & 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 flags |= FILEUTILS_FORCE;
Eric Andersen8876fb22003-06-20 09:01:58 +000031 if(opt & 2)
32 flags |= FILEUTILS_INTERACTIVE;
33 if(opt & 12)
34 flags |= FILEUTILS_RECUR;
Matt Kraai8810bdb2001-04-24 20:04:18 +000035
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 if (*(argv += optind) != NULL) {
37 do {
38 const char *base = bb_get_last_path_component(*argv);
Matt Kraai8810bdb2001-04-24 20:04:18 +000039
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 if ((base[0] == '.') && (!base[1] || ((base[1] == '.') && !base[2]))) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000041 bb_error_msg("cannot remove '.' or '..'");
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 } else if (remove_file(*argv, flags) >= 0) {
43 continue;
44 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000045 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 } while (*++argv);
47 } else if (!(flags & FILEUTILS_FORCE)) {
48 bb_show_usage();
Eric Andersena9c95ea1999-11-15 17:33:30 +000049 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000050
Matt Kraaid27753a2000-12-05 05:11:41 +000051 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000052}