Allow 'gzip -d' and 'bzip2 -d' without gunzip or bunzip2
[oweals/busybox.git] / coreutils / rm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rm implementation for busybox
4  *
5  * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
10  *
11  * Size reduction.
12  */
13 //config:config RM
14 //config:       bool "rm"
15 //config:       default y
16 //config:       help
17 //config:         rm is used to remove files or directories.
18
19 //applet:IF_RM(APPLET_NOFORK(rm, rm, BB_DIR_BIN, BB_SUID_DROP, rm))
20
21 //kbuild:lib-$(CONFIG_RM) += rm.o
22
23 /* BB_AUDIT SUSv3 compliant */
24 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rm.html */
25
26 //usage:#define rm_trivial_usage
27 //usage:       "[-irf] FILE..."
28 //usage:#define rm_full_usage "\n\n"
29 //usage:       "Remove (unlink) FILEs\n"
30 //usage:     "\n        -i      Always prompt before removing"
31 //usage:     "\n        -f      Never prompt"
32 //usage:     "\n        -R,-r   Recurse"
33 //usage:
34 //usage:#define rm_example_usage
35 //usage:       "$ rm -rf /tmp/foo\n"
36
37 #include "libbb.h"
38
39 /* This is a NOFORK applet. Be very careful! */
40
41 int rm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int rm_main(int argc UNUSED_PARAM, char **argv)
43 {
44         int status = 0;
45         int flags = 0;
46         unsigned opt;
47
48         opt_complementary = "f-i:i-f";
49         opt = getopt32(argv, "fiRrv");
50         argv += optind;
51         if (opt & 1)
52                 flags |= FILEUTILS_FORCE;
53         if (opt & 2)
54                 flags |= FILEUTILS_INTERACTIVE;
55         if (opt & (8|4))
56                 flags |= FILEUTILS_RECUR;
57         if ((opt & 16) && FILEUTILS_VERBOSE)
58                 flags |= FILEUTILS_VERBOSE;
59
60         if (*argv != NULL) {
61                 do {
62                         const char *base = bb_get_last_path_component_strip(*argv);
63
64                         if (DOT_OR_DOTDOT(base)) {
65                                 bb_error_msg("can't remove '.' or '..'");
66                         } else if (remove_file(*argv, flags) >= 0) {
67                                 continue;
68                         }
69                         status = 1;
70                 } while (*++argv);
71         } else if (!(flags & FILEUTILS_FORCE)) {
72                 bb_show_usage();
73         }
74
75         return status;
76 }