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