static char *sccsid
= "@(#)cp.c 4.11 (Berkeley) %G%";
while (argc
> 0 && **argv
== '-') {
while (**argv
) switch (*(*argv
)++) {
case 'p': /* preserve mtimes, atimes, and modes */
if (stat(argv
[argc
-1], &stb
) < 0)
if ((stb
.st_mode
&S_IFMT
) != S_IFDIR
)
for (i
= 0; i
< argc
-1; i
++)
rc
|= copy(argv
[i
], argv
[argc
-1]);
"Usage: cp [-ip] f1 f2; or: cp [-irp] f1 ... fn d2\n");
int fold
, fnew
, n
, exists
;
char *last
, destname
[MAXPATHLEN
+ 1], buf
[MAXBSIZE
];
struct stat stfrom
, stto
;
if (fstat(fold
, &stfrom
) < 0) {
if (stat(to
, &stto
) >= 0 &&
(stto
.st_mode
&S_IFMT
) == S_IFDIR
) {
last
= rindex(from
, '/');
if (last
) last
++; else last
= from
;
if (strlen(to
) + strlen(last
) >= sizeof destname
- 1) {
fprintf(stderr
, "cp: %s/%s: Name too long", to
, last
);
(void) sprintf(destname
, "%s/%s", to
, last
);
if (rflag
&& (stfrom
.st_mode
&S_IFMT
) == S_IFDIR
) {
if (stat(to
, &stto
) < 0) {
if (mkdir(to
, stfrom
.st_mode
& 07777) < 0) {
} else if ((stto
.st_mode
&S_IFMT
) != S_IFDIR
) {
fprintf(stderr
, "cp: %s: Not a directory.\n", to
);
(void) chmod(to
, stfrom
.st_mode
& 07777);
return (rcopy(from
, to
));
if ((stfrom
.st_mode
&S_IFMT
) == S_IFDIR
)
"cp: %s: Is a directory (copying as plain file).\n",
exists
= stat(to
, &stto
) == 0;
if (stfrom
.st_dev
== stto
.st_dev
&&
stfrom
.st_ino
== stto
.st_ino
) {
"cp: %s and %s are identical (not copied).\n",
if (iflag
&& isatty(fileno(stdin
))) {
fprintf (stderr
, "overwrite %s? ", to
);
while (c
!= '\n' && c
!= EOF
)
fnew
= creat(to
, stfrom
.st_mode
& 07777);
(void) close(fold
); return(1);
(void) fchmod(fnew
, stfrom
.st_mode
& 07777);
n
= read(fold
, buf
, sizeof buf
);
(void) close(fold
); (void) close(fnew
); return (1);
if (write(fnew
, buf
, n
) != n
) {
(void) close(fold
); (void) close(fnew
); return (1);
(void) close(fold
); (void) close(fnew
);
return (setimes(to
, &stfrom
));
DIR *fold
= opendir(from
);
char fromname
[MAXPATHLEN
+ 1];
if (fold
== 0 || (pflag
&& fstat(fold
->dd_fd
, &statb
) < 0)) {
return (setimes(to
, &statb
) + errs
);
if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
if (strlen(from
)+1+strlen(dp
->d_name
) >= sizeof fromname
- 1) {
fprintf(stderr
, "cp: %s/%s: Name too long.\n",
(void) sprintf(fromname
, "%s/%s", from
, dp
->d_name
);
errs
+= copy(fromname
, to
);
tv
[0].tv_sec
= statp
->st_atime
;
tv
[1].tv_sec
= statp
->st_mtime
;
tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
if (utimes(path
, tv
) < 0) {