1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#define FOR_dd
48#include "toys.h"
49
50GLOBALS(
51 int show_xfer, show_records;
52 unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
53 struct timeval start;
54 struct {
55 char *name;
56 int fd;
57 unsigned char *buff, *bp;
58 long sz, count;
59 unsigned long long offset;
60 } in, out;
61 unsigned conv, iflag, oflag;
62);
63
64struct dd_flag {
65 char *name;
66};
67
68static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
69 {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"},
70);
71
72static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
73 {"count_bytes"}, {"skip_bytes"},
74);
75
76static const struct dd_flag dd_oflag[] = TAGGED_ARRAY(DD_oflag,
77 {"seek_bytes"},
78);
79
80static void status()
81{
82 double seconds;
83 struct timeval now;
84
85 gettimeofday(&now, NULL);
86 seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
87 (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
88
89 if (TT.show_records)
90 fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
91 TT.in_full, TT.in_part, TT.out_full, TT.out_part);
92
93 if (TT.show_xfer) {
94 human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
95 fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
96 human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
97 fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
98 }
99}
100
101static void dd_sigint(int sig) {
102 status();
103 toys.exitval = sig|128;
104 xexit();
105}
106
107static void write_out(int all)
108{
109 TT.out.bp = TT.out.buff;
110 while (TT.out.count) {
111 ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
112
113 all = 0;
114 if (nw <= 0) perror_exit("%s: write error", TT.out.name);
115 if (nw == TT.out.sz) TT.out_full++;
116 else TT.out_part++;
117 TT.out.count -= nw;
118 TT.out.bp += nw;
119 TT.bytes += nw;
120 if (TT.out.count < TT.out.sz) break;
121 }
122 if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count);
123}
124
125static void parse_flags(char *what, char *arg,
126 const struct dd_flag* flags, int flag_count, unsigned *result)
127{
128 char *pre = xstrdup(arg);
129 int i;
130
131 for (i=0; i<flag_count; ++i) {
132 while (comma_remove(pre, flags[i].name)) *result |= 1<<i;
133 }
134 if (*pre) error_exit("bad %s=%s", what, pre);
135 free(pre);
136}
137
138void dd_main()
139{
140 char **args;
141 unsigned long long bs = 0;
142 int trunc = O_TRUNC;
143
144 TT.show_xfer = TT.show_records = 1;
145 TT.c_count = ULLONG_MAX;
146
147 TT.in.sz = TT.out.sz = 512;
148 for (args = toys.optargs; *args; args++) {
149 char *arg = *args;
150
151 if (strstart(&arg, "bs=")) bs = atolx_range(arg, 1, LONG_MAX);
152 else if (strstart(&arg, "ibs=")) TT.in.sz = atolx_range(arg, 1, LONG_MAX);
153 else if (strstart(&arg, "obs=")) TT.out.sz = atolx_range(arg, 1, LONG_MAX);
154 else if (strstart(&arg, "count="))
155 TT.c_count = atolx_range(arg, 0, LLONG_MAX);
156 else if (strstart(&arg, "if=")) TT.in.name = arg;
157 else if (strstart(&arg, "of=")) TT.out.name = arg;
158 else if (strstart(&arg, "seek="))
159 TT.out.offset = atolx_range(arg, 0, LLONG_MAX);
160 else if (strstart(&arg, "skip="))
161 TT.in.offset = atolx_range(arg, 0, LLONG_MAX);
162 else if (strstart(&arg, "status=")) {
163 if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
164 else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
165 else error_exit("unknown status '%s'", arg);
166 } else if (strstart(&arg, "conv=")) {
167 parse_flags("conv", arg, dd_conv, ARRAY_LEN(dd_conv), &TT.conv);
168 fprintf(stderr, "conv=%x\n", TT.conv);
169 } else if (strstart(&arg, "iflag="))
170 parse_flags("iflag", arg, dd_iflag, ARRAY_LEN(dd_iflag), &TT.iflag);
171 else if (strstart(&arg, "oflag="))
172 parse_flags("oflag", arg, dd_oflag, ARRAY_LEN(dd_oflag), &TT.oflag);
173 else error_exit("bad arg %s", arg);
174 }
175 if (bs) TT.in.sz = TT.out.sz = bs;
176
177 signal(SIGINT, dd_sigint);
178 signal(SIGUSR1, generic_signal);
179 gettimeofday(&TT.start, NULL);
180
181
182
183 TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
184 TT.in.bp = TT.out.bp = TT.in.buff;
185
186 if (!TT.in.name) TT.in.name = "stdin";
187 else TT.in.fd = xopenro(TT.in.name);
188
189 if (TT.conv & _DD_conv_notrunc) trunc = 0;
190
191 if (!TT.out.name) {
192 TT.out.name = "stdout";
193 TT.out.fd = 1;
194 } else TT.out.fd = xcreate(TT.out.name,
195 O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
196
197
198 if (TT.in.offset) {
199 off_t off = TT.in.offset;
200
201 if (!(TT.iflag & _DD_iflag_skip_bytes)) off *= TT.in.sz;
202 if (lseek(TT.in.fd, off, SEEK_CUR) < 0) {
203 while (off > 0) {
204 int chunk = off < TT.in.sz ? off : TT.in.sz;
205 ssize_t n = read(TT.in.fd, TT.in.bp, chunk);
206
207 if (n < 0) {
208 perror_msg("%s", TT.in.name);
209 if (TT.conv & _DD_conv_noerror) status();
210 else return;
211 } else if (!n) {
212 xprintf("%s: Can't skip\n", TT.in.name);
213 return;
214 }
215 off -= n;
216 }
217 }
218 }
219
220
221
222
223 bs = TT.out.offset;
224 if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
225 if (bs) {
226 xlseek(TT.out.fd, bs, SEEK_CUR);
227 if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
228 }
229
230 unsigned long long bytes_left = TT.c_count;
231 if (TT.c_count != ULLONG_MAX && !(TT.iflag & _DD_iflag_count_bytes)) {
232 bytes_left *= TT.in.sz;
233 }
234 while (bytes_left) {
235 int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz;
236 ssize_t n;
237
238
239 if (toys.signal) {
240 status();
241 if (toys.signal==SIGINT) exit_signal(toys.signal);
242 toys.signal = 0;
243 }
244
245 TT.in.bp = TT.in.buff + TT.in.count;
246 if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz);
247 if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break;
248 if (n < 0) {
249 if (errno == EINTR) continue;
250
251 perror_msg("%s: read error", TT.in.name);
252 if (!(TT.conv & _DD_conv_noerror)) exit(1);
253 status();
254 xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
255 if (!(TT.conv & _DD_conv_sync)) continue;
256
257 n = TT.in.sz;
258 }
259 if (n == TT.in.sz) {
260 TT.in_full++;
261 TT.in.count += n;
262 } else {
263 TT.in_part++;
264 if (TT.conv & _DD_conv_sync) TT.in.count += TT.in.sz;
265 else TT.in.count += n;
266 }
267 bytes_left -= n;
268
269 TT.out.count = TT.in.count;
270 if (bs) {
271 write_out(1);
272 TT.in.count = 0;
273 continue;
274 }
275
276 if (TT.in.count >= TT.out.sz) {
277 write_out(0);
278 TT.in.count = TT.out.count;
279 }
280 }
281 if (TT.out.count) write_out(1);
282 if ((TT.conv & _DD_conv_fsync) && fsync(TT.out.fd)<0)
283 perror_exit("%s: fsync", TT.out.name);
284
285 close(TT.in.fd);
286 close(TT.out.fd);
287 if (TT.in.buff) free(TT.in.buff);
288
289 status();
290}
291