tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

cp -i might violate POSIX



From src/bin/cp/cp.c:
>	while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1) 
>		[...]
>		case 'i':
>			iflag = isatty(fileno(stdin));
The -i in cp -i is ignored if standard input isn't a tty.

This breaks doing something along the lines of ``yes n | cp -i [...]''
(obviously overwriting files that weren't supposed to be overwritten,
as well was rendering the only way to stop cp from overwriting existing
files ineffective in scripts)

Our man page also doesn't mention this twist.


POSIX says (in the rationale at [1]):
> Although the 4.3 BSD version does not prompt if the standard input is
> not a terminal,
(And in fact the isatty line was not touched since ``initial import of
386bsd-0.1 sources'' in '93)

> the standard developers decided that use of -i is a request for
> interaction, so when the destination path exists, the utility takes
> instructions from whatever responds on standard input.
..which I believe conflicts with what our cp's -i does.

At the bottom of this mail is a patch that drops the ``isatty()''.


Cheers,
Timo Buhrmester

[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html

CC OpenBSD's tech@ because their cp does the same.

-----8<----------------------------------------------------------------

--- bin/cp/cp.c.orig
+++ bin/cp/cp.c
@@ -145,7 +145,7 @@ main(int argc, char *argv[])
 			iflag = 0;
 			break;
 		case 'i':
-			iflag = isatty(fileno(stdin));
+			iflag = 1;
 			fflag = 0;
 			break;
 		case 'l':


Home | Main Index | Thread Index | Old Index