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