cp: Make -P work without -R as per POSIX

According to POSIX, cp should allow the `-P` flag to work whether `-R`
is specified or not.  Currently, the `-P` option only works along with
`-R`.

PR:		199466
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D30012
This commit is contained in:
Cameron Katri 2022-02-23 12:55:13 -06:00 committed by Kyle Evans
parent ab7d095969
commit 97e1303791
3 changed files with 31 additions and 9 deletions

View File

@ -32,7 +32,7 @@
.\" @(#)cp.1 8.3 (Berkeley) 4/18/94 .\" @(#)cp.1 8.3 (Berkeley) 4/18/94
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd June 6, 2015 .Dd February 23, 2022
.Dt CP 1 .Dt CP 1
.Os .Os
.Sh NAME .Sh NAME
@ -55,6 +55,14 @@
.Op Fl f | i | n .Op Fl f | i | n
.Op Fl alpsvx .Op Fl alpsvx
.Ar source_file ... target_directory .Ar source_file ... target_directory
.Nm
.Op Fl f | i | n
.Op Fl alPpsvx
.Ar source_file target_file
.Nm
.Op Fl f | i | n
.Op Fl alPpsvx
.Ar source_file ... target_directory
.Sh DESCRIPTION .Sh DESCRIPTION
In the first synopsis form, the In the first synopsis form, the
.Nm .Nm
@ -84,10 +92,10 @@ If the
.Fl R .Fl R
option is specified, all symbolic links are followed. option is specified, all symbolic links are followed.
.It Fl P .It Fl P
If the No symbolic links are followed.
This is the default if the
.Fl R .Fl R
option is specified, no symbolic links are followed. option is specified.
This is the default.
.It Fl R .It Fl R
If If
.Ar source_file .Ar source_file

View File

@ -100,21 +100,23 @@ main(int argc, char *argv[])
{ {
struct stat to_stat, tmp_stat; struct stat to_stat, tmp_stat;
enum op type; enum op type;
int ch, fts_options, r, have_trailing_slash; int Pflag, ch, fts_options, r, have_trailing_slash;
char *target; char *target;
fts_options = FTS_NOCHDIR | FTS_PHYSICAL; fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
Pflag = 0;
while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1) while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
switch (ch) { switch (ch) {
case 'H': case 'H':
Hflag = 1; Hflag = 1;
Lflag = 0; Lflag = Pflag = 0;
break; break;
case 'L': case 'L':
Lflag = 1; Lflag = 1;
Hflag = 0; Hflag = Pflag = 0;
break; break;
case 'P': case 'P':
Pflag = 1;
Hflag = Lflag = 0; Hflag = Lflag = 0;
break; break;
case 'R': case 'R':
@ -123,6 +125,7 @@ main(int argc, char *argv[])
case 'a': case 'a':
pflag = 1; pflag = 1;
Rflag = 1; Rflag = 1;
Pflag = 1;
Hflag = Lflag = 0; Hflag = Lflag = 0;
break; break;
case 'f': case 'f':
@ -145,7 +148,7 @@ main(int argc, char *argv[])
break; break;
case 'r': case 'r':
rflag = Lflag = 1; rflag = Lflag = 1;
Hflag = 0; Hflag = Pflag = 0;
break; break;
case 's': case 's':
sflag = 1; sflag = 1;
@ -179,7 +182,7 @@ main(int argc, char *argv[])
fts_options &= ~FTS_PHYSICAL; fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL; fts_options |= FTS_LOGICAL;
} }
} else { } else if (!Pflag) {
fts_options &= ~FTS_PHYSICAL; fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL | FTS_COMFOLLOW; fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
} }

View File

@ -190,6 +190,16 @@ recursive_link_Lflag_body()
'(' ! -L foo-mirror/foo/baz ')' '(' ! -L foo-mirror/foo/baz ')'
} }
atf_test_case standalone_Pflag
standalone_Pflag_body()
{
echo "foo" > bar
ln -s bar foo
atf_check cp -P foo baz
atf_check -o inline:'Symbolic Link\n' stat -f %SHT baz
}
atf_init_test_cases() atf_init_test_cases()
{ {
atf_add_test_case basic atf_add_test_case basic
@ -202,4 +212,5 @@ atf_init_test_cases()
atf_add_test_case recursive_link_dflt atf_add_test_case recursive_link_dflt
atf_add_test_case recursive_link_Hflag atf_add_test_case recursive_link_Hflag
atf_add_test_case recursive_link_Lflag atf_add_test_case recursive_link_Lflag
atf_add_test_case standalone_Pflag
} }