1 /* vi: set sw=4 ts=4: */
3 * Mini `cp' and `mv' implementation for BusyBox.
6 * Copyright (C) 1999,2000,2001 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 const int is_cp = 0;
48 static const int is_mv = 1;
49 static int dz_i; /* index into cp_mv_usage */
51 static const char *cp_mv_usage[] = /* .rodata */
57 static int recursiveFlag;
58 static int followLinks;
59 static int preserveFlag;
62 static const char *baseSrcName;
63 static int srcDirFlag;
64 static struct stat srcStatBuf;
66 static char *pBaseDestName;
67 static size_t baseDestLen;
68 static int destDirFlag;
69 static struct stat destStatBuf;
72 static volatile int mv_Action_first_time;
74 static void name_too_long__exit (void) __attribute__((noreturn));
77 void name_too_long__exit (void)
79 error_msg_and_die(name_too_long);
83 fill_baseDest_buf(char *_buf, size_t * _buflen) {
84 const char *srcBasename;
85 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
86 srcBasename = baseSrcName;
87 if (_buf[*_buflen - 1] != '/') {
88 if (++(*_buflen) > BUFSIZ)
89 name_too_long__exit();
93 if (*_buflen + strlen(srcBasename) > BUFSIZ)
94 name_too_long__exit();
95 strcat(_buf, srcBasename);
101 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
103 char destName[BUFSIZ + 1];
105 const char *srcBasename;
108 strcpy(destName, pBaseDestName);
109 destLen = strlen(destName);
111 if (srcDirFlag == TRUE) {
112 if (recursiveFlag == FALSE) {
113 error_msg(omitting_directory, baseSrcName);
116 srcBasename = (strstr(fileName, baseSrcName)
117 + strlen(baseSrcName));
119 if (destLen + strlen(srcBasename) > BUFSIZ) {
120 error_msg(name_too_long);
123 strcat(destName, srcBasename);
125 else if (destDirFlag == TRUE) {
126 fill_baseDest_buf(&destName[0], &destLen);
129 srcBasename = baseSrcName;
131 if (mv_Action_first_time && (dz_i == is_mv)) {
132 mv_Action_first_time = errno = 0;
133 if (rename(fileName, destName) < 0 && errno != EXDEV) {
134 perror_msg("rename(%s, %s)", 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 perror_msg("link(%s, %s)", name, destName);
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 perror_msg("rmdir(%s)", fileName);
168 } else if (unlink(fileName) < 0) {
169 perror_msg("unlink(%s)", fileName);
175 extern int cp_mv_main(int argc, char **argv)
179 RESERVE_BB_BUFFER(baseDestName,BUFSIZ + 1);
180 pBaseDestName = baseDestName; /* available globally */
182 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
187 usage(cp_mv_usage[dz_i]);
190 recursiveFlag = preserveFlag = forceFlag = FALSE;
192 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
197 recursiveFlag = TRUE;
206 recursiveFlag = TRUE;
212 usage(cp_mv_usage[is_cp]);
215 if ((argc - optind) < 2) {
216 usage(cp_mv_usage[dz_i]);
218 } else { /* (dz_i == is_mv) */
219 /* Initialize optind to 1, since in libc5 optind
220 * is not initialized until getopt() is called
221 * (or until sneaky programmers force it...). */
223 recursiveFlag = preserveFlag = TRUE;
228 if (strlen(argv[argc - 1]) > BUFSIZ) {
229 error_msg(name_too_long);
232 strcpy(baseDestName, argv[argc - 1]);
233 baseDestLen = strlen(baseDestName);
234 if (baseDestLen == 0)
237 destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
238 if (argc - optind > 2 && destDirFlag == FALSE) {
239 error_msg(not_a_directory, baseDestName);
243 for (i = optind; i < (argc-1); i++) {
245 volatile int flags_memo;
250 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
251 name_too_long__exit();
253 if (srcLen == 0) continue; /* "" */
255 srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
257 if ((flags_memo = (recursiveFlag == TRUE &&
258 srcDirFlag == TRUE && destDirFlag == TRUE))) {
264 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
265 perror_msg("getcwd()");
268 if (chdir(baseDestName) < 0) {
269 perror_msg("chdir(%s)", baseSrcName);
272 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
273 perror_msg("getcwd()");
276 while (!state && *d != '\0') {
277 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
278 perror_msg("stat(%s)", d);
282 if ((sb.st_ino == srcStatBuf.st_ino) &&
283 (sb.st_dev == srcStatBuf.st_dev)) {
284 error_msg("Cannot %s `%s' into a subdirectory of itself, "
285 "`%s/%s'", applet_name, baseSrcName,
286 baseDestName, baseSrcName);
290 if ((p = strrchr(d, '/')) != NULL) {
294 if (chdir(pushd) < 0) {
295 perror_msg("chdir(%s)", pushd);
305 fill_baseDest_buf(baseDestName, &baseDestLen);
307 status = setjmp(catch);
309 mv_Action_first_time = 1;
310 if (recursive_action(baseSrcName,
311 recursiveFlag, followLinks, FALSE,
312 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
314 recursive_action(baseSrcName,
315 recursiveFlag, followLinks, TRUE,
316 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
319 *(baseDestName + baseDestLen) = '\0';
328 c-file-style: "linux"