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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 /* Used from NOFORK applets. Must not allocate anything */
14 int FAST_FUNC remove_file(const char *path, int flags)
16 struct stat path_stat;
18 if (lstat(path, &path_stat) < 0) {
19 if (errno != ENOENT) {
20 bb_perror_msg("cannot stat '%s'", path);
23 if (!(flags & FILEUTILS_FORCE)) {
24 bb_perror_msg("cannot remove '%s'", path);
30 if (S_ISDIR(path_stat.st_mode)) {
35 if (!(flags & FILEUTILS_RECUR)) {
36 bb_error_msg("%s: is a directory", path);
40 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
41 || (flags & FILEUTILS_INTERACTIVE)
43 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name,
45 if (!bb_ask_confirmation())
54 while ((d = readdir(dp)) != NULL) {
57 new_path = concat_subpath_file(path, d->d_name);
60 if (remove_file(new_path, flags) < 0)
65 if (closedir(dp) < 0) {
66 bb_perror_msg("cannot close '%s'", path);
70 if (flags & FILEUTILS_INTERACTIVE) {
71 fprintf(stderr, "%s: remove directory '%s'? ", applet_name, path);
72 if (!bb_ask_confirmation())
76 if (rmdir(path) < 0) {
77 bb_perror_msg("cannot remove '%s'", path);
85 if ((!(flags & FILEUTILS_FORCE)
86 && access(path, W_OK) < 0
87 && !S_ISLNK(path_stat.st_mode)
89 || (flags & FILEUTILS_INTERACTIVE)
91 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
92 if (!bb_ask_confirmation())
96 if (unlink(path) < 0) {
97 bb_perror_msg("cannot remove '%s'", path);