Hello tech@,
Don't know if this is too much magic numbers for copy_file, but this
"fixes" the case where we print the verbose line, even if we don't copy
it. This doesn't not happen in mv or rm.
Note that the current implementation also doesn't show a successful
copy of a fifo or special if a prior copy failed:
$ touch /tmp/test1
$ mkfifo /tmp/test2
$ chmod 0 /tmp/test1
$ cp -Rv /tmp/test* /tmp/tmp/
cp: /tmp/test1: Permission denied
$ ls /tmp/tmp
test2
$ ./obj/cp -Rv /tmp/test* /tmp/tmp/
cp: /tmp/test1: Permission denied
/tmp/test2 -> /tmp/tmp/test2
OK?
martijn@
Index: cp.c
===================================================================
RCS file: /cvs/src/bin/cp/cp.c,v
retrieving revision 1.46
diff -u -p -r1.46 cp.c
--- cp.c 27 Jun 2017 21:49:47 -0000 1.46
+++ cp.c 30 Aug 2018 18:43:26 -0000
@@ -264,7 +264,7 @@ copy(char *argv[], enum op type, int fts
struct stat to_stat;
FTS *ftsp;
FTSENT *curr;
- int base, nlen, rval;
+ int base, cval, nlen, rval;
char *p, *target_mid;
base = 0;
@@ -434,32 +434,35 @@ copy(char *argv[], enum op type, int fts
!fts_dne(curr)))
rval = 1;
} else
- if (copy_file(curr, fts_dne(curr)))
+ if ((cval = copy_file(curr, fts_dne(curr))) ==
1)
rval = 1;
- if (!rval && vflag)
+ if (!cval && vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
+ cval = 0;
break;
case S_IFIFO:
if (Rflag) {
if (copy_fifo(curr->fts_statp, !fts_dne(curr)))
rval = 1;
} else
- if (copy_file(curr, fts_dne(curr)))
+ if ((cval = copy_file(curr, fts_dne(curr))) ==
1)
rval = 1;
- if (!rval && vflag)
+ if (!cval && 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 (copy_file(curr, fts_dne(curr)))
+ if ((cval = copy_file(curr, fts_dne(curr))) == 1)
rval = 1;
- else if (vflag)
+ if (!cval && vflag)
(void)fprintf(stdout, "%s -> %s\n",
curr->fts_path, to.p_path);
+ cval = 0;
break;
}
}
Index: utils.c
===================================================================
RCS file: /cvs/src/bin/cp/utils.c,v
retrieving revision 1.40
diff -u -p -r1.40 utils.c
--- utils.c 27 Jun 2017 21:43:46 -0000 1.40
+++ utils.c 30 Aug 2018 18:43:26 -0000
@@ -99,7 +99,7 @@ copy_file(FTSENT *entp, int dne)
ch = getchar();
if (checkch != 'y' && checkch != 'Y') {
(void)close(from_fd);
- return (0);
+ return (2);
}
}
to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);