Use global applet_name instead of local versions.
[oweals/busybox.git] / cp_mv.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini `cp' and `mv' implementation for BusyBox.
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL)
10  * Extensively modified and rewritten by  Karl M. Hegbloom <karlheg@debian.org>
11  *
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.
16  *
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.
21  *
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
25  *
26  */
27
28 #include "internal.h"
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
33 #include "messages.c"
34
35 #include <stdio.h>
36 #include <time.h>
37 #include <utime.h>
38 #include <dirent.h>
39 #include <sys/param.h>
40 #include <setjmp.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44
45 #define is_cp 0
46 #define is_mv 1
47 static int         dz_i;                /* index into cp_mv_usage */
48 static const char *cp_mv_usage[] =      /* .rodata */
49 {
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"
54                 "\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"
60 #endif
61                 ,
62         "mv SOURCE DEST\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"
66 #endif
67 };
68
69 static int recursiveFlag;
70 static int followLinks;
71 static int preserveFlag;
72 static int forceFlag;
73
74 static const char *baseSrcName;
75 static int                 srcDirFlag;
76 static struct stat srcStatBuf;
77
78 static char                baseDestName[BUFSIZ + 1];
79 static size_t      baseDestLen;
80 static int                 destDirFlag;
81 static struct stat destStatBuf;
82
83 static jmp_buf      catch;
84 static volatile int mv_Action_first_time;
85
86 static void name_too_long__exit (void) __attribute__((noreturn));
87
88 static
89 void name_too_long__exit (void)
90 {
91         fprintf(stderr, name_too_long, applet_name);
92         exit(FALSE);
93 }
94
95 static void
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();
103                         strcat(_buf, "/");
104                 }
105         }
106         if (*_buflen + strlen(srcBasename) > BUFSIZ)
107                 name_too_long__exit();
108         strcat(_buf, srcBasename);
109         return;
110         
111 }
112
113 static int
114 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
115 {
116         char            destName[BUFSIZ + 1];
117         size_t          destLen;
118         const char *srcBasename;
119         char       *name;
120
121         strcpy(destName, baseDestName);
122         destLen = strlen(destName);
123
124         if (srcDirFlag == TRUE) {
125                 if (recursiveFlag == FALSE) {
126                         fprintf(stderr, omitting_directory, applet_name, baseSrcName);
127                         return TRUE;
128                 }
129                 srcBasename = (strstr(fileName, baseSrcName)
130                                            + strlen(baseSrcName));
131
132                 if (destLen + strlen(srcBasename) > BUFSIZ) {
133                         fprintf(stderr, name_too_long, applet_name);
134                         return FALSE;
135                 }
136                 strcat(destName, srcBasename);
137         }
138         else if (destDirFlag == TRUE) {
139                 fill_baseDest_buf(&destName[0], &destLen);
140         }
141         else {
142                 srcBasename = baseSrcName;
143         }
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... */
150                 }
151                 else if (errno == EXDEV)
152                         goto do_copyFile;
153                 else
154                         longjmp(catch, 1);      /* succeeded with rename() */
155         }
156  do_copyFile:
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));
162                                 return FALSE;
163                         }
164                         return TRUE;
165                 }
166                 else {
167                         add_to_ino_dev_hashtable(statbuf, destName);
168                 }
169         }
170         return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
171 }
172
173 static int
174 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
175 {
176         int status = TRUE;
177
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));
181                         status = FALSE;
182                 }
183         } else if (unlink(fileName) < 0) {
184                 fprintf(stderr, "%s: unlink(%s): %s\n", applet_name, fileName, strerror(errno));
185                 status = FALSE;
186         }
187         return status;
188 }
189
190 extern int cp_mv_main(int argc, char **argv)
191 {
192         if (*applet_name == 'c' && *(applet_name + 1) == 'p')
193                 dz_i = is_cp;
194         else
195                 dz_i = is_mv;
196         if (argc < 3)
197                 usage(cp_mv_usage[dz_i]);
198         argc--;
199         argv++;
200
201         if (dz_i == is_cp) {
202                 recursiveFlag = preserveFlag = forceFlag = FALSE;
203                 followLinks = TRUE;
204                 while (*argv && **argv == '-') {
205                         while (*++(*argv)) {
206                                 switch (**argv) {
207                                 case 'a':
208                                         followLinks = FALSE;
209                                         preserveFlag = TRUE;
210                                         recursiveFlag = TRUE;
211                                         break;
212                                 case 'd':
213                                         followLinks = FALSE;
214                                         break;
215                                 case 'p':
216                                         preserveFlag = TRUE;
217                                         break;
218                                 case 'R':
219                                         recursiveFlag = TRUE;
220                                         break;
221                                 case 'f':
222                                         forceFlag = TRUE;
223                                         break;
224                                 default:
225                                         usage(cp_mv_usage[is_cp]);
226                                 }
227                         }
228                         argc--;
229                         argv++;
230                 }
231                 if (argc < 2) {
232                         usage(cp_mv_usage[dz_i]);
233                 }
234         } else {                                        /* (dz_i == is_mv) */
235                 recursiveFlag = preserveFlag = TRUE;
236                 followLinks = FALSE;
237         }
238         
239
240         if (strlen(argv[argc - 1]) > BUFSIZ) {
241                 fprintf(stderr, name_too_long, "cp");
242                 goto exit_false;
243         }
244         strcpy(baseDestName, argv[argc - 1]);
245         baseDestLen = strlen(baseDestName);
246         if (baseDestLen == 0)
247                 goto exit_false;
248
249         destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
250         if ((argc > 3) && destDirFlag == FALSE) {
251                 fprintf(stderr, not_a_directory, "cp", baseDestName);
252                 goto exit_false;
253         }
254
255         while (argc-- > 1) {
256                 size_t srcLen;
257                 volatile int flags_memo;
258                 int        status;
259
260                 baseSrcName = *(argv++);
261
262                 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
263                         name_too_long__exit();
264
265                 if (srcLen == 0) continue; /* "" */
266
267                 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
268
269                 if ((flags_memo = (recursiveFlag == TRUE &&
270                                                    srcDirFlag == TRUE && destDirFlag == TRUE))) {
271
272                         struct stat sb;
273                         int                     state = 0;
274                         char            *pushd, *d, *p;
275
276                         if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
277                                 fprintf(stderr, "%s: getcwd(): %s\n", applet_name, strerror(errno));
278                                 continue;
279                         }
280                         if (chdir(baseDestName) < 0) {
281                                 fprintf(stderr, "%s: chdir(%s): %s\n", applet_name, baseSrcName, strerror(errno));
282                                 continue;
283                         }
284                         if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
285                                 fprintf(stderr, "%s: getcwd(): %s\n", applet_name, strerror(errno));
286                                 continue;
287                         }
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));
291                                         state = -1;
292                                         continue;
293                                 }
294                                 if ((sb.st_ino == srcStatBuf.st_ino) &&
295                                         (sb.st_dev == srcStatBuf.st_dev)) {
296                                         fprintf(stderr,
297                                                         "%s: Cannot %s `%s' "
298                                                         "into a subdirectory of itself, `%s/%s'\n",
299                                                         applet_name, applet_name, baseSrcName, baseDestName, baseSrcName);
300                                         state = -1;
301                                         continue;
302                                 }
303                                 if ((p = strrchr(d, '/')) != NULL) {
304                                         *p = '\0';
305                                 }
306                         }
307                         if (chdir(pushd) < 0) {
308                                 fprintf(stderr, "%s: chdir(%s): %s\n", applet_name, pushd, strerror(errno));
309                                 free(pushd);
310                                 free(d);
311                                 continue;
312                         }
313                         free(pushd);
314                         free(d);
315                         if (state < 0)
316                                 continue;
317                         else
318                                 fill_baseDest_buf(baseDestName, &baseDestLen);
319                 }
320                 status = setjmp(catch);
321                 if (status == 0) {
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;
326                         if (dz_i == is_mv &&
327                                 recursiveAction(baseSrcName,
328                                                                 recursiveFlag, followLinks, TRUE,
329                                                                 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
330                 }               
331                 if (flags_memo)
332                         *(baseDestName + baseDestLen) = '\0';
333         }
334         return( TRUE);
335  exit_false:
336         return( FALSE);
337 }
338
339 /*
340 Local Variables:
341 c-file-style: "linux"
342 c-basic-offset: 4
343 tab-width: 4
344 End:
345 */