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 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
12 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
18 #include "libcoreutils/coreutils.h"
20 int cp_main(int argc, char **argv)
22 struct stat source_stat;
23 struct stat dest_stat;
31 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
32 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
33 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
34 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
35 OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
38 // Soft- and hardlinking don't mix
39 // -P and -d are the same (-P is POSIX, -d is GNU)
40 // -r and -R are the same
42 opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
43 flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
44 /* Default behavior of cp is to dereference, so we don't have to do
45 * anything special when we are given -L.
46 * The behavior of -H is *almost* like -L, but not quite, so let's
47 * just ignore it too for fun.
48 if (flags & OPT_L) ...
49 if (flags & OPT_H) ... // deref command-line params only
52 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
54 if (optind + 2 > argc) {
58 last = argv[argc - 1];
61 /* If there are only two arguments and... */
62 if (optind + 2 == argc) {
63 s_flags = cp_mv_stat2(*argv, &source_stat,
64 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
67 d_flags = cp_mv_stat(last, &dest_stat);
71 /* ...if neither is a directory or... */
72 if ( !((s_flags | d_flags) & 2) ||
73 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
74 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
76 /* ...do a simple copy. */
78 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
83 dest = concat_path_file(last, bb_get_last_path_component(*argv));
85 if (copy_file(*argv, dest, flags) < 0) {
89 } while (*++argv != last);