Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini cp implementation for busybox |
| 4 | * |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */ |
| 24 | /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */ |
| 25 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ |
| 26 | |
| 27 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 28 | * |
| 29 | * Size reduction. |
| 30 | */ |
| 31 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <utime.h> |
| 37 | #include <errno.h> |
| 38 | #include <dirent.h> |
| 39 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | #include <assert.h> |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 41 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | #include "libcoreutils/coreutils.h" |
| 43 | |
Glenn L McGrath | 4766a2d | 2004-01-25 05:50:28 +0000 | [diff] [blame] | 44 | /* WARNING!! ORDER IS IMPORTANT!! */ |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame^] | 45 | static const char cp_opts[] = "pdRfiarPHL"; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 46 | |
| 47 | extern int cp_main(int argc, char **argv) |
| 48 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | struct stat source_stat; |
| 50 | struct stat dest_stat; |
| 51 | const char *last; |
| 52 | const char *dest; |
| 53 | int s_flags; |
| 54 | int d_flags; |
| 55 | int flags; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 56 | int status = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 57 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | /* Since these are enums, #if tests will not work. So use assert()s. */ |
| 59 | assert(FILEUTILS_PRESERVE_STATUS == 1); |
| 60 | assert(FILEUTILS_DEREFERENCE == 2); |
| 61 | assert(FILEUTILS_RECUR == 4); |
| 62 | assert(FILEUTILS_FORCE == 8); |
| 63 | assert(FILEUTILS_INTERACTIVE == 16); |
| 64 | |
| 65 | flags = bb_getopt_ulflags(argc, argv, cp_opts); |
| 66 | |
| 67 | if (flags & 32) { |
| 68 | flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE); |
| 69 | } |
Glenn L McGrath | 4766a2d | 2004-01-25 05:50:28 +0000 | [diff] [blame] | 70 | if (flags & 64) { |
| 71 | /* Make -r a synonym for -R, |
| 72 | * -r was marked as obsolete in SUSv3, but is included for compatability |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame^] | 73 | */ |
Glenn L McGrath | 4766a2d | 2004-01-25 05:50:28 +0000 | [diff] [blame] | 74 | flags |= FILEUTILS_RECUR; |
| 75 | } |
Mike Frysinger | 6077d90 | 2005-01-07 00:56:47 +0000 | [diff] [blame] | 76 | if (flags & 128) { |
| 77 | /* Make -P a synonym for -d, |
| 78 | * -d is the GNU option while -P is the POSIX 2003 option |
| 79 | */ |
| 80 | flags |= FILEUTILS_DEREFERENCE; |
| 81 | } |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame^] | 82 | /* Default behavior of cp is to dereference, so we don't have to do |
| 83 | * anything special when we are given -L. |
| 84 | * The behavior of -H is *almost* like -L, but not quite, so let's |
| 85 | * just ignore it too for fun. |
| 86 | if (flags & 256 || flags & 512) { |
| 87 | ; |
| 88 | } |
| 89 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | |
| 91 | flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ |
| 92 | |
| 93 | if (optind + 2 > argc) { |
| 94 | bb_show_usage(); |
| 95 | } |
| 96 | |
| 97 | last = argv[argc - 1]; |
| 98 | argv += optind; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 99 | |
| 100 | /* If there are only two arguments and... */ |
| 101 | if (optind + 2 == argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame^] | 103 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) { |
| 105 | exit(EXIT_FAILURE); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 106 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 107 | /* ...if neither is a directory or... */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | if ( !((s_flags | d_flags) & 2) || |
| 109 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
| 110 | /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */ |
| 111 | /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */ |
| 112 | ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags) |
| 113 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 114 | /* ...do a simple copy. */ |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame^] | 115 | dest = last; |
| 116 | goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | do { |
| 121 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
| 122 | DO_COPY: |
| 123 | if (copy_file(*argv, dest, flags) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 124 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | } |
| 126 | if (*++argv == last) { |
| 127 | break; |
| 128 | } |
| 129 | free((void *) dest); |
| 130 | } while (1); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 131 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | exit(status); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 133 | } |