diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2014-10-23 21:46:21 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2014-11-06 10:58:55 +0100 |
commit | 837e517669aee65173f185ed912d95f26b0ff5fd (patch) | |
tree | 67e2d257a4abf0739d40b3a698e751d0f3e85922 | |
parent | 9f118870096fcd4ec8e1bea6fa95ba6845cc4860 (diff) | |
download | barebox-837e517669aee65173f185ed912d95f26b0ff5fd.tar.gz |
cp: Add recursive copy
This adds the -r option to the cp command.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | commands/cp.c | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/commands/cp.c b/commands/cp.c index af7a3d4dc0..4f1c068dd6 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; |