Re: Order of files with 'cp'

From: Mario Hoerich <lists_at_MHoerich.de>
Date: Fri, 18 Nov 2005 15:50:53 +0100
# 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
_at__at_ -153,6 +153,8 _at__at_
 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
_at__at_ -83,7 +83,7 _at__at_
 
 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;
 
_at__at_ -102,7 +102,7 _at__at_
 	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;
_at__at_ -131,6 +131,9 _at__at_
 			nflag = 1;
 			fflag = iflag = 0;
 			break;
+		case 'o':
+			oflag = 1;
+			break;
 		case 'p':
 			pflag = 1;
 			break;
_at__at_ -270,7 +273,12 _at__at_
 	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
_at__at_ -331,8 +331,8 _at__at_
 {
 
 	(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
Received on Fri Nov 18 2005 - 13:50:57 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:47 UTC