redo synopsis line, define exit values
[unix-history] / usr / src / bin / cp / cp.c
CommitLineData
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
22char 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
0493752e 28static char sccsid[] = "@(#)cp.c 5.4 (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.
43 *
4d2ae24a 44 * Instead of handling directory entries in the order they appear on disk,
ae5e5236
KB
45 * copy() does non-directory files before directory files.
46 *
4d2ae24a 47 * There are two reasons to do directories last. The first is efficiency.
ae5e5236
KB
48 * Files tend to be in the same cylinder group as their parent, whereas
49 * directories tend not to be. Copying files all at once reduces seeking.
50 *
51 * Second, deeply nested tree's could use up all the file descriptors if we
52 * didn't close one directory before recursivly starting on the next.
1f978f4c
KM
53 */
54
4d2ae24a 55#include <sys/param.h>
84592a94 56#include <sys/stat.h>
ae5e5236 57#include <sys/file.h>
7afd0a98 58#include <sys/dir.h>
0908a03a 59#include <sys/time.h>
5f89032b 60
4d2ae24a
KB
61#include <stdio.h>
62#include <errno.h>
63#include <strings.h>
ae5e5236
KB
64
65typedef struct {
4d2ae24a
KB
66 char *p_path; /* Pointer to the start of a path. */
67 char *p_end; /* Pointer to NULL at end of path. */
68} path_t;
ae5e5236 69
4d2ae24a
KB
70char *path_append(), *path_basename();
71void path_restore();
ae5e5236 72
0493752e 73int exit_val, symfollow, my_umask;
4d2ae24a
KB
74int interactive_flag, preserve_flag, recursive_flag;
75char *buf; /* I/O; malloc for best alignment. */
76char from_buf[MAXPATHLEN + 1], /* Source path buffer. */
77 to_buf[MAXPATHLEN + 1]; /* Target path buffer. */
78path_t from = {from_buf, from_buf};
79path_t to = {to_buf, to_buf};
84592a94
BJ
80
81main(argc, argv)
4d2ae24a
KB
82 int argc;
83 char **argv;
84592a94 84{
4d2ae24a
KB
85 extern int optind, errno;
86 struct stat to_stat;
87 register int c, r;
88 char *old_to, *malloc();
89
0493752e 90 while ((c = getopt(argc, argv, "Rhipr")) != EOF) {
ae5e5236 91 switch ((char) c) {
0493752e
KB
92 case 'h':
93 symfollow = 1;
94 break;
4d2ae24a
KB
95 case 'i':
96 interactive_flag = isatty(fileno(stdin));
97 break;
98 case 'p':
99 preserve_flag = 1;
100 (void)umask(0);
101 break;
102 case 'r':
103 case 'R':
104 recursive_flag = 1;
105 break;
106 case '?':
107 default:
108 usage();
109 break;
110 }
84592a94 111 }
0493752e
KB
112 argc -= optind;
113 argv += optind;
ae5e5236 114
4d2ae24a
KB
115 if (argc < 2)
116 usage();
ae5e5236 117
4d2ae24a
KB
118 my_umask = umask(0);
119 (void)umask(my_umask);
ae5e5236 120
4d2ae24a
KB
121 buf = (char *)malloc(MAXBSIZE);
122 if (!buf) {
0493752e 123 (void)fprintf(stderr, "cp: out of space.\n");
4d2ae24a 124 exit(1);
ae5e5236
KB
125 }
126
4d2ae24a
KB
127 /* Consume last argument first. */
128 if (!path_set(&to, argv[--argc]))
129 exit(exit_val);
ae5e5236 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) {
149 error(to.p_path);
150 exit(1);
151 }
152 if (r == -1 || (to_stat.st_mode & S_IFMT) != S_IFDIR) {
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 */
168 for (; argc; --argc, ++argv) {
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();
175 path_restore(&to, old_to);
176 }
ae5e5236 177 }
4d2ae24a 178 exit(exit_val);
84592a94
BJ
179}
180
ae5e5236
KB
181/*
182 * Copy file or directory at "from" to "to".
183 */
184copy()
84592a94 185{
4d2ae24a 186 struct stat from_stat, to_stat;
0493752e 187 int new_target_dir, statval;
ae5e5236 188
0493752e
KB
189 statval = symfollow || !recursive_flag ?
190 stat(from.p_path, &from_stat) : lstat(from.p_path, &from_stat);
191 if (statval == -1) {
4d2ae24a
KB
192 error(from.p_path);
193 return;
194 }
0493752e
KB
195
196 /* not an error, but need to remember it happened */
4d2ae24a
KB
197 if (stat(to.p_path, &to_stat) == -1)
198 to_stat.st_ino = -1;
199 else if (to_stat.st_dev == from_stat.st_dev &&
200 to_stat.st_ino == from_stat.st_ino) {
0493752e 201 (void)fprintf(stderr,
03137eae 202 "cp: %s and %s are identical (not copied).\n",
4d2ae24a
KB
203 to.p_path, from.p_path);
204 exit_val = 1;
205 return;
84592a94 206 }
ae5e5236 207
0493752e
KB
208 if ((from_stat.st_mode & S_IFMT) == S_IFLNK) {
209 copy_link(to_stat.st_ino != -1);
210 return;
211 }
212
213 new_target_dir = 0;
4d2ae24a
KB
214 if ((from_stat.st_mode & S_IFMT) != S_IFDIR) {
215 if (!copy_file(from_stat.st_mode))
216 return;
5f89032b 217 }
4d2ae24a
KB
218 else {
219 if (!recursive_flag) {
220 (void)fprintf(stderr,
221 "cp: \"%s\" is a directory (not copied).\n",
222 from.p_path);
223 exit_val = 1;
224 return;
225 }
226 if (to_stat.st_ino == -1) {
227 if (mkdir(to.p_path, 0777) < 0) {
228 error(to.p_path);
229 return;
230 }
231 new_target_dir = 1;
232 }
233 else if ((to_stat.st_mode & S_IFMT) != S_IFDIR) {
234 (void)fprintf(stderr,
03137eae 235 "cp: %s: not a directory.\n",
ae5e5236 236 to.p_path);
4d2ae24a
KB
237 return;
238 }
239 copy_dir();
5f89032b 240 }
4d2ae24a
KB
241 /* Preserve old times/modes if necessary. */
242 if (preserve_flag)
243 (void)chmod(to.p_path, (int) from_stat.st_mode);
244 else if (new_target_dir)
245 (void)chmod(to.p_path, (int) from_stat.st_mode & ~my_umask);
246 if (preserve_flag || new_target_dir) {
247 static struct timeval tv[2];
248
249 tv[0].tv_sec = from_stat.st_atime;
250 tv[1].tv_sec = from_stat.st_mtime;
251 if (utimes(to.p_path, tv))
252 error(to.p_path);
84592a94 253 }
ae5e5236
KB
254}
255
ae5e5236 256copy_file(mode)
4d2ae24a 257 u_short mode; /* Permissions for new file. */
ae5e5236 258{
03137eae 259 int from_fd, to_fd, rcount, wcount;
4d2ae24a
KB
260
261 from_fd = open(from.p_path, O_RDONLY, 0);
262 if (from_fd == -1) {
263 error(from.p_path);
264 (void)close(from_fd);
265 return(0);
266 }
267
268 /*
269 * In the interactive case, use O_EXCL to notice existing files. If
270 * the file exists, verify with the user.
271 */
272 to_fd = open(to.p_path,
0493752e
KB
273 (interactive_flag ? O_EXCL : 0) | O_WRONLY | O_CREAT | O_TRUNC,
274 mode);
ae5e5236 275
4d2ae24a 276 if (to_fd == -1 && errno == EEXIST && interactive_flag) {
03137eae
KB
277 int checkch, ch;
278
279 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
280 checkch = ch = getchar();
281 while (ch != '\n' && ch != EOF)
282 ch = getchar();
283 if (checkch != 'y')
4d2ae24a 284 return(0);
03137eae 285 /* try again. */
4d2ae24a 286 to_fd = open(to.p_path, O_WRONLY | O_CREAT | O_TRUNC, mode);
5f89032b 287 }
ae5e5236 288
4d2ae24a
KB
289 if (to_fd == -1) {
290 error(to.p_path);
291 (void)close(from_fd);
292 return(0);
ae5e5236
KB
293 }
294
4d2ae24a
KB
295 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
296 wcount = write(to_fd, buf, rcount);
297 if (rcount != wcount || wcount == -1) {
298 error(from.p_path);
299 break;
300 }
ae5e5236 301 }
4d2ae24a
KB
302 (void)close(from_fd);
303 (void)close(to_fd);
304 return(1);
305}
ae5e5236 306
4d2ae24a
KB
307copy_dir()
308{
309 struct stat from_stat;
310 char *old_from, *old_to;
311 struct direct *dp, **dir_list;
312 int dir_cnt, i;
313
314 dir_cnt = scandir(from.p_path, &dir_list, NULL, NULL);
315 if (dir_cnt == -1) {
03137eae 316 (void)fprintf(stderr, "cp: can't read directory %s.\n",
4d2ae24a
KB
317 from.p_path);
318 exit_val = 1;
ae5e5236
KB
319 }
320
4d2ae24a
KB
321 /* Copy files first. */
322 for (i = 0; i < dir_cnt; ++i) {
323 dp = dir_list[i];
324 if (dp->d_namlen <= 2 && dp->d_name[0] == '.'
325 && (dp->d_name[1] == NULL || dp->d_name[1] == '.')) {
326 (void)free((char *)dp);
327 dir_list[i] = NULL;
328 continue;
329 }
330 old_from = path_append(&from, dp->d_name, (int)dp->d_namlen);
331 if (!old_from) {
332 dir_list[i] = NULL;
333 (void)free((char *)dp);
334 continue;
335 }
336
337 if (stat(from.p_path, &from_stat) < 0) {
338 error(dp->d_name);
339 path_restore(&from, old_from);
340 continue;
341 }
342
343 if ((from_stat.st_mode & S_IFMT) != S_IFDIR) {
344 old_to = path_append(&to, dp->d_name,
345 (int)dp->d_namlen);
346 if (!old_to) {
347 dir_list[i] = NULL;
348 (void)free((char *)dp);
349 continue;
350 }
351 copy();
352 path_restore(&to, old_to);
353 dir_list[i] = NULL;
354 (void)free((char *)dp);
355 }
356 path_restore(&from, old_from);
5f89032b 357 }
ae5e5236 358
4d2ae24a
KB
359 /* Then copy directories. */
360 for (i = 0; i < dir_cnt; ++i) {
361 dp = dir_list[i];
362 if (!dp)
363 continue;
364 old_from = path_append(&from, dp->d_name, (int) dp->d_namlen);
365 if (!old_from) {
366 (void)free((char *)dp);
367 continue;
368 }
369 old_to = path_append(&to, dp->d_name, (int) dp->d_namlen);
370 if (!old_to) {
371 (void)free((char *)dp);
372 path_restore(&from, old_from);
373 continue;
374 }
375 copy();
376 free((char *)dp);
377 path_restore(&from, old_from);
378 path_restore(&to, old_to);
5f89032b 379 }
4d2ae24a 380 free((char *)dir_list);
5f89032b 381}
1a4b831f 382
0493752e
KB
383copy_link(exists)
384 int exists;
385{
386 char link[MAXPATHLEN];
387
388 if (readlink(from.p_path, link, sizeof(link)) == -1) {
389 error(from.p_path);
390 return;
391 }
392 if (exists && unlink(to.p_path)) {
393 error(to.p_path);
394 return;
395 }
396 if (symlink(link, to.p_path)) {
397 error(link);
398 return;
399 }
400}
401
ae5e5236 402error(s)
4d2ae24a 403 char *s;
0908a03a 404{
4d2ae24a 405 extern int errno;
ae5e5236 406
4d2ae24a
KB
407 exit_val = 1;
408 (void)fprintf(stderr, "cp: %s: %s\n", s, strerror(errno));
409}
ae5e5236
KB
410
411/********************************************************************
412 * Path Manipulation Routines.
413 ********************************************************************/
414
415/*
416 * These functions manipulate paths in "path_t" structures.
417 *
418 * They eliminate multiple slashes in paths when they notice them, and keep
419 * the path non-slash terminated.
420 *
4d2ae24a 421 * Both path_set() and path_append() return 0 if the requested name
ae5e5236
KB
422 * would be too long.
423 */
424
4d2ae24a
KB
425#define STRIP_TRAILING_SLASH(p) { \
426 while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \
427 *--(p)->p_end = 0; \
428 }
ae5e5236
KB
429
430/*
431 * Move specified string into path. Convert "" to "." to handle BSD
432 * semantics for a null path. Strip trailing slashes.
433 */
434path_set(p, string)
4d2ae24a
KB
435 register path_t *p;
436 char *string;
ae5e5236 437{
4d2ae24a 438 if (strlen(string) > MAXPATHLEN) {
03137eae 439 fprintf(stderr, "cp: %s: name too long.\n", string);
4d2ae24a
KB
440 exit_val = 1;
441 return(0);
442 }
ae5e5236 443
4d2ae24a
KB
444 (void)strcpy(p->p_path, string);
445 p->p_end = p->p_path + strlen(p->p_path);
ae5e5236 446
4d2ae24a
KB
447 if (p->p_path == p->p_end) {
448 *p->p_end++ = '.';
449 *p->p_end = 0;
450 }
ae5e5236 451
4d2ae24a
KB
452 STRIP_TRAILING_SLASH(p);
453 return(1);
ae5e5236
KB
454}
455
456/*
457 * Append specified string to path, inserting '/' if necessary. Return a
458 * pointer to the old end of path for restoration.
459 */
4d2ae24a 460char *
ae5e5236 461path_append(p, name, len)
4d2ae24a
KB
462 register path_t *p;
463 char *name;
464 int len;
1a4b831f 465{
4d2ae24a 466 char *old;
ae5e5236 467
4d2ae24a
KB
468 old = p->p_end;
469 if (len == -1)
470 len = strlen(name);
ae5e5236 471
4d2ae24a
KB
472 /*
473 * The final "+ 1" accounts for the '/' between old path and name.
474 */
475 if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) {
476 fprintf(stderr,
03137eae 477 "cp: %s/%s: name too long.\n", p->p_path, name);
4d2ae24a
KB
478 exit_val = 1;
479 return(0);
480 }
ae5e5236 481
4d2ae24a
KB
482 /*
483 * This code should always be executed, since paths shouldn't
484 * end in '/'.
485 */
486 if (p->p_end[-1] != '/') {
487 *p->p_end++ = '/';
488 *p->p_end = 0;
489 }
ae5e5236 490
4d2ae24a
KB
491 (void)strncat(p->p_end, name, len);
492 p->p_end += len;
493 *p->p_end = 0;
ae5e5236 494
4d2ae24a
KB
495 STRIP_TRAILING_SLASH(p);
496 return(old);
ae5e5236
KB
497}
498
ae5e5236
KB
499/*
500 * Restore path to previous value. (As returned by path_append.)
501 */
502void
503path_restore(p, old)
4d2ae24a
KB
504 path_t *p;
505 char *old;
ae5e5236 506{
4d2ae24a
KB
507 p->p_end = old;
508 *p->p_end = 0;
ae5e5236
KB
509}
510
ae5e5236
KB
511/*
512 * Return basename of path. (Like basename(1).)
513 */
4d2ae24a 514char *
ae5e5236 515path_basename(p)
4d2ae24a 516 path_t *p;
ae5e5236 517{
4d2ae24a 518 char *basename;
ae5e5236 519
4d2ae24a
KB
520 basename = rindex(p->p_path, '/');
521 if (!basename)
522 basename = p->p_path;
523 return(basename);
524}
1a4b831f 525
4d2ae24a
KB
526usage()
527{
528 (void)fprintf(stderr,
529 "usage: cp [-ip] f1 f2; or: cp [-irp] f1 ... fn directory\n");
530 exit(1);
1a4b831f 531}