1 /* vi: set sw=4 ts=4: */
3 * Mini mv implementation for busybox
5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
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.
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.
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
23 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
25 * Size reduction and improved error checking.
28 #include <sys/types.h>
36 #include "libcoreutils/coreutils.h"
38 static const struct option mv_long_options[] = {
39 { "interactive", 0, NULL, 'i' },
40 { "force", 0, NULL, 'f' },
44 #define OPT_FILEUTILS_FORCE 1
45 #define OPT_FILEUTILS_INTERACTIVE 2
47 static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
49 extern int mv_main(int argc, char **argv)
51 struct stat dest_stat;
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) {
65 last = argv[argc - 1];
68 if (optind + 2 == argc) {
69 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
73 if (!(dest_exists & 2)) {
80 dest = concat_path_file(last, bb_get_last_path_component(*argv));
82 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
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! */
94 if (!bb_ask_confirmation()) {
98 if (rename(*argv, dest) < 0) {
99 struct stat source_stat;
102 if (errno != EXDEV) {
103 bb_perror_msg("unable to rename `%s'", *argv);
105 else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
107 if (dest_exists == 3) {
108 if (source_exists != 3) {
109 bb_error_msg(fmt, "", "non-");
113 if (source_exists == 3) {
114 bb_error_msg(fmt, "non-", "");
118 if (unlink(dest) < 0) {
119 bb_perror_msg("cannot remove `%s'", dest);
123 if ((copy_file(*argv, dest,
124 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
125 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
136 } while (*++argv != last);