blob: 64cf637972caaea2306390fb1682bf7593e37399 [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cp implementation for busybox
4 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Denis Vlasenko49622d72007-03-10 16:58:49 +00006 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
Matt Kraai91b28552001-04-23 18:53:07 +00007 *
Rob Landley2f309322005-11-01 21:55:14 +00008 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Matt Kraai91b28552001-04-23 18:53:07 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* 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
Matt Kraai91b28552001-04-23 18:53:07 +000018#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#include "libcoreutils/coreutils.h"
20
Denis Vlasenko06af2162007-02-03 17:28:39 +000021int cp_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000022int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000023{
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 struct stat source_stat;
25 struct stat dest_stat;
26 const char *last;
27 const char *dest;
28 int s_flags;
29 int d_flags;
30 int flags;
Matt Kraai91b28552001-04-23 18:53:07 +000031 int status = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000032 enum {
33 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
34 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
35 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
36 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
37 OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
38 };
Matt Kraai91b28552001-04-23 18:53:07 +000039
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000040 // Soft- and hardlinking don't mix
41 // -P and -d are the same (-P is POSIX, -d is GNU)
42 // -r and -R are the same
43 // -a = -pdR
44 opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
45 flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000046 /* Default behavior of cp is to dereference, so we don't have to do
47 * anything special when we are given -L.
48 * The behavior of -H is *almost* like -L, but not quite, so let's
49 * just ignore it too for fun.
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000050 if (flags & OPT_L) ...
51 if (flags & OPT_H) ... // deref command-line params only
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000052 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000053
Denis Vlasenko49622d72007-03-10 16:58:49 +000054#if ENABLE_SELINUX
55 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
56 selinux_or_die();
57 }
58#endif
59
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
61
62 if (optind + 2 > argc) {
63 bb_show_usage();
64 }
65
66 last = argv[argc - 1];
67 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000068
69 /* If there are only two arguments and... */
70 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 s_flags = cp_mv_stat2(*argv, &source_stat,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000072 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000073 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
Matt Kraai91b28552001-04-23 18:53:07 +000079 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 if ( !((s_flags | d_flags) & 2) ||
81 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000082 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 ) {
Matt Kraai91b28552001-04-23 18:53:07 +000084 /* ...do a simple copy. */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000085 dest = xstrdup(last);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000086 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
Matt Kraai91b28552001-04-23 18:53:07 +000087 }
88 }
89
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 do {
91 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000092 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000094 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000096 free((void*)dest);
97 } while (*++argv != last);
Matt Kraai91b28552001-04-23 18:53:07 +000098
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000099 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000100}