getopt-ify rm so that BB_FEATURE_RM_INTERACTIVE will work
[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 <getopt.h>
37 #include "busybox.h"
38
39 static int recursiveFlag = FALSE;
40 static int forceFlag = FALSE;
41 #ifdef BB_FEATURE_RM_INTERACTIVE
42         static int interactiveFlag = FALSE;
43 #endif
44 static const char *srcName;
45
46
47 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
48 {
49 #ifdef BB_FEATURE_RM_INTERACTIVE
50         if (interactiveFlag == TRUE) {
51                 printf("rm: remove `%s'? ", fileName);
52                 if (ask_confirmation() == 0)
53                         return (TRUE);
54         }
55 #endif
56         if (unlink(fileName) < 0) {
57                 perror_msg("%s", fileName);
58                 return (FALSE);
59         }
60         return (TRUE);
61 }
62
63 static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
64 {
65         if (recursiveFlag == FALSE) {
66                 errno = EISDIR;
67                 perror_msg("%s", fileName);
68                 return (FALSE);
69         } 
70 #ifdef BB_FEATURE_RM_INTERACTIVE
71         if (interactiveFlag == TRUE) {
72                 printf("rm: remove directory `%s'? ", fileName);
73                 if (ask_confirmation() == 0)
74                         return (TRUE);
75         }
76 #endif
77         if (rmdir(fileName) < 0) {
78                 perror_msg("%s", fileName);
79                 return (FALSE);
80         }
81         return (TRUE);
82 }
83
84 extern int rm_main(int argc, char **argv)
85 {
86         int opt;
87         int status = EXIT_SUCCESS;
88         int stopIt=FALSE;
89         struct stat statbuf;
90         
91         
92         /* do normal option parsing */
93         while ((opt = getopt(argc, argv, "Rrf-"
94 #ifdef BB_FEATURE_RM_INTERACTIVE
95 "i"
96 #endif
97 )) > 0) {
98                 switch (opt) {
99                         case 'R':
100                         case 'r':
101                                 recursiveFlag = TRUE;
102                                 break;
103                         case 'f':
104                                 forceFlag = TRUE;
105 #ifdef BB_FEATURE_RM_INTERACTIVE
106                                 interactiveFlag = FALSE;
107 #endif
108                                 break;
109                         case 'i':
110 #ifdef BB_FEATURE_RM_INTERACTIVE
111                                 interactiveFlag = TRUE;
112 #endif
113                                 break;
114                         case '-':
115                                 stopIt = TRUE;
116                                 break;
117                         default:
118                                 show_usage();
119                 }
120         }
121         
122         if ((argc-optind) < 1 && forceFlag == FALSE) {
123                 show_usage();
124         }
125
126         while (optind < argc) {
127                 srcName = argv[optind];
128                 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
129                         && errno == ENOENT) {
130                         /* do not reports errors for non-existent files if -f, just skip them */
131                 } else {
132                         if (recursive_action(srcName, recursiveFlag, FALSE,
133                                                                 TRUE, fileAction, dirAction, NULL) == FALSE) {
134                                 status = EXIT_FAILURE;
135                         }
136                 }
137                 optind++;
138         }
139         return status;
140 }