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
36 extern int remove_file(const char *path, int flags)
38 struct stat path_stat;
41 if (lstat(path, &path_stat) < 0) {
42 if (errno != ENOENT) {
43 perror_msg("unable to stat `%s'", path);
51 if (!(flags & FILEUTILS_FORCE)) {
52 perror_msg("cannot remove `%s'", path);
58 if (S_ISDIR(path_stat.st_mode)) {
63 if (!(flags & FILEUTILS_RECUR)) {
64 error_msg("%s: is a directory", path);
68 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
70 (flags & FILEUTILS_INTERACTIVE)) {
71 fprintf(stderr, "%s: descend into directory `%s'? ", applet_name,
73 if (!ask_confirmation())
77 if ((dp = opendir(path)) == NULL) {
78 perror_msg("unable to open `%s'", path);
82 while ((d = readdir(dp)) != NULL) {
85 if (strcmp(d->d_name, ".") == 0 ||
86 strcmp(d->d_name, "..") == 0)
89 new_path = concat_path_file(path, d->d_name);
90 if (remove_file(new_path, flags) < 0)
95 if (closedir(dp) < 0) {
96 perror_msg("unable to close `%s'", path);
100 if (flags & FILEUTILS_INTERACTIVE) {
101 fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path);
102 if (!ask_confirmation())
106 if (rmdir(path) < 0) {
107 perror_msg("unable to remove `%s'", path);
113 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
114 !S_ISLNK(path_stat.st_mode) &&
116 (flags & FILEUTILS_INTERACTIVE)) {
117 fprintf(stderr, "%s: remove `%s'? ", applet_name, path);
118 if (!ask_confirmation())
122 if (unlink(path) < 0) {
123 perror_msg("unable to remove `%s'", path);