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) MAIN_EXTERNALLY_VISIBLE;
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),
42 // Need at least two arguments
43 // Soft- and hardlinking doesn't mix
44 // -P and -d are the same (-P is POSIX, -d is GNU)
45 // -r and -R are the same
46 // -R (and therefore -r) turns on -d (coreutils does this)
48 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
49 // -v (--verbose) is ignored
50 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHv");
51 /* Options of cp from GNU coreutils 6.10:
57 * -P, --no-dereference
61 * -H follow command-line symbolic links in SOURCE
62 * -d same as --no-dereference --preserve=links
63 * -p same as --preserve=mode,ownership,timestamps
64 * -c same as --preserve=context
65 * NOT SUPPORTED IN BBOX:
66 * long options are not supported (even those above).
68 * make a backup of each existing destination file
69 * -b like --backup but does not accept an argument
71 * copy contents of special files when recursive
72 * --preserve[=ATTR_LIST]
73 * preserve attributes (default: mode,ownership,timestamps),
74 * if possible additional attributes: security context,links,all
75 * --no-preserve=ATTR_LIST
77 * use full source file name under DIRECTORY
78 * --remove-destination
79 * remove each existing destination file before attempting to open
81 * control creation of sparse files
82 * --strip-trailing-slashes
83 * remove any trailing slashes from each SOURCE argument
85 * override the usual backup suffix
86 * -t, --target-directory=DIRECTORY
87 * copy all SOURCE arguments into DIRECTORY
88 * -T, --no-target-directory
89 * treat DEST as a normal file
91 * copy only when the SOURCE file is newer than the destination
92 * file or when the destination file is missing
93 * -x, --one-file-system
94 * stay on this file system
95 * -Z, --context=CONTEXT
96 * (SELinux) set SELinux security context of copy to CONTEXT
100 flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
101 /* coreutils 6.9 compat:
102 * by default, "cp" derefs symlinks (creates regular dest files),
103 * but "cp -R" does not. We switch off deref if -r or -R (see above).
104 * However, "cp -RL" must still deref symlinks: */
105 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
106 flags |= FILEUTILS_DEREFERENCE;
107 /* The behavior of -H is *almost* like -L, but not quite, so let's
108 * just ignore it too for fun. TODO.
109 if (flags & OPT_H) ... // deref command-line params only
113 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
118 last = argv[argc - 1];
119 /* If there are only two arguments and... */
121 s_flags = cp_mv_stat2(*argv, &source_stat,
122 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
125 d_flags = cp_mv_stat(last, &dest_stat);
129 /* ...if neither is a directory or... */
130 if ( !((s_flags | d_flags) & 2) ||
131 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
132 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
134 /* ...do a simple copy. */
136 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
141 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
143 if (copy_file(*argv, dest, flags) < 0) {
146 if (*++argv == last) {
147 /* possibly leaking dest... */
153 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */