Commit | Line | Data |
---|---|---|
bcf1365c DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
d53fe8dd | 13 | #ifndef lint |
6521d648 | 14 | static char sccsid[] = "@(#)mv.c 5.5 (Berkeley) %G%"; |
bcf1365c | 15 | #endif not lint |
2a7e674d | 16 | |
3880b4a9 BJ |
17 | /* |
18 | * mv file1 file2 | |
19 | */ | |
d53fe8dd SL |
20 | #include <sys/param.h> |
21 | #include <sys/stat.h> | |
465786e2 | 22 | #include <sys/time.h> |
3880b4a9 BJ |
23 | |
24 | #include <stdio.h> | |
98288a4a | 25 | #include <sys/dir.h> |
d53fe8dd | 26 | #include <errno.h> |
3880b4a9 BJ |
27 | #include <signal.h> |
28 | ||
3880b4a9 | 29 | #define DELIM '/' |
3880b4a9 | 30 | #define MODEBITS 07777 |
3880b4a9 | 31 | |
d53fe8dd SL |
32 | #define ISDIR(st) (((st).st_mode&S_IFMT) == S_IFDIR) |
33 | #define ISLNK(st) (((st).st_mode&S_IFMT) == S_IFLNK) | |
34 | #define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG) | |
35 | #define ISDEV(st) \ | |
36 | (((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK) | |
37 | ||
3880b4a9 BJ |
38 | char *dname(); |
39 | struct stat s1, s2; | |
d53fe8dd SL |
40 | int iflag = 0; /* interactive mode */ |
41 | int fflag = 0; /* force overwriting */ | |
42 | extern unsigned errno; | |
3880b4a9 BJ |
43 | |
44 | main(argc, argv) | |
d53fe8dd | 45 | register char *argv[]; |
3880b4a9 BJ |
46 | { |
47 | register i, r; | |
ca02483b | 48 | register char *arg; |
29ba52aa | 49 | char *dest; |
3880b4a9 | 50 | |
3880b4a9 BJ |
51 | if (argc < 2) |
52 | goto usage; | |
d53fe8dd | 53 | while (argc > 1 && *argv[1] == '-') { |
3880b4a9 | 54 | argc--; |
ca02483b BJ |
55 | arg = *++argv; |
56 | ||
57 | /* | |
d53fe8dd SL |
58 | * all files following a null option |
59 | * are considered file names | |
ca02483b | 60 | */ |
d53fe8dd SL |
61 | if (*(arg+1) == '\0') |
62 | break; | |
63 | while (*++arg != '\0') switch (*arg) { | |
3880b4a9 | 64 | |
d53fe8dd SL |
65 | case 'i': |
66 | iflag++; | |
67 | break; | |
3880b4a9 | 68 | |
d53fe8dd SL |
69 | case 'f': |
70 | fflag++; | |
71 | break; | |
3880b4a9 | 72 | |
d53fe8dd SL |
73 | default: |
74 | goto usage; | |
75 | } | |
3880b4a9 BJ |
76 | } |
77 | if (argc < 3) | |
78 | goto usage; | |
29ba52aa | 79 | dest = argv[argc-1]; |
a7d0170a | 80 | if (stat(dest, &s2) >= 0 && ISDIR(s2)) { |
66a05c49 | 81 | r = 0; |
d53fe8dd SL |
82 | for (i = 1; i < argc-1; i++) |
83 | r |= movewithshortname(argv[i], dest); | |
84 | exit(r); | |
85 | } | |
29ba52aa KM |
86 | if (argc > 3) |
87 | goto usage; | |
88 | r = move(argv[1], argv[2]); | |
d53fe8dd SL |
89 | exit(r); |
90 | /*NOTREACHED*/ | |
3880b4a9 | 91 | usage: |
d53fe8dd | 92 | fprintf(stderr, |
a7d0170a | 93 | "usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n"); |
d53fe8dd SL |
94 | return (1); |
95 | } | |
96 | ||
97 | movewithshortname(src, dest) | |
98 | char *src, *dest; | |
99 | { | |
100 | register char *shortname; | |
101 | char target[MAXPATHLEN + 1]; | |
102 | ||
103 | shortname = dname(src); | |
104 | if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) { | |
105 | error("%s/%s: pathname too long", dest, | |
106 | shortname); | |
107 | return (1); | |
108 | } | |
6521d648 | 109 | (void)sprintf(target, "%s/%s", dest, shortname); |
d53fe8dd | 110 | return (move(src, target)); |
3880b4a9 BJ |
111 | } |
112 | ||
113 | move(source, target) | |
d53fe8dd | 114 | char *source, *target; |
3880b4a9 | 115 | { |
b07dfbb8 | 116 | int targetexists; |
3880b4a9 | 117 | |
2a7e674d | 118 | if (lstat(source, &s1) < 0) { |
465786e2 | 119 | Perror2(source, "Cannot access"); |
d53fe8dd | 120 | return (1); |
3880b4a9 | 121 | } |
d53fe8dd SL |
122 | /* |
123 | * First, try to rename source to destination. | |
124 | * The only reason we continue on failure is if | |
125 | * the move is on a nondirectory and not across | |
126 | * file systems. | |
127 | */ | |
b07dfbb8 SL |
128 | targetexists = lstat(target, &s2) >= 0; |
129 | if (targetexists) { | |
d53fe8dd SL |
130 | if (s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino) { |
131 | error("%s and %s are identical", source, target); | |
132 | return (1); | |
3880b4a9 | 133 | } |
465786e2 S |
134 | if (iflag && !fflag && isatty(fileno(stdin)) && |
135 | query("remove %s? ", target) == 0) | |
136 | return (1); | |
d53fe8dd SL |
137 | if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) { |
138 | if (query("override protection %o for %s? ", | |
139 | s2.st_mode & MODEBITS, target) == 0) | |
140 | return (1); | |
141 | } | |
b07dfbb8 SL |
142 | } |
143 | if (rename(source, target) >= 0) | |
144 | return (0); | |
145 | if (errno != EXDEV) { | |
73d60db3 JL |
146 | Perror2(errno == ENOENT && targetexists == 0 ? target : source, |
147 | "rename"); | |
b07dfbb8 SL |
148 | return (1); |
149 | } | |
150 | if (ISDIR(s1)) { | |
151 | error("can't mv directories across file systems"); | |
152 | return (1); | |
153 | } | |
154 | if (targetexists && unlink(target) < 0) { | |
465786e2 | 155 | Perror2(target, "Cannot unlink"); |
b07dfbb8 | 156 | return (1); |
3880b4a9 | 157 | } |
d53fe8dd SL |
158 | /* |
159 | * File can't be renamed, try to recreate the symbolic | |
160 | * link or special device, or copy the file wholesale | |
161 | * between file systems. | |
162 | */ | |
163 | if (ISLNK(s1)) { | |
2a7e674d | 164 | register m; |
465786e2 | 165 | char symln[MAXPATHLEN + 1]; |
2a7e674d | 166 | |
465786e2 S |
167 | m = readlink(source, symln, sizeof (symln) - 1); |
168 | if (m < 0) { | |
d53fe8dd | 169 | Perror(source); |
2a7e674d KM |
170 | return (1); |
171 | } | |
465786e2 S |
172 | symln[m] = '\0'; |
173 | ||
e2b49553 | 174 | (void) umask(~(s1.st_mode & MODEBITS)); |
2a7e674d | 175 | if (symlink(symln, target) < 0) { |
d53fe8dd | 176 | Perror(target); |
2a7e674d KM |
177 | return (1); |
178 | } | |
d53fe8dd SL |
179 | goto cleanup; |
180 | } | |
e2b49553 | 181 | (void) umask(0); |
d53fe8dd | 182 | if (ISDEV(s1)) { |
465786e2 | 183 | struct timeval tv[2]; |
e99dc6b8 | 184 | |
d53fe8dd SL |
185 | if (mknod(target, s1.st_mode, s1.st_rdev) < 0) { |
186 | Perror(target); | |
187 | return (1); | |
188 | } | |
465786e2 S |
189 | |
190 | tv[0].tv_sec = s1.st_atime; | |
191 | tv[0].tv_usec = 0; | |
192 | tv[1].tv_sec = s1.st_mtime; | |
193 | tv[1].tv_usec = 0; | |
194 | (void) utimes(target, tv); | |
d53fe8dd SL |
195 | goto cleanup; |
196 | } | |
197 | if (ISREG(s1)) { | |
465786e2 S |
198 | register int fi, fo, n; |
199 | struct timeval tv[2]; | |
200 | char buf[MAXBSIZE]; | |
d53fe8dd | 201 | |
465786e2 S |
202 | fi = open(source, 0); |
203 | if (fi < 0) { | |
204 | Perror(source); | |
d53fe8dd | 205 | return (1); |
3880b4a9 | 206 | } |
465786e2 S |
207 | |
208 | fo = creat(target, s1.st_mode & MODEBITS); | |
209 | if (fo < 0) { | |
210 | Perror(target); | |
211 | close(fi); | |
d53fe8dd | 212 | return (1); |
465786e2 S |
213 | } |
214 | ||
215 | for (;;) { | |
216 | n = read(fi, buf, sizeof buf); | |
217 | if (n == 0) { | |
218 | break; | |
219 | } else if (n < 0) { | |
220 | Perror2(source, "read"); | |
221 | close(fi); | |
222 | close(fo); | |
223 | return (1); | |
224 | } else if (write(fo, buf, n) != n) { | |
225 | Perror2(target, "write"); | |
226 | close(fi); | |
227 | close(fo); | |
228 | return (1); | |
229 | } | |
230 | } | |
231 | ||
232 | close(fi); | |
233 | close(fo); | |
234 | ||
235 | tv[0].tv_sec = s1.st_atime; | |
236 | tv[0].tv_usec = 0; | |
237 | tv[1].tv_sec = s1.st_mtime; | |
238 | tv[1].tv_usec = 0; | |
239 | (void) utimes(target, tv); | |
d53fe8dd | 240 | goto cleanup; |
3880b4a9 | 241 | } |
d53fe8dd SL |
242 | error("%s: unknown file type %o", source, s1.st_mode); |
243 | return (1); | |
3880b4a9 | 244 | |
d53fe8dd | 245 | cleanup: |
3880b4a9 | 246 | if (unlink(source) < 0) { |
465786e2 | 247 | Perror2(source, "Cannot unlink"); |
d53fe8dd | 248 | return (1); |
3880b4a9 | 249 | } |
d53fe8dd | 250 | return (0); |
3880b4a9 BJ |
251 | } |
252 | ||
d53fe8dd SL |
253 | /*VARARGS*/ |
254 | query(prompt, a1, a2) | |
255 | char *a1; | |
3880b4a9 | 256 | { |
465786e2 | 257 | register int i, c; |
3880b4a9 | 258 | |
d53fe8dd SL |
259 | fprintf(stderr, prompt, a1, a2); |
260 | i = c = getchar(); | |
261 | while (c != '\n' && c != EOF) | |
262 | c = getchar(); | |
263 | return (i == 'y'); | |
3880b4a9 BJ |
264 | } |
265 | ||
266 | char * | |
267 | dname(name) | |
d53fe8dd | 268 | register char *name; |
3880b4a9 BJ |
269 | { |
270 | register char *p; | |
271 | ||
272 | p = name; | |
273 | while (*p) | |
274 | if (*p++ == DELIM && *p) | |
275 | name = p; | |
276 | return name; | |
277 | } | |
278 | ||
d53fe8dd SL |
279 | /*VARARGS*/ |
280 | error(fmt, a1, a2) | |
281 | char *fmt; | |
3880b4a9 | 282 | { |
3880b4a9 | 283 | |
d53fe8dd SL |
284 | fprintf(stderr, "mv: "); |
285 | fprintf(stderr, fmt, a1, a2); | |
286 | fprintf(stderr, "\n"); | |
287 | } | |
3880b4a9 | 288 | |
d53fe8dd SL |
289 | Perror(s) |
290 | char *s; | |
291 | { | |
292 | char buf[MAXPATHLEN + 10]; | |
6521d648 KB |
293 | |
294 | (void)sprintf(buf, "mv: %s", s); | |
d53fe8dd | 295 | perror(buf); |
3880b4a9 BJ |
296 | } |
297 | ||
d53fe8dd SL |
298 | Perror2(s1, s2) |
299 | char *s1, *s2; | |
3880b4a9 | 300 | { |
d53fe8dd SL |
301 | char buf[MAXPATHLEN + 20]; |
302 | ||
6521d648 | 303 | (void)sprintf(buf, "mv: %s: %s", s1, s2); |
d53fe8dd | 304 | perror(buf); |
3880b4a9 | 305 | } |