Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Sep 1998 16:04:11 +0930 (CST)
From:      "Daniel O'Connor" <doconnor@gsoft.com.au>
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Subject:   bin/7828: Add a command line option to cp to make it never overwrite
Message-ID:  <199809040634.QAA09475@cain.gsoft.com.au>

next in thread | raw e-mail | index | archive | help

>Number:         7828
>Category:       bin
>Synopsis:       Add a command line option to cp to make it never overwrite
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Sep  3 23:40:01 PDT 1998
>Last-Modified:
>Originator:     Daniel O'Connor
>Organization:
>Release:        FreeBSD 2.2.6-BETA i386
>Environment:

Standard 2.2.6 system

>Description:

This patch adds the '-n' flag to cp which means "don't ever overwrite a file" it 
is basically copied from the -i stuff when you hit 'n'.
It probably has bugs I only took 5 minutes to write it :)

>How-To-Repeat:


>Fix:
	
Here is the patch

diff -ur cp.old/cp.c cp/cp.c
--- cp.old/cp.c	Wed Jul 15 11:31:06 1998
+++ cp/cp.c	Fri Sep  4 15:18:09 1998
@@ -86,7 +86,7 @@
 PATH_T to = { to.p_path, "" };
 
 uid_t myuid;
-int Rflag, iflag, pflag, rflag, fflag;
+int Rflag, iflag, pflag, rflag, fflag, nflag;
 int myumask;
 
 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
@@ -105,7 +105,7 @@
 	char *target;
 
 	Hflag = Lflag = Pflag = 0;
-	while ((ch = getopt(argc, argv, "HLPRfipr")) != -1)
+	while ((ch = getopt(argc, argv, "HLPRfiprn")) != -1)
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -124,10 +124,12 @@
 			break;
 		case 'f':
 			fflag = 1;
+			nflag = 0;
 			iflag = 0;
 			break;
 		case 'i':
 			iflag = 1;
+			nflag = 0;
 			fflag = 0;
 			break;
 		case 'p':
@@ -136,6 +138,11 @@
 		case 'r':
 			rflag = 1;
 			break;
+		case 'n':
+		       nflag = 1;
+		       iflag = 0;
+		       fflag = 0;
+		       break;
 		default:
 			usage();
 			break;
Only in cp: cp.o
diff -ur cp.old/extern.h cp/extern.h
--- cp.old/extern.h	Fri Mar  8 17:28:07 1996
+++ cp/extern.h	Fri Sep  4 15:21:36 1998
@@ -42,7 +42,7 @@
 
 extern PATH_T to;
 extern uid_t myuid;
-extern int iflag, pflag, fflag, myumask;
+extern int iflag, pflag, fflag, nflag, myumask;
 
 #include <sys/cdefs.h>
 
diff -ur cp.old/utils.c cp/utils.c
--- cp.old/utils.c	Wed Jul 15 11:31:06 1998
+++ cp/utils.c	Fri Sep  4 15:25:45 1998
@@ -96,6 +96,12 @@
 			}
 		}
 		
+		if (nflag) {	
+		    (void)close(from_fd);
+		    (void)fprintf(stderr, "not overwritten\n");
+		    return (0);
+		}
+		
 		if (fflag) {
 		    /* remove existing destination file name, 
 		     * create a new file  */
@@ -319,7 +325,7 @@
 usage()
 {
 	(void)fprintf(stderr, "%s\n%s\n",
-"usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target",
-"       cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory");
+"usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-p] src target",
+"       cp [-R [-H | -L | -P]] [-f | -i | -n] [-p] src1 ... srcN directory");
 	exit(1);
 }
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199809040634.QAA09475>