blob: 7b0de477bfe5508a77d259687d2bc662ffaeef2e [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>
6 *
Rob Landley2f309322005-11-01 21:55:14 +00007 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Matt Kraai91b28552001-04-23 18:53:07 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
11
12/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
13 *
14 * Size reduction.
15 */
16
Matt Kraai91b28552001-04-23 18:53:07 +000017#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000018#include "libcoreutils/coreutils.h"
19
Denis Vlasenko06af2162007-02-03 17:28:39 +000020int cp_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000021int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000022{
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 struct stat source_stat;
24 struct stat dest_stat;
25 const char *last;
26 const char *dest;
27 int s_flags;
28 int d_flags;
29 int flags;
Matt Kraai91b28552001-04-23 18:53:07 +000030 int status = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000031 enum {
32 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
33 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
34 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
35 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
36 OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
37 };
Matt Kraai91b28552001-04-23 18:53:07 +000038
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000039 // Soft- and hardlinking don't mix
40 // -P and -d are the same (-P is POSIX, -d is GNU)
41 // -r and -R are the same
42 // -a = -pdR
43 opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
44 flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000045 /* Default behavior of cp is to dereference, so we don't have to do
46 * anything special when we are given -L.
47 * The behavior of -H is *almost* like -L, but not quite, so let's
48 * just ignore it too for fun.
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000049 if (flags & OPT_L) ...
50 if (flags & OPT_H) ... // deref command-line params only
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000051 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000052
53 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
54
55 if (optind + 2 > argc) {
56 bb_show_usage();
57 }
58
59 last = argv[argc - 1];
60 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000061
62 /* If there are only two arguments and... */
63 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 s_flags = cp_mv_stat2(*argv, &source_stat,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000065 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000066 if (s_flags < 0)
67 return EXIT_FAILURE;
68 d_flags = cp_mv_stat(last, &dest_stat);
69 if (d_flags < 0)
70 return EXIT_FAILURE;
71
Matt Kraai91b28552001-04-23 18:53:07 +000072 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if ( !((s_flags | d_flags) & 2) ||
74 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000075 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 ) {
Matt Kraai91b28552001-04-23 18:53:07 +000077 /* ...do a simple copy. */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000078 dest = xstrdup(last);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000079 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
Matt Kraai91b28552001-04-23 18:53:07 +000080 }
81 }
82
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 do {
84 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000085 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000087 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000089 free((void*)dest);
90 } while (*++argv != last);
Matt Kraai91b28552001-04-23 18:53:07 +000091
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000092 return status;
Matt Kraai91b28552001-04-23 18:53:07 +000093}