blob: c5dd31ec352eff679e7bcb5b06b2ef53eb31f519 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
24/* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Size reduction.
30 */
31
Matt Kraai91b28552001-04-23 18:53:07 +000032#include <sys/types.h>
33#include <sys/stat.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <utime.h>
37#include <errno.h>
38#include <dirent.h>
39#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000040#include <assert.h>
Matt Kraai91b28552001-04-23 18:53:07 +000041#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000042#include "libcoreutils/coreutils.h"
43
44static const char cp_opts[] = "pdRfia"; /* WARNING!! ORDER IS IMPORTANT!! */
Matt Kraai91b28552001-04-23 18:53:07 +000045
46extern int cp_main(int argc, char **argv)
47{
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 struct stat source_stat;
49 struct stat dest_stat;
50 const char *last;
51 const char *dest;
52 int s_flags;
53 int d_flags;
54 int flags;
Matt Kraai91b28552001-04-23 18:53:07 +000055 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000056
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 /* Since these are enums, #if tests will not work. So use assert()s. */
58 assert(FILEUTILS_PRESERVE_STATUS == 1);
59 assert(FILEUTILS_DEREFERENCE == 2);
60 assert(FILEUTILS_RECUR == 4);
61 assert(FILEUTILS_FORCE == 8);
62 assert(FILEUTILS_INTERACTIVE == 16);
63
64 flags = bb_getopt_ulflags(argc, argv, cp_opts);
65
66 if (flags & 32) {
67 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
68 }
69
70 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
71
72 if (optind + 2 > argc) {
73 bb_show_usage();
74 }
75
76 last = argv[argc - 1];
77 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000078
79 /* If there are only two arguments and... */
80 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 s_flags = cp_mv_stat2(*argv, &source_stat,
82 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
83 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
84 exit(EXIT_FAILURE);
Matt Kraai91b28552001-04-23 18:53:07 +000085 }
Matt Kraai91b28552001-04-23 18:53:07 +000086 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 if ( !((s_flags | d_flags) & 2) ||
88 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
89 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
90 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
91 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
92 ) {
Matt Kraai91b28552001-04-23 18:53:07 +000093 /* ...do a simple copy. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 dest = last;
95 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
Matt Kraai91b28552001-04-23 18:53:07 +000096 }
97 }
98
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 do {
100 dest = concat_path_file(last, bb_get_last_path_component(*argv));
101 DO_COPY:
102 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000103 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 }
105 if (*++argv == last) {
106 break;
107 }
108 free((void *) dest);
109 } while (1);
Matt Kraai91b28552001-04-23 18:53:07 +0000110
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 exit(status);
Matt Kraai91b28552001-04-23 18:53:07 +0000112}