diff options
author | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-23 18:53:07 +0000 |
---|---|---|
committer | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-23 18:53:07 +0000 |
commit | 9788f99c9bc501de79f5e383291c24481bb63c57 (patch) | |
tree | b6ded0b21428442ba27167d8654410c8ce0c73d3 /cp.c | |
parent | 9ef67d9ca4a31f46dac4406e0925c77c3c21a017 (diff) | |
download | busybox-w32-9788f99c9bc501de79f5e383291c24481bb63c57.tar.gz busybox-w32-9788f99c9bc501de79f5e383291c24481bb63c57.tar.bz2 busybox-w32-9788f99c9bc501de79f5e383291c24481bb63c57.zip |
Rewrite cp and mv to be SUSv2 compliant.
git-svn-id: svn://busybox.net/trunk/busybox@2407 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'cp.c')
-rw-r--r-- | cp.c | 114 |
1 files changed, 114 insertions, 0 deletions
@@ -0,0 +1,114 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini cp implementation for busybox | ||
4 | * | ||
5 | * | ||
6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <unistd.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <utime.h> | ||
29 | #include <errno.h> | ||
30 | #include <dirent.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | #include "busybox.h" | ||
34 | |||
35 | extern int cp_main(int argc, char **argv) | ||
36 | { | ||
37 | int status = 0; | ||
38 | int opt; | ||
39 | int flags = 0; | ||
40 | int i; | ||
41 | |||
42 | while ((opt = getopt(argc, argv, "adfipR")) != -1) | ||
43 | switch (opt) { | ||
44 | case 'a': | ||
45 | flags |= CP_PRESERVE_STATUS | CP_RECUR; | ||
46 | /* fallthrough */ | ||
47 | case 'd': | ||
48 | flags |= CP_PRESERVE_SYMLINKS; | ||
49 | break; | ||
50 | case 'f': | ||
51 | flags |= CP_FORCE; | ||
52 | break; | ||
53 | case 'i': | ||
54 | flags |= CP_INTERACTIVE; | ||
55 | break; | ||
56 | case 'p': | ||
57 | flags |= CP_PRESERVE_STATUS; | ||
58 | break; | ||
59 | case 'R': | ||
60 | flags |= CP_RECUR; | ||
61 | break; | ||
62 | default: | ||
63 | show_usage(); | ||
64 | } | ||
65 | |||
66 | if (optind + 2 > argc) | ||
67 | show_usage(); | ||
68 | |||
69 | /* If there are only two arguments and... */ | ||
70 | if (optind + 2 == argc) { | ||
71 | struct stat source_stat; | ||
72 | struct stat dest_stat; | ||
73 | int source_exists = 1; | ||
74 | int dest_exists = 1; | ||
75 | |||
76 | if (((flags & CP_PRESERVE_SYMLINKS) && | ||
77 | lstat(argv[optind], &source_stat) < 0) || | ||
78 | (!(flags & CP_PRESERVE_SYMLINKS) && | ||
79 | stat(argv[optind], &source_stat))) { | ||
80 | if (errno != ENOENT) | ||
81 | perror_msg_and_die("unable to stat `%s'", argv[optind]); | ||
82 | source_exists = 0; | ||
83 | } | ||
84 | |||
85 | if (stat(argv[optind + 1], &dest_stat) < 0) { | ||
86 | if (errno != ENOENT) | ||
87 | perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); | ||
88 | dest_exists = 0; | ||
89 | } | ||
90 | |||
91 | /* ...if neither is a directory or... */ | ||
92 | if (((!source_exists || !S_ISDIR(source_stat.st_mode)) && | ||
93 | (!dest_exists || !S_ISDIR(dest_stat.st_mode))) || | ||
94 | /* ...recursing, the first is a directory, and the | ||
95 | * second doesn't exist, then... */ | ||
96 | ((flags & CP_RECUR) && S_ISDIR(source_stat.st_mode) && | ||
97 | !dest_exists)) { | ||
98 | /* ...do a simple copy. */ | ||
99 | if (copy_file(argv[optind], argv[optind + 1], flags) < 0) | ||
100 | status = 1; | ||
101 | return status; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | for (i = optind; i < argc - 1; i++) { | ||
106 | char *dest = concat_path_file(argv[argc - 1], | ||
107 | get_last_path_component(argv[i])); | ||
108 | if (copy_file(argv[i], dest, flags) < 0) | ||
109 | status = 1; | ||
110 | free(dest); | ||
111 | } | ||
112 | |||
113 | return status; | ||
114 | } | ||