source: MondoRescue/branches/stable/mindi-busybox/coreutils/cp.c @ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 14 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 2.8 KB
Line 
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 *
7 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8 */
9
10/* 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
19#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>
27#include <assert.h>
28#include "busybox.h"
29#include "libcoreutils/coreutils.h"
30
31int cp_main(int argc, char **argv)
32{
33    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;
40    int status = 0;
41
42    flags = bb_getopt_ulflags(argc, argv, "pdRfiarPHL");
43
44    if (flags & 32) {
45        flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
46    }
47    if (flags & 64) {
48        /* Make -r a synonym for -R,
49         * -r was marked as obsolete in SUSv3, but is included for compatibility
50         */
51        flags |= FILEUTILS_RECUR;
52    }
53    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    }
59    /* 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    */
67
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;
76
77    /* If there are only two arguments and...  */
78    if (optind + 2 == argc) {
79        s_flags = cp_mv_stat2(*argv, &source_stat,
80                              (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
81        if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
82            exit(EXIT_FAILURE);
83        }
84        /* ...if neither is a directory or...  */
85        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        ) {
91            /* ...do a simple copy.  */
92            dest = last;
93            goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
94        }
95    }
96
97    do {
98        dest = concat_path_file(last, bb_get_last_path_component(*argv));
99    DO_COPY:
100        if (copy_file(*argv, dest, flags) < 0) {
101            status = 1;
102        }
103        if (*++argv == last) {
104            break;
105        }
106        free((void *) dest);
107    } while (1);
108
109    exit(status);
110}
Note: See TracBrowser for help on using the repository browser.