summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-04-23 18:53:07 +0000
committerMatt Kraai <kraai@debian.org>2001-04-23 18:53:07 +0000
commit91b2855ba8b9918b79dbe4b9188a3acccb41f7b7 (patch)
treeb6ded0b21428442ba27167d8654410c8ce0c73d3 /coreutils
parent4e9267d76c7dc47064bc80b1f8542453725158d7 (diff)
downloadbusybox-w32-91b2855ba8b9918b79dbe4b9188a3acccb41f7b7.tar.gz
busybox-w32-91b2855ba8b9918b79dbe4b9188a3acccb41f7b7.tar.bz2
busybox-w32-91b2855ba8b9918b79dbe4b9188a3acccb41f7b7.zip
Rewrite cp and mv to be SUSv2 compliant.
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/cp.c114
-rw-r--r--coreutils/mv.c183
2 files changed, 297 insertions, 0 deletions
diff --git a/coreutils/cp.c b/coreutils/cp.c
new file mode 100644
index 000000000..254445f02
--- /dev/null
+++ b/coreutils/cp.c
@@ -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
35extern 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}
diff --git a/coreutils/mv.c b/coreutils/mv.c
new file mode 100644
index 000000000..13d1aae5b
--- /dev/null
+++ b/coreutils/mv.c
@@ -0,0 +1,183 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini mv 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 <dirent.h>
28#include <errno.h>
29#include <stdlib.h>
30
31#include "busybox.h"
32
33static int flags = CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS;
34
35static int remove_file(const char *path, struct stat *statbuf, void *junk)
36{
37 if (unlink(path) < 0)
38 return FALSE;
39 return TRUE;
40}
41
42static int remove_directory(const char *path, struct stat *statbuf, void *junk)
43{
44 if (rmdir(path) < 0)
45 return FALSE;
46 return TRUE;
47}
48
49static int manual_rename(const char *source, const char *dest)
50{
51 struct stat source_stat;
52 struct stat dest_stat;
53 int source_exists = 1;
54 int dest_exists = 1;
55
56 if (stat(source, &source_stat) < 0) {
57 if (errno != ENOENT) {
58 perror_msg("unable to stat `%s'", source);
59 return -1;
60 }
61 source_exists = 0;
62 }
63
64 if (stat(dest, &dest_stat) < 0) {
65 if (errno != ENOENT) {
66 perror_msg("unable to stat `%s'", dest);
67 return -1;
68 }
69 dest_exists = 0;
70 }
71
72 if (dest_exists) {
73 if (S_ISDIR(dest_stat.st_mode) &&
74 (!source_exists || !S_ISDIR(source_stat.st_mode))) {
75 error_msg("cannot overwrite directory with non-directory");
76 return -1;
77 }
78
79 if (!S_ISDIR(dest_stat.st_mode) && source_exists &&
80 S_ISDIR(source_stat.st_mode)) {
81 error_msg("cannot overwrite non-directory with directory");
82 return -1;
83 }
84
85 if (unlink(dest) < 0) {
86 perror_msg("cannot remove `%s'", dest);
87 return -1;
88 }
89 }
90
91 if (copy_file(source, dest,
92 CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS) < 0)
93 return -1;
94
95 if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file,
96 remove_directory, NULL))
97 return -1;
98
99 return 0;
100}
101
102static int move_file(const char *source, const char *dest)
103{
104 struct stat dest_stat;
105 int dest_exists = 1;
106
107 if (stat(dest, &dest_stat) < 0) {
108 if (errno != ENOENT) {
109 perror_msg("unable to stat `%s'", dest);
110 return -1;
111 }
112 dest_exists = 0;
113 }
114
115 if (dest_exists && !(flags & CP_FORCE) &&
116 ((access(dest, W_OK) < 0 && isatty(0)) ||
117 (flags & CP_INTERACTIVE))) {
118 fprintf(stderr, "mv: overwrite `%s'? ", dest);
119 if (!ask_confirmation())
120 return 0;
121 }
122
123 if (rename(source, dest) < 0) {
124 if (errno == EXDEV)
125 return manual_rename(source, dest);
126
127 perror_msg("unable to rename `%s'", source);
128 return -1;
129 }
130
131 return 0;
132}
133
134extern int mv_main(int argc, char **argv)
135{
136 int status = 0;
137 int opt;
138 int i;
139
140 while ((opt = getopt(argc, argv, "fi")) != -1)
141 switch (opt) {
142 case 'f':
143 flags &= ~CP_INTERACTIVE;
144 flags |= CP_FORCE;
145 break;
146 case 'i':
147 flags &= ~CP_FORCE;
148 flags |= CP_INTERACTIVE;
149 break;
150 default:
151 show_usage();
152 }
153
154 if (optind + 2 > argc)
155 show_usage();
156
157 if (optind + 2 == argc) {
158 struct stat dest_stat;
159 int dest_exists = 1;
160
161 if (stat(argv[optind + 1], &dest_stat) < 0) {
162 if (errno != ENOENT)
163 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
164 dest_exists = 0;
165 }
166
167 if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) {
168 if (move_file(argv[optind], argv[optind + 1]) < 0)
169 status = 1;
170 return status;
171 }
172 }
173
174 for (i = optind; i < argc - 1; i++) {
175 char *dest = concat_path_file(argv[argc - 1],
176 get_last_path_component(argv[i]));
177 if (move_file(argv[i], dest) < 0)
178 status = 1;
179 free(dest);
180 }
181
182 return status;
183}