More stuff
[oweals/busybox.git] / 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"
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 const char *skipName;
46 static int dirFlag = FALSE;
47
48
49 static int fileAction(const char *fileName, struct stat* statbuf)
50 {
51     char newdestName[NAME_MAX];
52     strcpy(newdestName, destName);
53     if (dirFlag==TRUE) {
54         strcat(newdestName, "/");
55         if ( skipName != NULL)
56             strcat(newdestName, strstr(fileName, skipName));
57     }
58     return (copyFile(fileName, newdestName, preserveFlag, followLinks));
59 }
60
61 extern int cp_main(int argc, char **argv)
62 {
63
64     if (argc < 3) {
65         usage (cp_usage);
66     }
67     argc--;
68     argv++;
69
70     /* Parse any options */
71     while (**argv == '-') {
72         while (*++(*argv))
73             switch (**argv) {
74             case 'a':
75                 followLinks = TRUE;
76                 preserveFlag = TRUE;
77                 recursiveFlag = TRUE;
78                 break;
79             case 'd':
80                 followLinks = TRUE;
81                 break;
82             case 'p':
83                 preserveFlag = TRUE;
84                 break;
85             case 'R':
86                 recursiveFlag = TRUE;
87                 break;
88             default:
89                 usage (cp_usage);
90             }
91         argc--;
92         argv++;
93     }
94
95
96     destName = argv[argc - 1];
97     dirFlag = isDirectory(destName);
98
99     if ((argc > 3) && dirFlag==FALSE) {
100         fprintf(stderr, "%s: not a directory\n", destName);
101         exit (FALSE);
102     }
103
104     while (argc-- > 1) {
105         srcName = *(argv++);
106         skipName = strrchr(srcName, '/');
107         if (skipName) 
108             skipName++;
109         if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
110                                fileAction, fileAction) == FALSE) {
111             exit( FALSE);
112         }
113     }
114     exit( TRUE);
115 }