aboutsummaryrefslogtreecommitdiffstats
path: root/cp-1-Make-P-work-without-R-as-per-POSIX.patch
blob: 59a48b959247c3d1243b671933d86a007dc92ab0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
From a8829b10000d33dd08471af6f3f1e2b55f4df6f6 Mon Sep 17 00:00:00 2001
From: Cameron Katri <me@cameronkatri.com>
Date: Sun, 19 Sep 2021 20:11:20 -0400
Subject: [PATCH] cp(1): 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
---
 bin/cp/cp.1 |  8 ++++----
 bin/cp/cp.c | 16 ++++++++++------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/bin/cp/cp.1 b/bin/cp/cp.1
index f7e2d639def..c9e62553bc5 100644
--- a/bin/cp/cp.1
+++ b/bin/cp/cp.1
@@ -32,7 +32,7 @@
 .\"	@(#)cp.1	8.3 (Berkeley) 4/18/94
 .\" $FreeBSD$
 .\"
-.Dd June 6, 2015
+.Dd August 24, 2021
 .Dt CP 1
 .Os
 .Sh NAME
@@ -84,10 +84,10 @@ If the
 .Fl R
 option is specified, all symbolic links are followed.
 .It Fl P
-If the
+No symbolic links are followed.
+This is the default if the
 .Fl R
-option is specified, no symbolic links are followed.
-This is the default.
+option is specified.
 .It Fl R
 If
 .Ar source_file
diff --git a/bin/cp/cp.c b/bin/cp/cp.c
index 3a23394df35..d1612003542 100644
--- a/bin/cp/cp.c
+++ b/bin/cp/cp.c
@@ -99,7 +99,7 @@ main(int argc, char *argv[])
 {
 	struct stat to_stat, tmp_stat;
 	enum op type;
-	int Hflag, Lflag, ch, fts_options, r, have_trailing_slash;
+	int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
 	char *target;
 
 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
@@ -108,13 +108,14 @@ main(int argc, char *argv[])
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
-			Lflag = 0;
+			Lflag = Pflag = 0;
 			break;
 		case 'L':
 			Lflag = 1;
-			Hflag = 0;
+			Hflag = Pflag = 0;
 			break;
 		case 'P':
+			Pflag = 1;
 			Hflag = Lflag = 0;
 			break;
 		case 'R':
@@ -123,6 +124,7 @@ main(int argc, char *argv[])
 		case 'a':
 			pflag = 1;
 			Rflag = 1;
+			Pflag = 1;
 			Hflag = Lflag = 0;
 			break;
 		case 'f':
@@ -145,7 +147,7 @@ main(int argc, char *argv[])
 			break;
 		case 'r':
 			rflag = Lflag = 1;
-			Hflag = 0;
+			Hflag = Pflag = 0;
 			break;
 		case 's':
 			sflag = 1;
@@ -180,8 +182,10 @@ main(int argc, char *argv[])
 			fts_options |= FTS_LOGICAL;
 		}
 	} else {
-		fts_options &= ~FTS_PHYSICAL;
-		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
+		if (!Pflag) {
+			fts_options &= ~FTS_PHYSICAL;
+			fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
+		}
 	}
 	(void)signal(SIGINFO, siginfo);
 
-- 
2.32.0