Credited Christophe Boyaniqu for interactive patch to rm.
[oweals/busybox.git] / coreutils / rm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rm implementation for busybox
4  *
5  *
6  * Copyright (C) 1999,2000,2001 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * INTERACTIVE feature Copyright (C) 2001 by Alcove
10  *   written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com>
11  *   for Ipanema Technologies
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  */
28
29 #include <stdio.h>
30 #include <time.h>
31 #include <utime.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include "busybox.h"
37
38 static int recursiveFlag = FALSE;
39 static int forceFlag = FALSE;
40 #ifdef BB_FEATURE_RM_INTERACTIVE
41         static int interactiveFlag = FALSE;
42 #endif
43 static const char *srcName;
44
45
46 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
47 {
48 #ifdef BB_FEATURE_RM_INTERACTIVE
49         if (interactiveFlag == TRUE) {
50                 printf("rm: remove `%s'? ", fileName);
51                 if (ask_confirmation() == 0)
52                         return (TRUE);
53         }
54 #endif
55         if (unlink(fileName) < 0) {
56                 perror_msg("%s", fileName);
57                 return (FALSE);
58         }
59         return (TRUE);
60 }
61
62 static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
63 {
64         if (recursiveFlag == FALSE) {
65                 errno = EISDIR;
66                 perror_msg("%s", fileName);
67                 return (FALSE);
68         } 
69 #ifdef BB_FEATURE_RM_INTERACTIVE
70         if (interactiveFlag == TRUE) {
71                 printf("rm: remove directory `%s'? ", fileName);
72                 if (ask_confirmation() == 0)
73                         return (TRUE);
74         }
75 #endif
76         if (rmdir(fileName) < 0) {
77                 perror_msg("%s", fileName);
78                 return (FALSE);
79         }
80         return (TRUE);
81 }
82
83 extern int rm_main(int argc, char **argv)
84 {
85         int status = EXIT_SUCCESS;
86         int stopIt=FALSE;
87         struct stat statbuf;
88
89         argc--;
90         argv++;
91
92         /* Parse any options */
93         while (argc > 0 && stopIt == FALSE) {
94                 if (**argv == '-') {
95                         while (*++(*argv))
96                                 switch (**argv) {
97                                         case 'R':
98                                         case 'r':
99                                                 recursiveFlag = TRUE;
100                                                 break;
101                                         case 'f':
102                                                 forceFlag = TRUE;
103 #ifdef BB_FEATURE_RM_INTERACTIVE
104                                                 interactiveFlag = FALSE;
105 #endif
106                                                 break;
107                                         case 'i':
108 #ifdef BB_FEATURE_RM_INTERACTIVE
109                                                 interactiveFlag = TRUE;
110 #endif
111                                                 break;
112                                         case '-':
113                                                 stopIt = TRUE;
114                                                 break;
115                                         default:
116                                                 show_usage();
117                                 }
118                         argc--;
119                         argv++;
120                 }
121                 else
122                         break;
123         }
124
125         if (argc < 1 && forceFlag == FALSE) {
126                 show_usage();
127         }
128
129         while (argc-- > 0) {
130                 srcName = *(argv++);
131                 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
132                         && errno == ENOENT) {
133                         /* do not reports errors for non-existent files if -f, just skip them */
134                 } else {
135                         if (recursive_action(srcName, recursiveFlag, FALSE,
136                                                                 TRUE, fileAction, dirAction, NULL) == FALSE) {
137                                 status = EXIT_FAILURE;
138                         }
139                 }
140         }
141         return status;
142 }