1 /* vi: set sw=4 ts=4: */
3 * Mini mv implementation for busybox
5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
13 * Size reduction and improved error checking.
16 #include <sys/types.h>
22 #include <getopt.h> /* struct option */
24 #include "libcoreutils/coreutils.h"
26 #if ENABLE_FEATURE_MV_LONG_OPTIONS
27 static const struct option mv_long_options[] = {
28 { "interactive", 0, NULL, 'i' },
29 { "force", 0, NULL, 'f' },
34 #define OPT_FILEUTILS_FORCE 1
35 #define OPT_FILEUTILS_INTERACTIVE 2
37 static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
39 int mv_main(int argc, char **argv);
40 int mv_main(int argc, char **argv)
42 struct stat dest_stat;
50 #if ENABLE_FEATURE_MV_LONG_OPTIONS
51 applet_long_options = mv_long_options;
53 opt_complementary = "f-i:i-f";
54 flags = getopt32(argc, argv, "fi");
55 if (optind + 2 > argc) {
59 last = argv[argc - 1];
62 if (optind + 2 == argc) {
63 dest_exists = cp_mv_stat(last, &dest_stat);
64 if (dest_exists < 0) {
68 if (!(dest_exists & 2)) {
75 dest = concat_path_file(last, bb_get_last_path_component(*argv));
76 dest_exists = cp_mv_stat(dest, &dest_stat);
77 if (dest_exists < 0) {
83 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
84 ((access(dest, W_OK) < 0 && isatty(0)) ||
85 (flags & OPT_FILEUTILS_INTERACTIVE))) {
86 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
87 goto RET_1; /* Ouch! fprintf failed! */
89 if (!bb_ask_confirmation()) {
93 if (rename(*argv, dest) < 0) {
94 struct stat source_stat;
98 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
99 bb_perror_msg("cannot rename '%s'", *argv);
102 if (dest_exists == 3) {
103 if (source_exists != 3) {
104 bb_error_msg(fmt, "", "non-");
108 if (source_exists == 3) {
109 bb_error_msg(fmt, "non-", "");
113 if (unlink(dest) < 0) {
114 bb_perror_msg("cannot remove '%s'", dest);
118 copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
120 copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
122 if ((copy_file(*argv, dest, copy_flag) >= 0) &&
123 (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
134 } while (*++argv != last);