1 /* vi: set sw=4 ts=4: */
3 * Mini cp implementation for busybox
5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
13 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
19 #include "libcoreutils/coreutils.h"
21 /* This is a NOEXEC applet. Be very careful! */
24 int cp_main(int argc, char **argv);
25 int cp_main(int argc, char **argv)
27 struct stat source_stat;
28 struct stat dest_stat;
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),
43 // Need at least two arguments
44 // 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
48 opt_complementary = "-2:l--s:s--l:Pd:rR:apdR";
49 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHL");
52 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
53 /* 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.
57 if (flags & OPT_L) ...
58 if (flags & OPT_H) ... // deref command-line params only
62 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
67 last = argv[argc - 1];
68 /* If there are only two arguments and... */
70 s_flags = cp_mv_stat2(*argv, &source_stat,
71 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
74 d_flags = cp_mv_stat(last, &dest_stat);
78 /* ...if neither is a directory or... */
79 if ( !((s_flags | d_flags) & 2) ||
80 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
81 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
83 /* ...do a simple copy. */
85 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
90 dest = concat_path_file(last, bb_get_last_path_component(*argv));
92 if (copy_file(*argv, dest, flags) < 0) {
95 if (*++argv == last) {
96 /* possibly leaking dest... */
102 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */