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>
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"
53 #ifndef BB_FEATURE_TRIVIAL_HELP
54 "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
56 "\t-a\tSame as -dpR\n"
57 "\t-d\tPreserves links\n"
58 "\t-p\tPreserves file attributes if possible\n"
59 "\t-R\tCopies directories recursively\n"
63 " or: mv SOURCE... DIRECTORY\n"
64 #ifndef BB_FEATURE_TRIVIAL_HELP
65 "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
69 static int recursiveFlag;
70 static int followLinks;
71 static int preserveFlag;
73 static const char *baseSrcName;
74 static int srcDirFlag;
75 static struct stat srcStatBuf;
77 static char baseDestName[BUFSIZ + 1];
78 static size_t baseDestLen;
79 static int destDirFlag;
80 static struct stat destStatBuf;
83 static volatile int mv_Action_first_time;
85 static void name_too_long__exit (void) __attribute__((noreturn));
88 void name_too_long__exit (void)
90 fprintf(stderr, name_too_long, dz);
95 fill_baseDest_buf(char *_buf, size_t * _buflen) {
96 const char *srcBasename;
97 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
98 srcBasename = baseSrcName;
99 if (_buf[*_buflen - 1] != '/') {
100 if (++(*_buflen) > BUFSIZ)
101 name_too_long__exit();
105 if (*_buflen + strlen(srcBasename) > BUFSIZ)
106 name_too_long__exit();
107 strcat(_buf, srcBasename);
113 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
115 char destName[BUFSIZ + 1];
117 const char *srcBasename;
120 strcpy(destName, baseDestName);
121 destLen = strlen(destName);
123 if (srcDirFlag == TRUE) {
124 if (recursiveFlag == FALSE) {
125 fprintf(stderr, omitting_directory, dz, baseSrcName);
128 srcBasename = (strstr(fileName, baseSrcName)
129 + strlen(baseSrcName));
131 if (destLen + strlen(srcBasename) > BUFSIZ) {
132 fprintf(stderr, name_too_long, dz);
135 strcat(destName, srcBasename);
137 else if (destDirFlag == TRUE) {
138 fill_baseDest_buf(&destName[0], &destLen);
141 srcBasename = baseSrcName;
143 if (mv_Action_first_time && (dz_i == is_mv)) {
144 mv_Action_first_time = errno = 0;
145 if (rename(fileName, destName) < 0 && errno != EXDEV) {
146 fprintf(stderr, "%s: rename(%s, %s): %s\n",
147 dz, fileName, destName, strerror(errno));
148 goto do_copyFile; /* Try anyway... */
150 else if (errno == EXDEV)
153 longjmp(catch, 1); /* succeeded with rename() */
156 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
157 if (is_in_ino_dev_hashtable(statbuf, &name)) {
158 if (link(name, destName) < 0) {
159 fprintf(stderr, "%s: link(%s, %s): %s\n",
160 dz, name, destName, strerror(errno));
166 add_to_ino_dev_hashtable(statbuf, destName);
169 return copyFile(fileName, destName, preserveFlag, followLinks);
173 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
177 if (S_ISDIR(statbuf->st_mode)) {
178 if (rmdir(fileName) < 0) {
179 fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
182 } else if (unlink(fileName) < 0) {
183 fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
189 extern int cp_mv_main(int argc, char **argv)
191 dz = *argv; /* already basename'd by busybox.c:main */
192 if (*dz == 'c' && *(dz + 1) == 'p')
197 usage(cp_mv_usage[dz_i]);
202 recursiveFlag = preserveFlag = FALSE;
204 while (**argv == '-') {
210 recursiveFlag = TRUE;
219 recursiveFlag = TRUE;
222 usage(cp_mv_usage[is_cp]);
228 } else { /* (dz_i == is_mv) */
229 recursiveFlag = preserveFlag = TRUE;
233 if (strlen(argv[argc - 1]) > BUFSIZ) {
234 fprintf(stderr, name_too_long, "cp");
237 strcpy(baseDestName, argv[argc - 1]);
238 baseDestLen = strlen(baseDestName);
239 if (baseDestLen == 0)
242 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
243 if ((argc > 3) && destDirFlag == FALSE) {
244 fprintf(stderr, not_a_directory, "cp", baseDestName);
250 volatile int flags_memo;
253 baseSrcName = *(argv++);
255 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
256 name_too_long__exit();
258 if (srcLen == 0) continue; /* "" */
260 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
262 if ((flags_memo = (recursiveFlag == TRUE &&
263 srcDirFlag == TRUE && destDirFlag == TRUE))) {
269 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
270 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
273 if (chdir(baseDestName) < 0) {
274 fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
277 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
278 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
281 while (!state && *d != '\0') {
282 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
283 fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
287 if ((sb.st_ino == srcStatBuf.st_ino) &&
288 (sb.st_dev == srcStatBuf.st_dev)) {
290 "%s: Cannot %s `%s' "
291 "into a subdirectory of itself, `%s/%s'\n",
292 dz, dz, baseSrcName, baseDestName, baseSrcName);
296 if ((p = strrchr(d, '/')) != NULL) {
300 if (chdir(pushd) < 0) {
301 fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
311 fill_baseDest_buf(baseDestName, &baseDestLen);
313 status = setjmp(catch);
315 mv_Action_first_time = 1;
316 if (recursiveAction(baseSrcName,
317 recursiveFlag, followLinks, FALSE,
318 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
320 recursiveAction(baseSrcName,
321 recursiveFlag, followLinks, TRUE,
322 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
325 *(baseDestName + baseDestLen) = '\0';
335 c-file-style: "linux"