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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
24 /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
25 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
27 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
32 #include <sys/types.h>
42 #include "libcoreutils/coreutils.h"
44 /* WARNING!! ORDER IS IMPORTANT!! */
45 static const char cp_opts[] = "pdRfiar";
47 extern int cp_main(int argc, char **argv)
49 struct stat source_stat;
50 struct stat dest_stat;
58 /* Since these are enums, #if tests will not work. So use assert()s. */
59 assert(FILEUTILS_PRESERVE_STATUS == 1);
60 assert(FILEUTILS_DEREFERENCE == 2);
61 assert(FILEUTILS_RECUR == 4);
62 assert(FILEUTILS_FORCE == 8);
63 assert(FILEUTILS_INTERACTIVE == 16);
65 flags = bb_getopt_ulflags(argc, argv, cp_opts);
68 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
71 /* Make -r a synonym for -R,
72 * -r was marked as obsolete in SUSv3, but is included for compatability
74 flags |= FILEUTILS_RECUR;
77 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
79 if (optind + 2 > argc) {
83 last = argv[argc - 1];
86 /* If there are only two arguments and... */
87 if (optind + 2 == argc) {
88 s_flags = cp_mv_stat2(*argv, &source_stat,
89 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
90 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
93 /* ...if neither is a directory or... */
94 if ( !((s_flags | d_flags) & 2) ||
95 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
96 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
97 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
98 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
100 /* ...do a simple copy. */
102 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
107 dest = concat_path_file(last, bb_get_last_path_component(*argv));
109 if (copy_file(*argv, dest, flags) < 0) {
112 if (*++argv == last) {