1 /* dd.c - program to convert and copy a file.
3 * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
6 * See http://opengroup.org/onlinepubs/9699919799/utilities/dd.html
7 USE_DD(NEWTOY(dd, NULL, TOYFLAG_USR|TOYFLAG_BIN))
13 usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
14 [seek=N] [conv=notrunc|noerror|sync|fsync]
17 if=FILE Read from FILE instead of stdin
18 of=FILE Write to FILE instead of stdout
19 bs=N Read and write N bytes at a time
20 ibs=N Read N bytes at a time
21 obs=N Write N bytes at a time
22 count=N Copy only N input blocks
23 skip=N Skip N input blocks
24 seek=N Skip N output blocks
25 conv=notrunc Don't truncate output file
26 conv=noerror Continue after read errors
27 conv=sync Pad blocks with zeros
28 conv=fsync Physically write data out before finishing
30 Numbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),
31 MD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)
32 Copy a file, converting and formatting according to the operands.
42 #define C_COUNT 0x0002
50 #define C_FSYNC 0x0200
51 #define C_NOERROR 0x0400
52 #define C_NOTRUNC 0x0800
57 unsigned char *buff, *bp;
59 unsigned long long offset;
63 unsigned long long in_full, in_part, out_full, out_part, bytes;
72 static struct pair suffixes[] = {
73 { "c", 1 }, { "w", 2 }, { "b", 512 },
74 { "kD", 1000 }, { "k", 1024 }, { "K", 1024 },
75 { "MD", 1000000 }, { "M", 1048576 },
76 { "GD", 1000000000 }, { "G", 1073741824 }
79 static struct pair clist[] = {
81 { "noerror", C_NOERROR },
82 { "notrunc", C_NOTRUNC },
86 static struct pair operands[] = {
87 // keep the array sorted by name, bsearch() can be used.
99 static struct io in, out;
100 static struct iostat st;
101 static unsigned long long c_count;
103 static unsigned long long strsuftoll(char* arg, int def, unsigned long long max)
105 unsigned long long result;
106 char *endp, *ch = arg;
110 while (isspace(*ch)) ch++;
111 if (*ch == '-') error_exit("invalid number '%s'",arg);
112 result = strtoull(arg, &endp, 10);
113 if (errno == ERANGE || result > max || result < def)
114 perror_exit("invalid number '%s'",arg);
116 for (i = 0; i < ARRAY_LEN(suffixes); i++)
117 if (!strcmp(endp, suffixes[i].name)) idx = i;
118 if (idx == -1 || (max/suffixes[idx].val < result))
119 error_exit("invalid number '%s'",arg);
120 result = result* suffixes[idx].val;
125 static void summary()
127 double seconds = 5.0;
130 gettimeofday(&now, NULL);
131 seconds = ((now.tv_sec * 1000000 + now.tv_usec) - (st.start.tv_sec * 1000000
132 + st.start.tv_usec))/1000000.0;
134 fprintf(stderr,"%llu+%llu records in\n%llu+%llu records out\n", st.in_full, st.in_part,
135 st.out_full, st.out_part);
136 human_readable(toybuf, st.bytes, HR_SPACE|HR_B);
137 fprintf(stderr, "%llu bytes (%s) copied, ",st.bytes, toybuf);
138 human_readable(toybuf, st.bytes/seconds, HR_SPACE|HR_B);
139 fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
142 static void sig_handler(int sig)
147 static int xmove_fd(int fd)
151 if (fd > STDERR_FILENO) return fd;
152 if ((newfd = fcntl(fd, F_DUPFD, 3) < 0)) perror_exit("dupfd IO");
157 static void setup_inout()
161 /* for C_BS, in/out is done as it is. so only in.sz is enough.
162 * With Single buffer there will be overflow in a read following partial read
164 in.buff = out.buff = xmalloc(in.sz + ((toys.optflags & C_BS)? 0: out.sz));
165 in.bp = out.bp = in.buff;
170 in.fd = STDIN_FILENO;
172 in.fd = xopen(in.name, O_RDONLY);
173 in.fd = xmove_fd(in.fd);
178 out.fd = STDOUT_FILENO;
180 out.fd = xcreate(out.name, O_WRONLY | O_CREAT, 0666);
181 out.fd = xmove_fd(out.fd);
185 if (lseek(in.fd, (off_t)(in.offset * in.sz), SEEK_CUR) < 0) {
186 while (in.offset--) {
187 if ((n = read(in.fd, in.bp, in.sz)) < 0) {
188 if (toys.optflags & C_NOERROR) { //warn message and summary
189 error_msg("%s: read error", in.name);
191 } else perror_exit("%s: read error", in.name);
193 xprintf("%s: Can't skip\n", in.name);
200 if (out.offset) xlseek(out.fd, (off_t)(out.offset * out.sz), SEEK_CUR);
203 static void write_out(int all)
208 nw = writeall(out.fd, out.bp, ((all)? out.count : out.sz));
209 all = 0; //further writes will be on obs
210 if (nw <= 0) perror_exit("%s: write error",out.name);
211 if (nw == out.sz) st.out_full++;
216 if (out.count < out.sz) break;
218 if (out.count) memmove(out.buff, out.bp, out.count); //move remainder to front
221 static void do_dd(void)
226 memset(&sa, 0, sizeof(sa));
227 sa.sa_handler = sig_handler;
228 sigaction(SIGINT, &sa, NULL);
229 sigaction(SIGUSR1, &sa, NULL);
231 gettimeofday(&st.start, NULL);
233 if (toys.optflags & (C_OF | C_SEEK) && !(toys.optflags & C_NOTRUNC))
234 ftruncate(out.fd, (off_t)out.offset * out.sz);
236 while (!(toys.optflags & C_COUNT) || (st.in_full + st.in_part) < c_count) {
237 if (TT.sig == SIGUSR1) {
240 } else if (TT.sig == SIGINT) exit(TT.sig | 128);
241 in.bp = in.buff + in.count;
242 if (toys.optflags & C_SYNC) memset(in.bp, 0, in.sz);
243 if (!(n = read(in.fd, in.bp, in.sz))) break;
245 if (errno == EINTR) continue;
247 perror_msg("%s: read error", in.name);
248 if (!(toys.optflags & C_NOERROR)) exit(1);
250 xlseek(in.fd, in.sz, SEEK_CUR);
251 if (!(toys.optflags & C_SYNC)) continue;
252 // if SYNC, then treat as full block of nuls
260 if (toys.optflags & C_SYNC) in.count += in.sz;
264 out.count = in.count;
265 if (toys.optflags & C_BS) {
271 if (in.count >= out.sz) {
273 in.count = out.count;
276 if (out.count) write_out(1); //write any remaining input blocks
277 if (toys.optflags & C_FSYNC && fsync(out.fd) < 0)
278 perror_exit("%s: fsync fail", out.name);
282 if (in.buff) free(in.buff);
285 static int comp(const void *a, const void *b) //const to shut compiler up
287 return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name);
292 struct pair *res, key;
296 in.sz = out.sz = 512; //default io block size
297 while (*toys.optargs) {
298 if (!(arg = strchr(*toys.optargs, '='))) error_exit("unknown arg %s", *toys.optargs);
304 key.name = *toys.optargs;
305 if (!(res = bsearch(&key, operands, ARRAY_LEN(operands), sizeof(struct pair),
306 comp))) error_exit("unknown arg %s", key.name);
308 toys.optflags |= res->val;
311 in.sz = out.sz = strsuftoll(arg, 1, LONG_MAX);
314 sz = strsuftoll(arg, 1, LONG_MAX);
315 if (!(toys.optflags & C_BS)) in.sz = sz;
318 sz = strsuftoll(arg, 1, LONG_MAX);
319 if (!(toys.optflags & C_BS)) out.sz = sz;
322 c_count = strsuftoll(arg, 0, ULLONG_MAX);
331 out.offset = strsuftoll(arg, 0, ULLONG_MAX);
334 in.offset = strsuftoll(arg, 0, ULLONG_MAX);
338 key.name = strsep(&arg, ",");
339 if (!(res = bsearch(&key, clist, ARRAY_LEN(clist),
340 sizeof(struct pair), comp)))
341 error_exit("unknown conversion %s", key.name);
343 toys.optflags |= res->val;