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>
33 static int flags = CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS;
35 static int remove_file(const char *path, struct stat *statbuf, void *junk)
42 static int remove_directory(const char *path, struct stat *statbuf, void *junk)
49 static int manual_rename(const char *source, const char *dest)
51 struct stat source_stat;
52 struct stat dest_stat;
53 int source_exists = 1;
56 if (stat(source, &source_stat) < 0) {
57 if (errno != ENOENT) {
58 perror_msg("unable to stat `%s'", source);
64 if (stat(dest, &dest_stat) < 0) {
65 if (errno != ENOENT) {
66 perror_msg("unable to stat `%s'", dest);
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");
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");
85 if (unlink(dest) < 0) {
86 perror_msg("cannot remove `%s'", dest);
91 if (copy_file(source, dest,
92 CP_RECUR | CP_PRESERVE_STATUS | CP_PRESERVE_SYMLINKS) < 0)
95 if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file,
96 remove_directory, NULL))
102 static int move_file(const char *source, const char *dest)
104 struct stat dest_stat;
107 if (stat(dest, &dest_stat) < 0) {
108 if (errno != ENOENT) {
109 perror_msg("unable to stat `%s'", dest);
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())
123 if (rename(source, dest) < 0) {
125 return manual_rename(source, dest);
127 perror_msg("unable to rename `%s'", source);
134 extern int mv_main(int argc, char **argv)
140 while ((opt = getopt(argc, argv, "fi")) != -1)
143 flags &= ~CP_INTERACTIVE;
148 flags |= CP_INTERACTIVE;
154 if (optind + 2 > argc)
157 if (optind + 2 == argc) {
158 struct stat dest_stat;
161 if (stat(argv[optind + 1], &dest_stat) < 0) {
163 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
167 if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) {
168 if (move_file(argv[optind], argv[optind + 1]) < 0)
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)