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
RevLine 
[36ab7c7]1/*
2 * Copyright (c) 2008 Tim Post
[39463ff]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
[36ab7c7]6 * modification, are permitted provided that the following conditions
7 * are met:
[39463ff]8 *
[36ab7c7]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.
[39463ff]16 *
[36ab7c7]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.
[39463ff]27 */
28
29#include <stdio.h>
30#include <stdlib.h>
[2640b6c]31#include <unistd.h>
32#include <getopt.h>
[19f857a]33#include <str.h>
[2640b6c]34#include <fcntl.h>
[39463ff]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
[2640b6c]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
[bffd91f]70static int64_t copy_file(const char *src, const char *dest,
71 size_t blen, int vb)
[2640b6c]72{
73 int fd1, fd2, bytes = 0;
[ed903174]74 off64_t total = 0;
[8df7a1c]75 int64_t copied = 0;
[2640b6c]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);
[890793b]83 return -1;
[2640b6c]84 }
85
86 if (-1 == (fd2 = open(dest, O_CREAT))) {
87 printf("Unable to open destination file %s\n", dest);
[8df7a1c]88 close(fd1);
[890793b]89 return -1;
[2640b6c]90 }
91
92 total = lseek(fd1, 0, SEEK_END);
93
94 if (vb)
[7e752b2]95 printf("%" PRIu64 " bytes to copy\n", total);
[2640b6c]96
97 lseek(fd1, 0, SEEK_SET);
98
[8df7a1c]99 if (NULL == (buff = (char *) malloc(blen))) {
[2640b6c]100 printf("Unable to allocate enough memory to read %s\n",
[8df7a1c]101 src);
102 copied = -1;
[2640b6c]103 goto out;
104 }
105
[8df7a1c]106 for (;;) {
107 ssize_t res;
[2815505]108 size_t written = 0;
[8df7a1c]109
110 bytes = read(fd1, buff, blen);
111 if (bytes <= 0)
[4c4ddbe9]112 break;
[2640b6c]113 copied += bytes;
[8df7a1c]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 */
[2815505]121 bytes = write(fd2, buff + written, res);
[8df7a1c]122 if (bytes < 0)
123 goto err;
[2815505]124 written += bytes;
[8df7a1c]125 res -= bytes;
126 } while (res > 0);
[b7be230]127
128 /* TODO: re-insert assert() once this is stand alone,
129 * removed as abort() exits the entire shell
130 */
131 if (res != 0) {
[7e752b2]132 printf("\n%zd more bytes than actually exist were copied\n", res);
[b7be230]133 goto err;
134 }
[8df7a1c]135 }
[2640b6c]136
[8df7a1c]137 if (bytes < 0) {
138err:
[b7be230]139 printf("\nError copying %s, (%d)\n", src, bytes);
[3b10e07b]140 copied = bytes;
[2640b6c]141 }
142
143out:
144 close(fd1);
145 close(fd2);
146 if (buff)
147 free(buff);
148 return copied;
149}
[39463ff]150
151void help_cmd_cp(unsigned int level)
152{
[8df7a1c]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";
[2640b6c]163 if (level == HELP_SHORT) {
164 printf("`%s' copies files and directories\n", cmdname);
165 } else {
166 help_cmd_cp(HELP_SHORT);
[8df7a1c]167 printf(helpfmt, cmdname, cmdname);
[2640b6c]168 }
169
[39463ff]170 return;
171}
172
173int cmd_cp(char **argv)
174{
[5b3cf90]175 unsigned int argc, verbose = 0;
176 int buffer = 0;
[3b10e07b]177 int c, opt_ind;
178 int64_t ret;
[2640b6c]179
180 argc = cli_count_args(argv);
[39463ff]181
[2640b6c]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':
[7e752b2]189 printf("%s\n", CP_VERSION);
[2640b6c]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, "
[8df7a1c]201 "(should be a number greater than zero)\n",
202 cmdname);
[2640b6c]203 return CMD_FAILURE;
204 }
[4c4ddbe9]205 if (verbose)
206 printf("Buffer = %d\n", buffer);
[2640b6c]207 break;
208 }
209 }
[39463ff]210
[4c4ddbe9]211 if (buffer == 0)
212 buffer = CP_DEFAULT_BUFLEN;
213
[2640b6c]214 argc -= optind;
[39463ff]215
[2640b6c]216 if (argc != 2) {
217 printf("%s: invalid number of arguments. Try %s --help\n",
[8df7a1c]218 cmdname, cmdname);
[2640b6c]219 return CMD_FAILURE;
[39463ff]220 }
221
[2640b6c]222 ret = copy_file(argv[optind], argv[optind + 1], buffer, verbose);
[39463ff]223
[2640b6c]224 if (verbose)
[7e752b2]225 printf("%" PRId64 " bytes copied\n", ret);
[2640b6c]226
[8df7a1c]227 if (ret >= 0)
[2640b6c]228 return CMD_SUCCESS;
229 else
230 return CMD_FAILURE;
[39463ff]231}
232
Note: See TracBrowser for help on using the repository browser.