blob: 9dac2afbc916c58bc23477bdb205ed771e83fd1a [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 Andersenf811e071999-10-09 00:25:00 +000031 " 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";
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;
Eric Andersen3c163821999-10-14 22:16:57 +000045static const char *skipName;
Eric Andersen9b587181999-10-17 05:43:39 +000046static int dirFlag = FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +000047
48
Eric Andersen9b587181999-10-17 05:43:39 +000049static int fileAction(const char *fileName, struct stat* statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +000050{
Eric Andersenf811e071999-10-09 00:25:00 +000051 char newdestName[NAME_MAX];
52 strcpy(newdestName, destName);
Eric Andersenbed30e91999-10-18 19:02:32 +000053 if (dirFlag==TRUE) {
Eric Andersen9b587181999-10-17 05:43:39 +000054 strcat(newdestName, "/");
55 if ( skipName != NULL)
56 strcat(newdestName, strstr(fileName, skipName));
57 }
Eric Andersenf811e071999-10-09 00:25:00 +000058 return (copyFile(fileName, newdestName, preserveFlag, followLinks));
59}
Eric Andersencc8ed391999-10-05 16:24:54 +000060
Eric Andersenf811e071999-10-09 00:25:00 +000061extern int cp_main(int argc, char **argv)
62{
63
Eric Andersenf811e071999-10-09 00:25:00 +000064 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000065 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000066 }
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:
Eric Andersenb0e9a701999-10-18 22:28:26 +000089 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000090 }
91 argc--;
92 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000093 }
94
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Eric Andersenf811e071999-10-09 00:25:00 +000096 destName = argv[argc - 1];
Eric Andersenf811e071999-10-09 00:25:00 +000097 dirFlag = isDirectory(destName);
98
Eric Andersen9b587181999-10-17 05:43:39 +000099 if ((argc > 3) && dirFlag==FALSE) {
Eric Andersenf811e071999-10-09 00:25:00 +0000100 fprintf(stderr, "%s: not a directory\n", destName);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000101 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000102 }
Eric Andersenf811e071999-10-09 00:25:00 +0000103
Eric Andersen3c163821999-10-14 22:16:57 +0000104 while (argc-- > 1) {
Eric Andersenf811e071999-10-09 00:25:00 +0000105 srcName = *(argv++);
Eric Andersen3c163821999-10-14 22:16:57 +0000106 skipName = strrchr(srcName, '/');
Eric Andersenbed30e91999-10-18 19:02:32 +0000107 if (skipName)
108 skipName++;
109 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
110 fileAction, fileAction) == FALSE) {
Eric Andersen3c163821999-10-14 22:16:57 +0000111 exit( FALSE);
Eric Andersenbed30e91999-10-18 19:02:32 +0000112 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000113 }
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000114 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000115}