diff options
-rw-r--r-- | Config.h | 3 | ||||
-rw-r--r-- | applets.h | 8 | ||||
-rw-r--r-- | coreutils/cp.c | 114 | ||||
-rw-r--r-- | coreutils/mv.c | 183 | ||||
-rw-r--r-- | cp.c | 114 | ||||
-rw-r--r-- | include/applets.h | 8 | ||||
-rw-r--r-- | include/libbb.h | 11 | ||||
-rw-r--r-- | libbb/copy_file.c | 332 | ||||
-rw-r--r-- | libbb/libbb.h | 11 | ||||
-rw-r--r-- | mv.c | 183 |
10 files changed, 811 insertions, 156 deletions
@@ -16,7 +16,7 @@ | |||
16 | #define BB_CHVT | 16 | #define BB_CHVT |
17 | #define BB_CLEAR | 17 | #define BB_CLEAR |
18 | //#define BB_CMP | 18 | //#define BB_CMP |
19 | #define BB_CP_MV | 19 | #define BB_CP |
20 | #define BB_CUT | 20 | #define BB_CUT |
21 | #define BB_DATE | 21 | #define BB_DATE |
22 | //#define BB_DC | 22 | //#define BB_DC |
@@ -76,6 +76,7 @@ | |||
76 | #define BB_MORE | 76 | #define BB_MORE |
77 | #define BB_MOUNT | 77 | #define BB_MOUNT |
78 | //#define BB_MT | 78 | //#define BB_MT |
79 | #define BB_MV | ||
79 | //#define BB_NSLOOKUP | 80 | //#define BB_NSLOOKUP |
80 | //#define BB_PING | 81 | //#define BB_PING |
81 | //#define BB_PIVOT_ROOT | 82 | //#define BB_PIVOT_ROOT |
@@ -80,8 +80,8 @@ | |||
80 | #ifdef BB_CMP | 80 | #ifdef BB_CMP |
81 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN) | 81 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN) |
82 | #endif | 82 | #endif |
83 | #ifdef BB_CP_MV | 83 | #ifdef BB_CP |
84 | APPLET(cp, cp_mv_main, _BB_DIR_BIN) | 84 | APPLET(cp, cp_main, _BB_DIR_BIN) |
85 | #endif | 85 | #endif |
86 | #ifdef BB_CUT | 86 | #ifdef BB_CUT |
87 | APPLET(cut, cut_main, _BB_DIR_USR_BIN) | 87 | APPLET(cut, cut_main, _BB_DIR_USR_BIN) |
@@ -269,8 +269,8 @@ | |||
269 | #ifdef BB_MT | 269 | #ifdef BB_MT |
270 | APPLET(mt, mt_main, _BB_DIR_BIN) | 270 | APPLET(mt, mt_main, _BB_DIR_BIN) |
271 | #endif | 271 | #endif |
272 | #ifdef BB_CP_MV | 272 | #ifdef BB_MV |
273 | APPLET(mv, cp_mv_main, _BB_DIR_BIN) | 273 | APPLET(mv, mv_main, _BB_DIR_BIN) |
274 | #endif | 274 | #endif |
275 | #ifdef BB_NC | 275 | #ifdef BB_NC |
276 | APPLET(nc, nc_main, _BB_DIR_USR_BIN) | 276 | APPLET(nc, nc_main, _BB_DIR_USR_BIN) |
diff --git a/coreutils/cp.c b/coreutils/cp.c new file mode 100644 index 000000000..254445f02 --- /dev/null +++ b/coreutils/cp.c | |||
@@ -0,0 +1,114 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini cp implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <unistd.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <utime.h> | ||
29 | #include <errno.h> | ||
30 | #include <dirent.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | #include "busybox.h" | ||
34 | |||
35 | extern int cp_main(int argc, char **argv) | ||
36 | { | ||
37 | int status = 0; | ||
38 | int opt; | ||
39 | int flags = 0; | ||
40 | int i; | ||
41 | |||
42 | while ((opt = getopt(argc, argv, "adfipR")) != -1) | ||
43 | switch (opt) { | ||
44 | case 'a': | ||
45 | flags |= CP_PRESERVE_STATUS | CP_RECUR; | ||
46 | /* fallthrough */ | ||
47 | case 'd': | ||
48 | flags |= CP_PRESERVE_SYMLINKS; | ||
49 | break; | ||
50 | case 'f': | ||
51 | flags |= CP_FORCE; | ||
52 | break; | ||
53 | case 'i': | ||
54 | flags |= CP_INTERACTIVE; | ||
55 | break; | ||
56 | case 'p': | ||
57 | flags |= CP_PRESERVE_STATUS; | ||
58 | break; | ||
59 | case 'R': | ||
60 | flags |= CP_RECUR; | ||
61 | break; | ||
62 | default: | ||
63 | show_usage(); | ||
64 | } | ||
65 | |||
66 | if (optind + 2 > argc) | ||
67 | show_usage(); | ||
68 | |||
69 | /* If there are only two arguments and... */ | ||
70 | if (optind + 2 == argc) { | ||
71 | struct stat source_stat; | ||
72 | struct stat dest_stat; | ||
73 | int source_exists = 1; | ||
74 | int dest_exists = 1; | ||
75 | |||
76 | if (((flags & CP_PRESERVE_SYMLINKS) && | ||
77 | lstat(argv[optind], &source_stat) < 0) || | ||
78 | (!(flags & CP_PRESERVE_SYMLINKS) && | ||
79 | stat(argv[optind], &source_stat))) { | ||
80 | if (errno != ENOENT) | ||
81 | perror_msg_and_die("unable to stat `%s'", argv[optind]); | ||
82 | source_exists = 0; | ||
83 | } | ||
84 | |||
85 | if (stat(argv[optind + 1], &dest_stat) < 0) { | ||
86 | if (errno != ENOENT) | ||
87 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); | ||
88 | dest_exists = 0; | ||
89 | } | ||
90 | |||
91 | /* ...if neither is a directory or... */ | ||
92 | if (((!source_exists || !S_ISDIR(source_stat.st_mode)) && | ||
93 | (!dest_exists || !S_ISDIR(dest_stat.st_mode))) || | ||
94 | /* ...recursing, the first is a directory, and the | ||
95 | * second doesn't exist, then... */ | ||
96 | ((flags & CP_RECUR) && S_ISDIR(source_stat.st_mode) && | ||
97 | !dest_exists)) { | ||
98 | /* ...do a simple copy. */ | ||
99 | if (copy_file(argv[optind], argv[optind + 1], flags) < 0) | ||
100 | status = 1; | ||
101 | return status; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | for (i = optind; i < argc - 1; i++) { | ||
106 | char *dest = concat_path_file(argv[argc - 1], | ||
107 | get_last_path_component(argv[i])); | ||
108 | if (copy_file(argv[i], dest, flags) < 0) | ||
109 | status = 1; | ||
110 | free(dest); | ||
111 | } | ||
112 | |||
113 | return status; | ||
114 | } | ||
diff --git a/coreutils/mv.c b/coreutils/mv.c new file mode 100644 index 000000000..13d1aae5b --- /dev/null +++ b/coreutils/mv.c | |||
@@ -0,0 +1,183 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini mv implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <unistd.h> | ||
27 | #include <dirent.h> | ||
28 | #include <errno.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | #include "busybox.h" | ||
32 | |||
33 | static int flags = CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS; | ||
34 | |||
35 | static 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 | |||
42 | static 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 | |||
49 | static int manual_rename(const char *source, const char *dest) | ||
50 | { | ||
51 | struct stat source_stat; | ||
52 | struct stat dest_stat; | ||
53 | int source_exists = 1; | ||
54 | int dest_exists = 1; | ||
55 | |||
56 | if (stat(source, &source_stat) < 0) { | ||
57 | if (errno != ENOENT) { | ||
58 | perror_msg("unable to stat `%s'", source); | ||
59 | return -1; | ||
60 | } | ||
61 | source_exists = 0; | ||
62 | } | ||
63 | |||
64 | if (stat(dest, &dest_stat) < 0) { | ||
65 | if (errno != ENOENT) { | ||
66 | perror_msg("unable to stat `%s'", dest); | ||
67 | return -1; | ||
68 | } | ||
69 | dest_exists = 0; | ||
70 | } | ||
71 | |||
72 | if (dest_exists) { | ||
73 | if (S_ISDIR(dest_stat.st_mode) && | ||
74 | (!source_exists || !S_ISDIR(source_stat.st_mode))) { | ||
75 | error_msg("cannot overwrite directory with non-directory"); | ||
76 | return -1; | ||
77 | } | ||
78 | |||
79 | if (!S_ISDIR(dest_stat.st_mode) && source_exists && | ||
80 | S_ISDIR(source_stat.st_mode)) { | ||
81 | error_msg("cannot overwrite non-directory with directory"); | ||
82 | return -1; | ||
83 | } | ||
84 | |||
85 | if (unlink(dest) < 0) { | ||
86 | perror_msg("cannot remove `%s'", dest); | ||
87 | return -1; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | if (copy_file(source, dest, | ||
92 | CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS) < 0) | ||
93 | return -1; | ||
94 | |||
95 | if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, | ||
96 | remove_directory, NULL)) | ||
97 | return -1; | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | static int move_file(const char *source, const char *dest) | ||
103 | { | ||
104 | struct stat dest_stat; | ||
105 | int dest_exists = 1; | ||
106 | |||
107 | if (stat(dest, &dest_stat) < 0) { | ||
108 | if (errno != ENOENT) { | ||
109 | perror_msg("unable to stat `%s'", dest); | ||
110 | return -1; | ||
111 | } | ||
112 | dest_exists = 0; | ||
113 | } | ||
114 | |||
115 | if (dest_exists && !(flags & CP_FORCE) && | ||
116 | ((access(dest, W_OK) < 0 && isatty(0)) || | ||
117 | (flags & CP_INTERACTIVE))) { | ||
118 | fprintf(stderr, "mv: overwrite `%s'? ", dest); | ||
119 | if (!ask_confirmation()) | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | if (rename(source, dest) < 0) { | ||
124 | if (errno == EXDEV) | ||
125 | return manual_rename(source, dest); | ||
126 | |||
127 | perror_msg("unable to rename `%s'", source); | ||
128 | return -1; | ||
129 | } | ||
130 | |||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | extern int mv_main(int argc, char **argv) | ||
135 | { | ||
136 | int status = 0; | ||
137 | int opt; | ||
138 | int i; | ||
139 | |||
140 | while ((opt = getopt(argc, argv, "fi")) != -1) | ||
141 | switch (opt) { | ||
142 | case 'f': | ||
143 | flags &= ~CP_INTERACTIVE; | ||
144 | flags |= CP_FORCE; | ||
145 | break; | ||
146 | case 'i': | ||
147 | flags &= ~CP_FORCE; | ||
148 | flags |= CP_INTERACTIVE; | ||
149 | break; | ||
150 | default: | ||
151 | show_usage(); | ||
152 | } | ||
153 | |||
154 | if (optind + 2 > argc) | ||
155 | show_usage(); | ||
156 | |||
157 | if (optind + 2 == argc) { | ||
158 | struct stat dest_stat; | ||
159 | int dest_exists = 1; | ||
160 | |||
161 | if (stat(argv[optind + 1], &dest_stat) < 0) { | ||
162 | if (errno != ENOENT) | ||
163 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); | ||
164 | dest_exists = 0; | ||
165 | } | ||
166 | |||
167 | if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) { | ||
168 | if (move_file(argv[optind], argv[optind + 1]) < 0) | ||
169 | status = 1; | ||
170 | return status; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | for (i = optind; i < argc - 1; i++) { | ||
175 | char *dest = concat_path_file(argv[argc - 1], | ||
176 | get_last_path_component(argv[i])); | ||
177 | if (move_file(argv[i], dest) < 0) | ||
178 | status = 1; | ||
179 | free(dest); | ||
180 | } | ||
181 | |||
182 | return status; | ||
183 | } | ||
@@ -0,0 +1,114 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini cp implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <unistd.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <utime.h> | ||
29 | #include <errno.h> | ||
30 | #include <dirent.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | #include "busybox.h" | ||
34 | |||
35 | extern int cp_main(int argc, char **argv) | ||
36 | { | ||
37 | int status = 0; | ||
38 | int opt; | ||
39 | int flags = 0; | ||
40 | int i; | ||
41 | |||
42 | while ((opt = getopt(argc, argv, "adfipR")) != -1) | ||
43 | switch (opt) { | ||
44 | case 'a': | ||
45 | flags |= CP_PRESERVE_STATUS | CP_RECUR; | ||
46 | /* fallthrough */ | ||
47 | case 'd': | ||
48 | flags |= CP_PRESERVE_SYMLINKS; | ||
49 | break; | ||
50 | case 'f': | ||
51 | flags |= CP_FORCE; | ||
52 | break; | ||
53 | case 'i': | ||
54 | flags |= CP_INTERACTIVE; | ||
55 | break; | ||
56 | case 'p': | ||
57 | flags |= CP_PRESERVE_STATUS; | ||
58 | break; | ||
59 | case 'R': | ||
60 | flags |= CP_RECUR; | ||
61 | break; | ||
62 | default: | ||
63 | show_usage(); | ||
64 | } | ||
65 | |||
66 | if (optind + 2 > argc) | ||
67 | show_usage(); | ||
68 | |||
69 | /* If there are only two arguments and... */ | ||
70 | if (optind + 2 == argc) { | ||
71 | struct stat source_stat; | ||
72 | struct stat dest_stat; | ||
73 | int source_exists = 1; | ||
74 | int dest_exists = 1; | ||
75 | |||
76 | if (((flags & CP_PRESERVE_SYMLINKS) && | ||
77 | lstat(argv[optind], &source_stat) < 0) || | ||
78 | (!(flags & CP_PRESERVE_SYMLINKS) && | ||
79 | stat(argv[optind], &source_stat))) { | ||
80 | if (errno != ENOENT) | ||
81 | perror_msg_and_die("unable to stat `%s'", argv[optind]); | ||
82 | source_exists = 0; | ||
83 | } | ||
84 | |||
85 | if (stat(argv[optind + 1], &dest_stat) < 0) { | ||
86 | if (errno != ENOENT) | ||
87 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); | ||
88 | dest_exists = 0; | ||
89 | } | ||
90 | |||
91 | /* ...if neither is a directory or... */ | ||
92 | if (((!source_exists || !S_ISDIR(source_stat.st_mode)) && | ||
93 | (!dest_exists || !S_ISDIR(dest_stat.st_mode))) || | ||
94 | /* ...recursing, the first is a directory, and the | ||
95 | * second doesn't exist, then... */ | ||
96 | ((flags & CP_RECUR) && S_ISDIR(source_stat.st_mode) && | ||
97 | !dest_exists)) { | ||
98 | /* ...do a simple copy. */ | ||
99 | if (copy_file(argv[optind], argv[optind + 1], flags) < 0) | ||
100 | status = 1; | ||
101 | return status; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | for (i = optind; i < argc - 1; i++) { | ||
106 | char *dest = concat_path_file(argv[argc - 1], | ||
107 | get_last_path_component(argv[i])); | ||
108 | if (copy_file(argv[i], dest, flags) < 0) | ||
109 | status = 1; | ||
110 | free(dest); | ||
111 | } | ||
112 | |||
113 | return status; | ||
114 | } | ||
diff --git a/include/applets.h b/include/applets.h index c3037973d..d3399b9af 100644 --- a/include/applets.h +++ b/include/applets.h | |||
@@ -80,8 +80,8 @@ | |||
80 | #ifdef BB_CMP | 80 | #ifdef BB_CMP |
81 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN) | 81 | APPLET(cmp, cmp_main, _BB_DIR_USR_BIN) |
82 | #endif | 82 | #endif |
83 | #ifdef BB_CP_MV | 83 | #ifdef BB_CP |
84 | APPLET(cp, cp_mv_main, _BB_DIR_BIN) | 84 | APPLET(cp, cp_main, _BB_DIR_BIN) |
85 | #endif | 85 | #endif |
86 | #ifdef BB_CUT | 86 | #ifdef BB_CUT |
87 | APPLET(cut, cut_main, _BB_DIR_USR_BIN) | 87 | APPLET(cut, cut_main, _BB_DIR_USR_BIN) |
@@ -269,8 +269,8 @@ | |||
269 | #ifdef BB_MT | 269 | #ifdef BB_MT |
270 | APPLET(mt, mt_main, _BB_DIR_BIN) | 270 | APPLET(mt, mt_main, _BB_DIR_BIN) |
271 | #endif | 271 | #endif |
272 | #ifdef BB_CP_MV | 272 | #ifdef BB_MV |
273 | APPLET(mv, cp_mv_main, _BB_DIR_BIN) | 273 | APPLET(mv, mv_main, _BB_DIR_BIN) |
274 | #endif | 274 | #endif |
275 | #ifdef BB_NC | 275 | #ifdef BB_NC |
276 | APPLET(nc, nc_main, _BB_DIR_USR_BIN) | 276 | APPLET(nc, nc_main, _BB_DIR_USR_BIN) |
diff --git a/include/libbb.h b/include/libbb.h index 19de73ca9..c47a6689e 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -93,8 +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 copy_file(const char *src_name, const char *dst_name, | 96 | int copy_file(const char *source, const char *dest, int flags); |
97 | int set_modes, int follow_links, int force_flag, int quiet_flag); | ||
98 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); | 97 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); |
99 | char *buildName(const char *dirName, const char *fileName); | 98 | char *buildName(const char *dirName, const char *fileName); |
100 | int makeString(int argc, const char **argv, char *buf, int bufLen); | 99 | int makeString(int argc, const char **argv, char *buf, int bufLen); |
@@ -255,4 +254,12 @@ extern int gz_open(FILE *compressed_file, int *pid); | |||
255 | #define CT_DOS2UNIX 2 | 254 | #define CT_DOS2UNIX 2 |
256 | /* extern int convert(char *fn, int ConvType); */ | 255 | /* extern int convert(char *fn, int ConvType); */ |
257 | 256 | ||
257 | enum { | ||
258 | CP_PRESERVE_STATUS = 1, | ||
259 | CP_PRESERVE_SYMLINKS = 2, | ||
260 | CP_RECUR = 4, | ||
261 | CP_FORCE = 8, | ||
262 | CP_INTERACTIVE = 16 | ||
263 | }; | ||
264 | |||
258 | #endif /* __LIBBB_H__ */ | 265 | #endif /* __LIBBB_H__ */ |
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 7e5d11e67..113c253b9 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c | |||
@@ -1,10 +1,9 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * Mini copy_file implementation for busybox |
4 | * | 4 | * |
5 | * Copyright (C) tons of folks. Tracking down who wrote what | 5 | * |
6 | * isn't something I'm going to worry about... If you wrote something | 6 | * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
7 | * here, please feel free to acknowledge your work. | ||
8 | * | 7 | * |
9 | * This program is free software; you can redistribute it and/or modify | 8 | * 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 | 9 | * it under the terms of the GNU General Public License as published by |
@@ -20,179 +19,226 @@ | |||
20 | * along with this program; if not, write to the Free Software | 19 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 | * | 21 | * |
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | 22 | */ |
27 | 23 | ||
28 | #include <stdio.h> | 24 | #include <sys/types.h> |
29 | #include <errno.h> | 25 | #include <sys/stat.h> |
30 | #include <utime.h> | ||
31 | #include <unistd.h> | 26 | #include <unistd.h> |
32 | #include <fcntl.h> | 27 | #include <fcntl.h> |
33 | #include <sys/stat.h> | 28 | #include <utime.h> |
34 | #include "libbb.h" | 29 | #include <errno.h> |
30 | #include <dirent.h> | ||
31 | #include <stdlib.h> | ||
35 | 32 | ||
33 | #include "libbb.h" | ||
36 | 34 | ||
37 | /* | 35 | int copy_file(const char *source, const char *dest, int flags) |
38 | * Copy one file to another, while possibly preserving its modes, times, and | ||
39 | * modes. Returns TRUE if successful, or FALSE on a failure with an error | ||
40 | * message output. (Failure is not indicated if attributes cannot be set.) | ||
41 | * -Erik Andersen | ||
42 | */ | ||
43 | int | ||
44 | copy_file(const char *src_name, const char *dst_name, | ||
45 | int set_modes, int follow_links, int force_flag, int quiet_flag) | ||
46 | { | 36 | { |
47 | FILE *src_file = NULL; | 37 | struct stat source_stat; |
48 | FILE *dst_file = NULL; | 38 | struct stat dest_stat; |
49 | struct stat srcStatBuf; | 39 | int dest_exists = 1; |
50 | struct stat dstStatBuf; | 40 | int status = 0; |
51 | struct utimbuf times; | 41 | |
52 | int src_status; | 42 | if (((flags & CP_PRESERVE_SYMLINKS) && lstat(source, &source_stat) < 0) || |
53 | int dst_status; | 43 | (!(flags & CP_PRESERVE_SYMLINKS) && |
54 | 44 | stat(source, &source_stat) < 0)) { | |
55 | if (follow_links == TRUE) { | 45 | perror_msg("%s", source); |
56 | src_status = stat(src_name, &srcStatBuf); | 46 | return -1; |
57 | dst_status = stat(dst_name, &dstStatBuf); | ||
58 | } else { | ||
59 | src_status = lstat(src_name, &srcStatBuf); | ||
60 | dst_status = lstat(dst_name, &dstStatBuf); | ||
61 | } | 47 | } |
62 | 48 | ||
63 | if (src_status < 0) { | 49 | if (stat(dest, &dest_stat) < 0) { |
64 | if (!quiet_flag) { | 50 | if (errno != ENOENT) { |
65 | perror_msg("%s", src_name); | 51 | perror_msg("unable to stat `%s'", dest); |
52 | return -1; | ||
66 | } | 53 | } |
67 | return FALSE; | 54 | dest_exists = 0; |
68 | } | 55 | } |
69 | 56 | ||
70 | if ((dst_status < 0) || force_flag) { | 57 | if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev && |
71 | unlink(dst_name); | 58 | source_stat.st_ino == dest_stat.st_ino) { |
72 | dstStatBuf.st_ino = -1; | 59 | error_msg("`%s' and `%s' are the same file", source, dest); |
73 | dstStatBuf.st_dev = -1; | 60 | return -1; |
74 | } | 61 | } |
75 | 62 | ||
76 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && | 63 | if (S_ISDIR(source_stat.st_mode)) { |
77 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { | 64 | DIR *dp; |
78 | if (!quiet_flag) { | 65 | struct dirent *d; |
79 | error_msg("Copying file \"%s\" to itself", src_name); | 66 | |
67 | if (!(flags & CP_RECUR)) { | ||
68 | error_msg("%s: omitting directory", source); | ||
69 | return -1; | ||
80 | } | 70 | } |
81 | return FALSE; | ||
82 | } | ||
83 | 71 | ||
84 | if (S_ISDIR(srcStatBuf.st_mode)) { | 72 | /* Create DEST. */ |
85 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); | 73 | if (dest_exists) { |
86 | /* Make sure the directory is writable */ | 74 | if (!S_ISDIR(dest_stat.st_mode)) { |
87 | dst_status = create_path(dst_name, 0777777 ^ umask(0)); | 75 | error_msg("`%s' is not a directory", dest); |
88 | if ((dst_status < 0) && (errno != EEXIST)) { | 76 | return -1; |
89 | if (!quiet_flag) { | ||
90 | perror_msg("%s", dst_name); | ||
91 | } | 77 | } |
92 | return FALSE; | 78 | } else { |
93 | } | 79 | mode_t mode, saved_umask; |
94 | } else if (S_ISLNK(srcStatBuf.st_mode)) { | 80 | saved_umask = umask(0); |
95 | char link_val[BUFSIZ + 1]; | 81 | |
96 | int link_size; | 82 | mode = source_stat.st_mode; |
97 | 83 | if (!(flags & CP_PRESERVE_STATUS)) | |
98 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); | 84 | mode = source_stat.st_mode & ~saved_umask; |
99 | /* Warning: This could possibly truncate silently, to BUFSIZ chars */ | 85 | mode |= S_IRWXU; |
100 | link_size = readlink(src_name, &link_val[0], BUFSIZ); | 86 | |
101 | if (link_size < 0) { | 87 | if (mkdir(dest, mode) < 0) { |
102 | if (quiet_flag) { | 88 | umask(saved_umask); |
103 | perror_msg("%s", src_name); | 89 | perror_msg("cannot create directory `%s'", dest); |
90 | return -1; | ||
104 | } | 91 | } |
105 | return FALSE; | 92 | |
93 | umask(saved_umask); | ||
106 | } | 94 | } |
107 | link_val[link_size] = '\0'; | 95 | |
108 | src_status = symlink(link_val, dst_name); | 96 | /* Recursively copy files in SOURCE. */ |
109 | if (src_status < 0) { | 97 | if ((dp = opendir(source)) == NULL) { |
110 | if (!quiet_flag) { | 98 | perror_msg("unable to open directory `%s'", source); |
111 | perror_msg("%s", dst_name); | 99 | status = -1; |
112 | } | 100 | goto end; |
113 | return FALSE; | ||
114 | } | 101 | } |
115 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) | 102 | |
116 | if (set_modes == TRUE) { | 103 | while ((d = readdir(dp)) != NULL) { |
117 | /* Try to set owner, but fail silently like GNU cp */ | 104 | char *new_source, *new_dest; |
118 | lchown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid); | 105 | |
106 | if (strcmp(d->d_name, ".") == 0 || | ||
107 | strcmp(d->d_name, "..") == 0) | ||
108 | continue; | ||
109 | |||
110 | new_source = concat_path_file(source, d->d_name); | ||
111 | new_dest = concat_path_file(dest, d->d_name); | ||
112 | if (copy_file(new_source, new_dest, flags) < 0) | ||
113 | status = -1; | ||
114 | free(new_source); | ||
115 | free(new_dest); | ||
119 | } | 116 | } |
120 | #endif | 117 | |
121 | return TRUE; | 118 | /* ??? What if an error occurs in readdir? */ |
122 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { | 119 | |
123 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); | 120 | if (closedir(dp) < 0) { |
124 | if (mkfifo(dst_name, 0644) < 0) { | 121 | perror_msg("unable to close directory `%s'", source); |
125 | if (!quiet_flag) { | 122 | status = -1; |
126 | perror_msg("%s", dst_name); | 123 | } |
127 | } | 124 | } else if (S_ISREG(source_stat.st_mode)) { |
128 | return FALSE; | 125 | FILE *sfp, *dfp; |
129 | } | 126 | |
130 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) | 127 | if (dest_exists) { |
131 | || S_ISSOCK(srcStatBuf.st_mode)) { | 128 | if (flags & CP_INTERACTIVE) { |
132 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); | 129 | fprintf(stderr, "cp: overwrite `%s'? ", dest); |
133 | if (mknod(dst_name, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) { | 130 | if (!ask_confirmation()) |
134 | if (!quiet_flag) { | 131 | return 0; |
135 | perror_msg("%s", dst_name); | ||
136 | } | 132 | } |
137 | return FALSE; | 133 | |
138 | } | 134 | if ((dfp = fopen(dest, "w")) == NULL) { |
139 | } else if (S_ISREG(srcStatBuf.st_mode)) { | 135 | if (!(flags & CP_FORCE)) { |
140 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); | 136 | perror_msg("unable to open `%s'", dest); |
141 | src_file = fopen(src_name, "r"); | 137 | return -1; |
142 | if (src_file == NULL) { | 138 | } |
143 | if (!quiet_flag) { | 139 | |
144 | perror_msg("%s", src_name); | 140 | if (unlink(dest) < 0) { |
141 | perror_msg("unable to remove `%s'", dest); | ||
142 | return -1; | ||
143 | } | ||
144 | |||
145 | dest_exists = 0; | ||
145 | } | 146 | } |
146 | return FALSE; | ||
147 | } | 147 | } |
148 | 148 | ||
149 | dst_file = fopen(dst_name, "w"); | 149 | if (!dest_exists) { |
150 | chmod(dst_name, srcStatBuf.st_mode); | 150 | int fd; |
151 | if (dst_file == NULL) { | 151 | |
152 | if (!quiet_flag) { | 152 | if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || |
153 | perror_msg("%s", dst_name); | 153 | (dfp = fdopen(fd, "w")) == NULL) { |
154 | if (fd >= 0) | ||
155 | close(fd); | ||
156 | perror_msg("unable to open `%s'", dest); | ||
157 | return -1; | ||
154 | } | 158 | } |
155 | fclose(src_file); | ||
156 | return FALSE; | ||
157 | } | 159 | } |
158 | 160 | ||
159 | if (copy_file_chunk(src_file, dst_file, srcStatBuf.st_size)==FALSE) { | 161 | if ((sfp = fopen(source, "r")) == NULL) { |
160 | goto error_exit; | 162 | fclose(dfp); |
163 | perror_msg("unable to open `%s'", source); | ||
164 | status = -1; | ||
165 | goto end; | ||
166 | } | ||
167 | |||
168 | copy_file_chunk(sfp, dfp, source_stat.st_size); | ||
169 | |||
170 | if (fclose(dfp) < 0) { | ||
171 | perror_msg("unable to close `%s'", dest); | ||
172 | status = -1; | ||
161 | } | 173 | } |
162 | 174 | ||
163 | fclose(src_file); | 175 | if (fclose(sfp) < 0) { |
164 | if (fclose(dst_file) < 0) { | 176 | perror_msg("unable to close `%s'", source); |
165 | return FALSE; | 177 | status = -1; |
166 | } | 178 | } |
167 | } | 179 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || |
180 | S_ISSOCK(source_stat.st_mode)) { | ||
181 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { | ||
182 | perror_msg("unable to create `%s'", dest); | ||
183 | return -1; | ||
184 | } | ||
185 | } else if (S_ISFIFO(source_stat.st_mode)) { | ||
186 | mode_t mode, saved_umask; | ||
187 | saved_umask = umask(0); | ||
188 | |||
189 | mode = source_stat.st_mode; | ||
190 | if (!(flags & CP_PRESERVE_STATUS)) | ||
191 | mode = source_stat.st_mode & ~saved_umask; | ||
192 | mode |= S_IRWXU; | ||
193 | |||
194 | if (mkfifo(dest, mode) < 0) { | ||
195 | umask(saved_umask); | ||
196 | perror_msg("cannot create fifo `%s'", dest); | ||
197 | return -1; | ||
198 | } | ||
199 | |||
200 | umask(saved_umask); | ||
201 | } else if (S_ISLNK(source_stat.st_mode)) { | ||
202 | char buf[BUFSIZ + 1]; | ||
168 | 203 | ||
169 | if (set_modes == TRUE) { | 204 | if (readlink(source, buf, BUFSIZ) < 0) { |
170 | /* This is fine, since symlinks never get here */ | 205 | perror_msg("cannot read `%s'", source); |
171 | if (chown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) | 206 | return -1; |
172 | perror_msg("%s", dst_name); | 207 | } |
173 | if (chmod(dst_name, srcStatBuf.st_mode) < 0) | 208 | buf[BUFSIZ] = '\0'; |
174 | perror_msg("%s", dst_name); | 209 | |
175 | times.actime = srcStatBuf.st_atime; | 210 | if (symlink(buf, dest) < 0) { |
176 | times.modtime = srcStatBuf.st_mtime; | 211 | perror_msg("cannot create symlink `%s'", dest); |
177 | if (utime(dst_name, ×) < 0) | 212 | return -1; |
178 | perror_msg("%s", dst_name); | 213 | } |
214 | |||
215 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) | ||
216 | if (flags & CP_PRESERVE_STATUS) | ||
217 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) | ||
218 | perror_msg("unable to preserve ownership of `%s'", dest); | ||
219 | #endif | ||
220 | return 0; | ||
221 | } else { | ||
222 | error_msg("internal error: unrecognized file type"); | ||
223 | return -1; | ||
179 | } | 224 | } |
180 | 225 | ||
181 | return TRUE; | 226 | end: |
182 | 227 | ||
183 | error_exit: | 228 | if (flags & CP_PRESERVE_STATUS) { |
184 | perror_msg("%s", dst_name); | 229 | struct utimbuf times; |
185 | fclose(src_file); | ||
186 | fclose(dst_file); | ||
187 | 230 | ||
188 | return FALSE; | 231 | times.actime = source_stat.st_atime; |
189 | } | 232 | times.modtime = source_stat.st_mtime; |
233 | if (utime(dest, ×) < 0) | ||
234 | perror_msg("unable to preserve times of `%s'", dest); | ||
235 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { | ||
236 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); | ||
237 | perror_msg("unable to preserve ownership of `%s'", dest); | ||
238 | } | ||
239 | if (chmod(dest, source_stat.st_mode) < 0) | ||
240 | perror_msg("unable to preserve permissions of `%s'", dest); | ||
241 | } | ||
190 | 242 | ||
191 | /* END CODE */ | 243 | return 0; |
192 | /* | 244 | } |
193 | Local Variables: | ||
194 | c-file-style: "linux" | ||
195 | c-basic-offset: 4 | ||
196 | tab-width: 4 | ||
197 | End: | ||
198 | */ | ||
diff --git a/libbb/libbb.h b/libbb/libbb.h index 19de73ca9..c47a6689e 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -93,8 +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 copy_file(const char *src_name, const char *dst_name, | 96 | int copy_file(const char *source, const char *dest, int flags); |
97 | int set_modes, int follow_links, int force_flag, int quiet_flag); | ||
98 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); | 97 | int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); |
99 | char *buildName(const char *dirName, const char *fileName); | 98 | char *buildName(const char *dirName, const char *fileName); |
100 | int makeString(int argc, const char **argv, char *buf, int bufLen); | 99 | int makeString(int argc, const char **argv, char *buf, int bufLen); |
@@ -255,4 +254,12 @@ extern int gz_open(FILE *compressed_file, int *pid); | |||
255 | #define CT_DOS2UNIX 2 | 254 | #define CT_DOS2UNIX 2 |
256 | /* extern int convert(char *fn, int ConvType); */ | 255 | /* extern int convert(char *fn, int ConvType); */ |
257 | 256 | ||
257 | enum { | ||
258 | CP_PRESERVE_STATUS = 1, | ||
259 | CP_PRESERVE_SYMLINKS = 2, | ||
260 | CP_RECUR = 4, | ||
261 | CP_FORCE = 8, | ||
262 | CP_INTERACTIVE = 16 | ||
263 | }; | ||
264 | |||
258 | #endif /* __LIBBB_H__ */ | 265 | #endif /* __LIBBB_H__ */ |
@@ -0,0 +1,183 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini mv implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <unistd.h> | ||
27 | #include <dirent.h> | ||
28 | #include <errno.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | #include "busybox.h" | ||
32 | |||
33 | static int flags = CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS; | ||
34 | |||
35 | static 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 | |||
42 | static 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 | |||
49 | static int manual_rename(const char *source, const char *dest) | ||
50 | { | ||
51 | struct stat source_stat; | ||
52 | struct stat dest_stat; | ||
53 | int source_exists = 1; | ||
54 | int dest_exists = 1; | ||
55 | |||
56 | if (stat(source, &source_stat) < 0) { | ||
57 | if (errno != ENOENT) { | ||
58 | perror_msg("unable to stat `%s'", source); | ||
59 | return -1; | ||
60 | } | ||
61 | source_exists = 0; | ||
62 | } | ||
63 | |||
64 | if (stat(dest, &dest_stat) < 0) { | ||
65 | if (errno != ENOENT) { | ||
66 | perror_msg("unable to stat `%s'", dest); | ||
67 | return -1; | ||
68 | } | ||
69 | dest_exists = 0; | ||
70 | } | ||
71 | |||
72 | if (dest_exists) { | ||
73 | if (S_ISDIR(dest_stat.st_mode) && | ||
74 | (!source_exists || !S_ISDIR(source_stat.st_mode))) { | ||
75 | error_msg("cannot overwrite directory with non-directory"); | ||
76 | return -1; | ||
77 | } | ||
78 | |||
79 | if (!S_ISDIR(dest_stat.st_mode) && source_exists && | ||
80 | S_ISDIR(source_stat.st_mode)) { | ||
81 | error_msg("cannot overwrite non-directory with directory"); | ||
82 | return -1; | ||
83 | } | ||
84 | |||
85 | if (unlink(dest) < 0) { | ||
86 | perror_msg("cannot remove `%s'", dest); | ||
87 | return -1; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | if (copy_file(source, dest, | ||
92 | CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS) < 0) | ||
93 | return -1; | ||
94 | |||
95 | if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, | ||
96 | remove_directory, NULL)) | ||
97 | return -1; | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | static int move_file(const char *source, const char *dest) | ||
103 | { | ||
104 | struct stat dest_stat; | ||
105 | int dest_exists = 1; | ||
106 | |||
107 | if (stat(dest, &dest_stat) < 0) { | ||
108 | if (errno != ENOENT) { | ||
109 | perror_msg("unable to stat `%s'", dest); | ||
110 | return -1; | ||
111 | } | ||
112 | dest_exists = 0; | ||
113 | } | ||
114 | |||
115 | if (dest_exists && !(flags & CP_FORCE) && | ||
116 | ((access(dest, W_OK) < 0 && isatty(0)) || | ||
117 | (flags & CP_INTERACTIVE))) { | ||
118 | fprintf(stderr, "mv: overwrite `%s'? ", dest); | ||
119 | if (!ask_confirmation()) | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | if (rename(source, dest) < 0) { | ||
124 | if (errno == EXDEV) | ||
125 | return manual_rename(source, dest); | ||
126 | |||
127 | perror_msg("unable to rename `%s'", source); | ||
128 | return -1; | ||
129 | } | ||
130 | |||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | extern int mv_main(int argc, char **argv) | ||
135 | { | ||
136 | int status = 0; | ||
137 | int opt; | ||
138 | int i; | ||
139 | |||
140 | while ((opt = getopt(argc, argv, "fi")) != -1) | ||
141 | switch (opt) { | ||
142 | case 'f': | ||
143 | flags &= ~CP_INTERACTIVE; | ||
144 | flags |= CP_FORCE; | ||
145 | break; | ||
146 | case 'i': | ||
147 | flags &= ~CP_FORCE; | ||
148 | flags |= CP_INTERACTIVE; | ||
149 | break; | ||
150 | default: | ||
151 | show_usage(); | ||
152 | } | ||
153 | |||
154 | if (optind + 2 > argc) | ||
155 | show_usage(); | ||
156 | |||
157 | if (optind + 2 == argc) { | ||
158 | struct stat dest_stat; | ||
159 | int dest_exists = 1; | ||
160 | |||
161 | if (stat(argv[optind + 1], &dest_stat) < 0) { | ||
162 | if (errno != ENOENT) | ||
163 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); | ||
164 | dest_exists = 0; | ||
165 | } | ||
166 | |||
167 | if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) { | ||
168 | if (move_file(argv[optind], argv[optind + 1]) < 0) | ||
169 | status = 1; | ||
170 | return status; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | for (i = optind; i < argc - 1; i++) { | ||
175 | char *dest = concat_path_file(argv[argc - 1], | ||
176 | get_last_path_component(argv[i])); | ||
177 | if (move_file(argv[i], dest) < 0) | ||
178 | status = 1; | ||
179 | free(dest); | ||
180 | } | ||
181 | |||
182 | return status; | ||
183 | } | ||