1 /* vi: set sw=4 ts=4: */
3 * Mini cp implementation for busybox
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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 GNU
16 * 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
24 #include <sys/types.h>
35 extern int cp_main(int argc, char **argv)
42 while ((opt = getopt(argc, argv, "adfipR")) != -1)
45 flags |= FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR;
48 flags |= FILEUTILS_PRESERVE_SYMLINKS;
51 flags |= FILEUTILS_FORCE;
54 flags |= FILEUTILS_INTERACTIVE;
57 flags |= FILEUTILS_PRESERVE_STATUS;
60 flags |= FILEUTILS_RECUR;
66 if (optind + 2 > argc)
69 /* If there are only two arguments and... */
70 if (optind + 2 == argc) {
71 struct stat source_stat;
72 struct stat dest_stat;
73 int source_exists = 1;
76 if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
77 lstat(argv[optind], &source_stat) < 0) ||
78 (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
79 stat(argv[optind], &source_stat))) {
81 perror_msg_and_die("unable to stat `%s'", argv[optind]);
85 if (stat(argv[optind + 1], &dest_stat) < 0) {
87 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
91 /* ...if neither is a directory or... */
92 if (((!source_exists || !S_ISDIR(source_stat.st_mode)) &&
93 (!dest_exists || !S_ISDIR(dest_stat.st_mode))) ||
94 /* ...recursing, the first is a directory, and the
95 * second doesn't exist, then... */
96 ((flags & FILEUTILS_RECUR) && S_ISDIR(source_stat.st_mode) &&
98 /* ...do a simple copy. */
99 if (copy_file(argv[optind], argv[optind + 1], flags) < 0)
105 for (i = optind; i < argc - 1; i++) {
106 char *dest = concat_path_file(argv[argc - 1],
107 get_last_path_component(argv[i]));
108 if (copy_file(argv[i], dest, flags) < 0)