Order of files with 'cp'

Mario Hoerich lists at MHoerich.de
Fri Nov 18 06:50:57 PST 2005


# Ulrich Spoerlein:
> Brian Candler wrote:
> > I've noticed on FreeBSD-5.4 and -6.0 that the order in which 'cp' copies
> > multiple files does not match the order they're given on the command line.
> > This is noticeable when the target server is remote and/or slow (e.g. NFS;
> > USB flash device).
[...] 
> If it can be done without creating too much confusion in the code, I'm
> all for it. Sadly I'm not familiar with the code ...

That's pretty simple, actually. :)

This just adds a -o flag to cp, which preserves order.

diff -ur cp.orig/cp.1 cp/cp.1
--- cp.orig/cp.1	Fri Nov 18 15:48:22 2005
+++ cp/cp.1	Fri Nov 18 15:45:48 2005
@@ -153,6 +153,8 @@
 or
 .Fl i
 options.)
+.It Fl o
+Preserve the order given on the command line.
 .It Fl p
 Cause
 .Nm
diff -ur cp.orig/cp.c cp/cp.c
--- cp.orig/cp.c	Fri Nov 18 15:48:22 2005
+++ cp/cp.c	Fri Nov 18 15:45:45 2005
@@ -83,7 +83,7 @@
 
 PATH_T to = { to.p_path, emptystring, "" };
 
-int fflag, iflag, nflag, pflag, vflag;
+int fflag, iflag, nflag, pflag, vflag, oflag;
 static int Rflag, rflag;
 volatile sig_atomic_t info;
 
@@ -102,7 +102,7 @@
 	char *target;
 
 	Hflag = Lflag = Pflag = 0;
-	while ((ch = getopt(argc, argv, "HLPRfinprv")) != -1)
+	while ((ch = getopt(argc, argv, "HLPRfinoprv")) != -1)
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -131,6 +131,9 @@
 			nflag = 1;
 			fflag = iflag = 0;
 			break;
+		case 'o':
+			oflag = 1;
+			break;
 		case 'p':
 			pflag = 1;
 			break;
@@ -270,7 +273,12 @@
 	mask = ~umask(0777);
 	umask(~mask);
 
-	if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
+	if (oflag == 1)
+		ftsp = fts_open(argv, fts_options, NULL);
+	else
+		ftsp = fts_open(argv, fts_options, mastercmp);
+	
+	if (ftsp == NULL)
 		err(1, "fts_open");
 	for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
 		switch (curr->fts_info) {
diff -ur cp.orig/utils.c cp/utils.c
--- cp.orig/utils.c	Fri Nov 18 15:48:22 2005
+++ cp/utils.c	Fri Nov 18 15:45:40 2005
@@ -331,8 +331,8 @@
 {
 
 	(void)fprintf(stderr, "%s\n%s\n",
-"usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] source_file target_file",
-"       cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] source_file ... "
+"usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-o] [-pv] source_file target_file",
+"       cp [-R [-H | -L | -P]] [-f | -i | -n] [-o] [-pv] source_file ... "
 "target_directory");
 	exit(EX_USAGE);
 }


Regards,
Mario


More information about the freebsd-current mailing list