- add libbb function str_tolower to convert a string to lowercase.
[oweals/busybox.git] / coreutils / mv.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini mv implementation for busybox
4  *
5  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
12  *
13  * Size reduction and improved error checking.
14  */
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <getopt.h> /* struct option */
23 #include "busybox.h"
24 #include "libcoreutils/coreutils.h"
25
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' },
30         { 0, 0, 0, 0 }
31 };
32 #endif
33
34 #define OPT_FILEUTILS_FORCE       1
35 #define OPT_FILEUTILS_INTERACTIVE 2
36
37 static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
38
39 int mv_main(int argc, char **argv);
40 int mv_main(int argc, char **argv)
41 {
42         struct stat dest_stat;
43         const char *last;
44         const char *dest;
45         unsigned long flags;
46         int dest_exists;
47         int status = 0;
48         int copy_flag = 0;
49
50 #if ENABLE_FEATURE_MV_LONG_OPTIONS
51         applet_long_options = mv_long_options;
52 #endif
53         opt_complementary = "f-i:i-f";
54         flags = getopt32(argc, argv, "fi");
55         if (optind + 2 > argc) {
56                 bb_show_usage();
57         }
58
59         last = argv[argc - 1];
60         argv += optind;
61
62         if (optind + 2 == argc) {
63                 dest_exists = cp_mv_stat(last, &dest_stat);
64                 if (dest_exists < 0) {
65                         return 1;
66                 }
67
68                 if (!(dest_exists & 2)) {
69                         dest = last;
70                         goto DO_MOVE;
71                 }
72         }
73
74         do {
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) {
78                         goto RET_1;
79                 }
80
81 DO_MOVE:
82
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! */
88                         }
89                         if (!bb_ask_confirmation()) {
90                                 goto RET_0;
91                         }
92                 }
93                 if (rename(*argv, dest) < 0) {
94                         struct stat source_stat;
95                         int source_exists;
96
97                         if (errno != EXDEV ||
98                                 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
99                                 bb_perror_msg("cannot rename '%s'", *argv);
100                         } else {
101                                 if (dest_exists) {
102                                         if (dest_exists == 3) {
103                                                 if (source_exists != 3) {
104                                                         bb_error_msg(fmt, "", "non-");
105                                                         goto RET_1;
106                                                 }
107                                         } else {
108                                                 if (source_exists == 3) {
109                                                         bb_error_msg(fmt, "non-", "");
110                                                         goto RET_1;
111                                                 }
112                                         }
113                                         if (unlink(dest) < 0) {
114                                                 bb_perror_msg("cannot remove '%s'", dest);
115                                                 goto RET_1;
116                                         }
117                                 }
118                                 copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
119 #if ENABLE_SELINUX
120                                 copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
121 #endif
122                                 if ((copy_file(*argv, dest, copy_flag) >= 0) &&
123                                         (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
124                                         goto RET_0;
125                                 }
126                         }
127 RET_1:
128                         status = 1;
129                 }
130 RET_0:
131                 if (dest != last) {
132                         free((void *) dest);
133                 }
134         } while (*++argv != last);
135
136         return status;
137 }