Module Name:    src
Committed By:   darcy
Date:           Sun Feb  6 12:37:49 UTC 2011

Modified Files:
        src/bin/cp: cp.1 cp.c extern.h utils.c

Log Message:
Add -l option to copy a tree as links.
Non-standard option similar to Gnutools cp(1)
Approved by core.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/bin/cp/cp.1
cvs rdiff -u -r1.54 -r1.55 src/bin/cp/cp.c
cvs rdiff -u -r1.15 -r1.16 src/bin/cp/extern.h
cvs rdiff -u -r1.38 -r1.39 src/bin/cp/utils.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/cp/cp.1
diff -u src/bin/cp/cp.1:1.35 src/bin/cp/cp.1:1.36
--- src/bin/cp/cp.1:1.35	Mon Jan 24 07:22:57 2011
+++ src/bin/cp/cp.1	Sun Feb  6 12:37:48 2011
@@ -1,4 +1,4 @@
-.\"	$NetBSD: cp.1,v 1.35 2011/01/24 07:22:57 wiz Exp $
+.\"	$NetBSD: cp.1,v 1.36 2011/02/06 12:37:48 darcy Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	@(#)cp.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd December 21, 2010
+.Dd February 6, 2010
 .Dt CP 1
 .Os
 .Sh NAME
@@ -45,7 +45,7 @@
 .Op Fl H | Fl L | Fl P
 .Oc
 .Op Fl f | i
-.Op Fl aNpv
+.Op Fl alNpv
 .Ar source_file target_file
 .Nm cp
 .Oo
@@ -53,7 +53,7 @@
 .Op Fl H | Fl L | Fl P
 .Oc
 .Op Fl f | i
-.Op Fl aNpv
+.Op Fl alNpv
 .Ar source_file ... target_directory
 .Sh DESCRIPTION
 In the first synopsis form, the
@@ -104,6 +104,9 @@
 If the
 .Fl R
 option is specified, all symbolic links are followed.
+.It Fl l
+Create hard links to regular files in a hierarchy instead of copying.
+The -l option is expected to behave similar to cp(1) in GNU coreutils.
 .It Fl N
 When used with
 .Fl p ,

Index: src/bin/cp/cp.c
diff -u src/bin/cp/cp.c:1.54 src/bin/cp/cp.c:1.55
--- src/bin/cp/cp.c:1.54	Tue Dec 21 20:56:01 2010
+++ src/bin/cp/cp.c	Sun Feb  6 12:37:49 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: cp.c,v 1.54 2010/12/21 20:56:01 christos Exp $ */
+/* $NetBSD: cp.c,v 1.55 2011/02/06 12:37:49 darcy Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)cp.c	8.5 (Berkeley) 4/29/95";
 #else
-__RCSID("$NetBSD: cp.c,v 1.54 2010/12/21 20:56:01 christos Exp $");
+__RCSID("$NetBSD: cp.c,v 1.55 2011/02/06 12:37:49 darcy Exp $");
 #endif
 #endif /* not lint */
 
@@ -86,7 +86,7 @@
 PATH_T to = { .p_end = to.p_path, .target_end = empty  };
 
 uid_t myuid;
-int Hflag, Lflag, Rflag, Pflag, fflag, iflag, pflag, rflag, vflag, Nflag;
+int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag, vflag, Nflag;
 mode_t myumask;
 
 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
@@ -106,7 +106,7 @@
 	(void)setlocale(LC_ALL, "");
 
 	Hflag = Lflag = Pflag = Rflag = 0;
-	while ((ch = getopt(argc, argv, "HLNPRfaiprv")) != -1) 
+	while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1) 
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -140,6 +140,9 @@
 			iflag = isatty(fileno(stdin));
 			fflag = 0;
 			break;
+		case 'l':
+			lflag = 1;
+			break;
 		case 'p':
 			pflag = 1;
 			break;

Index: src/bin/cp/extern.h
diff -u src/bin/cp/extern.h:1.15 src/bin/cp/extern.h:1.16
--- src/bin/cp/extern.h:1.15	Sun Jul 16 16:22:24 2006
+++ src/bin/cp/extern.h	Sun Feb  6 12:37:49 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: extern.h,v 1.15 2006/07/16 16:22:24 jschauma Exp $ */
+/* $NetBSD: extern.h,v 1.16 2011/02/06 12:37:49 darcy Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -42,7 +42,7 @@
 
 extern PATH_T to;
 extern uid_t myuid;
-extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, pflag, Nflag;
+extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, pflag, Nflag;
 extern mode_t myumask;
 
 #include <sys/cdefs.h>

Index: src/bin/cp/utils.c
diff -u src/bin/cp/utils.c:1.38 src/bin/cp/utils.c:1.39
--- src/bin/cp/utils.c:1.38	Tue Jan  4 10:35:10 2011
+++ src/bin/cp/utils.c	Sun Feb  6 12:37:49 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: utils.c,v 1.38 2011/01/04 10:35:10 wiz Exp $ */
+/* $NetBSD: utils.c,v 1.39 2011/02/06 12:37:49 darcy Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
 #else
-__RCSID("$NetBSD: utils.c,v 1.38 2011/01/04 10:35:10 wiz Exp $");
+__RCSID("$NetBSD: utils.c,v 1.39 2011/02/06 12:37:49 darcy Exp $");
 #endif
 #endif /* not lint */
 
@@ -145,11 +145,23 @@
 
 	rval = 0;
 
+	/* if hard linking then simply close the open fds, link and return */
+	if (lflag) {
+		(void)close(from_fd);
+		(void)close(to_fd);
+		(void)unlink(to.p_path);
+		if (link(entp->fts_path, to.p_path)) {
+			warn("%s", to.p_path);
+			return (1);
+		}
+		return (0);
+	}
+	/* NOTREACHED */
+
 	/*
 	 * There's no reason to do anything other than close the file
 	 * now if it's empty, so let's not bother.
 	 */
-
 	if (fs->st_size > 0) {
 		/*
 		 * Mmap and write if less than 8M (the limit is so
@@ -215,8 +227,9 @@
 		}
 	}
 
+	(void)close(from_fd);
+
 	if (rval == 1) {
-		(void)close(from_fd);
 		(void)close(to_fd);
 		return (1);
 	}
@@ -240,7 +253,6 @@
 			rval = 1;
 		}
 	}
-	(void)close(from_fd);
 	if (close(to_fd)) {
 		warn("%s", to.p_path);
 		rval = 1;
@@ -368,8 +380,8 @@
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "usage: %s [-R [-H | -L | -P]] [-f | -i] [-aNpv] src target\n"
-	    "       %s [-R [-H | -L | -P]] [-f | -i] [-aNpv] src1 ... srcN directory\n",
+	    "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
+	    "       %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
 	    getprogname(), getprogname());
 	exit(1);
 	/* NOTREACHED */

Reply via email to