[PATCH 08/11] cp: Add recursive copy

Sascha Hauer s.hauer at pengutronix.de
Thu Nov 6 04:59:35 PST 2014


This adds the -r option to the cp command.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/cp.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/commands/cp.c b/commands/cp.c
index af7a3d4..4f1c068 100644
--- a/commands/cp.c
+++ b/commands/cp.c
@@ -39,14 +39,17 @@ static int do_cp(int argc, char *argv[])
 	int last_is_dir = 0;
 	int i;
 	int opt;
-	int verbose = 0;
+	int verbose = 0, recursive = 0;
 	int argc_min;
 
-	while ((opt = getopt(argc, argv, "v")) > 0) {
+	while ((opt = getopt(argc, argv, "vr")) > 0) {
 		switch (opt) {
 		case 'v':
 			verbose = 1;
 			break;
+		case 'r':
+			recursive = 1;
+			break;
 		}
 	}
 
@@ -60,24 +63,31 @@ static int do_cp(int argc, char *argv[])
 			last_is_dir = 1;
 	}
 
-	if (argc > argc_min && !last_is_dir) {
+	if (((recursive && argc - optind > 2) || (argc > argc_min)) && !last_is_dir) {
 		printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
 		return 1;
 	}
 
+	if (recursive && argc - optind == 2 && !last_is_dir) {
+		ret = make_directory(argv[argc - 1]);
+		if (ret)
+			goto out;
+	}
+
 	for (i = optind; i < argc - 1; i++) {
-		if (last_is_dir) {
-			char *dst;
-			dst = concat_path_file(argv[argc - 1], basename(argv[i]));
+		char *dst;
+		dst = concat_path_file(argv[argc - 1], basename(argv[i]));
+
+		if (recursive)
+			ret = copy_recursive(argv[i], dst);
+		else if (last_is_dir)
 			ret = copy_file(argv[i], dst, verbose);
-			free(dst);
-			if (ret)
-				goto out;
-		} else {
+		else
 			ret = copy_file(argv[i], argv[argc - 1], verbose);
-			if (ret)
-				goto out;
-		}
+
+		free(dst);
+		if (ret)
+			goto out;
 	}
 
 	ret = 0;
-- 
2.1.1




More information about the barebox mailing list