Another update from Larry:
[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 <getopt.h>
33 #include "libbb.h"
34
35 extern int remove_file(const char *path, int flags)
36 {
37         struct stat path_stat;
38         int path_exists = 1;
39
40         if (stat(path, &path_stat) < 0) {
41                 if (errno != ENOENT) {
42                         perror_msg("unable to stat `%s'", path);
43                         return -1;
44                 }
45
46                 path_exists = 0;
47         }
48
49         if (!path_exists) {
50                 if (!(flags & FILEUTILS_FORCE)) {
51                         perror_msg("cannot remove `%s'", path);
52                         return -1;
53                 }
54                 return 0;
55         }
56
57         if (S_ISDIR(path_stat.st_mode)) {
58                 DIR *dp;
59                 struct dirent *d;
60                 int status = 0;
61
62                 if (!(flags & FILEUTILS_RECUR)) {
63                         error_msg("%s: is a directory", path);
64                         return -1;
65                 }
66
67                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
68                                         isatty(0)) ||
69                                 (flags & FILEUTILS_INTERACTIVE)) {
70                         fprintf(stderr, "%s: descend into directory `%s'? ", applet_name,
71                                         path);
72                         if (!ask_confirmation())
73                                 return 0;
74                 }
75
76                 if ((dp = opendir(path)) == NULL) {
77                         perror_msg("unable to open `%s'", path);
78                         return -1;
79                 }
80
81                 while ((d = readdir(dp)) != NULL) {
82                         char *new_path;
83
84                         if (strcmp(d->d_name, ".") == 0 ||
85                                         strcmp(d->d_name, "..") == 0)
86                                 continue;
87
88                         new_path = concat_path_file(path, d->d_name);
89                         if (remove_file(new_path, flags) < 0)
90                                 status = -1;
91                         free(new_path);
92                 }
93
94                 if (closedir(dp) < 0) {
95                         perror_msg("unable to close `%s'", path);
96                         return -1;
97                 }
98
99                 if (flags & FILEUTILS_INTERACTIVE) {
100                         fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path);
101                         if (!ask_confirmation())
102                                 return status;
103                 }
104
105                 if (rmdir(path) < 0) {
106                         perror_msg("unable to remove `%s'", path);
107                         return -1;
108                 }
109
110                 return status;
111         } else {
112                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
113                                         isatty(0)) ||
114                                 (flags & FILEUTILS_INTERACTIVE)) {
115                         fprintf(stderr, "%s: remove `%s'? ", applet_name, path);
116                         if (!ask_confirmation())
117                                 return 0;
118                 }
119
120                 if (unlink(path) < 0) {
121                         perror_msg("unable to remove `%s'", path);
122                         return -1;
123                 }
124
125                 return 0;
126         }
127 }