[PATCH] cp: implement -T

Aaro Koskinen aaro.koskinen at iki.fi
Tue Jan 30 22:30:58 UTC 2018


Implement "cp -T". Some Linux kernel Makefiles started using this recently,
so allow also building on systems using busybox cp.

Signed-off-by: Aaro Koskinen <aaro.koskinen at iki.fi>
---
 coreutils/cp.c  | 10 ++++++++--
 include/libbb.h |  7 ++++---
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/coreutils/cp.c b/coreutils/cp.c
index 05c725c..e66a21b 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -48,6 +48,7 @@
 //usage:     "\n	-f	Overwrite"
 //usage:     "\n	-i	Prompt before overwrite"
 //usage:     "\n	-l,-s	Create (sym)links"
+//usage:     "\n	-T	Treat DEST as a normal file"
 //usage:     "\n	-u	Copy only newer files"
 
 #include "libbb.h"
@@ -93,6 +94,7 @@ int cp_main(int argc, char **argv)
 		"no-dereference\0" No_argument "P"
 		"recursive\0"      No_argument "R"
 		"symbolic-link\0"  No_argument "s"
+		"no-target-directory\0" No_argument "T"
 		"verbose\0"        No_argument "v"
 		"update\0"         No_argument "u"
 		"remove-destination\0" No_argument "\xff"
@@ -140,8 +142,6 @@ int cp_main(int argc, char **argv)
 	 *	override the usual backup suffix
 	 * -t, --target-directory=DIRECTORY
 	 *	copy all SOURCE arguments into DIRECTORY
-	 * -T, --no-target-directory
-	 *	treat DEST as a normal file
 	 * -x, --one-file-system
 	 *	stay on this file system
 	 * -Z, --context=CONTEXT
@@ -175,6 +175,9 @@ int cp_main(int argc, char **argv)
 		d_flags = cp_mv_stat(last, &dest_stat);
 		if (d_flags < 0)
 			return EXIT_FAILURE;
+		else if ((flags & FILEUTILS_NO_TARGET_DIR) && !(s_flags & 2) &&
+			 (d_flags & 2))
+			bb_error_msg_and_die("'%s' is a directory", last);
 
 #if ENABLE_FEATURE_CP_LONG_OPTIONS
 		//bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
@@ -193,11 +196,14 @@ int cp_main(int argc, char **argv)
 		if (!((s_flags | d_flags) & 2)
 		    /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
 		 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
+		 || (flags & FILEUTILS_NO_TARGET_DIR)
 		) {
 			/* Do a simple copy */
 			dest = last;
 			goto DO_COPY; /* NB: argc==2 -> *++argv==last */
 		}
+	} else if (flags & FILEUTILS_NO_TARGET_DIR) {
+		bb_error_msg_and_die("too many arguments");
 	}
 
 	while (1) {
diff --git a/include/libbb.h b/include/libbb.h
index 5f25b5d..a938640 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -404,10 +404,11 @@ enum {	/* cp.c, mv.c, install.c depend on these values. CAREFUL when changing th
 	/* -P = -d   (mapped in cp.c) */
 	FILEUTILS_VERBOSE         = (1 << 12) * ENABLE_FEATURE_VERBOSE,	/* -v */
 	FILEUTILS_UPDATE          = 1 << 13, /* -u */
+	FILEUTILS_NO_TARGET_DIR	  = 1 << 14, /* -T */
 #if ENABLE_SELINUX
-	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 14, /* -c */
+	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 15, /* -c */
 #endif
-	FILEUTILS_RMDEST          = 1 << (15 - !ENABLE_SELINUX), /* --remove-destination */
+	FILEUTILS_RMDEST          = 1 << (16 - !ENABLE_SELINUX), /* --remove-destination */
 	/*
 	 * Hole. cp may have some bits set here,
 	 * they should not affect remove_file()/copy_file()
@@ -417,7 +418,7 @@ enum {	/* cp.c, mv.c, install.c depend on these values. CAREFUL when changing th
 #endif
 	FILEUTILS_IGNORE_CHMOD_ERR = 1 << 31,
 };
-#define FILEUTILS_CP_OPTSTR "pdRfilsLHarPvu" IF_SELINUX("c")
+#define FILEUTILS_CP_OPTSTR "pdRfilsLHarPvuT" IF_SELINUX("c")
 extern int remove_file(const char *path, int flags) FAST_FUNC;
 /* NB: without FILEUTILS_RECUR in flags, it will basically "cat"
  * the source, not copy (unless "source" is a directory).
-- 
2.9.2



More information about the busybox mailing list