blob: 82d43adff625824fcc98ef535333797be8a840a9 [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cp implementation for busybox
4 *
5 *
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <utime.h>
29#include <errno.h>
30#include <dirent.h>
31#include <stdlib.h>
32
33#include "busybox.h"
34
35extern int cp_main(int argc, char **argv)
36{
37 int status = 0;
38 int opt;
39 int flags = 0;
40 int i;
41
42 while ((opt = getopt(argc, argv, "adfipR")) != -1)
43 switch (opt) {
44 case 'a':
Matt Kraai01441032001-04-24 01:30:02 +000045 flags |= FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR;
Matt Kraai91b28552001-04-23 18:53:07 +000046 /* fallthrough */
47 case 'd':
Matt Kraai01441032001-04-24 01:30:02 +000048 flags |= FILEUTILS_PRESERVE_SYMLINKS;
Matt Kraai91b28552001-04-23 18:53:07 +000049 break;
50 case 'f':
Matt Kraai01441032001-04-24 01:30:02 +000051 flags |= FILEUTILS_FORCE;
Matt Kraai91b28552001-04-23 18:53:07 +000052 break;
53 case 'i':
Matt Kraai01441032001-04-24 01:30:02 +000054 flags |= FILEUTILS_INTERACTIVE;
Matt Kraai91b28552001-04-23 18:53:07 +000055 break;
56 case 'p':
Matt Kraai01441032001-04-24 01:30:02 +000057 flags |= FILEUTILS_PRESERVE_STATUS;
Matt Kraai91b28552001-04-23 18:53:07 +000058 break;
59 case 'R':
Matt Kraai01441032001-04-24 01:30:02 +000060 flags |= FILEUTILS_RECUR;
Matt Kraai91b28552001-04-23 18:53:07 +000061 break;
62 default:
63 show_usage();
64 }
65
66 if (optind + 2 > argc)
67 show_usage();
68
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;
74 int dest_exists = 1;
75
Matt Kraai01441032001-04-24 01:30:02 +000076 if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
Matt Kraai91b28552001-04-23 18:53:07 +000077 lstat(argv[optind], &source_stat) < 0) ||
Matt Kraai01441032001-04-24 01:30:02 +000078 (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
Matt Kraai91b28552001-04-23 18:53:07 +000079 stat(argv[optind], &source_stat))) {
80 if (errno != ENOENT)
81 perror_msg_and_die("unable to stat `%s'", argv[optind]);
82 source_exists = 0;
83 }
84
85 if (stat(argv[optind + 1], &dest_stat) < 0) {
86 if (errno != ENOENT)
87 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
88 dest_exists = 0;
89 }
90
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... */
Matt Kraai01441032001-04-24 01:30:02 +000096 ((flags & FILEUTILS_RECUR) && S_ISDIR(source_stat.st_mode) &&
Matt Kraai91b28552001-04-23 18:53:07 +000097 !dest_exists)) {
98 /* ...do a simple copy. */
99 if (copy_file(argv[optind], argv[optind + 1], flags) < 0)
100 status = 1;
101 return status;
102 }
103 }
104
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)
109 status = 1;
110 free(dest);
111 }
112
113 return status;
114}