Initial public busybox upstream commit
[busybox4maemo] / coreutils / cp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini cp implementation for busybox
4  *
5  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
7  *
8  * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
12
13 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
14  *
15  * Size reduction.
16  */
17
18 #include "libbb.h"
19 #include "libcoreutils/coreutils.h"
20
21 /* This is a NOEXEC applet. Be very careful! */
22
23
24 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25 int cp_main(int argc, char **argv)
26 {
27         struct stat source_stat;
28         struct stat dest_stat;
29         const char *last;
30         const char *dest;
31         int s_flags;
32         int d_flags;
33         int flags;
34         int status = 0;
35         enum {
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),
41         };
42
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
47         // -R (and therefore -r) switches on -d (coreutils does this)
48         // -a = -pdR
49         opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
50         flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHL");
51         argc -= optind;
52         argv += optind;
53         flags ^= FILEUTILS_DEREFERENCE;         /* The sense of this flag was reversed. */
54         /* Default behavior of cp is to dereference, so we don't have to do
55          * anything special when we are given -L.
56          * The behavior of -H is *almost* like -L, but not quite, so let's
57          * just ignore it too for fun.
58         if (flags & OPT_L) ...
59         if (flags & OPT_H) ... // deref command-line params only
60         */
61
62 #if ENABLE_SELINUX
63         if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
64                 selinux_or_die();
65         }
66 #endif
67
68         last = argv[argc - 1];
69         /* If there are only two arguments and...  */
70         if (argc == 2) {
71                 s_flags = cp_mv_stat2(*argv, &source_stat,
72                                       (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
73                 if (s_flags < 0)
74                         return EXIT_FAILURE;
75                 d_flags = cp_mv_stat(last, &dest_stat);
76                 if (d_flags < 0)
77                         return EXIT_FAILURE;
78
79                 /* ...if neither is a directory or...  */
80                 if ( !((s_flags | d_flags) & 2) ||
81                         /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
82                         ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
83                 ) {
84                         /* ...do a simple copy.  */
85                         dest = last;
86                         goto DO_COPY; /* NB: argc==2 -> *++argv==last */
87                 }
88         }
89
90         while (1) {
91                 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
92  DO_COPY:
93                 if (copy_file(*argv, dest, flags) < 0) {
94                         status = 1;
95                 }
96                 if (*++argv == last) {
97                         /* possibly leaking dest... */
98                         break;
99                 }
100                 free((void*)dest);
101         }
102
103         /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
104         return status;
105 }