copy_file_chunk uses streams now.
[oweals/busybox.git] / 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         struct stat statbuf;
89         
90         
91         /* do normal option parsing */
92         while ((opt = getopt(argc, argv, "Rrf-"
93 #ifdef BB_FEATURE_RM_INTERACTIVE
94 "i"
95 #endif
96 )) > 0) {
97                 switch (opt) {
98                         case 'R':
99                         case 'r':
100                                 recursiveFlag = TRUE;
101                                 break;
102                         case 'f':
103                                 forceFlag = TRUE;
104                                 break;
105 #ifdef BB_FEATURE_RM_INTERACTIVE
106                         case 'i':
107                                 interactiveFlag = TRUE;
108 #endif
109                                 break;
110                         default:
111                                 show_usage();
112                 }
113         }
114         
115         if (argc == optind && forceFlag == FALSE) {
116                 show_usage();
117         }
118 #ifdef BB_FEATURE_RM_INTERACTIVE
119         if (forceFlag == TRUE)
120                 interactiveFlag = FALSE;
121 #endif
122
123
124         while (optind < argc) {
125                 srcName = argv[optind];
126                 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
127                         && errno == ENOENT) {
128                         /* do not reports errors for non-existent files if -f, just skip them */
129                 } else {
130                         if (recursive_action(srcName, recursiveFlag, FALSE,
131                                                                 TRUE, fileAction, dirAction, NULL) == FALSE) {
132                                 status = EXIT_FAILURE;
133                         }
134                 }
135                 optind++;
136         }
137         return status;
138 }