blob: ac572548f47c822c25291c8ed6d652a4294dea8e [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/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
11/* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
13
14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
15 *
16 * Size reduction.
17 */
18
Matt Kraai91b28552001-04-23 18:53:07 +000019#include <sys/types.h>
20#include <sys/stat.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <utime.h>
24#include <errno.h>
25#include <dirent.h>
26#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000027#include <assert.h>
Matt Kraai91b28552001-04-23 18:53:07 +000028#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000029#include "libcoreutils/coreutils.h"
30
Matt Kraai91b28552001-04-23 18:53:07 +000031extern int cp_main(int argc, char **argv)
32{
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 struct stat source_stat;
34 struct stat dest_stat;
35 const char *last;
36 const char *dest;
37 int s_flags;
38 int d_flags;
39 int flags;
Matt Kraai91b28552001-04-23 18:53:07 +000040 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000041
Rob Landley2f309322005-11-01 21:55:14 +000042 flags = bb_getopt_ulflags(argc, argv, "pdRfiarPHL");
Manuel Novoa III cad53642003-03-19 09:13:01 +000043
44 if (flags & 32) {
45 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
46 }
Glenn L McGrath4766a2d2004-01-25 05:50:28 +000047 if (flags & 64) {
48 /* Make -r a synonym for -R,
49 * -r was marked as obsolete in SUSv3, but is included for compatability
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000050 */
Glenn L McGrath4766a2d2004-01-25 05:50:28 +000051 flags |= FILEUTILS_RECUR;
52 }
Mike Frysinger6077d902005-01-07 00:56:47 +000053 if (flags & 128) {
54 /* Make -P a synonym for -d,
55 * -d is the GNU option while -P is the POSIX 2003 option
56 */
57 flags |= FILEUTILS_DEREFERENCE;
58 }
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000059 /* Default behavior of cp is to dereference, so we don't have to do
60 * anything special when we are given -L.
61 * The behavior of -H is *almost* like -L, but not quite, so let's
62 * just ignore it too for fun.
63 if (flags & 256 || flags & 512) {
64 ;
65 }
66 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
68 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
69
70 if (optind + 2 > argc) {
71 bb_show_usage();
72 }
73
74 last = argv[argc - 1];
75 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000076
77 /* If there are only two arguments and... */
78 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 s_flags = cp_mv_stat2(*argv, &source_stat,
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000080 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
82 exit(EXIT_FAILURE);
Matt Kraai91b28552001-04-23 18:53:07 +000083 }
Matt Kraai91b28552001-04-23 18:53:07 +000084 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 if ( !((s_flags | d_flags) & 2) ||
86 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
87 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
88 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
89 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
90 ) {
Matt Kraai91b28552001-04-23 18:53:07 +000091 /* ...do a simple copy. */
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000092 dest = last;
93 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
Matt Kraai91b28552001-04-23 18:53:07 +000094 }
95 }
96
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 do {
98 dest = concat_path_file(last, bb_get_last_path_component(*argv));
99 DO_COPY:
100 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000101 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 }
103 if (*++argv == last) {
104 break;
105 }
106 free((void *) dest);
107 } while (1);
Matt Kraai91b28552001-04-23 18:53:07 +0000108
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 exit(status);
Matt Kraai91b28552001-04-23 18:53:07 +0000110}