1 /* vi: set sw=4 ts=4: */
3 * Mini remove_file implementation for busybox
5 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 int remove_file(const char *path, int flags)
35 struct stat path_stat;
38 if (lstat(path, &path_stat) < 0) {
39 if (errno != ENOENT) {
40 bb_perror_msg("unable to stat `%s'", path);
48 if (!(flags & FILEUTILS_FORCE)) {
49 bb_perror_msg("cannot remove `%s'", path);
55 if (S_ISDIR(path_stat.st_mode)) {
60 if (!(flags & FILEUTILS_RECUR)) {
61 bb_error_msg("%s: is a directory", path);
65 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
67 (flags & FILEUTILS_INTERACTIVE)) {
68 fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name,
70 if (!bb_ask_confirmation())
74 if ((dp = opendir(path)) == NULL) {
75 bb_perror_msg("unable to open `%s'", path);
79 while ((d = readdir(dp)) != NULL) {
82 new_path = concat_subpath_file(path, d->d_name);
85 if (remove_file(new_path, flags) < 0)
90 if (closedir(dp) < 0) {
91 bb_perror_msg("unable to close `%s'", path);
95 if (flags & FILEUTILS_INTERACTIVE) {
96 fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path);
97 if (!bb_ask_confirmation())
101 if (rmdir(path) < 0) {
102 bb_perror_msg("unable to remove `%s'", path);
108 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
109 !S_ISLNK(path_stat.st_mode) &&
111 (flags & FILEUTILS_INTERACTIVE)) {
112 fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path);
113 if (!bb_ask_confirmation())
117 if (unlink(path) < 0) {
118 bb_perror_msg("unable to remove `%s'", path);