1 /* vi: set sw=4 ts=4: */
3 * Mini mv implementation for busybox
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
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.
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.
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
24 #include <sys/types.h>
35 static int manual_rename(const char *source, const char *dest)
37 struct stat source_stat;
38 struct stat dest_stat;
39 int source_exists = 1;
42 if (stat(source, &source_stat) < 0) {
43 if (errno != ENOENT) {
44 perror_msg("unable to stat `%s'", source);
50 if (stat(dest, &dest_stat) < 0) {
51 if (errno != ENOENT) {
52 perror_msg("unable to stat `%s'", dest);
59 if (S_ISDIR(dest_stat.st_mode) &&
60 (!source_exists || !S_ISDIR(source_stat.st_mode))) {
61 error_msg("cannot overwrite directory with non-directory");
65 if (!S_ISDIR(dest_stat.st_mode) && source_exists &&
66 S_ISDIR(source_stat.st_mode)) {
67 error_msg("cannot overwrite non-directory with directory");
71 if (unlink(dest) < 0) {
72 perror_msg("cannot remove `%s'", dest);
77 if (copy_file(source, dest, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS |
78 FILEUTILS_PRESERVE_SYMLINKS) < 0)
81 if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0)
87 static int move_file(const char *source, const char *dest)
89 struct stat dest_stat;
92 if (stat(dest, &dest_stat) < 0) {
93 if (errno != ENOENT) {
94 perror_msg("unable to stat `%s'", dest);
100 if (dest_exists && !(flags & FILEUTILS_FORCE) &&
101 ((access(dest, W_OK) < 0 && isatty(0)) ||
102 (flags & FILEUTILS_INTERACTIVE))) {
103 fprintf(stderr, "mv: overwrite `%s'? ", dest);
104 if (!ask_confirmation())
108 if (rename(source, dest) < 0) {
110 return manual_rename(source, dest);
112 perror_msg("unable to rename `%s'", source);
119 extern int mv_main(int argc, char **argv)
125 while ((opt = getopt(argc, argv, "fi")) != -1)
128 flags &= ~FILEUTILS_INTERACTIVE;
129 flags |= FILEUTILS_FORCE;
132 flags &= ~FILEUTILS_FORCE;
133 flags |= FILEUTILS_INTERACTIVE;
139 if (optind + 2 > argc)
142 if (optind + 2 == argc) {
143 struct stat dest_stat;
146 if (stat(argv[optind + 1], &dest_stat) < 0) {
148 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
152 if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) {
153 if (move_file(argv[optind], argv[optind + 1]) < 0)
159 for (i = optind; i < argc - 1; i++) {
160 char *dest = concat_path_file(argv[argc - 1],
161 get_last_path_component(argv[i]));
162 if (move_file(argv[i], dest) < 0)