Implemented "ping -s", fixed error messages and argument parsing
[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 *dz;                  /* dollar zero, .bss */
49 static const char *cp_mv_usage[] =      /* .rodata */
50 {
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"
55                 "\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"
61 #endif
62                 ,
63         "mv SOURCE DEST\n"
64                 "   or: mv SOURCE... DIRECTORY\n"
65 #ifndef BB_FEATURE_TRIVIAL_HELP
66                 "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
67 #endif
68 };
69
70 static int recursiveFlag;
71 static int followLinks;
72 static int preserveFlag;
73 static int forceFlag;
74
75 static const char *baseSrcName;
76 static int                 srcDirFlag;
77 static struct stat srcStatBuf;
78
79 static char                baseDestName[BUFSIZ + 1];
80 static size_t      baseDestLen;
81 static int                 destDirFlag;
82 static struct stat destStatBuf;
83
84 static jmp_buf      catch;
85 static volatile int mv_Action_first_time;
86
87 static void name_too_long__exit (void) __attribute__((noreturn));
88
89 static
90 void name_too_long__exit (void)
91 {
92         fprintf(stderr, name_too_long, dz);
93         exit FALSE;
94 }
95
96 static void
97 fill_baseDest_buf(char *_buf, size_t * _buflen) {
98         const char *srcBasename;
99         if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
100                 srcBasename = baseSrcName;
101                 if (_buf[*_buflen - 1] != '/') {
102                         if (++(*_buflen) > BUFSIZ)
103                                 name_too_long__exit();
104                         strcat(_buf, "/");
105                 }
106         }
107         if (*_buflen + strlen(srcBasename) > BUFSIZ)
108                 name_too_long__exit();
109         strcat(_buf, srcBasename);
110         return;
111         
112 }
113
114 static int
115 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
116 {
117         char            destName[BUFSIZ + 1];
118         size_t          destLen;
119         const char *srcBasename;
120         char       *name;
121
122         strcpy(destName, baseDestName);
123         destLen = strlen(destName);
124
125         if (srcDirFlag == TRUE) {
126                 if (recursiveFlag == FALSE) {
127                         fprintf(stderr, omitting_directory, dz, baseSrcName);
128                         return TRUE;
129                 }
130                 srcBasename = (strstr(fileName, baseSrcName)
131                                            + strlen(baseSrcName));
132
133                 if (destLen + strlen(srcBasename) > BUFSIZ) {
134                         fprintf(stderr, name_too_long, dz);
135                         return FALSE;
136                 }
137                 strcat(destName, srcBasename);
138         }
139         else if (destDirFlag == TRUE) {
140                 fill_baseDest_buf(&destName[0], &destLen);
141         }
142         else {
143                 srcBasename = baseSrcName;
144         }
145         if (mv_Action_first_time && (dz_i == is_mv)) {
146                 mv_Action_first_time = errno = 0;
147                 if (rename(fileName, destName) < 0 && errno != EXDEV) {
148                         fprintf(stderr, "%s: rename(%s, %s): %s\n",
149                                         dz, fileName, destName, strerror(errno));
150                         goto do_copyFile;       /* Try anyway... */
151                 }
152                 else if (errno == EXDEV)
153                         goto do_copyFile;
154                 else
155                         longjmp(catch, 1);      /* succeeded with rename() */
156         }
157  do_copyFile:
158         if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
159                 if (is_in_ino_dev_hashtable(statbuf, &name)) {
160                         if (link(name, destName) < 0) {
161                                 fprintf(stderr, "%s: link(%s, %s): %s\n",
162                                                 dz, name, destName, strerror(errno));
163                                 return FALSE;
164                         }
165                         return TRUE;
166                 }
167                 else {
168                         add_to_ino_dev_hashtable(statbuf, destName);
169                 }
170         }
171         return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
172 }
173
174 static int
175 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
176 {
177         int status = TRUE;
178
179         if (S_ISDIR(statbuf->st_mode)) {
180                 if (rmdir(fileName) < 0) {
181                         fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
182                         status = FALSE;
183                 }
184         } else if (unlink(fileName) < 0) {
185                 fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
186                 status = FALSE;
187         }
188         return status;
189 }
190
191 extern int cp_mv_main(int argc, char **argv)
192 {
193         dz = *argv;                                     /* already basename'd by busybox.c:main */
194         if (*dz == 'c' && *(dz + 1) == 'p')
195                 dz_i = is_cp;
196         else
197                 dz_i = is_mv;
198         if (argc < 3)
199                 usage(cp_mv_usage[dz_i]);
200         argc--;
201         argv++;
202
203         if (dz_i == is_cp) {
204                 recursiveFlag = preserveFlag = forceFlag = FALSE;
205                 followLinks = TRUE;
206                 while (--argc >= 0 && *argv && **argv) {
207                         while (**argv == '-') {
208                                 while (*++(*argv)) {
209                                         switch (**argv) {
210                                         case 'a':
211                                                 followLinks = FALSE;
212                                                 preserveFlag = TRUE;
213                                                 recursiveFlag = TRUE;
214                                                 break;
215                                         case 'd':
216                                                 followLinks = FALSE;
217                                                 break;
218                                         case 'p':
219                                                 preserveFlag = TRUE;
220                                                 break;
221                                         case 'R':
222                                                 recursiveFlag = TRUE;
223                                                 break;
224                                         case 'f':
225                                                 forceFlag = TRUE;
226                                                 break;
227                                         default:
228                                                 usage(cp_mv_usage[is_cp]);
229                                         }
230                                 }
231                         }
232                         argv++;
233                 }
234                 if (argc < 1) {
235                         fatalError("cp: missing file argument\n");
236                 }
237         } else {                                        /* (dz_i == is_mv) */
238                 recursiveFlag = preserveFlag = TRUE;
239                 followLinks = FALSE;
240         }
241         
242
243         if (strlen(argv[argc - 1]) > BUFSIZ) {
244                 fprintf(stderr, name_too_long, "cp");
245                 goto exit_false;
246         }
247         strcpy(baseDestName, argv[argc - 1]);
248         baseDestLen = strlen(baseDestName);
249         if (baseDestLen == 0)
250                 goto exit_false;
251
252         destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
253         if ((argc > 3) && destDirFlag == FALSE) {
254                 fprintf(stderr, not_a_directory, "cp", baseDestName);
255                 goto exit_false;
256         }
257
258         while (argc-- > 1) {
259                 size_t srcLen;
260                 volatile int flags_memo;
261                 int        status;
262
263                 baseSrcName = *(argv++);
264
265                 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
266                         name_too_long__exit();
267
268                 if (srcLen == 0) continue; /* "" */
269
270                 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
271
272                 if ((flags_memo = (recursiveFlag == TRUE &&
273                                                    srcDirFlag == TRUE && destDirFlag == TRUE))) {
274
275                         struct stat sb;
276                         int                     state = 0;
277                         char            *pushd, *d, *p;
278
279                         if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
280                                 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
281                                 continue;
282                         }
283                         if (chdir(baseDestName) < 0) {
284                                 fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
285                                 continue;
286                         }
287                         if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
288                                 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
289                                 continue;
290                         }
291                         while (!state && *d != '\0') {
292                                 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
293                                         fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
294                                         state = -1;
295                                         continue;
296                                 }
297                                 if ((sb.st_ino == srcStatBuf.st_ino) &&
298                                         (sb.st_dev == srcStatBuf.st_dev)) {
299                                         fprintf(stderr,
300                                                         "%s: Cannot %s `%s' "
301                                                         "into a subdirectory of itself, `%s/%s'\n",
302                                                         dz, dz, baseSrcName, baseDestName, baseSrcName);
303                                         state = -1;
304                                         continue;
305                                 }
306                                 if ((p = strrchr(d, '/')) != NULL) {
307                                         *p = '\0';
308                                 }
309                         }
310                         if (chdir(pushd) < 0) {
311                                 fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
312                                 free(pushd);
313                                 free(d);
314                                 continue;
315                         }
316                         free(pushd);
317                         free(d);
318                         if (state < 0)
319                                 continue;
320                         else
321                                 fill_baseDest_buf(baseDestName, &baseDestLen);
322                 }
323                 status = setjmp(catch);
324                 if (status == 0) {
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;
329                         if (dz_i == is_mv &&
330                                 recursiveAction(baseSrcName,
331                                                                 recursiveFlag, followLinks, TRUE,
332                                                                 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
333                 }               
334                 if (flags_memo)
335                         *(baseDestName + baseDestLen) = '\0';
336         }
337 // exit_true:
338         exit TRUE;
339  exit_false:
340         exit FALSE;
341 }
342
343 /*
344 Local Variables:
345 c-file-style: "linux"
346 c-basic-offset: 4
347 tab-width: 4
348 End:
349 */