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