1 /* vi: set sw=4 ts=4: */
3 * Mini remove_file implementation for busybox
6 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
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.
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.
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
35 extern int remove_file(const char *path, int flags)
37 struct stat path_stat;
40 if (stat(path, &path_stat) < 0) {
41 if (errno != ENOENT) {
42 perror_msg("unable to stat `%s'", path);
50 if (!(flags & FILEUTILS_FORCE)) {
51 perror_msg("cannot remove `%s'", path);
57 if (S_ISDIR(path_stat.st_mode)) {
62 if (!(flags & FILEUTILS_RECUR)) {
63 error_msg("%s: is a directory", path);
67 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
69 (flags & FILEUTILS_INTERACTIVE)) {
70 fprintf(stderr, "%s: descend into directory `%s'? ", applet_name,
72 if (!ask_confirmation())
76 if ((dp = opendir(path)) == NULL) {
77 perror_msg("unable to open `%s'", path);
81 while ((d = readdir(dp)) != NULL) {
84 if (strcmp(d->d_name, ".") == 0 ||
85 strcmp(d->d_name, "..") == 0)
88 new_path = concat_path_file(path, d->d_name);
89 if (remove_file(new_path, flags) < 0)
94 if (closedir(dp) < 0) {
95 perror_msg("unable to close `%s'", path);
99 if (flags & FILEUTILS_INTERACTIVE) {
100 fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path);
101 if (!ask_confirmation())
105 if (rmdir(path) < 0) {
106 perror_msg("unable to remove `%s'", path);
112 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
114 (flags & FILEUTILS_INTERACTIVE)) {
115 fprintf(stderr, "%s: remove `%s'? ", applet_name, path);
116 if (!ask_confirmation())
120 if (unlink(path) < 0) {
121 perror_msg("unable to remove `%s'", path);