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 int remove_file(const char *path, int flags)
14 struct stat path_stat;
16 if (lstat(path, &path_stat) < 0) {
17 if (errno != ENOENT) {
18 bb_perror_msg("cannot stat '%s'", path);
21 if (!(flags & FILEUTILS_FORCE)) {
22 bb_perror_msg("cannot remove '%s'", path);
28 if (S_ISDIR(path_stat.st_mode)) {
33 if (!(flags & FILEUTILS_RECUR)) {
34 bb_error_msg("%s: is a directory", path);
38 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
39 || (flags & FILEUTILS_INTERACTIVE)
41 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name,
43 if (!bb_ask_confirmation())
52 while ((d = readdir(dp)) != NULL) {
55 new_path = concat_subpath_file(path, d->d_name);
58 if (remove_file(new_path, flags) < 0)
63 if (closedir(dp) < 0) {
64 bb_perror_msg("cannot close '%s'", path);
68 if (flags & FILEUTILS_INTERACTIVE) {
69 fprintf(stderr, "%s: remove directory '%s'? ", applet_name, path);
70 if (!bb_ask_confirmation())
74 if (rmdir(path) < 0) {
75 bb_perror_msg("cannot remove '%s'", path);
83 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0
84 && !S_ISLNK(path_stat.st_mode) && isatty(0))
85 || (flags & FILEUTILS_INTERACTIVE)
87 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
88 if (!bb_ask_confirmation())
92 if (unlink(path) < 0) {
93 bb_perror_msg("cannot remove '%s'", path);