blob: ae0ee92e49d345773f40300fb44372bb30f03cae [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini mv implementation for busybox
4 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
24 *
25 * Size reduction and improved error checking.
26 */
27
Matt Kraai91b28552001-04-23 18:53:07 +000028#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>
Matt Kraai91b28552001-04-23 18:53:07 +000034#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000035#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000036
Manuel Novoa III cad53642003-03-19 09:13:01 +000037static const char *fmt = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000038
39extern int mv_main(int argc, char **argv)
40{
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 struct stat source_stat;
42 struct stat dest_stat;
43 const char *last;
44 const char *dest;
45 int dest_exists;
46 int source_exists;
Matt Kraai91b28552001-04-23 18:53:07 +000047 int opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 int flags = 0;
49 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000050
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 while ((opt = getopt(argc, argv, "fi")) > 0) {
52 flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
53 if (opt == 'i') {
Matt Kraai01441032001-04-24 01:30:02 +000054 flags |= FILEUTILS_INTERACTIVE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 } else if (opt == 'f') {
56 flags |= FILEUTILS_FORCE;
57 } else {
58 bb_show_usage();
Matt Kraai91b28552001-04-23 18:53:07 +000059 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 }
Matt Kraai91b28552001-04-23 18:53:07 +000061
62 if (optind + 2 > argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_show_usage();
64
65 last = argv[argc - 1];
66 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000067
68 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
70 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000071 }
72
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if (!(dest_exists & 2)) {
74 dest = last;
75 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000076 }
77 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000078
79 do {
80 dest = concat_path_file(last,
81 bb_get_last_path_component(*argv));
Matt Kraai91b28552001-04-23 18:53:07 +000082
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
84 goto RET_1;
85 }
86
87 DO_MOVE:
88
89 if (dest_exists && !(flags & FILEUTILS_FORCE) &&
90 ((access(dest, W_OK) < 0 && isatty(0)) ||
91 (flags & FILEUTILS_INTERACTIVE))) {
92 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
93 goto RET_1; /* Ouch! fprintf failed! */
94 }
95 if (!bb_ask_confirmation())
96 goto RET_0;
97 }
98
99 if (rename(*argv, dest) < 0) {
100 if (errno != EXDEV) {
101 bb_perror_msg("unable to rename `%s'", *argv);
102 } else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
103 if (dest_exists) {
104 if (dest_exists & 2) {
105 if (!(source_exists & 2)) {
106 bb_error_msg(fmt, "", "non-");
107 goto RET_1;
108 }
109 } else {
110 if (source_exists & 2) {
111 bb_error_msg(fmt, "non-", "");
112 goto RET_1;
113 }
114 }
115 if (unlink(dest) < 0) {
116 bb_perror_msg("cannot remove `%s'", dest);
117 goto RET_1;
118 }
119 }
120
121 if ((copy_file(*argv, dest,
122 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0)
123 && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
124 ) {
125 goto RET_0;
126 }
127
128 }
129 RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000130 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 }
Matt Kraai91b28552001-04-23 18:53:07 +0000132
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 RET_0:
134 if (dest != last) {
135 free((void *) dest);
136 }
137
138 } while (*++argv != last);
139
140 exit(status);
Matt Kraai91b28552001-04-23 18:53:07 +0000141}