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 *cp_mv_usage[] = /* .rodata */
50 "cp [OPTION]... SOURCE DEST\n"
51 " or: cp [OPTION]... SOURCE... DIRECTORY\n"
52 #ifndef BB_FEATURE_TRIVIAL_HELP
53 "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
55 "\t-a\tSame as -dpR\n"
56 "\t-d\tPreserves links\n"
57 "\t-p\tPreserves file attributes if possible\n"
58 "\t-f\tforce (implied; ignored) - always set\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;
74 static const char *baseSrcName;
75 static int srcDirFlag;
76 static struct stat srcStatBuf;
78 static char baseDestName[BUFSIZ + 1];
79 static size_t baseDestLen;
80 static int destDirFlag;
81 static struct stat destStatBuf;
84 static volatile int mv_Action_first_time;
86 static void name_too_long__exit (void) __attribute__((noreturn));
89 void name_too_long__exit (void)
91 fprintf(stderr, name_too_long, applet_name);
96 fill_baseDest_buf(char *_buf, size_t * _buflen) {
97 const char *srcBasename;
98 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
99 srcBasename = baseSrcName;
100 if (_buf[*_buflen - 1] != '/') {
101 if (++(*_buflen) > BUFSIZ)
102 name_too_long__exit();
106 if (*_buflen + strlen(srcBasename) > BUFSIZ)
107 name_too_long__exit();
108 strcat(_buf, srcBasename);
114 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
116 char destName[BUFSIZ + 1];
118 const char *srcBasename;
121 strcpy(destName, baseDestName);
122 destLen = strlen(destName);
124 if (srcDirFlag == TRUE) {
125 if (recursiveFlag == FALSE) {
126 fprintf(stderr, omitting_directory, applet_name, baseSrcName);
129 srcBasename = (strstr(fileName, baseSrcName)
130 + strlen(baseSrcName));
132 if (destLen + strlen(srcBasename) > BUFSIZ) {
133 fprintf(stderr, name_too_long, applet_name);
136 strcat(destName, srcBasename);
138 else if (destDirFlag == TRUE) {
139 fill_baseDest_buf(&destName[0], &destLen);
142 srcBasename = baseSrcName;
144 if (mv_Action_first_time && (dz_i == is_mv)) {
145 mv_Action_first_time = errno = 0;
146 if (rename(fileName, destName) < 0 && errno != EXDEV) {
147 fprintf(stderr, "%s: rename(%s, %s): %s\n",
148 applet_name, fileName, destName, strerror(errno));
149 goto do_copyFile; /* Try anyway... */
151 else if (errno == EXDEV)
154 longjmp(catch, 1); /* succeeded with rename() */
157 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
158 if (is_in_ino_dev_hashtable(statbuf, &name)) {
159 if (link(name, destName) < 0) {
160 fprintf(stderr, "%s: link(%s, %s): %s\n",
161 applet_name, name, destName, strerror(errno));
167 add_to_ino_dev_hashtable(statbuf, destName);
170 return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
174 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
178 if (S_ISDIR(statbuf->st_mode)) {
179 if (rmdir(fileName) < 0) {
180 fprintf(stderr, "%s: rmdir(%s): %s\n", applet_name, fileName, strerror(errno));
183 } else if (unlink(fileName) < 0) {
184 fprintf(stderr, "%s: unlink(%s): %s\n", applet_name, fileName, strerror(errno));
190 extern int cp_mv_main(int argc, char **argv)
192 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
197 usage(cp_mv_usage[dz_i]);
202 recursiveFlag = preserveFlag = forceFlag = FALSE;
204 while (*argv && **argv == '-') {
210 recursiveFlag = TRUE;
219 recursiveFlag = TRUE;
225 usage(cp_mv_usage[is_cp]);
232 usage(cp_mv_usage[dz_i]);
234 } else { /* (dz_i == is_mv) */
235 recursiveFlag = preserveFlag = TRUE;
240 if (strlen(argv[argc - 1]) > BUFSIZ) {
241 fprintf(stderr, name_too_long, "cp");
244 strcpy(baseDestName, argv[argc - 1]);
245 baseDestLen = strlen(baseDestName);
246 if (baseDestLen == 0)
249 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
250 if ((argc > 3) && destDirFlag == FALSE) {
251 fprintf(stderr, not_a_directory, "cp", baseDestName);
257 volatile int flags_memo;
260 baseSrcName = *(argv++);
262 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
263 name_too_long__exit();
265 if (srcLen == 0) continue; /* "" */
267 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
269 if ((flags_memo = (recursiveFlag == TRUE &&
270 srcDirFlag == TRUE && destDirFlag == TRUE))) {
276 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
277 fprintf(stderr, "%s: getcwd(): %s\n", applet_name, strerror(errno));
280 if (chdir(baseDestName) < 0) {
281 fprintf(stderr, "%s: chdir(%s): %s\n", applet_name, baseSrcName, strerror(errno));
284 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
285 fprintf(stderr, "%s: getcwd(): %s\n", applet_name, strerror(errno));
288 while (!state && *d != '\0') {
289 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
290 fprintf(stderr, "%s: stat(%s) :%s\n", applet_name, d, strerror(errno));
294 if ((sb.st_ino == srcStatBuf.st_ino) &&
295 (sb.st_dev == srcStatBuf.st_dev)) {
297 "%s: Cannot %s `%s' "
298 "into a subdirectory of itself, `%s/%s'\n",
299 applet_name, applet_name, baseSrcName, baseDestName, baseSrcName);
303 if ((p = strrchr(d, '/')) != NULL) {
307 if (chdir(pushd) < 0) {
308 fprintf(stderr, "%s: chdir(%s): %s\n", applet_name, pushd, strerror(errno));
318 fill_baseDest_buf(baseDestName, &baseDestLen);
320 status = setjmp(catch);
322 mv_Action_first_time = 1;
323 if (recursiveAction(baseSrcName,
324 recursiveFlag, followLinks, FALSE,
325 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
327 recursiveAction(baseSrcName,
328 recursiveFlag, followLinks, TRUE,
329 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
332 *(baseDestName + baseDestLen) = '\0';
341 c-file-style: "linux"