Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini cp implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <utime.h> |
| 28 | #include <dirent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 30 | static const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 31 | " or: cp [OPTION]... SOURCE... DIRECTORY\n\n" |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 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" |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame^] | 36 | "\t-p\tpreserve file attributes if possible\n" |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 37 | "\t-R\tcopy directories recursively\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 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; |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 45 | static int destDirFlag = FALSE; |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 46 | static int srcDirFlag = FALSE; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 48 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 50 | char newdestName[NAME_MAX]; |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 51 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 52 | strcpy(newdestName, destName); |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 53 | if ( srcDirFlag == TRUE ) { |
| 54 | if (recursiveFlag!=TRUE ) { |
| 55 | fprintf(stderr, "cp: %s: omitting directory\n", srcName); |
| 56 | return( TRUE); |
| 57 | } |
| 58 | strcat(newdestName, strstr(fileName, srcName) + strlen(srcName)); |
| 59 | } |
| 60 | |
| 61 | if (destDirFlag==TRUE && srcDirFlag == FALSE) { |
| 62 | if (newdestName[strlen(newdestName)-1] != '/' ) { |
| 63 | strcat(newdestName, "/"); |
| 64 | } |
| 65 | strcat(newdestName, srcName); |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 66 | } |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 67 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 68 | return (copyFile(fileName, newdestName, preserveFlag, followLinks)); |
| 69 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 71 | extern int cp_main(int argc, char **argv) |
| 72 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 73 | if (argc < 3) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 74 | usage (cp_usage); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 75 | } |
| 76 | argc--; |
| 77 | argv++; |
| 78 | |
| 79 | /* Parse any options */ |
| 80 | while (**argv == '-') { |
| 81 | while (*++(*argv)) |
| 82 | switch (**argv) { |
| 83 | case 'a': |
| 84 | followLinks = TRUE; |
| 85 | preserveFlag = TRUE; |
| 86 | recursiveFlag = TRUE; |
| 87 | break; |
| 88 | case 'd': |
| 89 | followLinks = TRUE; |
| 90 | break; |
| 91 | case 'p': |
| 92 | preserveFlag = TRUE; |
| 93 | break; |
| 94 | case 'R': |
| 95 | recursiveFlag = TRUE; |
| 96 | break; |
| 97 | default: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 98 | usage (cp_usage); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 99 | } |
| 100 | argc--; |
| 101 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 105 | destName = argv[argc - 1]; |
Erik Andersen | f664c00 | 1999-12-29 02:36:29 +0000 | [diff] [blame] | 106 | destDirFlag = isDirectory(destName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 107 | |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 108 | if ((argc > 3) && destDirFlag==FALSE) { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 109 | fprintf(stderr, "%s: not a directory\n", destName); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 110 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 112 | |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 113 | while (argc-- > 1) { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 114 | srcName = *(argv++); |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 115 | srcDirFlag = isDirectory(srcName); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 116 | if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE, |
| 117 | fileAction, fileAction) == FALSE) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 118 | exit( FALSE); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 119 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 120 | } |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 121 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 122 | } |