summaryrefslogtreecommitdiff
path: root/busybox/coreutils/mv.c
diff options
context:
space:
mode:
authornobody <nobody@localhost>2004-10-13 09:42:10 +0000
committernobody <nobody@localhost>2004-10-13 09:42:10 +0000
commit8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373 (patch)
tree1826706cd4fd009fcd14f4f8021005ec8ec0fa59 /busybox/coreutils/mv.c
downloadbusybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.tar.gz
busybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.tar.bz2
busybox-w32-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.zip
This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
Diffstat (limited to 'busybox/coreutils/mv.c')
-rw-r--r--busybox/coreutils/mv.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/busybox/coreutils/mv.c b/busybox/coreutils/mv.c
new file mode 100644
index 000000000..4f08dedc0
--- /dev/null
+++ b/busybox/coreutils/mv.c
@@ -0,0 +1,139 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini mv implementation for busybox
4 *
5 * 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
23/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
24 *
25 * Size reduction and improved error checking.
26 */
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <dirent.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <getopt.h>
35#include "busybox.h"
36#include "libcoreutils/coreutils.h"
37
38static const struct option mv_long_options[] = {
39 { "interactive", 0, NULL, 'i' },
40 { "force", 0, NULL, 'f' },
41 { 0, 0, 0, 0 }
42};
43
44#define OPT_FILEUTILS_FORCE 1
45#define OPT_FILEUTILS_INTERACTIVE 2
46
47static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
48
49extern int mv_main(int argc, char **argv)
50{
51 struct stat dest_stat;
52 const char *last;
53 const char *dest;
54 unsigned long flags;
55 int dest_exists;
56 int status = 0;
57
58 bb_applet_long_options = mv_long_options;
59 bb_opt_complementaly = "f-i:i-f";
60 flags = bb_getopt_ulflags(argc, argv, "fi");
61 if (optind + 2 > argc) {
62 bb_show_usage();
63 }
64
65 last = argv[argc - 1];
66 argv += optind;
67
68 if (optind + 2 == argc) {
69 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
70 return 1;
71 }
72
73 if (!(dest_exists & 2)) {
74 dest = last;
75 goto DO_MOVE;
76 }
77 }
78
79 do {
80 dest = concat_path_file(last, bb_get_last_path_component(*argv));
81
82 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
83 goto RET_1;
84 }
85
86DO_MOVE:
87
88 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
89 ((access(dest, W_OK) < 0 && isatty(0)) ||
90 (flags & OPT_FILEUTILS_INTERACTIVE))) {
91 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
92 goto RET_1; /* Ouch! fprintf failed! */
93 }
94 if (!bb_ask_confirmation()) {
95 goto RET_0;
96 }
97 }
98 if (rename(*argv, dest) < 0) {
99 struct stat source_stat;
100 int source_exists;
101
102 if (errno != EXDEV) {
103 bb_perror_msg("unable to rename `%s'", *argv);
104 }
105 else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
106 if (dest_exists) {
107 if (dest_exists == 3) {
108 if (source_exists != 3) {
109 bb_error_msg(fmt, "", "non-");
110 goto RET_1;
111 }
112 } else {
113 if (source_exists == 3) {
114 bb_error_msg(fmt, "non-", "");
115 goto RET_1;
116 }
117 }
118 if (unlink(dest) < 0) {
119 bb_perror_msg("cannot remove `%s'", dest);
120 goto RET_1;
121 }
122 }
123 if ((copy_file(*argv, dest,
124 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
125 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
126 goto RET_0;
127 }
128 }
129RET_1:
130 status = 1;
131 }
132RET_0:
133 if (dest != last) {
134 free((void *) dest);
135 }
136 } while (*++argv != last);
137
138 return (status);
139}