-/* $OpenBSD: cp.c,v 1.49 2018/09/07 07:44:15 martijn Exp $ */
+/* $OpenBSD: cp.c,v 1.50 2018/09/07 11:01:22 stsp Exp $ */
/* $NetBSD: cp.c,v 1.14 1995/09/07 06:14:51 jtc Exp $ */
/*
struct stat to_stat;
FTS *ftsp;
FTSENT *curr;
- int base, cval, nlen, rval;
+ int base, nlen, rval;
char *p, *target_mid;
base = 0;
switch (curr->fts_statp->st_mode & S_IFMT) {
case S_IFLNK:
- if ((cval = copy_link(curr, !fts_dne(curr))) == 1)
+ if (copy_link(curr, !fts_dne(curr)))
rval = 1;
- if (!cval && vflag)
+ else if (vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
break;
case S_IFBLK:
case S_IFCHR:
if (Rflag) {
- if ((cval = copy_special(curr->fts_statp,
- !fts_dne(curr))) == 1)
+ if (copy_special(curr->fts_statp,
+ !fts_dne(curr)))
rval = 1;
} else
- if ((cval = copy_file(curr, !fts_dne(curr))) == 1)
+ if (copy_file(curr, fts_dne(curr)))
rval = 1;
- if (!cval && vflag)
+ if (!rval && vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
- cval = 0;
break;
case S_IFIFO:
if (Rflag) {
- if ((cval = copy_fifo(curr->fts_statp,
- !fts_dne(curr))) == 1)
+ if (copy_fifo(curr->fts_statp, !fts_dne(curr)))
rval = 1;
} else
- if ((cval = copy_file(curr, !fts_dne(curr))) == 1)
+ if (copy_file(curr, fts_dne(curr)))
rval = 1;
- if (!cval && vflag)
+ if (!rval && vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
- cval = 0;
break;
case S_IFSOCK:
warnc(EOPNOTSUPP, "%s", curr->fts_path);
break;
default:
- if ((cval = copy_file(curr, !fts_dne(curr))) == 1)
+ if (copy_file(curr, fts_dne(curr)))
rval = 1;
- if (!cval && vflag)
+ else if (vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
- cval = 0;
break;
}
}
-/* $OpenBSD: utils.c,v 1.44 2018/09/07 07:44:15 martijn Exp $ */
+/* $OpenBSD: utils.c,v 1.45 2018/09/07 11:01:22 stsp Exp $ */
/* $NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $ */
/*-
#include "extern.h"
-int copy_overwrite(void);
-
int
-copy_file(FTSENT *entp, int exists)
+copy_file(FTSENT *entp, int dne)
{
static char *buf;
static char *zeroes;
struct stat to_stat, *fs;
- int from_fd, rcount, rval, to_fd, wcount;
+ int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
char *p;
#endif
* In -f (force) mode, we always unlink the destination first
* if it exists. Note that -i and -f are mutually exclusive.
*/
- if (exists && fflag)
+ if (!dne && fflag)
(void)unlink(to.p_path);
/*
+ * If the file exists and we're interactive, verify with the user.
* If the file DNE, set the mode to be the from file, minus setuid
* bits, modified by the umask; arguably wrong, but it makes copying
* executables work right and it's been that way forever. (The
* other choice is 666 or'ed with the execute bits on the from file
* modified by the umask.)
*/
- if (exists && !fflag) {
- if (!copy_overwrite()) {
- (void)close(from_fd);
- return 2;
- }
+ if (!dne && !fflag) {
+ if (iflag) {
+ (void)fprintf(stderr, "overwrite %s? ", to.p_path);
+ checkch = ch = getchar();
+ while (ch != '\n' && ch != EOF)
+ ch = getchar();
+ if (checkch != 'y' && checkch != 'Y') {
+ (void)close(from_fd);
+ return (0);
+ }
+ }
to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
} else
to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
*/
#define RETAINBITS \
(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
- if (!pflag && !exists &&
+ if (!pflag && dne &&
fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
if (fstat(to_fd, &to_stat)) {
warn("%s", to.p_path);
int len;
char name[PATH_MAX];
- if (exists && !copy_overwrite())
- return (2);
if ((len = readlink(p->fts_path, name, sizeof(name)-1)) == -1) {
warn("readlink: %s", p->fts_path);
return (1);
int
copy_fifo(struct stat *from_stat, int exists)
{
- if (exists && !copy_overwrite())
- return (2);
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
int
copy_special(struct stat *from_stat, int exists)
{
- if (exists && !copy_overwrite())
- return (2);
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
return (pflag ? setfile(from_stat, -1) : 0);
}
-/*
- * If the file exists and we're interactive, verify with the user.
- */
-int
-copy_overwrite(void)
-{
- int ch, checkch;
-
- if (iflag) {
- (void)fprintf(stderr, "overwrite %s? ", to.p_path);
- checkch = ch = getchar();
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (checkch != 'y' && checkch != 'Y')
- return (0);
- }
- return 1;
-}
int
setfile(struct stat *fs, int fd)