Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/cp.c
Parent Directory | Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 3007 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 3007 byte(s)
-updated to busybox-1.13.4
1 | niro | 532 | /* 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 | niro | 816 | * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp> |
7 | niro | 532 | * |
8 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | |||
11 | /* 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 | |||
18 | niro | 816 | #include "libbb.h" |
19 | niro | 532 | #include "libcoreutils/coreutils.h" |
20 | |||
21 | niro | 816 | /* This is a NOEXEC applet. Be very careful! */ |
22 | |||
23 | |||
24 | int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
25 | niro | 532 | int cp_main(int argc, char **argv) |
26 | { | ||
27 | struct stat source_stat; | ||
28 | struct stat dest_stat; | ||
29 | const char *last; | ||
30 | const char *dest; | ||
31 | int s_flags; | ||
32 | int d_flags; | ||
33 | int flags; | ||
34 | int status = 0; | ||
35 | enum { | ||
36 | OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), | ||
37 | OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)), | ||
38 | OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1), | ||
39 | OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), | ||
40 | }; | ||
41 | |||
42 | niro | 816 | // Need at least two arguments |
43 | // Soft- and hardlinking doesn't mix | ||
44 | niro | 532 | // -P and -d are the same (-P is POSIX, -d is GNU) |
45 | // -r and -R are the same | ||
46 | niro | 816 | // -R (and therefore -r) turns on -d (coreutils does this) |
47 | niro | 532 | // -a = -pdR |
48 | niro | 816 | opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL"; |
49 | flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPH"); | ||
50 | argc -= optind; | ||
51 | argv += optind; | ||
52 | flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */ | ||
53 | /* coreutils 6.9 compat: | ||
54 | * by default, "cp" derefs symlinks (creates regular dest files), | ||
55 | * but "cp -R" does not. We switch off deref if -r or -R (see above). | ||
56 | * However, "cp -RL" must still deref symlinks: */ | ||
57 | if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */ | ||
58 | flags |= FILEUTILS_DEREFERENCE; | ||
59 | /* The behavior of -H is *almost* like -L, but not quite, so let's | ||
60 | * just ignore it too for fun. TODO. | ||
61 | niro | 532 | if (flags & OPT_H) ... // deref command-line params only |
62 | */ | ||
63 | |||
64 | niro | 816 | #if ENABLE_SELINUX |
65 | if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) { | ||
66 | selinux_or_die(); | ||
67 | niro | 532 | } |
68 | niro | 816 | #endif |
69 | niro | 532 | |
70 | last = argv[argc - 1]; | ||
71 | /* If there are only two arguments and... */ | ||
72 | niro | 816 | if (argc == 2) { |
73 | niro | 532 | s_flags = cp_mv_stat2(*argv, &source_stat, |
74 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); | ||
75 | if (s_flags < 0) | ||
76 | return EXIT_FAILURE; | ||
77 | d_flags = cp_mv_stat(last, &dest_stat); | ||
78 | if (d_flags < 0) | ||
79 | return EXIT_FAILURE; | ||
80 | |||
81 | /* ...if neither is a directory or... */ | ||
82 | if ( !((s_flags | d_flags) & 2) || | ||
83 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ | ||
84 | ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) | ||
85 | ) { | ||
86 | /* ...do a simple copy. */ | ||
87 | niro | 816 | dest = last; |
88 | goto DO_COPY; /* NB: argc==2 -> *++argv==last */ | ||
89 | niro | 532 | } |
90 | } | ||
91 | |||
92 | niro | 816 | while (1) { |
93 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); | ||
94 | niro | 532 | DO_COPY: |
95 | if (copy_file(*argv, dest, flags) < 0) { | ||
96 | status = 1; | ||
97 | } | ||
98 | niro | 816 | if (*++argv == last) { |
99 | /* possibly leaking dest... */ | ||
100 | break; | ||
101 | } | ||
102 | niro | 532 | free((void*)dest); |
103 | niro | 816 | } |
104 | niro | 532 | |
105 | niro | 816 | /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */ |
106 | niro | 532 | return status; |
107 | } |