blob: 55da2cc6884ce499578d9c2f9e5f873aca6ae75b [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>
Eric Andersen8876fb22003-06-20 09:01:58 +000034#include <getopt.h>
Matt Kraai91b28552001-04-23 18:53:07 +000035#include "busybox.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#include "libcoreutils/coreutils.h"
Matt Kraai91b28552001-04-23 18:53:07 +000037
Eric Andersen8876fb22003-06-20 09:01:58 +000038static const struct option mv_long_options[] = {
39 { "interactive", 0, NULL, 'i' },
40 { "force", 0, NULL, 'f' },
41 { 0, 0, 0, 0 }
42};
43
44static const char mv_getopt_short_option[] = "fi";
45#define OPT_FILEUTILS_FORCE 1
46#define OPT_FILEUTILS_INTERACTIVE 2
47
48static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
Matt Kraai91b28552001-04-23 18:53:07 +000049
50extern int mv_main(int argc, char **argv)
51{
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 struct stat source_stat;
53 struct stat dest_stat;
54 const char *last;
55 const char *dest;
56 int dest_exists;
57 int source_exists;
Eric Andersen8876fb22003-06-20 09:01:58 +000058 unsigned long flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 int status = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000060
Eric Andersen8876fb22003-06-20 09:01:58 +000061 bb_applet_long_options = mv_long_options;
62 bb_opt_complementaly = "f-i:i-f";
63 flags = bb_getopt_ulflags(argc, argv, mv_getopt_short_option);
Matt Kraai91b28552001-04-23 18:53:07 +000064
65 if (optind + 2 > argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_show_usage();
67
68 last = argv[argc - 1];
69 argv += optind;
Matt Kraai91b28552001-04-23 18:53:07 +000070
71 if (optind + 2 == argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
73 return 1;
Matt Kraai91b28552001-04-23 18:53:07 +000074 }
75
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 if (!(dest_exists & 2)) {
77 dest = last;
78 goto DO_MOVE;
Matt Kraai91b28552001-04-23 18:53:07 +000079 }
80 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000081
82 do {
Eric Andersen8876fb22003-06-20 09:01:58 +000083 dest = concat_path_file(last, bb_get_last_path_component(*argv));
Matt Kraai91b28552001-04-23 18:53:07 +000084
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
86 goto RET_1;
87 }
88
89 DO_MOVE:
90
Eric Andersen8876fb22003-06-20 09:01:58 +000091 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 ((access(dest, W_OK) < 0 && isatty(0)) ||
Eric Andersen8876fb22003-06-20 09:01:58 +000093 (flags & OPT_FILEUTILS_INTERACTIVE))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
95 goto RET_1; /* Ouch! fprintf failed! */
96 }
97 if (!bb_ask_confirmation())
98 goto RET_0;
99 }
100
101 if (rename(*argv, dest) < 0) {
102 if (errno != EXDEV) {
103 bb_perror_msg("unable to rename `%s'", *argv);
104 } else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
105 if (dest_exists) {
106 if (dest_exists & 2) {
107 if (!(source_exists & 2)) {
108 bb_error_msg(fmt, "", "non-");
109 goto RET_1;
110 }
111 } else {
112 if (source_exists & 2) {
113 bb_error_msg(fmt, "non-", "");
114 goto RET_1;
115 }
116 }
117 if (unlink(dest) < 0) {
118 bb_perror_msg("cannot remove `%s'", dest);
119 goto RET_1;
120 }
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 ) {
127 goto RET_0;
128 }
129
130 }
131 RET_1:
Matt Kraai91b28552001-04-23 18:53:07 +0000132 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 }
Matt Kraai91b28552001-04-23 18:53:07 +0000134
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 RET_0:
136 if (dest != last) {
137 free((void *) dest);
138 }
139
140 } while (*++argv != last);
141
142 exit(status);
Matt Kraai91b28552001-04-23 18:53:07 +0000143}