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 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ |
| 11 | |
| 12 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 13 | * |
| 14 | * Size reduction. |
| 15 | */ |
| 16 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | #include "libcoreutils/coreutils.h" |
| 19 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 20 | int cp_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 21 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | struct stat source_stat; |
| 23 | struct stat dest_stat; |
| 24 | const char *last; |
| 25 | const char *dest; |
| 26 | int s_flags; |
| 27 | int d_flags; |
| 28 | int flags; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 29 | int status = 0; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 30 | enum { |
| 31 | OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), |
| 32 | OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)), |
| 33 | OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1), |
| 34 | OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), |
| 35 | OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), |
| 36 | }; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 38 | // Soft- and hardlinking don't mix |
| 39 | // -P and -d are the same (-P is POSIX, -d is GNU) |
| 40 | // -r and -R are the same |
| 41 | // -a = -pdR |
| 42 | opt_complementary = "?:l--s:s--l:Pd:rR:apdR"; |
| 43 | flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL"); |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 44 | /* Default behavior of cp is to dereference, so we don't have to do |
| 45 | * anything special when we are given -L. |
| 46 | * The behavior of -H is *almost* like -L, but not quite, so let's |
| 47 | * just ignore it too for fun. |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 48 | if (flags & OPT_L) ... |
| 49 | if (flags & OPT_H) ... // deref command-line params only |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 50 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | |
| 52 | flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ |
| 53 | |
| 54 | if (optind + 2 > argc) { |
| 55 | bb_show_usage(); |
| 56 | } |
| 57 | |
| 58 | last = argv[argc - 1]; |
| 59 | argv += optind; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 60 | |
| 61 | /* If there are only two arguments and... */ |
| 62 | if (optind + 2 == argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 64 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 65 | if (s_flags < 0) |
| 66 | return EXIT_FAILURE; |
| 67 | d_flags = cp_mv_stat(last, &dest_stat); |
| 68 | if (d_flags < 0) |
| 69 | return EXIT_FAILURE; |
| 70 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 71 | /* ...if neither is a directory or... */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | if ( !((s_flags | d_flags) & 2) || |
| 73 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 74 | ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 76 | /* ...do a simple copy. */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 77 | dest = xstrdup(last); |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 78 | goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | do { |
| 83 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 84 | DO_COPY: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | if (copy_file(*argv, dest, flags) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 86 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 88 | free((void*)dest); |
| 89 | } while (*++argv != last); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 91 | return status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 92 | } |