mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 08/11] cp: Add recursive copy
Date: Thu,  6 Nov 2014 13:59:35 +0100	[thread overview]
Message-ID: <1415278778-20826-9-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1415278778-20826-1-git-send-email-s.hauer@pengutronix.de>

This adds the -r option to the cp command.

Signed-off-by: Sascha Hauer <s.hauer@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


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2014-11-06 13:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-06 12:59 non volatile variables and incremental envrironment Sascha Hauer
2014-11-06 12:59 ` [PATCH 01/11] environment: drop unnecessary casts Sascha Hauer
2014-11-06 12:59 ` [PATCH 02/11] environment: remove unused variable Sascha Hauer
2014-11-06 12:59 ` [PATCH 03/11] environment: refactor saveenv Sascha Hauer
2014-11-06 12:59 ` [PATCH 04/11] environment: Only save changes to the defaultenv Sascha Hauer
2014-11-06 12:59 ` [PATCH 05/11] magicvar: Add support for dynamically added magicvars Sascha Hauer
2014-11-06 12:59 ` [PATCH 06/11] Add support for non volatile variables Sascha Hauer
2014-11-06 12:59 ` [PATCH 07/11] libfile: Add copy_recursive Sascha Hauer
2014-11-06 12:59 ` Sascha Hauer [this message]
2014-11-06 12:59 ` [PATCH 09/11] Add defaultenv command Sascha Hauer
2014-11-06 12:59 ` [PATCH 10/11] globalvar: Add support for printing all global variables Sascha Hauer
2014-11-06 12:59 ` [PATCH 11/11] defaultenv-2: Make use of nonvolatile variables Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1415278778-20826-9-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox