aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--coreutils/mv.c17
-rw-r--r--coreutils/rm.c124
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/libbb.h1
-rw-r--r--libbb/remove_file.c127
-rw-r--r--mv.c17
-rw-r--r--rm.c124
8 files changed, 195 insertions, 219 deletions
diff --git a/Makefile b/Makefile
index 791357fa9..28cd3403a 100644
--- a/Makefile
+++ b/Makefile
@@ -247,7 +247,8 @@ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \
247process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ 247process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \
248recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ 248recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
249syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ 249syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
250verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c 250verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \
251remove_file.c
251 252
252LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 253LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
253LIBBB_CFLAGS = -I$(LIBBB) 254LIBBB_CFLAGS = -I$(LIBBB)
diff --git a/coreutils/mv.c b/coreutils/mv.c
index efc4ae6d8..b890abf6e 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -32,20 +32,6 @@
32 32
33static int flags; 33static int flags;
34 34
35static int remove_file(const char *path, struct stat *statbuf, void *junk)
36{
37 if (unlink(path) < 0)
38 return FALSE;
39 return TRUE;
40}
41
42static int remove_directory(const char *path, struct stat *statbuf, void *junk)
43{
44 if (rmdir(path) < 0)
45 return FALSE;
46 return TRUE;
47}
48
49static int manual_rename(const char *source, const char *dest) 35static int manual_rename(const char *source, const char *dest)
50{ 36{
51 struct stat source_stat; 37 struct stat source_stat;
@@ -92,8 +78,7 @@ static int manual_rename(const char *source, const char *dest)
92 FILEUTILS_PRESERVE_SYMLINKS) < 0) 78 FILEUTILS_PRESERVE_SYMLINKS) < 0)
93 return -1; 79 return -1;
94 80
95 if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, 81 if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0)
96 remove_directory, NULL))
97 return -1; 82 return -1;
98 83
99 return 0; 84 return 0;
diff --git a/coreutils/rm.c b/coreutils/rm.c
index ea7f86ce1..f8e16625c 100644
--- a/coreutils/rm.c
+++ b/coreutils/rm.c
@@ -3,12 +3,8 @@
3 * Mini rm implementation for busybox 3 * Mini rm implementation for busybox
4 * 4 *
5 * 5 *
6 * Copyright (C) 1999,2000,2001 by Lineo, inc. 6 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * 7 *
9 * INTERACTIVE feature Copyright (C) 2001 by Alcove
10 * written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com>
11 * for Ipanema Technologies
12 * 8 *
13 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -36,103 +32,45 @@
36#include <getopt.h> 32#include <getopt.h>
37#include "busybox.h" 33#include "busybox.h"
38 34
39static int recursiveFlag = FALSE;
40static int forceFlag = FALSE;
41#ifdef BB_FEATURE_RM_INTERACTIVE
42 static int interactiveFlag = FALSE;
43#endif
44static const char *srcName;
45
46
47static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
48{
49#ifdef BB_FEATURE_RM_INTERACTIVE
50 if (interactiveFlag == TRUE) {
51 printf("rm: remove `%s'? ", fileName);
52 if (ask_confirmation() == 0)
53 return (TRUE);
54 }
55#endif
56 if (unlink(fileName) < 0) {
57 perror_msg("%s", fileName);
58 return (FALSE);
59 }
60 return (TRUE);
61}
62
63static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
64{
65 if (recursiveFlag == FALSE) {
66 errno = EISDIR;
67 perror_msg("%s", fileName);
68 return (FALSE);
69 }
70#ifdef BB_FEATURE_RM_INTERACTIVE
71 if (interactiveFlag == TRUE) {
72 printf("rm: remove directory `%s'? ", fileName);
73 if (ask_confirmation() == 0)
74 return (TRUE);
75 }
76#endif
77 if (rmdir(fileName) < 0) {
78 perror_msg("%s", fileName);
79 return (FALSE);
80 }
81 return (TRUE);
82}
83
84extern int rm_main(int argc, char **argv) 35extern int rm_main(int argc, char **argv)
85{ 36{
37 int status = 0;
86 int opt; 38 int opt;
87 int status = EXIT_SUCCESS; 39 int flags = 0;
88 struct stat statbuf; 40 int i;
89
90
91 /* do normal option parsing */
92 while ((opt = getopt(argc, argv, "Rrf-"
93#ifdef BB_FEATURE_RM_INTERACTIVE
94"i"
95#endif
96)) > 0) {
97 switch (opt) {
98 case 'R':
99 case 'r':
100 recursiveFlag = TRUE;
101 break;
102 case 'f':
103 forceFlag = TRUE;
104#ifdef BB_FEATURE_RM_INTERACTIVE
105 41
106 interactiveFlag = FALSE; 42 while ((opt = getopt(argc, argv, "fiRr")) != -1) {
107#endif 43 switch (opt) {
108 break; 44 case 'f':
109#ifdef BB_FEATURE_RM_INTERACTIVE 45 flags &= ~FILEUTILS_INTERACTIVE;
110 case 'i': 46 flags |= FILEUTILS_FORCE;
111 interactiveFlag = TRUE; 47 break;
112 forceFlag = FALSE; 48 case 'i':
113 break; 49 flags &= ~FILEUTILS_FORCE;
114#endif 50 flags |= FILEUTILS_INTERACTIVE;
115 default: 51 break;
116 show_usage(); 52 case 'R':
53 case 'r':
54 flags |= FILEUTILS_RECUR;
55 break;
117 } 56 }
118 } 57 }
119 58
120 if (argc == optind && forceFlag == FALSE) { 59 if (!(flags & FILEUTILS_FORCE) && optind == argc)
121 show_usage(); 60 show_usage();
122 }
123 61
124 while (optind < argc) { 62 for (i = optind; i < argc; i++) {
125 srcName = argv[optind]; 63 char *base = get_last_path_component(argv[i]);
126 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 64
127 && errno == ENOENT) { 65 if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) {
128 /* do not reports errors for non-existent files if -f, just skip them */ 66 error_msg("cannot remove `.' or `..'");
129 } else { 67 status = 1;
130 if (recursive_action(srcName, recursiveFlag, FALSE, 68 continue;
131 TRUE, fileAction, dirAction, NULL) == FALSE) {
132 status = EXIT_FAILURE;
133 }
134 } 69 }
135 optind++; 70
71 if (remove_file(argv[i], flags) < 0)
72 status = 1;
136 } 73 }
74
137 return status; 75 return status;
138} 76}
diff --git a/include/libbb.h b/include/libbb.h
index 404d7076c..bbfffda6d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -93,6 +93,7 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
93void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); 93void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
94void reset_ino_dev_hashtable(void); 94void reset_ino_dev_hashtable(void);
95 95
96int remove_file(const char *path, int flags);
96int copy_file(const char *source, const char *dest, int flags); 97int copy_file(const char *source, const char *dest, int flags);
97int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); 98int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize);
98char *buildName(const char *dirName, const char *fileName); 99char *buildName(const char *dirName, const char *fileName);
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);
93void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); 93void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
94void reset_ino_dev_hashtable(void); 94void reset_ino_dev_hashtable(void);
95 95
96int remove_file(const char *path, int flags);
96int copy_file(const char *source, const char *dest, int flags); 97int copy_file(const char *source, const char *dest, int flags);
97int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); 98int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize);
98char *buildName(const char *dirName, const char *fileName); 99char *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
35extern 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}
diff --git a/mv.c b/mv.c
index efc4ae6d8..b890abf6e 100644
--- a/mv.c
+++ b/mv.c
@@ -32,20 +32,6 @@
32 32
33static int flags; 33static int flags;
34 34
35static int remove_file(const char *path, struct stat *statbuf, void *junk)
36{
37 if (unlink(path) < 0)
38 return FALSE;
39 return TRUE;
40}
41
42static int remove_directory(const char *path, struct stat *statbuf, void *junk)
43{
44 if (rmdir(path) < 0)
45 return FALSE;
46 return TRUE;
47}
48
49static int manual_rename(const char *source, const char *dest) 35static int manual_rename(const char *source, const char *dest)
50{ 36{
51 struct stat source_stat; 37 struct stat source_stat;
@@ -92,8 +78,7 @@ static int manual_rename(const char *source, const char *dest)
92 FILEUTILS_PRESERVE_SYMLINKS) < 0) 78 FILEUTILS_PRESERVE_SYMLINKS) < 0)
93 return -1; 79 return -1;
94 80
95 if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, 81 if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0)
96 remove_directory, NULL))
97 return -1; 82 return -1;
98 83
99 return 0; 84 return 0;
diff --git a/rm.c b/rm.c
index ea7f86ce1..f8e16625c 100644
--- a/rm.c
+++ b/rm.c
@@ -3,12 +3,8 @@
3 * Mini rm implementation for busybox 3 * Mini rm implementation for busybox
4 * 4 *
5 * 5 *
6 * Copyright (C) 1999,2000,2001 by Lineo, inc. 6 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * 7 *
9 * INTERACTIVE feature Copyright (C) 2001 by Alcove
10 * written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com>
11 * for Ipanema Technologies
12 * 8 *
13 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -36,103 +32,45 @@
36#include <getopt.h> 32#include <getopt.h>
37#include "busybox.h" 33#include "busybox.h"
38 34
39static int recursiveFlag = FALSE;
40static int forceFlag = FALSE;
41#ifdef BB_FEATURE_RM_INTERACTIVE
42 static int interactiveFlag = FALSE;
43#endif
44static const char *srcName;
45
46
47static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
48{
49#ifdef BB_FEATURE_RM_INTERACTIVE
50 if (interactiveFlag == TRUE) {
51 printf("rm: remove `%s'? ", fileName);
52 if (ask_confirmation() == 0)
53 return (TRUE);
54 }
55#endif
56 if (unlink(fileName) < 0) {
57 perror_msg("%s", fileName);
58 return (FALSE);
59 }
60 return (TRUE);
61}
62
63static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
64{
65 if (recursiveFlag == FALSE) {
66 errno = EISDIR;
67 perror_msg("%s", fileName);
68 return (FALSE);
69 }
70#ifdef BB_FEATURE_RM_INTERACTIVE
71 if (interactiveFlag == TRUE) {
72 printf("rm: remove directory `%s'? ", fileName);
73 if (ask_confirmation() == 0)
74 return (TRUE);
75 }
76#endif
77 if (rmdir(fileName) < 0) {
78 perror_msg("%s", fileName);
79 return (FALSE);
80 }
81 return (TRUE);
82}
83
84extern int rm_main(int argc, char **argv) 35extern int rm_main(int argc, char **argv)
85{ 36{
37 int status = 0;
86 int opt; 38 int opt;
87 int status = EXIT_SUCCESS; 39 int flags = 0;
88 struct stat statbuf; 40 int i;
89
90
91 /* do normal option parsing */
92 while ((opt = getopt(argc, argv, "Rrf-"
93#ifdef BB_FEATURE_RM_INTERACTIVE
94"i"
95#endif
96)) > 0) {
97 switch (opt) {
98 case 'R':
99 case 'r':
100 recursiveFlag = TRUE;
101 break;
102 case 'f':
103 forceFlag = TRUE;
104#ifdef BB_FEATURE_RM_INTERACTIVE
105 41
106 interactiveFlag = FALSE; 42 while ((opt = getopt(argc, argv, "fiRr")) != -1) {
107#endif 43 switch (opt) {
108 break; 44 case 'f':
109#ifdef BB_FEATURE_RM_INTERACTIVE 45 flags &= ~FILEUTILS_INTERACTIVE;
110 case 'i': 46 flags |= FILEUTILS_FORCE;
111 interactiveFlag = TRUE; 47 break;
112 forceFlag = FALSE; 48 case 'i':
113 break; 49 flags &= ~FILEUTILS_FORCE;
114#endif 50 flags |= FILEUTILS_INTERACTIVE;
115 default: 51 break;
116 show_usage(); 52 case 'R':
53 case 'r':
54 flags |= FILEUTILS_RECUR;
55 break;
117 } 56 }
118 } 57 }
119 58
120 if (argc == optind && forceFlag == FALSE) { 59 if (!(flags & FILEUTILS_FORCE) && optind == argc)
121 show_usage(); 60 show_usage();
122 }
123 61
124 while (optind < argc) { 62 for (i = optind; i < argc; i++) {
125 srcName = argv[optind]; 63 char *base = get_last_path_component(argv[i]);
126 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 64
127 && errno == ENOENT) { 65 if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) {
128 /* do not reports errors for non-existent files if -f, just skip them */ 66 error_msg("cannot remove `.' or `..'");
129 } else { 67 status = 1;
130 if (recursive_action(srcName, recursiveFlag, FALSE, 68 continue;
131 TRUE, fileAction, dirAction, NULL) == FALSE) {
132 status = EXIT_FAILURE;
133 }
134 } 69 }
135 optind++; 70
71 if (remove_file(argv[i], flags) < 0)
72 status = 1;
136 } 73 }
74
137 return status; 75 return status;
138} 76}