2 * Mini cp implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 static const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
31 " or: cp [OPTION]... SOURCE... DIRECTORY\n\n"
32 "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
34 "\t-a\tsame as -dpR\n"
35 "\t-d\tpreserve links\n"
36 "\t-p\tpreserve file attributes if possible\n"
37 "\t-R\tcopy directories recursively\n";
40 static int recursiveFlag = FALSE;
41 static int followLinks = FALSE;
42 static int preserveFlag = FALSE;
43 static const char *srcName;
44 static const char *destName;
45 static int destDirFlag = FALSE;
46 static int srcDirFlag = FALSE;
48 static int fileAction(const char *fileName, struct stat* statbuf)
50 char newdestName[NAME_MAX];
51 char* newsrcName = NULL;
53 strcpy(newdestName, destName);
54 if ( srcDirFlag == TRUE ) {
55 if (recursiveFlag!=TRUE ) {
56 fprintf(stderr, "cp: %s: omitting directory\n", srcName);
59 strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
62 if (destDirFlag==TRUE && srcDirFlag == FALSE) {
63 if (newdestName[strlen(newdestName)-1] != '/' ) {
64 strcat(newdestName, "/");
66 newsrcName = strrchr(srcName, '/');
67 if (newsrcName && *newsrcName != '\0')
68 strcat(newdestName, newsrcName);
70 strcat(newdestName, srcName);
73 return (copyFile(fileName, newdestName, preserveFlag, followLinks));
76 extern int cp_main(int argc, char **argv)
84 /* Parse any options */
85 while (**argv == '-') {
100 recursiveFlag = TRUE;
110 destName = argv[argc - 1];
111 destDirFlag = isDirectory(destName);
113 if ((argc > 3) && destDirFlag==FALSE) {
114 fprintf(stderr, "%s: not a directory\n", destName);
120 srcDirFlag = isDirectory(srcName);
121 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
122 fileAction, fileAction) == FALSE) {