aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-03-26 23:14:38 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-03-26 23:14:38 +0000
commitf4d40c87d3a18fccb8c0946fc09f1d8f24a2bcf3 (patch)
tree6d19aec08819020a40c3841928a2e1c67a97a440
parent3983bd5593ae3a18dd69072e549e3860820cb8ff (diff)
downloadbusybox-w32-f4d40c87d3a18fccb8c0946fc09f1d8f24a2bcf3.tar.gz
busybox-w32-f4d40c87d3a18fccb8c0946fc09f1d8f24a2bcf3.tar.bz2
busybox-w32-f4d40c87d3a18fccb8c0946fc09f1d8f24a2bcf3.zip
remove_file: cosmetic code improvement, a few bytes saved
-rw-r--r--libbb/remove_file.c55
1 files changed, 21 insertions, 34 deletions
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index ab159a481..3aaaef8c7 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -7,32 +7,17 @@
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */ 8 */
9 9
10#include <stdio.h>
11#include <time.h>
12#include <utime.h>
13#include <dirent.h>
14#include <errno.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <string.h>
18#include <getopt.h>
19#include "libbb.h" 10#include "libbb.h"
20 11
21int remove_file(const char *path, int flags) 12int remove_file(const char *path, int flags)
22{ 13{
23 struct stat path_stat; 14 struct stat path_stat;
24 int path_exists = 1;
25 15
26 if (lstat(path, &path_stat) < 0) { 16 if (lstat(path, &path_stat) < 0) {
27 if (errno != ENOENT) { 17 if (errno != ENOENT) {
28 bb_perror_msg("cannot stat '%s'", path); 18 bb_perror_msg("cannot stat '%s'", path);
29 return -1; 19 return -1;
30 } 20 }
31
32 path_exists = 0;
33 }
34
35 if (!path_exists) {
36 if (!(flags & FILEUTILS_FORCE)) { 21 if (!(flags & FILEUTILS_FORCE)) {
37 bb_perror_msg("cannot remove '%s'", path); 22 bb_perror_msg("cannot remove '%s'", path);
38 return -1; 23 return -1;
@@ -50,16 +35,17 @@ int remove_file(const char *path, int flags)
50 return -1; 35 return -1;
51 } 36 }
52 37
53 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && 38 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
54 isatty(0)) || 39 || (flags & FILEUTILS_INTERACTIVE)
55 (flags & FILEUTILS_INTERACTIVE)) { 40 ) {
56 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name, 41 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name,
57 path); 42 path);
58 if (!bb_ask_confirmation()) 43 if (!bb_ask_confirmation())
59 return 0; 44 return 0;
60 } 45 }
61 46
62 if ((dp = opendir(path)) == NULL) { 47 dp = opendir(path);
48 if (dp == NULL) {
63 return -1; 49 return -1;
64 } 50 }
65 51
@@ -67,7 +53,7 @@ int remove_file(const char *path, int flags)
67 char *new_path; 53 char *new_path;
68 54
69 new_path = concat_subpath_file(path, d->d_name); 55 new_path = concat_subpath_file(path, d->d_name);
70 if(new_path == NULL) 56 if (new_path == NULL)
71 continue; 57 continue;
72 if (remove_file(new_path, flags) < 0) 58 if (remove_file(new_path, flags) < 0)
73 status = -1; 59 status = -1;
@@ -91,21 +77,22 @@ int remove_file(const char *path, int flags)
91 } 77 }
92 78
93 return status; 79 return status;
94 } else { 80 }
95 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
96 !S_ISLNK(path_stat.st_mode) &&
97 isatty(0)) ||
98 (flags & FILEUTILS_INTERACTIVE)) {
99 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
100 if (!bb_ask_confirmation())
101 return 0;
102 }
103 81
104 if (unlink(path) < 0) { 82 /* !ISDIR */
105 bb_perror_msg("cannot remove '%s'", path); 83 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0
106 return -1; 84 && !S_ISLNK(path_stat.st_mode) && isatty(0))
107 } 85 || (flags & FILEUTILS_INTERACTIVE)
86 ) {
87 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
88 if (!bb_ask_confirmation())
89 return 0;
90 }
108 91
109 return 0; 92 if (unlink(path) < 0) {
93 bb_perror_msg("cannot remove '%s'", path);
94 return -1;
110 } 95 }
96
97 return 0;
111} 98}