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>
40 #include <setjmp.h> /* Ok to use this since `ash' does, therefore it's in the libc subset already. */
47 static int dz_i; /* index into cp_mv_usage */
48 static const char *dz; /* dollar zero, .bss */
49 static const char *cp_mv_usage[] = /* .rodata */
51 "cp [OPTION]... SOURCE DEST\n"
52 " or: cp [OPTION]... SOURCE... DIRECTORY\n\n"
53 "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
55 "\t-a\tsame as -dpR\n"
56 "\t-d\tpreserve links\n"
57 "\t-p\tpreserve file attributes if possible\n"
58 "\t-R\tcopy directories recursively\n",
60 " or: mv SOURCE... DIRECTORY\n\n"
61 "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
64 static int recursiveFlag;
65 static int followLinks;
66 static int preserveFlag;
68 static const char *baseSrcName;
69 static int srcDirFlag;
70 static struct stat srcStatBuf;
72 static char baseDestName[PATH_MAX + 1];
73 static size_t baseDestLen;
74 static int destDirFlag;
75 static struct stat destStatBuf;
78 static volatile int mv_Action_first_time;
80 static void name_too_long__exit (void) __attribute__((noreturn));
83 void name_too_long__exit (void)
85 fprintf(stderr, name_too_long, dz);
90 fill_baseDest_buf(char *_buf, size_t * _buflen) {
91 const char *srcBasename;
92 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
93 srcBasename = baseSrcName;
94 if (_buf[*_buflen - 1] != '/') {
95 if (++(*_buflen) > PATH_MAX)
96 name_too_long__exit();
100 if (*_buflen + strlen(srcBasename) > PATH_MAX)
101 name_too_long__exit();
102 strcat(_buf, srcBasename);
108 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
110 char destName[PATH_MAX + 1];
112 const char *srcBasename;
115 strcpy(destName, baseDestName);
116 destLen = strlen(destName);
118 if (srcDirFlag == TRUE) {
119 if (recursiveFlag == FALSE) {
120 fprintf(stderr, omitting_directory, dz, baseSrcName);
123 srcBasename = (strstr(fileName, baseSrcName)
124 + strlen(baseSrcName));
126 if (destLen + strlen(srcBasename) > PATH_MAX) {
127 fprintf(stderr, name_too_long, dz);
130 strcat(destName, srcBasename);
132 else if (destDirFlag == TRUE) {
133 fill_baseDest_buf(&destName[0], &destLen);
136 srcBasename = baseSrcName;
138 if (mv_Action_first_time && (dz_i == is_mv)) {
139 mv_Action_first_time = errno = 0;
140 if (rename(fileName, destName) < 0 && errno != EXDEV) {
141 fprintf(stderr, "%s: rename(%s, %s): %s\n",
142 dz, fileName, destName, strerror(errno));
143 goto do_copyFile; /* Try anyway... */
145 else if (errno == EXDEV)
148 longjmp(catch, 1); /* succeeded with rename() */
151 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
152 if (is_in_ino_dev_hashtable(statbuf, &name)) {
153 if (link(name, destName) < 0) {
154 fprintf(stderr, "%s: link(%s, %s): %s\n",
155 dz, name, destName, strerror(errno));
161 add_to_ino_dev_hashtable(statbuf, destName);
164 return copyFile(fileName, destName, preserveFlag, followLinks);
168 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
172 if (S_ISDIR(statbuf->st_mode)) {
173 if (rmdir(fileName) < 0) {
174 fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
177 } else if (unlink(fileName) < 0) {
178 fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
184 extern int cp_mv_main(int argc, char **argv)
186 dz = *argv; /* already basename'd by busybox.c:main */
187 if (*dz == 'c' && *(dz + 1) == 'p')
192 usage(cp_mv_usage[dz_i]);
197 recursiveFlag = preserveFlag = FALSE;
199 while (**argv == '-') {
205 recursiveFlag = TRUE;
214 recursiveFlag = TRUE;
217 usage(cp_mv_usage[is_cp]);
223 } else { /* (dz_i == is_mv) */
224 recursiveFlag = preserveFlag = TRUE;
228 if (strlen(argv[argc - 1]) > PATH_MAX) {
229 fprintf(stderr, name_too_long, "cp");
232 strcpy(baseDestName, argv[argc - 1]);
233 baseDestLen = strlen(baseDestName);
234 if (baseDestLen == 0)
237 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
238 if ((argc > 3) && destDirFlag == FALSE) {
239 fprintf(stderr, not_a_directory, "cp", baseDestName);
245 volatile int flags_memo;
248 baseSrcName = *(argv++);
250 if ((srcLen = strlen(baseSrcName)) > PATH_MAX)
251 name_too_long__exit();
253 if (srcLen == 0) continue; /* "" */
255 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
257 if ((flags_memo = (recursiveFlag == TRUE &&
258 srcDirFlag == TRUE && destDirFlag == TRUE))) {
264 if ((pushd = getcwd(NULL, PATH_MAX + 1)) == NULL) {
265 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
268 if (chdir(baseDestName) < 0) {
269 fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
272 if ((d = getcwd(NULL, PATH_MAX + 1)) == NULL) {
273 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
276 while (!state && *d != '\0') {
277 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
278 fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
282 if ((sb.st_ino == srcStatBuf.st_ino) &&
283 (sb.st_dev == srcStatBuf.st_dev)) {
285 "%s: Cannot %s `%s' "
286 "into a subdirectory of itself, `%s/%s'\n",
287 dz, dz, baseSrcName, baseDestName, baseSrcName);
291 if ((p = strrchr(d, '/')) != NULL) {
295 if (chdir(pushd) < 0) {
296 fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
306 fill_baseDest_buf(baseDestName, &baseDestLen);
308 status = setjmp(catch);
310 mv_Action_first_time = 1;
311 if (recursiveAction(baseSrcName,
312 recursiveFlag, followLinks, FALSE,
313 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
315 recursiveAction(baseSrcName,
316 recursiveFlag, followLinks, TRUE,
317 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
320 *(baseDestName + baseDestLen) = '\0';
330 c-file-style: "linux"