Commit | Line | Data |
---|---|---|
1f978f4c | 1 | /* |
4d2ae24a KB |
2 | * Copyright (c) 1988 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * David Hitz of Auspex Systems Inc. | |
7 | * | |
27c71911 | 8 | * %sccs.include.redist.c% |
4d2ae24a KB |
9 | */ |
10 | ||
11 | #ifndef lint | |
12 | char copyright[] = | |
13 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ | |
14 | All rights reserved.\n"; | |
15 | #endif /* not lint */ | |
16 | ||
17 | #ifndef lint | |
12c865aa | 18 | static char sccsid[] = "@(#)cp.c 5.21 (Berkeley) %G%"; |
4d2ae24a KB |
19 | #endif /* not lint */ |
20 | ||
21 | /* | |
22 | * cp copies source files to target files. | |
ae5e5236 | 23 | * |
9f329525 | 24 | * The global PATH_T structures "to" and "from" always contain paths to the |
4d2ae24a KB |
25 | * current source and target files, respectively. Since cp does not change |
26 | * directories, these paths can be either absolute or dot-realative. | |
ae5e5236 KB |
27 | * |
28 | * The basic algorithm is to initialize "to" and "from", and then call the | |
29 | * recursive copy() function to do the actual work. If "from" is a file, | |
30 | * copy copies the data. If "from" is a directory, copy creates the | |
31 | * corresponding "to" directory, and calls itself recursively on all of | |
32 | * the entries in the "from" directory. | |
1f978f4c KM |
33 | */ |
34 | ||
4d2ae24a | 35 | #include <sys/param.h> |
84592a94 | 36 | #include <sys/stat.h> |
ae5e5236 | 37 | #include <sys/file.h> |
7afd0a98 | 38 | #include <sys/dir.h> |
0908a03a | 39 | #include <sys/time.h> |
4d2ae24a KB |
40 | #include <stdio.h> |
41 | #include <errno.h> | |
335a3e32 | 42 | #include <stdlib.h> |
6ebcb998 | 43 | #include <string.h> |
ae5e5236 | 44 | |
9f329525 KB |
45 | #define type(st) ((st).st_mode & S_IFMT) |
46 | ||
ae5e5236 | 47 | typedef struct { |
9f329525 KB |
48 | char p_path[MAXPATHLEN + 1]; /* pointer to the start of a path. */ |
49 | char *p_end; /* pointer to NULL at end of path. */ | |
50 | } PATH_T; | |
ae5e5236 | 51 | |
9f329525 KB |
52 | PATH_T from = { "", from.p_path }; |
53 | PATH_T to = { "", to.p_path }; | |
a783899b | 54 | |
1a4aed3b KB |
55 | uid_t myuid; |
56 | int exit_val, myumask; | |
3c3c3fc0 | 57 | int iflag, pflag, orflag, rflag; |
d57b5e76 KB |
58 | int (*statfcn)(); |
59 | char *buf, *pname; | |
1a4aed3b KB |
60 | char *path_append(), *path_basename(); |
61 | ||
84592a94 | 62 | main(argc, argv) |
4d2ae24a KB |
63 | int argc; |
64 | char **argv; | |
84592a94 | 65 | { |
4bf6a19a | 66 | extern int optind; |
4d2ae24a KB |
67 | struct stat to_stat; |
68 | register int c, r; | |
335a3e32 KB |
69 | int symfollow, lstat(), stat(); |
70 | char *old_to, *p; | |
d57b5e76 KB |
71 | |
72 | /* | |
73 | * cp is used by mv(1) -- except for usage statements, print | |
74 | * the "called as" program name. | |
75 | */ | |
76 | pname = (p = rindex(*argv,'/')) ? ++p : *argv; | |
4d2ae24a | 77 | |
335a3e32 | 78 | symfollow = 0; |
a783899b KB |
79 | while ((c = getopt(argc, argv, "Rfhipr")) != EOF) { |
80 | switch ((char)c) { | |
81 | case 'f': | |
335a3e32 | 82 | iflag = 0; |
a783899b | 83 | break; |
0493752e KB |
84 | case 'h': |
85 | symfollow = 1; | |
86 | break; | |
4d2ae24a | 87 | case 'i': |
335a3e32 | 88 | iflag = isatty(fileno(stdin)); |
4d2ae24a KB |
89 | break; |
90 | case 'p': | |
335a3e32 | 91 | pflag = 1; |
4d2ae24a | 92 | break; |
4d2ae24a | 93 | case 'R': |
335a3e32 | 94 | rflag = 1; |
4d2ae24a | 95 | break; |
3c3c3fc0 KB |
96 | case 'r': |
97 | orflag = 1; | |
98 | break; | |
4d2ae24a KB |
99 | case '?': |
100 | default: | |
101 | usage(); | |
102 | break; | |
103 | } | |
84592a94 | 104 | } |
0493752e KB |
105 | argc -= optind; |
106 | argv += optind; | |
ae5e5236 | 107 | |
4d2ae24a KB |
108 | if (argc < 2) |
109 | usage(); | |
ae5e5236 | 110 | |
3c3c3fc0 KB |
111 | if (rflag && orflag) { |
112 | (void)fprintf(stderr, | |
113 | "cp: the -R and -r options are mutually exclusive.\n"); | |
114 | exit(1); | |
115 | } | |
116 | ||
4d2ae24a KB |
117 | buf = (char *)malloc(MAXBSIZE); |
118 | if (!buf) { | |
d57b5e76 | 119 | (void)fprintf(stderr, "%s: out of space.\n", pname); |
4d2ae24a | 120 | exit(1); |
ae5e5236 KB |
121 | } |
122 | ||
1a4aed3b KB |
123 | myuid = getuid(); |
124 | ||
125 | /* copy the umask for explicit mode setting */ | |
126 | myumask = umask(0); | |
127 | (void)umask(myumask); | |
128 | ||
24e5a125 | 129 | /* consume last argument first. */ |
4d2ae24a KB |
130 | if (!path_set(&to, argv[--argc])) |
131 | exit(exit_val); | |
ae5e5236 | 132 | |
335a3e32 | 133 | statfcn = symfollow || !rflag ? stat : lstat; |
a68fe064 | 134 | |
ae5e5236 | 135 | /* |
4d2ae24a KB |
136 | * Cp has two distinct cases: |
137 | * | |
138 | * Case (1) $ cp [-rip] source target | |
139 | * | |
140 | * Case (2) $ cp [-rip] source1 ... directory | |
141 | * | |
142 | * In both cases, source can be either a file or a directory. | |
143 | * | |
144 | * In (1), the target becomes a copy of the source. That is, if the | |
145 | * source is a file, the target will be a file, and likewise for | |
146 | * directories. | |
147 | * | |
148 | * In (2), the real target is not directory, but "directory/source". | |
ae5e5236 | 149 | */ |
ae5e5236 | 150 | |
4d2ae24a KB |
151 | r = stat(to.p_path, &to_stat); |
152 | if (r == -1 && errno != ENOENT) { | |
24e5a125 | 153 | error(to.p_path); |
4d2ae24a KB |
154 | exit(1); |
155 | } | |
a783899b | 156 | if (r == -1 || type(to_stat) != S_IFDIR) { |
4d2ae24a KB |
157 | /* |
158 | * Case (1). Target is not a directory. | |
159 | */ | |
160 | if (argc > 1) { | |
161 | usage(); | |
162 | exit(1); | |
163 | } | |
164 | if (!path_set(&from, *argv)) | |
165 | exit(exit_val); | |
166 | copy(); | |
167 | } | |
168 | else { | |
169 | /* | |
170 | * Case (2). Target is a directory. | |
171 | */ | |
24e5a125 | 172 | for (;; ++argv) { |
4d2ae24a KB |
173 | if (!path_set(&from, *argv)) |
174 | continue; | |
175 | old_to = path_append(&to, path_basename(&from), -1); | |
176 | if (!old_to) | |
177 | continue; | |
178 | copy(); | |
24e5a125 KB |
179 | if (!--argc) |
180 | break; | |
4d2ae24a KB |
181 | path_restore(&to, old_to); |
182 | } | |
ae5e5236 | 183 | } |
4d2ae24a | 184 | exit(exit_val); |
84592a94 BJ |
185 | } |
186 | ||
a783899b | 187 | /* copy file or directory at "from" to "to". */ |
ae5e5236 | 188 | copy() |
84592a94 | 189 | { |
4d2ae24a | 190 | struct stat from_stat, to_stat; |
24e5a125 | 191 | int dne, statval; |
ae5e5236 | 192 | |
a68fe064 | 193 | statval = statfcn(from.p_path, &from_stat); |
0493752e | 194 | if (statval == -1) { |
24e5a125 | 195 | error(from.p_path); |
4d2ae24a KB |
196 | return; |
197 | } | |
0493752e KB |
198 | |
199 | /* not an error, but need to remember it happened */ | |
4d2ae24a | 200 | if (stat(to.p_path, &to_stat) == -1) |
24e5a125 KB |
201 | dne = 1; |
202 | else { | |
203 | if (to_stat.st_dev == from_stat.st_dev && | |
204 | to_stat.st_ino == from_stat.st_ino) { | |
205 | (void)fprintf(stderr, | |
d57b5e76 KB |
206 | "%s: %s and %s are identical (not copied).\n", |
207 | pname, to.p_path, from.p_path); | |
24e5a125 KB |
208 | exit_val = 1; |
209 | return; | |
210 | } | |
211 | dne = 0; | |
84592a94 | 212 | } |
ae5e5236 | 213 | |
a783899b KB |
214 | switch(type(from_stat)) { |
215 | case S_IFLNK: | |
24e5a125 | 216 | copy_link(!dne); |
0493752e | 217 | return; |
a783899b | 218 | case S_IFDIR: |
3c3c3fc0 | 219 | if (!rflag && !orflag) { |
4d2ae24a | 220 | (void)fprintf(stderr, |
d57b5e76 KB |
221 | "%s: %s is a directory (not copied).\n", |
222 | pname, from.p_path); | |
4d2ae24a KB |
223 | exit_val = 1; |
224 | return; | |
225 | } | |
24e5a125 KB |
226 | if (dne) { |
227 | /* | |
228 | * If the directory doesn't exist, create the new | |
229 | * one with the from file mode plus owner RWX bits, | |
230 | * modified by the umask. Trade-off between being | |
231 | * able to write the directory (if from directory is | |
232 | * 555) and not causing a permissions race. If the | |
233 | * umask blocks owner writes cp fails. | |
234 | */ | |
235 | if (mkdir(to.p_path, from_stat.st_mode|S_IRWXU) < 0) { | |
236 | error(to.p_path); | |
4d2ae24a KB |
237 | return; |
238 | } | |
4d2ae24a | 239 | } |
a783899b | 240 | else if (type(to_stat) != S_IFDIR) { |
d57b5e76 KB |
241 | (void)fprintf(stderr, "%s: %s: not a directory.\n", |
242 | pname, to.p_path); | |
4d2ae24a KB |
243 | return; |
244 | } | |
245 | copy_dir(); | |
24e5a125 | 246 | /* |
9ac69c84 KB |
247 | * If not -p and directory didn't exist, set it to be the |
248 | * same as the from directory, umodified by the umask; | |
249 | * arguably wrong, but it's been that way forever. | |
24e5a125 | 250 | */ |
335a3e32 | 251 | if (pflag) |
9ac69c84 KB |
252 | setfile(&from_stat, 0); |
253 | else if (dne) | |
24e5a125 | 254 | (void)chmod(to.p_path, from_stat.st_mode); |
3c3c3fc0 | 255 | return; |
a783899b KB |
256 | case S_IFCHR: |
257 | case S_IFBLK: | |
335a3e32 | 258 | if (rflag) { |
3c66b1ab | 259 | copy_special(&from_stat, !dne); |
a783899b KB |
260 | return; |
261 | } | |
3c3c3fc0 KB |
262 | break; |
263 | case S_IFIFO: | |
264 | if (rflag) { | |
265 | copy_fifo(&from_stat, !dne); | |
266 | return; | |
267 | } | |
268 | break; | |
5f89032b | 269 | } |
3c3c3fc0 | 270 | copy_file(&from_stat, dne); |
ae5e5236 KB |
271 | } |
272 | ||
1a4aed3b | 273 | copy_file(fs, dne) |
24e5a125 | 274 | struct stat *fs; |
1a4aed3b | 275 | int dne; |
ae5e5236 | 276 | { |
a783899b | 277 | register int from_fd, to_fd, rcount, wcount; |
1a4aed3b | 278 | struct stat to_stat; |
4d2ae24a | 279 | |
24e5a125 KB |
280 | if ((from_fd = open(from.p_path, O_RDONLY, 0)) == -1) { |
281 | error(from.p_path); | |
282 | return; | |
4d2ae24a KB |
283 | } |
284 | ||
285 | /* | |
1a4aed3b KB |
286 | * If the file exists and we're interactive, verify with the user. |
287 | * If the file DNE, set the mode to be the from file, minus setuid | |
288 | * bits, modified by the umask; arguably wrong, but it makes copying | |
289 | * executables work right and it's been that way forever. (The | |
290 | * other choice is 666 or'ed with the execute bits on the from file | |
291 | * modified by the umask.) | |
4d2ae24a | 292 | */ |
1a4aed3b | 293 | if (!dne) { |
335a3e32 | 294 | if (iflag) { |
1a4aed3b KB |
295 | int checkch, ch; |
296 | ||
297 | (void)fprintf(stderr, "overwrite %s? ", to.p_path); | |
298 | checkch = ch = getchar(); | |
299 | while (ch != '\n' && ch != EOF) | |
300 | ch = getchar(); | |
301 | if (checkch != 'y') { | |
302 | (void)close(from_fd); | |
303 | return; | |
304 | } | |
305 | } | |
306 | to_fd = open(to.p_path, O_WRONLY|O_TRUNC, 0); | |
307 | } else | |
308 | to_fd = open(to.p_path, O_WRONLY|O_CREAT|O_TRUNC, | |
309 | fs->st_mode & ~(S_ISUID|S_ISGID)); | |
ae5e5236 | 310 | |
4d2ae24a | 311 | if (to_fd == -1) { |
24e5a125 | 312 | error(to.p_path); |
4d2ae24a | 313 | (void)close(from_fd); |
24e5a125 | 314 | return; |
ae5e5236 KB |
315 | } |
316 | ||
4d2ae24a KB |
317 | while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { |
318 | wcount = write(to_fd, buf, rcount); | |
319 | if (rcount != wcount || wcount == -1) { | |
24e5a125 | 320 | error(to.p_path); |
4d2ae24a KB |
321 | break; |
322 | } | |
ae5e5236 | 323 | } |
24e5a125 KB |
324 | if (rcount < 0) |
325 | error(from.p_path); | |
335a3e32 | 326 | if (pflag) |
24e5a125 | 327 | setfile(fs, to_fd); |
1a4aed3b | 328 | /* |
3c3c3fc0 KB |
329 | * If the source was setuid or setgid, lose the bits unless the |
330 | * copy is owned by the same user and group. | |
1a4aed3b | 331 | */ |
3c3c3fc0 KB |
332 | else if (fs->st_mode & (S_ISUID|S_ISGID) && fs->st_uid == myuid) |
333 | if (fstat(to_fd, &to_stat)) | |
334 | error(to.p_path); | |
335 | #define RETAINBITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) | |
336 | else if (fs->st_gid == to_stat.st_gid && fchmod(to_fd, | |
337 | fs->st_mode & RETAINBITS & ~myumask)) | |
1a4aed3b | 338 | error(to.p_path); |
4d2ae24a KB |
339 | (void)close(from_fd); |
340 | (void)close(to_fd); | |
4d2ae24a | 341 | } |
ae5e5236 | 342 | |
4d2ae24a KB |
343 | copy_dir() |
344 | { | |
345 | struct stat from_stat; | |
4d2ae24a | 346 | struct direct *dp, **dir_list; |
24e5a125 KB |
347 | register int dir_cnt, i; |
348 | char *old_from, *old_to; | |
4d2ae24a KB |
349 | |
350 | dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL); | |
351 | if (dir_cnt == -1) { | |
d57b5e76 KB |
352 | (void)fprintf(stderr, "%s: can't read directory %s.\n", |
353 | pname, from.p_path); | |
4d2ae24a | 354 | exit_val = 1; |
ae5e5236 KB |
355 | } |
356 | ||
24e5a125 KB |
357 | /* |
358 | * Instead of handling directory entries in the order they appear | |
359 | * on disk, do non-directory files before directory files. | |
360 | * There are two reasons to do directories last. The first is | |
361 | * efficiency. Files tend to be in the same cylinder group as | |
362 | * their parent, whereas directories tend not to be. Copying files | |
363 | * all at once reduces seeking. Second, deeply nested tree's | |
364 | * could use up all the file descriptors if we didn't close one | |
365 | * directory before recursivly starting on the next. | |
366 | */ | |
367 | /* copy files */ | |
4d2ae24a KB |
368 | for (i = 0; i < dir_cnt; ++i) { |
369 | dp = dir_list[i]; | |
370 | if (dp->d_namlen <= 2 && dp->d_name[0] == '.' | |
24e5a125 KB |
371 | && (dp->d_name[1] == NULL || dp->d_name[1] == '.')) |
372 | goto done; | |
4d2ae24a | 373 | old_from = path_append(&from, dp->d_name, (int)dp->d_namlen); |
24e5a125 KB |
374 | if (!old_from) |
375 | goto done; | |
4d2ae24a | 376 | |
a68fe064 | 377 | if (statfcn(from.p_path, &from_stat) < 0) { |
24e5a125 KB |
378 | error(dp->d_name); |
379 | path_restore(&from, old_from); | |
380 | goto done; | |
381 | } | |
382 | if (type(from_stat) == S_IFDIR) { | |
4d2ae24a KB |
383 | path_restore(&from, old_from); |
384 | continue; | |
385 | } | |
24e5a125 KB |
386 | old_to = path_append(&to, dp->d_name, (int)dp->d_namlen); |
387 | if (old_to) { | |
4d2ae24a KB |
388 | copy(); |
389 | path_restore(&to, old_to); | |
4d2ae24a KB |
390 | } |
391 | path_restore(&from, old_from); | |
24e5a125 | 392 | done: dir_list[i] = NULL; |
335a3e32 | 393 | (void)free((void *)dp); |
5f89032b | 394 | } |
ae5e5236 | 395 | |
24e5a125 | 396 | /* copy directories */ |
4d2ae24a KB |
397 | for (i = 0; i < dir_cnt; ++i) { |
398 | dp = dir_list[i]; | |
399 | if (!dp) | |
400 | continue; | |
401 | old_from = path_append(&from, dp->d_name, (int) dp->d_namlen); | |
402 | if (!old_from) { | |
335a3e32 | 403 | (void)free((void *)dp); |
4d2ae24a KB |
404 | continue; |
405 | } | |
406 | old_to = path_append(&to, dp->d_name, (int) dp->d_namlen); | |
407 | if (!old_to) { | |
335a3e32 | 408 | (void)free((void *)dp); |
4d2ae24a KB |
409 | path_restore(&from, old_from); |
410 | continue; | |
411 | } | |
412 | copy(); | |
335a3e32 | 413 | free((void *)dp); |
4d2ae24a KB |
414 | path_restore(&from, old_from); |
415 | path_restore(&to, old_to); | |
5f89032b | 416 | } |
335a3e32 | 417 | free((void *)dir_list); |
5f89032b | 418 | } |
1a4b831f | 419 | |
0493752e KB |
420 | copy_link(exists) |
421 | int exists; | |
422 | { | |
14dc7750 | 423 | int len; |
0493752e KB |
424 | char link[MAXPATHLEN]; |
425 | ||
14dc7750 | 426 | if ((len = readlink(from.p_path, link, sizeof(link))) == -1) { |
24e5a125 | 427 | error(from.p_path); |
0493752e KB |
428 | return; |
429 | } | |
14dc7750 | 430 | link[len] = '\0'; |
0493752e | 431 | if (exists && unlink(to.p_path)) { |
24e5a125 | 432 | error(to.p_path); |
0493752e KB |
433 | return; |
434 | } | |
435 | if (symlink(link, to.p_path)) { | |
24e5a125 | 436 | error(link); |
0493752e KB |
437 | return; |
438 | } | |
439 | } | |
440 | ||
3c3c3fc0 KB |
441 | copy_fifo(from_stat, exists) |
442 | struct stat *from_stat; | |
443 | int exists; | |
444 | { | |
445 | if (exists && unlink(to.p_path)) { | |
446 | error(to.p_path); | |
447 | return; | |
448 | } | |
449 | if (mkfifo(to.p_path, from_stat->st_mode)) { | |
450 | error(to.p_path); | |
451 | return; | |
452 | } | |
453 | if (pflag) | |
454 | setfile(from_stat, 0); | |
455 | } | |
456 | ||
3c66b1ab KB |
457 | copy_special(from_stat, exists) |
458 | struct stat *from_stat; | |
459 | int exists; | |
a783899b | 460 | { |
3c66b1ab | 461 | if (exists && unlink(to.p_path)) { |
24e5a125 | 462 | error(to.p_path); |
a783899b KB |
463 | return; |
464 | } | |
465 | if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) { | |
24e5a125 | 466 | error(to.p_path); |
a783899b KB |
467 | return; |
468 | } | |
3c3c3fc0 KB |
469 | if (pflag) |
470 | setfile(from_stat, 0); | |
a783899b KB |
471 | } |
472 | ||
24e5a125 KB |
473 | setfile(fs, fd) |
474 | register struct stat *fs; | |
475 | int fd; | |
a783899b KB |
476 | { |
477 | static struct timeval tv[2]; | |
12c865aa | 478 | char path[100]; |
a783899b | 479 | |
3ca1480b KB |
480 | fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
481 | ||
a783899b KB |
482 | tv[0].tv_sec = fs->st_atime; |
483 | tv[1].tv_sec = fs->st_mtime; | |
12c865aa KB |
484 | if (utimes(to.p_path, tv)) { |
485 | (void)snprintf(path, sizeof(path), "utimes: %s", to.p_path); | |
486 | error(path); | |
487 | } | |
24e5a125 | 488 | /* |
c942f008 | 489 | * Changing the ownership probably won't succeed, unless we're root |
3ca1480b KB |
490 | * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting |
491 | * the mode; current BSD behavior is to remove all setuid bits on | |
492 | * chown. If chown fails, lose setuid/setgid bits. | |
24e5a125 | 493 | */ |
3ca1480b KB |
494 | if (fd ? fchown(fd, fs->st_uid, fs->st_gid) : |
495 | chown(to.p_path, fs->st_uid, fs->st_gid)) { | |
12c865aa KB |
496 | if (errno != EPERM) { |
497 | (void)snprintf(path, sizeof(path), | |
498 | "chown: %s", to.p_path); | |
499 | error(path); | |
500 | } | |
3ca1480b | 501 | fs->st_mode &= ~(S_ISUID|S_ISGID); |
1a4aed3b | 502 | } |
12c865aa KB |
503 | if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) { |
504 | (void)snprintf(path, sizeof(path), "chown: %s", to.p_path); | |
505 | error(path); | |
506 | } | |
1a4aed3b KB |
507 | } |
508 | ||
509 | ismember(gid) | |
510 | gid_t gid; | |
511 | { | |
1a4aed3b KB |
512 | register int cnt; |
513 | static int ngroups, groups[NGROUPS]; | |
514 | ||
515 | if (!ngroups) { | |
516 | ngroups = getgroups(NGROUPS, groups); | |
517 | if (ngroups == -1) { | |
518 | ngroups = 0; | |
519 | exit_val = 1; | |
d57b5e76 KB |
520 | (void)fprintf(stderr, "%s: %s\n", |
521 | pname, strerror(errno)); | |
1a4aed3b KB |
522 | return(0); |
523 | } | |
24e5a125 | 524 | } |
1a4aed3b KB |
525 | for (cnt = ngroups; cnt--;) |
526 | if (gid == groups[cnt]) | |
527 | return(1); | |
528 | return(0); | |
a783899b KB |
529 | } |
530 | ||
24e5a125 KB |
531 | error(s) |
532 | char *s; | |
0908a03a | 533 | { |
4d2ae24a | 534 | exit_val = 1; |
d57b5e76 | 535 | (void)fprintf(stderr, "%s: %s: %s\n", pname, s, strerror(errno)); |
4d2ae24a | 536 | } |
ae5e5236 KB |
537 | |
538 | /******************************************************************** | |
539 | * Path Manipulation Routines. | |
540 | ********************************************************************/ | |
541 | ||
542 | /* | |
9f329525 | 543 | * These functions manipulate paths in PATH_T structures. |
ae5e5236 KB |
544 | * |
545 | * They eliminate multiple slashes in paths when they notice them, and keep | |
546 | * the path non-slash terminated. | |
547 | * | |
4d2ae24a | 548 | * Both path_set() and path_append() return 0 if the requested name |
ae5e5236 KB |
549 | * would be too long. |
550 | */ | |
551 | ||
4d2ae24a KB |
552 | #define STRIP_TRAILING_SLASH(p) { \ |
553 | while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \ | |
554 | *--(p)->p_end = 0; \ | |
555 | } | |
ae5e5236 KB |
556 | |
557 | /* | |
558 | * Move specified string into path. Convert "" to "." to handle BSD | |
559 | * semantics for a null path. Strip trailing slashes. | |
560 | */ | |
561 | path_set(p, string) | |
9f329525 | 562 | register PATH_T *p; |
4d2ae24a | 563 | char *string; |
ae5e5236 | 564 | { |
4d2ae24a | 565 | if (strlen(string) > MAXPATHLEN) { |
d57b5e76 KB |
566 | (void)fprintf(stderr, "%s: %s: name too long.\n", |
567 | pname, string); | |
4d2ae24a KB |
568 | exit_val = 1; |
569 | return(0); | |
570 | } | |
ae5e5236 | 571 | |
4d2ae24a KB |
572 | (void)strcpy(p->p_path, string); |
573 | p->p_end = p->p_path + strlen(p->p_path); | |
ae5e5236 | 574 | |
4d2ae24a KB |
575 | if (p->p_path == p->p_end) { |
576 | *p->p_end++ = '.'; | |
577 | *p->p_end = 0; | |
578 | } | |
ae5e5236 | 579 | |
4d2ae24a KB |
580 | STRIP_TRAILING_SLASH(p); |
581 | return(1); | |
ae5e5236 KB |
582 | } |
583 | ||
584 | /* | |
585 | * Append specified string to path, inserting '/' if necessary. Return a | |
586 | * pointer to the old end of path for restoration. | |
587 | */ | |
4d2ae24a | 588 | char * |
ae5e5236 | 589 | path_append(p, name, len) |
9f329525 | 590 | register PATH_T *p; |
4d2ae24a KB |
591 | char *name; |
592 | int len; | |
1a4b831f | 593 | { |
4d2ae24a | 594 | char *old; |
ae5e5236 | 595 | |
4d2ae24a KB |
596 | old = p->p_end; |
597 | if (len == -1) | |
598 | len = strlen(name); | |
ae5e5236 | 599 | |
4d2ae24a KB |
600 | /* |
601 | * The final "+ 1" accounts for the '/' between old path and name. | |
602 | */ | |
603 | if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) { | |
24e5a125 | 604 | (void)fprintf(stderr, |
d57b5e76 | 605 | "%s: %s/%s: name too long.\n", pname, p->p_path, name); |
4d2ae24a KB |
606 | exit_val = 1; |
607 | return(0); | |
608 | } | |
ae5e5236 | 609 | |
4d2ae24a KB |
610 | /* |
611 | * This code should always be executed, since paths shouldn't | |
612 | * end in '/'. | |
613 | */ | |
614 | if (p->p_end[-1] != '/') { | |
615 | *p->p_end++ = '/'; | |
616 | *p->p_end = 0; | |
617 | } | |
ae5e5236 | 618 | |
4d2ae24a KB |
619 | (void)strncat(p->p_end, name, len); |
620 | p->p_end += len; | |
621 | *p->p_end = 0; | |
ae5e5236 | 622 | |
4d2ae24a KB |
623 | STRIP_TRAILING_SLASH(p); |
624 | return(old); | |
ae5e5236 KB |
625 | } |
626 | ||
ae5e5236 KB |
627 | /* |
628 | * Restore path to previous value. (As returned by path_append.) | |
629 | */ | |
ae5e5236 | 630 | path_restore(p, old) |
9f329525 | 631 | PATH_T *p; |
4d2ae24a | 632 | char *old; |
ae5e5236 | 633 | { |
4d2ae24a KB |
634 | p->p_end = old; |
635 | *p->p_end = 0; | |
ae5e5236 KB |
636 | } |
637 | ||
ae5e5236 KB |
638 | /* |
639 | * Return basename of path. (Like basename(1).) | |
640 | */ | |
4d2ae24a | 641 | char * |
ae5e5236 | 642 | path_basename(p) |
9f329525 | 643 | PATH_T *p; |
ae5e5236 | 644 | { |
4d2ae24a | 645 | char *basename; |
ae5e5236 | 646 | |
4d2ae24a | 647 | basename = rindex(p->p_path, '/'); |
24e5a125 | 648 | return(basename ? ++basename : p->p_path); |
4d2ae24a | 649 | } |
1a4b831f | 650 | |
4d2ae24a KB |
651 | usage() |
652 | { | |
653 | (void)fprintf(stderr, | |
3c3c3fc0 | 654 | "usage: cp [-Rfhip] src target;\n or: cp [-Rfhip] src1 ... srcN directory\n"); |
4d2ae24a | 655 | exit(1); |
1a4b831f | 656 | } |