blob: 34c12922dbbfbfe53d7fd99200e1b3b1b75e4725 [file] [log] [blame]
Eric Andersenf811e071999-10-09 00:25:00 +00001/*
2 * Mini cp implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Eric Andersencc8ed391999-10-05 16:24:54 +000022#include "internal.h"
23#include <stdio.h>
Eric Andersenf811e071999-10-09 00:25:00 +000024#include <time.h>
25#include <utime.h>
26#include <dirent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027
Eric Andersene77ae3a1999-10-19 20:03:34 +000028static const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
Eric Andersenf811e071999-10-09 00:25:00 +000029 " or: cp [OPTION]... SOURCE... DIRECTORY\n"
30 "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
31 "\n"
32 "\t-a\tsame as -dpR\n"
33 "\t-d\tpreserve links\n"
34 "\t-p\tpreserve file attributes if possable\n"
35 "\t-R\tcopy directories recursively\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersenf811e071999-10-09 00:25:00 +000037
38static int recursiveFlag = FALSE;
39static int followLinks = FALSE;
40static int preserveFlag = FALSE;
41static const char *srcName;
42static const char *destName;
Eric Andersen3c163821999-10-14 22:16:57 +000043static const char *skipName;
Eric Andersen9b587181999-10-17 05:43:39 +000044static int dirFlag = FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +000045
46
Eric Andersen9b587181999-10-17 05:43:39 +000047static int fileAction(const char *fileName, struct stat* statbuf)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Eric Andersenf811e071999-10-09 00:25:00 +000049 char newdestName[NAME_MAX];
50 strcpy(newdestName, destName);
Eric Andersenbed30e91999-10-18 19:02:32 +000051 if (dirFlag==TRUE) {
Eric Andersen9b587181999-10-17 05:43:39 +000052 strcat(newdestName, "/");
53 if ( skipName != NULL)
54 strcat(newdestName, strstr(fileName, skipName));
55 }
Eric Andersenf811e071999-10-09 00:25:00 +000056 return (copyFile(fileName, newdestName, preserveFlag, followLinks));
57}
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Eric Andersenf811e071999-10-09 00:25:00 +000059extern int cp_main(int argc, char **argv)
60{
61
Eric Andersenf811e071999-10-09 00:25:00 +000062 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000063 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000064 }
65 argc--;
66 argv++;
67
68 /* Parse any options */
69 while (**argv == '-') {
70 while (*++(*argv))
71 switch (**argv) {
72 case 'a':
73 followLinks = TRUE;
74 preserveFlag = TRUE;
75 recursiveFlag = TRUE;
76 break;
77 case 'd':
78 followLinks = TRUE;
79 break;
80 case 'p':
81 preserveFlag = TRUE;
82 break;
83 case 'R':
84 recursiveFlag = TRUE;
85 break;
86 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000087 usage (cp_usage);
Eric Andersenf811e071999-10-09 00:25:00 +000088 }
89 argc--;
90 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000091 }
92
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Eric Andersenf811e071999-10-09 00:25:00 +000094 destName = argv[argc - 1];
Eric Andersenf811e071999-10-09 00:25:00 +000095 dirFlag = isDirectory(destName);
96
Eric Andersen9b587181999-10-17 05:43:39 +000097 if ((argc > 3) && dirFlag==FALSE) {
Eric Andersenf811e071999-10-09 00:25:00 +000098 fprintf(stderr, "%s: not a directory\n", destName);
Eric Andersen2ce1edc1999-10-12 15:42:48 +000099 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000100 }
Eric Andersenf811e071999-10-09 00:25:00 +0000101
Eric Andersen3c163821999-10-14 22:16:57 +0000102 while (argc-- > 1) {
Eric Andersenf811e071999-10-09 00:25:00 +0000103 srcName = *(argv++);
Eric Andersen3c163821999-10-14 22:16:57 +0000104 skipName = strrchr(srcName, '/');
Eric Andersenbed30e91999-10-18 19:02:32 +0000105 if (skipName)
106 skipName++;
107 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
108 fileAction, fileAction) == FALSE) {
Eric Andersen3c163821999-10-14 22:16:57 +0000109 exit( FALSE);
Eric Andersenbed30e91999-10-18 19:02:32 +0000110 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000111 }
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000112 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000113}