Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / libbb / remove_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini remove_file implementation for busybox
4  *
5  *
6  * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <time.h>
27 #include <utime.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <getopt.h>
34 #include "libbb.h"
35
36 extern int remove_file(const char *path, int flags)
37 {
38         struct stat path_stat;
39         int path_exists = 1;
40
41         if (lstat(path, &path_stat) < 0) {
42                 if (errno != ENOENT) {
43                         perror_msg("unable to stat `%s'", path);
44                         return -1;
45                 }
46
47                 path_exists = 0;
48         }
49
50         if (!path_exists) {
51                 if (!(flags & FILEUTILS_FORCE)) {
52                         perror_msg("cannot remove `%s'", path);
53                         return -1;
54                 }
55                 return 0;
56         }
57
58         if (S_ISDIR(path_stat.st_mode)) {
59                 DIR *dp;
60                 struct dirent *d;
61                 int status = 0;
62
63                 if (!(flags & FILEUTILS_RECUR)) {
64                         error_msg("%s: is a directory", path);
65                         return -1;
66                 }
67
68                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
69                                         isatty(0)) ||
70                                 (flags & FILEUTILS_INTERACTIVE)) {
71                         fprintf(stderr, "%s: descend into directory `%s'? ", applet_name,
72                                         path);
73                         if (!ask_confirmation())
74                                 return 0;
75                 }
76
77                 if ((dp = opendir(path)) == NULL) {
78                         perror_msg("unable to open `%s'", path);
79                         return -1;
80                 }
81
82                 while ((d = readdir(dp)) != NULL) {
83                         char *new_path;
84
85                         if (strcmp(d->d_name, ".") == 0 ||
86                                         strcmp(d->d_name, "..") == 0)
87                                 continue;
88
89                         new_path = concat_path_file(path, d->d_name);
90                         if (remove_file(new_path, flags) < 0)
91                                 status = -1;
92                         free(new_path);
93                 }
94
95                 if (closedir(dp) < 0) {
96                         perror_msg("unable to close `%s'", path);
97                         return -1;
98                 }
99
100                 if (flags & FILEUTILS_INTERACTIVE) {
101                         fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path);
102                         if (!ask_confirmation())
103                                 return status;
104                 }
105
106                 if (rmdir(path) < 0) {
107                         perror_msg("unable to remove `%s'", path);
108                         return -1;
109                 }
110
111                 return status;
112         } else {
113                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
114                                         !S_ISLNK(path_stat.st_mode) &&
115                                         isatty(0)) ||
116                                 (flags & FILEUTILS_INTERACTIVE)) {
117                         fprintf(stderr, "%s: remove `%s'? ", applet_name, path);
118                         if (!ask_confirmation())
119                                 return 0;
120                 }
121
122                 if (unlink(path) < 0) {
123                         perror_msg("unable to remove `%s'", path);
124                         return -1;
125                 }
126
127                 return 0;
128         }
129 }