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 | * |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 7 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */ |
| 11 | /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */ |
| 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ |
| 13 | |
| 14 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 15 | * |
| 16 | * Size reduction. |
| 17 | */ |
| 18 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <utime.h> |
| 24 | #include <errno.h> |
| 25 | #include <dirent.h> |
| 26 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | #include <assert.h> |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | #include "libcoreutils/coreutils.h" |
| 30 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 31 | extern int cp_main(int argc, char **argv) |
| 32 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | struct stat source_stat; |
| 34 | struct stat dest_stat; |
| 35 | const char *last; |
| 36 | const char *dest; |
| 37 | int s_flags; |
| 38 | int d_flags; |
| 39 | int flags; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 40 | int status = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 41 | |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 42 | flags = bb_getopt_ulflags(argc, argv, "pdRfiarPHL"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | |
| 44 | if (flags & 32) { |
| 45 | flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE); |
| 46 | } |
Glenn L McGrath | 4766a2d | 2004-01-25 05:50:28 +0000 | [diff] [blame] | 47 | if (flags & 64) { |
| 48 | /* Make -r a synonym for -R, |
| 49 | * -r was marked as obsolete in SUSv3, but is included for compatability |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 50 | */ |
Glenn L McGrath | 4766a2d | 2004-01-25 05:50:28 +0000 | [diff] [blame] | 51 | flags |= FILEUTILS_RECUR; |
| 52 | } |
Mike Frysinger | 6077d90 | 2005-01-07 00:56:47 +0000 | [diff] [blame] | 53 | if (flags & 128) { |
| 54 | /* Make -P a synonym for -d, |
| 55 | * -d is the GNU option while -P is the POSIX 2003 option |
| 56 | */ |
| 57 | flags |= FILEUTILS_DEREFERENCE; |
| 58 | } |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 59 | /* Default behavior of cp is to dereference, so we don't have to do |
| 60 | * anything special when we are given -L. |
| 61 | * The behavior of -H is *almost* like -L, but not quite, so let's |
| 62 | * just ignore it too for fun. |
| 63 | if (flags & 256 || flags & 512) { |
| 64 | ; |
| 65 | } |
| 66 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | |
| 68 | flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ |
| 69 | |
| 70 | if (optind + 2 > argc) { |
| 71 | bb_show_usage(); |
| 72 | } |
| 73 | |
| 74 | last = argv[argc - 1]; |
| 75 | argv += optind; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 76 | |
| 77 | /* If there are only two arguments and... */ |
| 78 | if (optind + 2 == argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 80 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) { |
| 82 | exit(EXIT_FAILURE); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 83 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 84 | /* ...if neither is a directory or... */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | if ( !((s_flags | d_flags) & 2) || |
| 86 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
| 87 | /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */ |
| 88 | /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */ |
| 89 | ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags) |
| 90 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 91 | /* ...do a simple copy. */ |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 92 | dest = last; |
| 93 | goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | do { |
| 98 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
| 99 | DO_COPY: |
| 100 | if (copy_file(*argv, dest, flags) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 101 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | } |
| 103 | if (*++argv == last) { |
| 104 | break; |
| 105 | } |
| 106 | free((void *) dest); |
| 107 | } while (1); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 108 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | exit(status); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 110 | } |