tech-userlevel archive

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

Adding -l option to cp



I find the -l option in FreeBSD extremely usefule and find myself
installing coreutils on NetBSD just to get the GNU cp.  It would be
nice to add this to NetBSD.  Would anyone object if I applied the
following patch?

Index: cp.1
===================================================================
RCS file: /cvsroot/src/bin/cp/cp.1,v
retrieving revision 1.33
diff -u -r1.33 cp.1
--- cp.1        22 Dec 2010 09:02:32 -0000      1.33
+++ cp.1        22 Jan 2011 22:38:21 -0000
@@ -111,6 +111,9 @@
 .It Fl P
 No symbolic links are followed.
 This is the default.
+.It Fl l
+Create hard links to regular files in a hierarchy instead of copying.
+.It Fl n
 .It Fl p
 Causes
 .Nm
Index: cp.c
===================================================================
RCS file: /cvsroot/src/bin/cp/cp.c,v
retrieving revision 1.54
diff -u -r1.54 cp.c
--- cp.c        21 Dec 2010 20:56:01 -0000      1.54
+++ cp.c        22 Jan 2011 22:38:22 -0000
@@ -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: extern.h
===================================================================
RCS file: /cvsroot/src/bin/cp/extern.h,v
retrieving revision 1.15
diff -u -r1.15 extern.h
--- extern.h    16 Jul 2006 16:22:24 -0000      1.15
+++ extern.h    22 Jan 2011 22:38:22 -0000
@@ -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: utils.c
===================================================================
RCS file: /cvsroot/src/bin/cp/utils.c,v
retrieving revision 1.38
diff -u -r1.38 utils.c
--- utils.c     4 Jan 2011 10:35:10 -0000       1.38
+++ utils.c     22 Jan 2011 22:38:22 -0000
@@ -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
@@ -192,6 +204,7 @@
                                        remainder -= chunk;
                                } while (remainder > 0);
 
+                               /* Some systems don't unmap on close(2). */
                                if (munmap(p, fsize) < 0) {
                                        warn("%s", entp->fts_path);
                                        rval = 1;
@@ -215,8 +228,9 @@
                }
        }
 
+       (void)close(from_fd);
+
        if (rval == 1) {
-               (void)close(from_fd);
                (void)close(to_fd);
                return (1);
        }
@@ -240,7 +254,6 @@
                        rval = 1;
                }
        }
-       (void)close(from_fd);
        if (close(to_fd)) {
                warn("%s", to.p_path);
                rval = 1;
@@ -368,8 +381,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] [-aNlpv] src target\n"
+           "       %s [-R [-H | -L | -P]] [-f | -i] [-aNlpv] src1 ... srcN 
directory\n",
            getprogname(), getprogname());
        exit(1);
        /* NOTREACHED */

-- 
D'Arcy J.M. Cain <darcy%NetBSD.org@localhost>
http://www.NetBSD.org/


Home | Main Index | Thread Index | Old Index