getopt32: remove opt_complementary
[oweals/busybox.git] / 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 GPLv2 or later, see file LICENSE in this source tree.
9  */
10 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
11  *
12  * Size reduction.
13  */
14 //config:config CP
15 //config:       bool "cp (9.7 kb)"
16 //config:       default y
17 //config:       help
18 //config:       cp is used to copy files and directories.
19 //config:
20 //config:config FEATURE_CP_LONG_OPTIONS
21 //config:       bool "Enable long options"
22 //config:       default y
23 //config:       depends on CP && LONG_OPTS
24 //config:       help
25 //config:       Enable long options.
26 //config:       Also add support for --parents option.
27
28 //applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp))
29
30 //kbuild:lib-$(CONFIG_CP) += cp.o
31
32 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
33
34 //usage:#define cp_trivial_usage
35 //usage:       "[OPTIONS] SOURCE... DEST"
36 //usage:#define cp_full_usage "\n\n"
37 //usage:       "Copy SOURCE(s) to DEST\n"
38 //usage:     "\n        -a      Same as -dpR"
39 //usage:        IF_SELINUX(
40 //usage:     "\n        -c      Preserve security context"
41 //usage:        )
42 //usage:     "\n        -R,-r   Recurse"
43 //usage:     "\n        -d,-P   Preserve symlinks (default if -R)"
44 //usage:     "\n        -L      Follow all symlinks"
45 //usage:     "\n        -H      Follow symlinks on command line"
46 //usage:     "\n        -p      Preserve file attributes if possible"
47 //usage:     "\n        -f      Overwrite"
48 //usage:     "\n        -i      Prompt before overwrite"
49 //usage:     "\n        -l,-s   Create (sym)links"
50 //usage:     "\n        -u      Copy only newer files"
51
52 #include "libbb.h"
53 #include "libcoreutils/coreutils.h"
54
55 /* This is a NOEXEC applet. Be very careful! */
56
57 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
58 int cp_main(int argc, char **argv)
59 {
60         struct stat source_stat;
61         struct stat dest_stat;
62         const char *last;
63         const char *dest;
64         int s_flags;
65         int d_flags;
66         int flags;
67         int status;
68         enum {
69                 FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
70 #if ENABLE_FEATURE_CP_LONG_OPTIONS
71                 /*OPT_rmdest  = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
72                 OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
73 #endif
74         };
75
76 #if ENABLE_FEATURE_CP_LONG_OPTIONS
77         flags = getopt32long(argv, "^"
78                 FILEUTILS_CP_OPTSTR
79                 "\0"
80                 // Need at least two arguments
81                 // Soft- and hardlinking doesn't mix
82                 // -P and -d are the same (-P is POSIX, -d is GNU)
83                 // -r and -R are the same
84                 // -R (and therefore -r) turns on -d (coreutils does this)
85                 // -a = -pdR
86                 "-2:l--s:s--l:Pd:rRd:Rd:apdR",
87                 "archive\0"        No_argument "a"
88                 "force\0"          No_argument "f"
89                 "interactive\0"    No_argument "i"
90                 "link\0"           No_argument "l"
91                 "dereference\0"    No_argument "L"
92                 "no-dereference\0" No_argument "P"
93                 "recursive\0"      No_argument "R"
94                 "symbolic-link\0"  No_argument "s"
95                 "verbose\0"        No_argument "v"
96                 "update\0"         No_argument "u"
97                 "remove-destination\0" No_argument "\xff"
98                 "parents\0"        No_argument "\xfe"
99         );
100 #else
101         flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
102 #endif
103         /* Options of cp from GNU coreutils 6.10:
104          * -a, --archive
105          * -f, --force
106          * -i, --interactive
107          * -l, --link
108          * -L, --dereference
109          * -P, --no-dereference
110          * -R, -r, --recursive
111          * -s, --symbolic-link
112          * -v, --verbose
113          * -H   follow command-line symbolic links in SOURCE
114          * -d   same as --no-dereference --preserve=links
115          * -p   same as --preserve=mode,ownership,timestamps
116          * -c   same as --preserve=context
117          * -u, --update
118          *      copy only when the SOURCE file is newer than the destination
119          *      file or when the destination file is missing
120          * --remove-destination
121          *      remove each existing destination file before attempting to open
122          * --parents
123          *      use full source file name under DIRECTORY
124          * NOT SUPPORTED IN BBOX:
125          * --backup[=CONTROL]
126          *      make a backup of each existing destination file
127          * -b   like --backup but does not accept an argument
128          * --copy-contents
129          *      copy contents of special files when recursive
130          * --preserve[=ATTR_LIST]
131          *      preserve attributes (default: mode,ownership,timestamps),
132          *      if possible additional attributes: security context,links,all
133          * --no-preserve=ATTR_LIST
134          * --sparse=WHEN
135          *      control creation of sparse files
136          * --strip-trailing-slashes
137          *      remove any trailing slashes from each SOURCE argument
138          * -S, --suffix=SUFFIX
139          *      override the usual backup suffix
140          * -t, --target-directory=DIRECTORY
141          *      copy all SOURCE arguments into DIRECTORY
142          * -T, --no-target-directory
143          *      treat DEST as a normal file
144          * -x, --one-file-system
145          *      stay on this file system
146          * -Z, --context=CONTEXT
147          *      (SELinux) set SELinux security context of copy to CONTEXT
148          */
149         argc -= optind;
150         argv += optind;
151         /* Reverse this bit. If there is -d, bit is not set: */
152         flags ^= FILEUTILS_DEREFERENCE;
153         /* coreutils 6.9 compat:
154          * by default, "cp" derefs symlinks (creates regular dest files),
155          * but "cp -R" does not. We switch off deref if -r or -R (see above).
156          * However, "cp -RL" must still deref symlinks: */
157         if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
158                 flags |= FILEUTILS_DEREFERENCE;
159
160 #if ENABLE_SELINUX
161         if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
162                 selinux_or_die();
163         }
164 #endif
165
166         status = EXIT_SUCCESS;
167         last = argv[argc - 1];
168         /* If there are only two arguments and...  */
169         if (argc == 2) {
170                 s_flags = cp_mv_stat2(*argv, &source_stat,
171                                 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
172                 if (s_flags < 0)
173                         return EXIT_FAILURE;
174                 d_flags = cp_mv_stat(last, &dest_stat);
175                 if (d_flags < 0)
176                         return EXIT_FAILURE;
177
178 #if ENABLE_FEATURE_CP_LONG_OPTIONS
179                 //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
180                 //      flags, FILEUTILS_RMDEST, OPT_parents);
181                 if (flags & OPT_parents) {
182                         if (!(d_flags & 2)) {
183                                 bb_error_msg_and_die("with --parents, the destination must be a directory");
184                         }
185                 }
186                 if (flags & FILEUTILS_RMDEST) {
187                         flags |= FILEUTILS_FORCE;
188                 }
189 #endif
190
191                 /* ...if neither is a directory...  */
192                 if (!((s_flags | d_flags) & 2)
193                     /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
194                  || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
195                 ) {
196                         /* Do a simple copy */
197                         dest = last;
198                         goto DO_COPY; /* NB: argc==2 -> *++argv==last */
199                 }
200         }
201
202         while (1) {
203 #if ENABLE_FEATURE_CP_LONG_OPTIONS
204                 if (flags & OPT_parents) {
205                         char *dest_dup;
206                         char *dest_dir;
207                         dest = concat_path_file(last, *argv);
208                         dest_dup = xstrdup(dest);
209                         dest_dir = dirname(dest_dup);
210                         if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
211                                 return EXIT_FAILURE;
212                         }
213                         free(dest_dup);
214                         goto DO_COPY;
215                 }
216 #endif
217                 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
218  DO_COPY:
219                 if (copy_file(*argv, dest, flags) < 0) {
220                         status = EXIT_FAILURE;
221                 }
222                 if (*++argv == last) {
223                         /* possibly leaking dest... */
224                         break;
225                 }
226                 /* don't move up: dest may be == last and not malloced! */
227                 free((void*)dest);
228         }
229
230         /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
231         return status;
232 }