1 /* vi: set sw=4 ts=4: */
3 * Mini `cp' and `mv' implementation for BusyBox.
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
9 * Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL)
10 * Extensively modified and rewritten by Karl M. Hegbloom <karlheg@debian.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define BB_DECLARE_EXTERN
30 #define bb_need_name_too_long
31 #define bb_need_omitting_directory
32 #define bb_need_not_a_directory
39 #include <sys/param.h>
48 static int dz_i; /* index into cp_mv_usage */
50 static const char *cp_mv_usage[] = /* .rodata */
56 static int recursiveFlag;
57 static int followLinks;
58 static int preserveFlag;
61 static const char *baseSrcName;
62 static int srcDirFlag;
63 static struct stat srcStatBuf;
65 static char baseDestName[BUFSIZ + 1];
66 static size_t baseDestLen;
67 static int destDirFlag;
68 static struct stat destStatBuf;
71 static volatile int mv_Action_first_time;
73 static void name_too_long__exit (void) __attribute__((noreturn));
76 void name_too_long__exit (void)
78 error_msg_and_die(name_too_long);
82 fill_baseDest_buf(char *_buf, size_t * _buflen) {
83 const char *srcBasename;
84 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
85 srcBasename = baseSrcName;
86 if (_buf[*_buflen - 1] != '/') {
87 if (++(*_buflen) > BUFSIZ)
88 name_too_long__exit();
92 if (*_buflen + strlen(srcBasename) > BUFSIZ)
93 name_too_long__exit();
94 strcat(_buf, srcBasename);
100 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
102 char destName[BUFSIZ + 1];
104 const char *srcBasename;
107 strcpy(destName, baseDestName);
108 destLen = strlen(destName);
110 if (srcDirFlag == TRUE) {
111 if (recursiveFlag == FALSE) {
112 error_msg(omitting_directory, baseSrcName);
115 srcBasename = (strstr(fileName, baseSrcName)
116 + strlen(baseSrcName));
118 if (destLen + strlen(srcBasename) > BUFSIZ) {
119 error_msg(name_too_long);
122 strcat(destName, srcBasename);
124 else if (destDirFlag == TRUE) {
125 fill_baseDest_buf(&destName[0], &destLen);
128 srcBasename = baseSrcName;
130 if (mv_Action_first_time && (dz_i == is_mv)) {
131 mv_Action_first_time = errno = 0;
132 if (rename(fileName, destName) < 0 && errno != EXDEV) {
133 error_msg("rename(%s, %s): %s\n", fileName, destName,
135 goto do_copyFile; /* Try anyway... */
137 else if (errno == EXDEV)
140 longjmp(catch, 1); /* succeeded with rename() */
143 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
144 if (is_in_ino_dev_hashtable(statbuf, &name)) {
145 if (link(name, destName) < 0) {
146 error_msg("link(%s, %s): %s\n", name, destName, strerror(errno));
152 add_to_ino_dev_hashtable(statbuf, destName);
155 return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
159 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
163 if (S_ISDIR(statbuf->st_mode)) {
164 if (rmdir(fileName) < 0) {
165 error_msg("rmdir(%s): %s\n", fileName, strerror(errno));
168 } else if (unlink(fileName) < 0) {
169 error_msg("unlink(%s): %s\n", fileName, strerror(errno));
175 extern int cp_mv_main(int argc, char **argv)
180 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
185 usage(cp_mv_usage[dz_i]);
188 recursiveFlag = preserveFlag = forceFlag = FALSE;
190 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
195 recursiveFlag = TRUE;
204 recursiveFlag = TRUE;
210 usage(cp_mv_usage[is_cp]);
213 if ((argc - optind) < 2) {
214 usage(cp_mv_usage[dz_i]);
216 } else { /* (dz_i == is_mv) */
217 /* Initialize optind to 1, since in libc5 optind
218 * is not initialized until getopt() is called
219 * (or until sneaky programmers force it...). */
221 recursiveFlag = preserveFlag = TRUE;
226 if (strlen(argv[argc - 1]) > BUFSIZ) {
227 error_msg(name_too_long);
230 strcpy(baseDestName, argv[argc - 1]);
231 baseDestLen = strlen(baseDestName);
232 if (baseDestLen == 0)
235 destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
236 if (argc - optind > 2 && destDirFlag == FALSE) {
237 error_msg(not_a_directory, baseDestName);
241 for (i = optind; i < (argc-1); i++) {
243 volatile int flags_memo;
248 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
249 name_too_long__exit();
251 if (srcLen == 0) continue; /* "" */
253 srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
255 if ((flags_memo = (recursiveFlag == TRUE &&
256 srcDirFlag == TRUE && destDirFlag == TRUE))) {
262 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
263 error_msg("getcwd(): %s\n", strerror(errno));
266 if (chdir(baseDestName) < 0) {
267 error_msg("chdir(%s): %s\n", baseSrcName, strerror(errno));
270 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
271 error_msg("getcwd(): %s\n", strerror(errno));
274 while (!state && *d != '\0') {
275 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
276 error_msg("stat(%s): %s\n", d, strerror(errno));
280 if ((sb.st_ino == srcStatBuf.st_ino) &&
281 (sb.st_dev == srcStatBuf.st_dev)) {
282 error_msg("Cannot %s `%s' into a subdirectory of itself, "
283 "`%s/%s'\n", applet_name, baseSrcName,
284 baseDestName, baseSrcName);
288 if ((p = strrchr(d, '/')) != NULL) {
292 if (chdir(pushd) < 0) {
293 error_msg("chdir(%s): %s\n", pushd, strerror(errno));
303 fill_baseDest_buf(baseDestName, &baseDestLen);
305 status = setjmp(catch);
307 mv_Action_first_time = 1;
308 if (recursive_action(baseSrcName,
309 recursiveFlag, followLinks, FALSE,
310 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
312 recursive_action(baseSrcName,
313 recursiveFlag, followLinks, TRUE,
314 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
317 *(baseDestName + baseDestLen) = '\0';
326 c-file-style: "linux"