tech-userlevel archive

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

cp -n diff



Hello list,

attached is a patch that implements the -n option for cp, which skips existing
files on the target.
This is useful if you want copy two directories and want only the
non-existing/new files to be be copied.
This has also been implemented in FreeBSD as cp -n, in Linux its named
-u for update.
I have followed the FreeBSD implementation.
The patch is easy and straightforward. It is against current.
I would like to commit it.
Please have a look.

Any comments ?

Zafer.
Index: cp.1
===================================================================
RCS file: /cvsroot/src/bin/cp/cp.1,v
retrieving revision 1.38
diff -u -r1.38 cp.1
--- cp.1        2 Apr 2011 08:38:56 -0000       1.38
+++ cp.1        29 Apr 2011 21:18:23 -0000
@@ -44,7 +44,7 @@
 .Fl R
 .Op Fl H | Fl L | Fl P
 .Oc
-.Op Fl f | i
+.Op Fl f | i | n
 .Op Fl alNpv
 .Ar source_file target_file
 .Nm cp
@@ -82,8 +82,10 @@
 For each existing destination pathname, attempt to overwrite it.
 If permissions do not allow copy to succeed, remove it and create a new
 file, without prompting for confirmation.
-(The
+(Any previous
 .Fl i
+or
+.Fl n
 option is ignored if the
 .Fl f
 option is specified.)
@@ -100,6 +102,22 @@
 If the response from the standard input begins with the character
 .Sq Li y ,
 the file copy is attempted.
+(The
+.Fl i
+option overrides any previous
+.Fl f
+or
+.Fl n
+options.)
+.It Fl n
+Do not overwrite an existing file.
+(The
+.Fl n
+option overrides any previous
+.Fl f
+or
+.Fl i
+options.)
 .It Fl L
 If the
 .Fl R
Index: cp.c
===================================================================
RCS file: /cvsroot/src/bin/cp/cp.c,v
retrieving revision 1.55
diff -u -r1.55 cp.c
--- cp.c        6 Feb 2011 12:37:49 -0000       1.55
+++ cp.c        29 Apr 2011 21:18:23 -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, lflag, pflag, rflag, vflag, 
Nflag;
+int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, nflag, 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, "HLNPRfailprv")) != -1) 
+       while ((ch = getopt(argc, argv, "HLNPRfailnprv")) != -1) 
                switch (ch) {
                case 'H':
                        Hflag = 1;
@@ -143,6 +143,10 @@
                case 'l':
                        lflag = 1;
                        break;
+               case 'n':
+                       nflag = 1;
+                       fflag = iflag = 0;
+                       break;
                case 'p':
                        pflag = 1;
                        break;
Index: extern.h
===================================================================
RCS file: /cvsroot/src/bin/cp/extern.h,v
retrieving revision 1.16
diff -u -r1.16 extern.h
--- extern.h    6 Feb 2011 12:37:49 -0000       1.16
+++ extern.h    29 Apr 2011 21:18:23 -0000
@@ -42,7 +42,7 @@
 
 extern PATH_T to;
 extern uid_t myuid;
-extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, pflag, 
Nflag;
+extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, nflag, 
pflag, vflag, Nflag;
 extern mode_t myumask;
 
 #include <sys/cdefs.h>
Index: utils.c
===================================================================
RCS file: /cvsroot/src/bin/cp/utils.c,v
retrieving revision 1.39
diff -u -r1.39 utils.c
--- utils.c     6 Feb 2011 12:37:49 -0000       1.39
+++ utils.c     29 Apr 2011 21:18:23 -0000
@@ -102,7 +102,11 @@
                struct stat sb;
                int sval;
 
-               if (iflag) {
+               if (nflag) {
+                       if (vflag) 
+                               printf("%s not overwritten\n", to.p_path);
+                       return (0);
+               } else if (iflag) {
                        (void)fprintf(stderr, "overwrite %s? ", to.p_path);
                        checkch = ch = getchar();
                        while (ch != '\n' && ch != EOF)
@@ -380,8 +384,8 @@
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
-           "       %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN 
directory\n",
+           "usage: %s [-R [-H | -L | -P]] [-f | -i | -n] [-alNpv] src target\n"
+           "       %s [-R [-H | -L | -P]] [-f | -i | -n] [-alNpv] src1 ... 
srcN directory\n",
            getprogname(), getprogname());
        exit(1);
        /* NOTREACHED */


Home | Main Index | Thread Index | Old Index