Source-Changes-HG archive

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

[src/trunk]: src/bin/cp Add -l option to copy a tree as links.



details:   https://anonhg.NetBSD.org/src/rev/ddd07d772bb4
branches:  trunk
changeset: 761726:ddd07d772bb4
user:      darcy <darcy%NetBSD.org@localhost>
date:      Sun Feb 06 12:37:48 2011 +0000

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

diffstat:

 bin/cp/cp.1     |  11 +++++++----
 bin/cp/cp.c     |  11 +++++++----
 bin/cp/extern.h |   4 ++--
 bin/cp/utils.c  |  26 +++++++++++++++++++-------
 4 files changed, 35 insertions(+), 17 deletions(-)

diffs (183 lines):

diff -r 1bd007434503 -r ddd07d772bb4 bin/cp/cp.1
--- a/bin/cp/cp.1       Sun Feb 06 12:02:59 2011 +0000
+++ b/bin/cp/cp.1       Sun Feb 06 12:37:48 2011 +0000
@@ -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 ,
diff -r 1bd007434503 -r ddd07d772bb4 bin/cp/cp.c
--- a/bin/cp/cp.c       Sun Feb 06 12:02:59 2011 +0000
+++ b/bin/cp/cp.c       Sun Feb 06 12:37:48 2011 +0000
@@ -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;
diff -r 1bd007434503 -r ddd07d772bb4 bin/cp/extern.h
--- a/bin/cp/extern.h   Sun Feb 06 12:02:59 2011 +0000
+++ b/bin/cp/extern.h   Sun Feb 06 12:37:48 2011 +0000
@@ -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>
diff -r 1bd007434503 -r ddd07d772bb4 bin/cp/utils.c
--- a/bin/cp/utils.c    Sun Feb 06 12:02:59 2011 +0000
+++ b/bin/cp/utils.c    Sun Feb 06 12:37:48 2011 +0000
@@ -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 */



Home | Main Index | Thread Index | Old Index