BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / mv / mv.c
CommitLineData
bcf1365c 1/*
50da9e1a
KB
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
7 *
af359dea
C
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
bcf1365c
DF
35 */
36
37#ifndef lint
38char copyright[] =
50da9e1a 39"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
bcf1365c 40 All rights reserved.\n";
8161a03a 41#endif /* not lint */
bcf1365c 42
d53fe8dd 43#ifndef lint
af359dea 44static char sccsid[] = "@(#)mv.c 5.11 (Berkeley) 4/3/91";
8161a03a 45#endif /* not lint */
2a7e674d 46
d53fe8dd 47#include <sys/param.h>
465786e2 48#include <sys/time.h>
50da9e1a
KB
49#include <sys/wait.h>
50#include <sys/stat.h>
52b78a7d
KB
51#include <fcntl.h>
52#include <errno.h>
53#include <unistd.h>
3880b4a9 54#include <stdio.h>
52b78a7d 55#include <stdlib.h>
50da9e1a
KB
56#include <string.h>
57#include "pathnames.h"
3880b4a9 58
50da9e1a 59int fflg, iflg;
3880b4a9
BJ
60
61main(argc, argv)
50da9e1a
KB
62 int argc;
63 char **argv;
3880b4a9 64{
50da9e1a 65 extern char *optarg;
8161a03a 66 extern int optind;
50da9e1a
KB
67 register int baselen, exitval, len;
68 register char *p, *endp;
52b78a7d 69 struct stat sb;
50da9e1a
KB
70 int ch;
71 char path[MAXPATHLEN + 1];
3880b4a9 72
50da9e1a 73 while (((ch = getopt(argc, argv, "-if")) != EOF))
8161a03a 74 switch((char)ch) {
d53fe8dd 75 case 'i':
52b78a7d 76 iflg = 1;
50da9e1a
KB
77 break;
78 case 'f':
52b78a7d 79 fflg = 1;
d53fe8dd 80 break;
50da9e1a
KB
81 case '-': /* undocumented; for compatibility */
82 goto endarg;
8161a03a 83 case '?':
d53fe8dd 84 default:
8161a03a 85 usage();
d53fe8dd 86 }
50da9e1a
KB
87endarg: argc -= optind;
88 argv += optind;
8161a03a
KB
89
90 if (argc < 2)
91 usage();
d53fe8dd 92
50da9e1a 93 /*
52b78a7d
KB
94 * If the stat on the target fails or the target isn't a directory,
95 * try the move. More than 2 arguments is an error in this case.
50da9e1a 96 */
52b78a7d 97 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
50da9e1a
KB
98 if (argc > 2)
99 usage();
100 exit(do_move(argv[0], argv[1]));
101 }
d53fe8dd 102
52b78a7d 103 /* It's a directory, move each file into it. */
50da9e1a
KB
104 (void)strcpy(path, argv[argc - 1]);
105 baselen = strlen(path);
106 endp = &path[baselen];
107 *endp++ = '/';
108 ++baselen;
109 for (exitval = 0; --argc; ++argv) {
110 if ((p = rindex(*argv, '/')) == NULL)
111 p = *argv;
112 else
113 ++p;
114 if ((baselen + (len = strlen(p))) >= MAXPATHLEN)
115 (void)fprintf(stderr,
116 "mv: %s: destination pathname too long\n", *argv);
117 else {
118 bcopy(p, endp, len + 1);
119 exitval |= do_move(*argv, path);
120 }
d53fe8dd 121 }
50da9e1a 122 exit(exitval);
3880b4a9
BJ
123}
124
50da9e1a
KB
125do_move(from, to)
126 char *from, *to;
3880b4a9 127{
52b78a7d 128 struct stat sb;
50da9e1a 129 int ask, ch;
3880b4a9 130
d53fe8dd 131 /*
52b78a7d 132 * Check access. If interactive and file exists, ask user if it
50da9e1a
KB
133 * should be replaced. Otherwise if file exists but isn't writable
134 * make sure the user wants to clobber it.
d53fe8dd 135 */
50da9e1a
KB
136 if (!fflg && !access(to, F_OK)) {
137 ask = 0;
138 if (iflg) {
139 (void)fprintf(stderr, "overwrite %s? ", to);
140 ask = 1;
141 }
52b78a7d 142 else if (access(to, W_OK) && !stat(to, &sb)) {
50da9e1a 143 (void)fprintf(stderr, "override mode %o on %s? ",
52b78a7d 144 sb.st_mode & 07777, to);
50da9e1a
KB
145 ask = 1;
146 }
147 if (ask) {
148 if ((ch = getchar()) != EOF && ch != '\n')
149 while (getchar() != '\n');
150 if (ch != 'y')
151 return(0);
3880b4a9 152 }
b07dfbb8 153 }
50da9e1a
KB
154 if (!rename(from, to))
155 return(0);
52b78a7d 156
b07dfbb8 157 if (errno != EXDEV) {
50da9e1a
KB
158 (void)fprintf(stderr,
159 "mv: rename %s to %s: %s\n", from, to, strerror(errno));
160 return(1);
3880b4a9 161 }
52b78a7d 162
d53fe8dd 163 /*
52b78a7d
KB
164 * If rename fails, and it's a regular file, do the copy internally;
165 * otherwise, use cp and rm.
d53fe8dd 166 */
52b78a7d
KB
167 if (stat(from, &sb)) {
168 (void)fprintf(stderr, "mv: %s: %s\n", from, strerror(errno));
50da9e1a 169 return(1);
d53fe8dd 170 }
52b78a7d
KB
171 return(S_ISREG(sb.st_mode) ?
172 fastcopy(from, to, &sb) : copy(from, to));
50da9e1a 173}
465786e2 174
50da9e1a
KB
175fastcopy(from, to, sbp)
176 char *from, *to;
177 struct stat *sbp;
178{
179 struct timeval tval[2];
180 static u_int blen;
181 static char *bp;
182 register int nread, from_fd, to_fd;
50da9e1a
KB
183
184 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
52b78a7d 185 error(from);
50da9e1a 186 return(1);
d53fe8dd 187 }
52b78a7d
KB
188 if ((to_fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, sbp->st_mode)) < 0) {
189 error(to);
50da9e1a
KB
190 (void)close(from_fd);
191 return(1);
3880b4a9 192 }
50da9e1a 193 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
52b78a7d 194 error(NULL);
50da9e1a 195 return(1);
3880b4a9 196 }
50da9e1a
KB
197 while ((nread = read(from_fd, bp, blen)) > 0)
198 if (write(to_fd, bp, nread) != nread) {
52b78a7d 199 error(to);
50da9e1a
KB
200 goto err;
201 }
202 if (nread < 0) {
52b78a7d 203 error(from);
50da9e1a
KB
204err: (void)unlink(to);
205 (void)close(from_fd);
206 (void)close(to_fd);
207 return(1);
208 }
209 (void)fchown(to_fd, sbp->st_uid, sbp->st_gid);
210 (void)fchmod(to_fd, sbp->st_mode);
211
212 (void)close(from_fd);
213 (void)close(to_fd);
214
215 tval[0].tv_sec = sbp->st_atime;
216 tval[1].tv_sec = sbp->st_mtime;
217 tval[0].tv_usec = tval[1].tv_usec = 0;
218 (void)utimes(to, tval);
219 (void)unlink(from);
220 return(0);
3880b4a9
BJ
221}
222
50da9e1a
KB
223copy(from, to)
224 char *from, *to;
3880b4a9 225{
50da9e1a 226 int pid, status;
d53fe8dd 227
50da9e1a 228 if (!(pid = vfork())) {
79688ddf 229 execl(_PATH_CP, "mv", "-pr", from, to, NULL);
52b78a7d 230 error(_PATH_CP);
50da9e1a
KB
231 _exit(1);
232 }
233 (void)waitpid(pid, &status, 0);
234 if (!WIFEXITED(status) || WEXITSTATUS(status))
235 return(1);
236 if (!(pid = vfork())) {
79688ddf 237 execl(_PATH_RM, "mv", "-rf", from, NULL);
52b78a7d 238 error(_PATH_RM);
50da9e1a
KB
239 _exit(1);
240 }
241 (void)waitpid(pid, &status, 0);
242 return(!WIFEXITED(status) || WEXITSTATUS(status));
3880b4a9 243}
8161a03a 244
52b78a7d
KB
245error(s)
246 char *s;
247{
248 if (s)
249 (void)fprintf(stderr, "mv: %s: %s\n", s, strerror(errno));
250 else
251 (void)fprintf(stderr, "mv: %s\n", strerror(errno));
252}
253
8161a03a
KB
254usage()
255{
50da9e1a
KB
256 (void)fprintf(stderr,
257"usage: mv [-if] src target;\n or: mv [-if] src1 ... srcN directory\n");
8161a03a
KB
258 exit(1);
259}