1e10f286839035b0ced254c0df02895d47b7b802
[oweals/busybox.git] / coreutils / cp.c
1 /*
2  * Mini cp implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <time.h>
27 #include <utime.h>
28 #include <dirent.h>
29
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"
33     "\n"
34     "\t-a\tsame as -dpR\n"
35     "\t-d\tpreserve links\n"
36     "\t-p\tpreserve file attributes if possable\n"
37     "\t-R\tcopy directories recursively\n";
38
39
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 destExistsFlag = FALSE;
47 static int srcDirFlag = FALSE;
48
49 static int fileAction(const char *fileName, struct stat* statbuf)
50 {
51     char newdestName[NAME_MAX];
52
53     strcpy(newdestName, destName);
54     if ( srcDirFlag == TRUE ) {
55         if (recursiveFlag!=TRUE ) {
56             fprintf(stderr, "cp: %s: omitting directory\n", srcName);
57             return( TRUE);
58         }
59         strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
60     } 
61     
62     if (destDirFlag==TRUE && srcDirFlag == FALSE) {
63         if (newdestName[strlen(newdestName)-1] != '/' ) {
64             strcat(newdestName, "/");
65         }
66         strcat(newdestName, srcName);
67     }
68     
69     return (copyFile(fileName, newdestName, preserveFlag, followLinks));
70 }
71
72 extern int cp_main(int argc, char **argv)
73 {
74     struct stat statBuf;
75
76     if (argc < 3) {
77         usage (cp_usage);
78     }
79     argc--;
80     argv++;
81
82     /* Parse any options */
83     while (**argv == '-') {
84         while (*++(*argv))
85             switch (**argv) {
86             case 'a':
87                 followLinks = TRUE;
88                 preserveFlag = TRUE;
89                 recursiveFlag = TRUE;
90                 break;
91             case 'd':
92                 followLinks = TRUE;
93                 break;
94             case 'p':
95                 preserveFlag = TRUE;
96                 break;
97             case 'R':
98                 recursiveFlag = TRUE;
99                 break;
100             default:
101                 usage (cp_usage);
102             }
103         argc--;
104         argv++;
105     }
106
107
108     destName = argv[argc - 1];
109     if (stat(destName, &statBuf) >= 0) {
110         destExistsFlag = TRUE;
111         if (S_ISDIR(statBuf.st_mode))
112             destDirFlag = TRUE;
113     }
114
115     if ((argc > 3) && destDirFlag==FALSE) {
116         fprintf(stderr, "%s: not a directory\n", destName);
117         exit (FALSE);
118     }
119
120     while (argc-- > 1) {
121         srcName = *(argv++);
122         srcDirFlag = isDirectory(srcName);
123         if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
124                                fileAction, fileAction) == FALSE) {
125             exit( FALSE);
126         }
127     }
128     exit( TRUE);
129 }