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 source tree.
11 /* Used from NOFORK applets. Must not allocate anything */
13 int FAST_FUNC remove_file(const char *path, int flags)
15 struct stat path_stat;
17 if (lstat(path, &path_stat) < 0) {
18 if (errno != ENOENT) {
19 bb_perror_msg("can't stat '%s'", path);
22 if (!(flags & FILEUTILS_FORCE)) {
23 bb_perror_msg("can't remove '%s'", path);
29 if (S_ISDIR(path_stat.st_mode)) {
34 if (!(flags & FILEUTILS_RECUR)) {
35 bb_error_msg("'%s' is a directory", path);
39 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
40 || (flags & FILEUTILS_INTERACTIVE)
42 fprintf(stderr, "%s: descend into directory '%s'? ",
44 if (!bb_ask_y_confirmation())
53 while ((d = readdir(dp)) != NULL) {
56 new_path = concat_subpath_file(path, d->d_name);
59 if (remove_file(new_path, flags) < 0)
64 if (closedir(dp) < 0) {
65 bb_perror_msg("can't close '%s'", path);
69 if (flags & FILEUTILS_INTERACTIVE) {
70 fprintf(stderr, "%s: remove directory '%s'? ",
72 if (!bb_ask_y_confirmation())
76 if (rmdir(path) < 0) {
77 bb_perror_msg("can't remove '%s'", path);
81 if (flags & FILEUTILS_VERBOSE) {
82 printf("removed directory: '%s'\n", path);
89 if ((!(flags & FILEUTILS_FORCE)
90 && access(path, W_OK) < 0
91 && !S_ISLNK(path_stat.st_mode)
93 || (flags & FILEUTILS_INTERACTIVE)
95 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
96 if (!bb_ask_y_confirmation())
100 if (unlink(path) < 0) {
101 bb_perror_msg("can't remove '%s'", path);
105 if (flags & FILEUTILS_VERBOSE) {
106 printf("removed '%s'\n", path);