cfi new: fix new disabling buffer support
[barebox-mini2440.git] / commands / cp.c
blobd0cb570a6f25426ed9a032b70c6ae278995fbb2e
1 /*
2 * cp.c - copy files
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /**
24 * @file
25 * @brief cp: copy file command
27 #include <common.h>
28 #include <command.h>
29 #include <xfuncs.h>
30 #include <linux/stat.h>
31 #include <libbb.h>
32 #include <fs.h>
33 #include <malloc.h>
35 /**
36 * @param[in] cmdtp FIXME
37 * @param[in] argc Argument count from command line
38 * @param[in] argv List of input arguments
40 static int do_cp ( cmd_tbl_t *cmdtp, int argc, char *argv[])
42 int ret = 1;
43 struct stat statbuf;
44 int last_is_dir = 0;
45 int i;
47 if (argc < 3)
48 return COMMAND_ERROR_USAGE;
50 if (!stat(argv[argc - 1], &statbuf)) {
51 if (S_ISDIR(statbuf.st_mode))
52 last_is_dir = 1;
55 if (argc > 3 && !last_is_dir) {
56 printf("cp: target `%s' is not a directory\n", argv[argc - 1]);
57 return 1;
60 for (i = 1; i < argc - 1; i++) {
61 if (last_is_dir) {
62 char *dst;
63 dst = concat_path_file(argv[argc - 1], argv[i]);
64 ret = copy_file(argv[i], dst);
65 if (ret)
66 goto out;
67 free(dst);
68 } else {
69 ret = copy_file(argv[i], argv[argc - 1]);
70 if (ret)
71 goto out;
75 ret = 0;
76 out:
77 return ret;
80 static const __maybe_unused char cmd_cp_help[] =
81 "Usage: cp <source> <destination>\n"
82 "cp copies file <source> to <destination>.\n"
83 "Currently only this form is supported and you have to specify the exact target\n"
84 "filename (not a target directory).\n"
85 "This command is file based only. See memcpy for memory copy\n";
87 U_BOOT_CMD_START(cp)
88 .cmd = do_cp,
89 .usage = "copy files",
90 U_BOOT_CMD_HELP(cmd_cp_help)
91 U_BOOT_CMD_END
93 /**
94 * @page cp_command cp: Copy file
96 * Usage: cp \<source> [\<source>] \<destination>
98 * \c cp copies file \<source> to \<destination>
100 * Currently only this form is supported and you have to specify the exact
101 * target filename (not a target directory).\n
102 * This command is file based only. See memcpy for generic memory copy