source: mainline/uspace/app/bdsh/cmds/modules/cp/cp.c@ 36ab7c7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36ab7c7 was 36ab7c7, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Standardize formatting of copyright headers in Bdsh and add some that were
missing.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <getopt.h>
33#include <str.h>
34#include <fcntl.h>
35#include "config.h"
36#include "util.h"
37#include "errors.h"
38#include "entry.h"
39#include "cp.h"
40#include "cmds.h"
41
42#define CP_VERSION "0.0.1"
43#define CP_DEFAULT_BUFLEN 1024
44
45static const char *cmdname = "cp";
46
47static struct option const long_options[] = {
48 { "buffer", required_argument, 0, 'b' },
49 { "force", no_argument, 0, 'f' },
50 { "recursive", no_argument, 0, 'r' },
51 { "help", no_argument, 0, 'h' },
52 { "version", no_argument, 0, 'v' },
53 { "verbose", no_argument, 0, 'V' },
54 { 0, 0, 0, 0 }
55};
56
57static int strtoint(const char *s1)
58{
59 long t1;
60
61 if (-1 == (t1 = strtol(s1, (char **) NULL, 10)))
62 return -1;
63
64 if (t1 <= 0)
65 return -1;
66
67 return (int) t1;
68}
69
70static int64_t copy_file(const char *src, const char *dest,
71 size_t blen, int vb)
72{
73 int fd1, fd2, bytes = 0;
74 off64_t total = 0;
75 int64_t copied = 0;
76 char *buff = NULL;
77
78 if (vb)
79 printf("Copying %s to %s\n", src, dest);
80
81 if (-1 == (fd1 = open(src, O_RDONLY))) {
82 printf("Unable to open source file %s\n", src);
83 return -1;
84 }
85
86 if (-1 == (fd2 = open(dest, O_CREAT))) {
87 printf("Unable to open destination file %s\n", dest);
88 close(fd1);
89 return -1;
90 }
91
92 total = lseek(fd1, 0, SEEK_END);
93
94 if (vb)
95 printf("%" PRIu64 " bytes to copy\n", total);
96
97 lseek(fd1, 0, SEEK_SET);
98
99 if (NULL == (buff = (char *) malloc(blen))) {
100 printf("Unable to allocate enough memory to read %s\n",
101 src);
102 copied = -1;
103 goto out;
104 }
105
106 for (;;) {
107 ssize_t res;
108 size_t written = 0;
109
110 bytes = read(fd1, buff, blen);
111 if (bytes <= 0)
112 break;
113 copied += bytes;
114 res = bytes;
115 do {
116 /*
117 * Theoretically, it may not be enough to call write()
118 * only once. Also the previous read() may have
119 * returned less data than requested.
120 */
121 bytes = write(fd2, buff + written, res);
122 if (bytes < 0)
123 goto err;
124 written += bytes;
125 res -= bytes;
126 } while (res > 0);
127
128 /* TODO: re-insert assert() once this is stand alone,
129 * removed as abort() exits the entire shell
130 */
131 if (res != 0) {
132 printf("\n%zd more bytes than actually exist were copied\n", res);
133 goto err;
134 }
135 }
136
137 if (bytes < 0) {
138err:
139 printf("\nError copying %s, (%d)\n", src, bytes);
140 copied = bytes;
141 }
142
143out:
144 close(fd1);
145 close(fd2);
146 if (buff)
147 free(buff);
148 return copied;
149}
150
151void help_cmd_cp(unsigned int level)
152{
153 static char helpfmt[] =
154 "Usage: %s [options] <source> <dest>\n"
155 "Options: (* indicates not yet implemented)\n"
156 " -h, --help A short option summary\n"
157 " -v, --version Print version information and exit\n"
158 "* -V, --verbose Be annoyingly noisy about what's being done\n"
159 "* -f, --force Do not complain when <dest> exists\n"
160 "* -r, --recursive Copy entire directories\n"
161 " -b, --buffer ## Set the read buffer size to ##\n"
162 "Currently, %s is under development, some options may not work.\n";
163 if (level == HELP_SHORT) {
164 printf("`%s' copies files and directories\n", cmdname);
165 } else {
166 help_cmd_cp(HELP_SHORT);
167 printf(helpfmt, cmdname, cmdname);
168 }
169
170 return;
171}
172
173int cmd_cp(char **argv)
174{
175 unsigned int argc, verbose = 0;
176 int buffer = 0;
177 int c, opt_ind;
178 int64_t ret;
179
180 argc = cli_count_args(argv);
181
182 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
183 c = getopt_long(argc, argv, "hvVfrb:", long_options, &opt_ind);
184 switch (c) {
185 case 'h':
186 help_cmd_cp(1);
187 return CMD_SUCCESS;
188 case 'v':
189 printf("%s\n", CP_VERSION);
190 return CMD_SUCCESS;
191 case 'V':
192 verbose = 1;
193 break;
194 case 'f':
195 break;
196 case 'r':
197 break;
198 case 'b':
199 if (-1 == (buffer = strtoint(optarg))) {
200 printf("%s: Invalid buffer specification, "
201 "(should be a number greater than zero)\n",
202 cmdname);
203 return CMD_FAILURE;
204 }
205 if (verbose)
206 printf("Buffer = %d\n", buffer);
207 break;
208 }
209 }
210
211 if (buffer == 0)
212 buffer = CP_DEFAULT_BUFLEN;
213
214 argc -= optind;
215
216 if (argc != 2) {
217 printf("%s: invalid number of arguments. Try %s --help\n",
218 cmdname, cmdname);
219 return CMD_FAILURE;
220 }
221
222 ret = copy_file(argv[optind], argv[optind + 1], buffer, verbose);
223
224 if (verbose)
225 printf("%" PRId64 " bytes copied\n", ret);
226
227 if (ret >= 0)
228 return CMD_SUCCESS;
229 else
230 return CMD_FAILURE;
231}
232
Note: See TracBrowser for help on using the repository browser.