blob: e96012d500fea7916d4b84ae2ad6545ef521a26d [file] [log] [blame]
Eric Andersenf811e071999-10-09 00:25:00 +00001/*
2 * Mini cp implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf811e071999-10-09 00:25:00 +00007 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
Eric Andersenf811e071999-10-09 00:25:00 +000026#include <time.h>
27#include <utime.h>
28#include <dirent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Eric Andersene77ae3a1999-10-19 20:03:34 +000030static const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000031 " or: cp [OPTION]... SOURCE... DIRECTORY\n\n"
Eric Andersenf811e071999-10-09 00:25:00 +000032 "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"
Erik Andersen7dc16072000-01-04 01:10:25 +000036 "\t-p\tpreserve file attributes if possible\n"
Eric Andersenf811e071999-10-09 00:25:00 +000037 "\t-R\tcopy directories recursively\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Eric Andersenf811e071999-10-09 00:25:00 +000039
40static int recursiveFlag = FALSE;
41static int followLinks = FALSE;
42static int preserveFlag = FALSE;
43static const char *srcName;
44static const char *destName;
Erik Andersen2fe08c71999-12-29 02:10:35 +000045static int destDirFlag = FALSE;
Erik Andersen2fe08c71999-12-29 02:10:35 +000046static int srcDirFlag = FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +000047
Eric Andersen9b587181999-10-17 05:43:39 +000048static int fileAction(const char *fileName, struct stat* statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +000049{
Eric Andersenf811e071999-10-09 00:25:00 +000050 char newdestName[NAME_MAX];
Erik Andersen1dbc17f2000-01-08 21:16:29 +000051 char* newsrcName = NULL;
Erik Andersen2fe08c71999-12-29 02:10:35 +000052
Eric Andersenf811e071999-10-09 00:25:00 +000053 strcpy(newdestName, destName);
Erik Andersen2fe08c71999-12-29 02:10:35 +000054 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 }
Erik Andersen1dbc17f2000-01-08 21:16:29 +000066 newsrcName = strrchr(srcName, '/');
67 if (newsrcName && *newsrcName != '\0')
68 strcat(newdestName, newsrcName);
69 else
70 strcat(newdestName, srcName);
Eric Andersen9b587181999-10-17 05:43:39 +000071 }
Erik Andersen2fe08c71999-12-29 02:10:35 +000072
Eric Andersenf811e071999-10-09 00:25:00 +000073 return (copyFile(fileName, newdestName, preserveFlag, followLinks));
74}
Eric Andersencc8ed391999-10-05 16:24:54 +000075
Eric Andersenf811e071999-10-09 00:25:00 +000076extern int cp_main(int argc, char **argv)
77{
Eric Andersenf811e071999-10-09 00:25:00 +000078 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000079 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000080 }
81 argc--;
82 argv++;
83
84 /* Parse any options */
85 while (**argv == '-') {
86 while (*++(*argv))
87 switch (**argv) {
88 case 'a':
89 followLinks = TRUE;
90 preserveFlag = TRUE;
91 recursiveFlag = TRUE;
92 break;
93 case 'd':
94 followLinks = TRUE;
95 break;
96 case 'p':
97 preserveFlag = TRUE;
98 break;
99 case 'R':
100 recursiveFlag = TRUE;
101 break;
102 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000103 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +0000104 }
105 argc--;
106 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000107 }
108
Eric Andersencc8ed391999-10-05 16:24:54 +0000109
Eric Andersenf811e071999-10-09 00:25:00 +0000110 destName = argv[argc - 1];
Erik Andersenf664c001999-12-29 02:36:29 +0000111 destDirFlag = isDirectory(destName);
Eric Andersenf811e071999-10-09 00:25:00 +0000112
Erik Andersen2fe08c71999-12-29 02:10:35 +0000113 if ((argc > 3) && destDirFlag==FALSE) {
Eric Andersenf811e071999-10-09 00:25:00 +0000114 fprintf(stderr, "%s: not a directory\n", destName);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000115 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000116 }
Eric Andersenf811e071999-10-09 00:25:00 +0000117
Eric Andersen3c163821999-10-14 22:16:57 +0000118 while (argc-- > 1) {
Eric Andersenf811e071999-10-09 00:25:00 +0000119 srcName = *(argv++);
Erik Andersen2fe08c71999-12-29 02:10:35 +0000120 srcDirFlag = isDirectory(srcName);
Eric Andersenbed30e91999-10-18 19:02:32 +0000121 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
122 fileAction, fileAction) == FALSE) {
Eric Andersen3c163821999-10-14 22:16:57 +0000123 exit( FALSE);
Eric Andersenbed30e91999-10-18 19:02:32 +0000124 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000125 }
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000126 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000127}