blob: 76dc566b3832b6e007d8fc2463b1b827b5ff39ca [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cp implementation for busybox
4 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Denis Vlasenko49622d72007-03-10 16:58:49 +00006 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
Matt Kraai91b28552001-04-23 18:53:07 +00007 *
Rob Landley2f309322005-11-01 21:55:14 +00008 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Matt Kraai91b28552001-04-23 18:53:07 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* 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 Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#include "libcoreutils/coreutils.h"
20
Denis Vlasenko99912ca2007-04-10 15:43:37 +000021/* This is a NOEXEC applet. Be very careful! */
22
23
Denis Vlasenko06af2162007-02-03 17:28:39 +000024int cp_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000025int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000026{
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 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 Kraai91b28552001-04-23 18:53:07 +000034 int status = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000035 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 Kraai91b28552001-04-23 18:53:07 +000042
Denis Vlasenko6666ac42007-08-24 21:46:24 +000043 // Need at least two arguments
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000044 // Soft- and hardlinking don't mix
45 // -P and -d are the same (-P is POSIX, -d is GNU)
46 // -r and -R are the same
47 // -a = -pdR
Denis Vlasenko6666ac42007-08-24 21:46:24 +000048 opt_complementary = "-2:l--s:s--l:Pd:rR:apdR";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000049 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHL");
Denis Vlasenko6666ac42007-08-24 21:46:24 +000050 argc -= optind;
51 argv += optind;
52 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000053 /* Default behavior of cp is to dereference, so we don't have to do
54 * anything special when we are given -L.
55 * The behavior of -H is *almost* like -L, but not quite, so let's
56 * just ignore it too for fun.
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000057 if (flags & OPT_L) ...
58 if (flags & OPT_H) ... // deref command-line params only
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000059 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000060
Denis Vlasenkoc86e0522007-03-20 11:30:28 +000061#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +000062 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
63 selinux_or_die();
64 }
65#endif
66
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +000068 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +000069 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 s_flags = cp_mv_stat2(*argv, &source_stat,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000071 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000072 if (s_flags < 0)
73 return EXIT_FAILURE;
74 d_flags = cp_mv_stat(last, &dest_stat);
75 if (d_flags < 0)
76 return EXIT_FAILURE;
77
Matt Kraai91b28552001-04-23 18:53:07 +000078 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 if ( !((s_flags | d_flags) & 2) ||
80 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000081 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 ) {
Matt Kraai91b28552001-04-23 18:53:07 +000083 /* ...do a simple copy. */
Denis Vlasenko6666ac42007-08-24 21:46:24 +000084 dest = last;
85 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +000086 }
87 }
88
Denis Vlasenko6666ac42007-08-24 21:46:24 +000089 while (1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000091 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000093 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 }
Denis Vlasenko6666ac42007-08-24 21:46:24 +000095 if (*++argv == last) {
96 /* possibly leaking dest... */
97 break;
98 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000099 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000100 }
Matt Kraai91b28552001-04-23 18:53:07 +0000101
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000102 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000103 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000104}