diff options
author | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-24 20:04:18 +0000 |
---|---|---|
committer | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-24 20:04:18 +0000 |
commit | 8695b23e5809a0b952accd98da299f86b126d5fd (patch) | |
tree | d1403572448114ecd20030316914f473816f882d /libbb | |
parent | c1db72874a1dbb92ee3dca555e1561e6a95b5ce2 (diff) | |
download | busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.tar.gz busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.tar.bz2 busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.zip |
Rewrote rm.
git-svn-id: svn://busybox.net/trunk/busybox@2423 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/libbb.h | 1 | ||||
-rw-r--r-- | libbb/remove_file.c | 127 |
2 files changed, 128 insertions, 0 deletions
diff --git a/libbb/libbb.h b/libbb/libbb.h index 404d7076c..bbfffda6d 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -93,6 +93,7 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name); | |||
93 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); | 93 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); |
94 | void reset_ino_dev_hashtable(void); | 94 | void reset_ino_dev_hashtable(void); |
95 | 95 | ||
96 | int remove_file(const char *path, int flags); | ||
96 | int copy_file(const char *source, const char *dest, int flags); | 97 | int copy_file(const char *source, const char *dest, int flags); |
97 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); | 98 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); |
98 | char *buildName(const char *dirName, const char *fileName); | 99 | char *buildName(const char *dirName, const char *fileName); |
diff --git a/libbb/remove_file.c b/libbb/remove_file.c new file mode 100644 index 000000000..52b3211e6 --- /dev/null +++ b/libbb/remove_file.c | |||
@@ -0,0 +1,127 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini remove_file implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include <time.h> | ||
27 | #include <utime.h> | ||
28 | #include <dirent.h> | ||
29 | #include <errno.h> | ||
30 | #include <unistd.h> | ||
31 | #include <stdlib.h> | ||
32 | #include <getopt.h> | ||
33 | #include "libbb.h" | ||
34 | |||
35 | extern int remove_file(const char *path, int flags) | ||
36 | { | ||
37 | struct stat path_stat; | ||
38 | int path_exists = 1; | ||
39 | |||
40 | if (stat(path, &path_stat) < 0) { | ||
41 | if (errno != ENOENT) { | ||
42 | perror_msg("unable to stat `%s'", path); | ||
43 | return -1; | ||
44 | } | ||
45 | |||
46 | path_exists = 0; | ||
47 | } | ||
48 | |||
49 | if (!path_exists) { | ||
50 | if (!(flags & FILEUTILS_FORCE)) { | ||
51 | perror_msg("cannot remove `%s'", path); | ||
52 | return -1; | ||
53 | } | ||
54 | return 0; | ||
55 | } | ||
56 | |||
57 | if (S_ISDIR(path_stat.st_mode)) { | ||
58 | DIR *dp; | ||
59 | struct dirent *d; | ||
60 | int status = 0; | ||
61 | |||
62 | if (!(flags & FILEUTILS_RECUR)) { | ||
63 | error_msg("%s: is a directory", path); | ||
64 | return -1; | ||
65 | } | ||
66 | |||
67 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && | ||
68 | isatty(0)) || | ||
69 | (flags & FILEUTILS_INTERACTIVE)) { | ||
70 | fprintf(stderr, "%s: descend into directory `%s'? ", applet_name, | ||
71 | path); | ||
72 | if (!ask_confirmation()) | ||
73 | return 0; | ||
74 | } | ||
75 | |||
76 | if ((dp = opendir(path)) == NULL) { | ||
77 | perror_msg("unable to open `%s'", path); | ||
78 | return -1; | ||
79 | } | ||
80 | |||
81 | while ((d = readdir(dp)) != NULL) { | ||
82 | char *new_path; | ||
83 | |||
84 | if (strcmp(d->d_name, ".") == 0 || | ||
85 | strcmp(d->d_name, "..") == 0) | ||
86 | continue; | ||
87 | |||
88 | new_path = concat_path_file(path, d->d_name); | ||
89 | if (remove_file(new_path, flags) < 0) | ||
90 | status = -1; | ||
91 | free(new_path); | ||
92 | } | ||
93 | |||
94 | if (closedir(dp) < 0) { | ||
95 | perror_msg("unable to close `%s'", path); | ||
96 | return -1; | ||
97 | } | ||
98 | |||
99 | if (flags & FILEUTILS_INTERACTIVE) { | ||
100 | fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path); | ||
101 | if (!ask_confirmation()) | ||
102 | return status; | ||
103 | } | ||
104 | |||
105 | if (rmdir(path) < 0) { | ||
106 | perror_msg("unable to remove `%s'", path); | ||
107 | return -1; | ||
108 | } | ||
109 | |||
110 | return status; | ||
111 | } else { | ||
112 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && | ||
113 | isatty(0)) || | ||
114 | (flags & FILEUTILS_INTERACTIVE)) { | ||
115 | fprintf(stderr, "%s: remove `%s'? ", applet_name, path); | ||
116 | if (!ask_confirmation()) | ||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | if (unlink(path) < 0) { | ||
121 | perror_msg("unable to remove `%s'", path); | ||
122 | return -1; | ||
123 | } | ||
124 | |||
125 | return 0; | ||
126 | } | ||
127 | } | ||