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 const char cp_usage[] =
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-f\tforce (implied; ignored) - always set\n"
60 "\t-R\tCopies directories recursively\n"
64 const char mv_usage[] =
66 " or: mv SOURCE... DIRECTORY\n"
67 #ifndef BB_FEATURE_TRIVIAL_HELP
68 "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
72 static const char *cp_mv_usage[] = /* .rodata */
78 static int recursiveFlag;
79 static int followLinks;
80 static int preserveFlag;
83 static const char *baseSrcName;
84 static int srcDirFlag;
85 static struct stat srcStatBuf;
87 static char baseDestName[BUFSIZ + 1];
88 static size_t baseDestLen;
89 static int destDirFlag;
90 static struct stat destStatBuf;
93 static volatile int mv_Action_first_time;
95 static void name_too_long__exit (void) __attribute__((noreturn));
98 void name_too_long__exit (void)
100 fatalError(name_too_long);
104 fill_baseDest_buf(char *_buf, size_t * _buflen) {
105 const char *srcBasename;
106 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
107 srcBasename = baseSrcName;
108 if (_buf[*_buflen - 1] != '/') {
109 if (++(*_buflen) > BUFSIZ)
110 name_too_long__exit();
114 if (*_buflen + strlen(srcBasename) > BUFSIZ)
115 name_too_long__exit();
116 strcat(_buf, srcBasename);
122 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
124 char destName[BUFSIZ + 1];
126 const char *srcBasename;
129 strcpy(destName, baseDestName);
130 destLen = strlen(destName);
132 if (srcDirFlag == TRUE) {
133 if (recursiveFlag == FALSE) {
134 errorMsg(omitting_directory, baseSrcName);
137 srcBasename = (strstr(fileName, baseSrcName)
138 + strlen(baseSrcName));
140 if (destLen + strlen(srcBasename) > BUFSIZ) {
141 errorMsg(name_too_long);
144 strcat(destName, srcBasename);
146 else if (destDirFlag == TRUE) {
147 fill_baseDest_buf(&destName[0], &destLen);
150 srcBasename = baseSrcName;
152 if (mv_Action_first_time && (dz_i == is_mv)) {
153 mv_Action_first_time = errno = 0;
154 if (rename(fileName, destName) < 0 && errno != EXDEV) {
155 errorMsg("rename(%s, %s): %s\n", fileName, destName,
157 goto do_copyFile; /* Try anyway... */
159 else if (errno == EXDEV)
162 longjmp(catch, 1); /* succeeded with rename() */
165 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
166 if (is_in_ino_dev_hashtable(statbuf, &name)) {
167 if (link(name, destName) < 0) {
168 errorMsg("link(%s, %s): %s\n", name, destName, strerror(errno));
174 add_to_ino_dev_hashtable(statbuf, destName);
177 return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
181 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
185 if (S_ISDIR(statbuf->st_mode)) {
186 if (rmdir(fileName) < 0) {
187 errorMsg("rmdir(%s): %s\n", fileName, strerror(errno));
190 } else if (unlink(fileName) < 0) {
191 errorMsg("unlink(%s): %s\n", fileName, strerror(errno));
197 extern int cp_mv_main(int argc, char **argv)
202 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
207 usage(cp_mv_usage[dz_i]);
210 recursiveFlag = preserveFlag = forceFlag = FALSE;
212 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
217 recursiveFlag = TRUE;
226 recursiveFlag = TRUE;
232 usage(cp_mv_usage[is_cp]);
235 if ((argc - optind) < 2) {
236 usage(cp_mv_usage[dz_i]);
238 } else { /* (dz_i == is_mv) */
239 recursiveFlag = preserveFlag = TRUE;
244 if (strlen(argv[argc - 1]) > BUFSIZ) {
245 errorMsg(name_too_long);
248 strcpy(baseDestName, argv[argc - 1]);
249 baseDestLen = strlen(baseDestName);
250 if (baseDestLen == 0)
253 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
254 if ((argc > 3) && destDirFlag == FALSE) {
255 errorMsg(not_a_directory, baseDestName);
259 for (i = optind; i < (argc-1); i++) {
261 volatile int flags_memo;
266 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
267 name_too_long__exit();
269 if (srcLen == 0) continue; /* "" */
271 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
273 if ((flags_memo = (recursiveFlag == TRUE &&
274 srcDirFlag == TRUE && destDirFlag == TRUE))) {
280 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
281 errorMsg("getcwd(): %s\n", strerror(errno));
284 if (chdir(baseDestName) < 0) {
285 errorMsg("chdir(%s): %s\n", baseSrcName, strerror(errno));
288 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
289 errorMsg("getcwd(): %s\n", strerror(errno));
292 while (!state && *d != '\0') {
293 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
294 errorMsg("stat(%s): %s\n", d, strerror(errno));
298 if ((sb.st_ino == srcStatBuf.st_ino) &&
299 (sb.st_dev == srcStatBuf.st_dev)) {
300 errorMsg("Cannot %s `%s' into a subdirectory of itself, "
301 "`%s/%s'\n", applet_name, baseSrcName,
302 baseDestName, baseSrcName);
306 if ((p = strrchr(d, '/')) != NULL) {
310 if (chdir(pushd) < 0) {
311 errorMsg("chdir(%s): %s\n", pushd, strerror(errno));
321 fill_baseDest_buf(baseDestName, &baseDestLen);
323 status = setjmp(catch);
325 mv_Action_first_time = 1;
326 if (recursiveAction(baseSrcName,
327 recursiveFlag, followLinks, FALSE,
328 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
330 recursiveAction(baseSrcName,
331 recursiveFlag, followLinks, TRUE,
332 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
335 *(baseDestName + baseDestLen) = '\0';
344 c-file-style: "linux"