git: 5a52e3d00dd5 - main - cp: Add -N flag, inspired by NetBSD's similar flag

From: Warner Losh <imp_at_FreeBSD.org>
Date: Thu, 07 Dec 2023 20:38:40 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=5a52e3d00dd5e0209f6fcb1e41b5985191e6f4e7

commit 5a52e3d00dd5e0209f6fcb1e41b5985191e6f4e7
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-12-07 19:32:27 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-12-07 20:36:44 +0000

    cp: Add -N flag, inspired by NetBSD's similar flag
    
    Add -N to supress copying of file flags when -p is specified (explicitly
    or implicitly). Often times we don't care about the flags or wish to be
    able to copy to NFS, and this comes in handy for that. FreeBSD's and
    NetBSD's cp are somewhat different, so I had to reimplement all but one
    of the patch hunks...
    
    Obtained from:          NetBSD (cp.1 1.25, cp.c 1.37, utils.c 1.28 by elad)
    Sponsored by:           Netflix
    
    Differential Revision:  https://reviews.freebsd.org/D42673
---
 bin/cp/cp.1     | 14 +++++++++-----
 bin/cp/cp.c     |  7 +++++--
 bin/cp/extern.h |  2 +-
 bin/cp/utils.c  |  2 +-
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/bin/cp/cp.1 b/bin/cp/cp.1
index 0bf28937d7fc..32e6fe295b35 100644
--- a/bin/cp/cp.1
+++ b/bin/cp/cp.1
@@ -29,7 +29,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd February 23, 2022
+.Dd December 7, 2023
 .Dt CP 1
 .Os
 .Sh NAME
@@ -42,7 +42,7 @@
 .Op Fl H | Fl L | Fl P
 .Oc
 .Op Fl f | i | n
-.Op Fl alpsvx
+.Op Fl alNpsvx
 .Ar source_file target_file
 .Nm
 .Oo
@@ -50,15 +50,15 @@
 .Op Fl H | Fl L | Fl P
 .Oc
 .Op Fl f | i | n
-.Op Fl alpsvx
+.Op Fl alNpsvx
 .Ar source_file ... target_directory
 .Nm
 .Op Fl f | i | n
-.Op Fl alPpsvx
+.Op Fl alNPpsvx
 .Ar source_file target_file
 .Nm
 .Op Fl f | i | n
-.Op Fl alPpsvx
+.Op Fl alNPpsvx
 .Ar source_file ... target_directory
 .Sh DESCRIPTION
 In the first synopsis form, the
@@ -88,6 +88,10 @@ option is specified, symbolic links on the command line are followed.
 If the
 .Fl R
 option is specified, all symbolic links are followed.
+.It Fl N
+When used with
+.Fl p ,
+suppress copying file flags.
 .It Fl P
 No symbolic links are followed.
 This is the default if the
diff --git a/bin/cp/cp.c b/bin/cp/cp.c
index 1510943ab5f6..78ded7af3d5a 100644
--- a/bin/cp/cp.c
+++ b/bin/cp/cp.c
@@ -72,7 +72,7 @@ static char emptystring[] = "";
 
 PATH_T to = { to.p_path, emptystring, "" };
 
-int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
+int Nflag, fflag, iflag, lflag, nflag, pflag, sflag, vflag;
 static int Hflag, Lflag, Rflag, rflag;
 volatile sig_atomic_t info;
 
@@ -91,7 +91,7 @@ main(int argc, char *argv[])
 
 	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
 	Pflag = 0;
-	while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
+	while ((ch = getopt(argc, argv, "HLNPRafilnprsvx")) != -1)
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -101,6 +101,9 @@ main(int argc, char *argv[])
 			Lflag = 1;
 			Hflag = Pflag = 0;
 			break;
+		case 'N':
+			Nflag = 1;
+			break;
 		case 'P':
 			Pflag = 1;
 			Hflag = Lflag = 0;
diff --git a/bin/cp/extern.h b/bin/cp/extern.h
index 742b5676f1d7..272454bb5871 100644
--- a/bin/cp/extern.h
+++ b/bin/cp/extern.h
@@ -36,7 +36,7 @@ typedef struct {
 } PATH_T;
 
 extern PATH_T to;
-extern int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
+extern int Nflag, fflag, iflag, lflag, nflag, pflag, sflag, vflag;
 extern volatile sig_atomic_t info;
 
 __BEGIN_DECLS
diff --git a/bin/cp/utils.c b/bin/cp/utils.c
index 2e1a50635a15..686db13ef0cf 100644
--- a/bin/cp/utils.c
+++ b/bin/cp/utils.c
@@ -349,7 +349,7 @@ setfile(struct stat *fs, int fd)
 			rval = 1;
 		}
 
-	if (!gotstat || fs->st_flags != ts.st_flags)
+	if (!Nflag && (!gotstat || fs->st_flags != ts.st_flags))
 		if (fdval ?
 		    fchflags(fd, fs->st_flags) :
 		    (islink ? lchflags(to.p_path, fs->st_flags) :