1 /* vi: set sw=4 ts=4: */
3 * Mini cp implementation for busybox
5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
11 /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
14 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
19 #include <sys/types.h>
29 #include "libcoreutils/coreutils.h"
31 int cp_main(int argc, char **argv)
33 struct stat source_stat;
34 struct stat dest_stat;
42 flags = bb_getopt_ulflags(argc, argv, "pdRfiarPHL");
45 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
48 /* Make -r a synonym for -R,
49 * -r was marked as obsolete in SUSv3, but is included for compatibility
51 flags |= FILEUTILS_RECUR;
54 /* Make -P a synonym for -d,
55 * -d is the GNU option while -P is the POSIX 2003 option
57 flags |= FILEUTILS_DEREFERENCE;
59 /* Default behavior of cp is to dereference, so we don't have to do
60 * anything special when we are given -L.
61 * The behavior of -H is *almost* like -L, but not quite, so let's
62 * just ignore it too for fun.
63 if (flags & 256 || flags & 512) {
68 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
70 if (optind + 2 > argc) {
74 last = argv[argc - 1];
77 /* If there are only two arguments and... */
78 if (optind + 2 == argc) {
79 s_flags = cp_mv_stat2(*argv, &source_stat,
80 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
81 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
84 /* ...if neither is a directory or... */
85 if ( !((s_flags | d_flags) & 2) ||
86 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
87 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
88 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
89 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
91 /* ...do a simple copy. */
93 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
98 dest = concat_path_file(last, bb_get_last_path_component(*argv));
100 if (copy_file(*argv, dest, flags) < 0) {
103 if (*++argv == last) {