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
8 USE_DD(NEWTOY(dd, NULL, TOYFLAG_USR|TOYFLAG_BIN))
14 usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
15 [seek=N] [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
18 if=FILE Read from FILE instead of stdin
19 of=FILE Write to FILE instead of stdout
20 bs=N Read and write N bytes at a time
21 ibs=N Read N bytes at a time
22 obs=N Write N bytes at a time
23 count=N Copy only N input blocks
24 skip=N Skip N input blocks
25 seek=N Skip N output blocks
26 conv=notrunc Don't truncate output file
27 conv=noerror Continue after read errors
28 conv=sync Pad blocks with zeros
29 conv=fsync Physically write data out before finishing
30 status=noxfer Don't show transfer rate
31 status=none Don't show transfer rate or records in/out
33 Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
34 MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
43 unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
48 unsigned char *buff, *bp;
50 unsigned long long offset;
56 #define C_COUNT 0x0002
64 #define C_FSYNC 0x0200
65 #define C_NOERROR 0x0400
66 #define C_NOTRUNC 0x0800
67 #define C_STATUS 0x1000
74 static struct pair suffixes[] = {
75 { "c", 1 }, { "w", 2 }, { "b", 512 },
76 { "kD", 1000 }, { "k", 1024 }, { "K", 1024 },
77 { "MD", 1000000 }, { "M", 1048576 },
78 { "GD", 1000000000 }, { "G", 1073741824 }
81 static struct pair clist[] = {
83 { "noerror", C_NOERROR },
84 { "notrunc", C_NOTRUNC },
88 static struct pair operands[] = {
89 // keep the array sorted by name, bsearch() can be used.
99 { "status", C_STATUS},
102 static unsigned long long strsuftoll(char *arg, int def, unsigned long long max)
104 unsigned long long result;
108 while (isspace(*p)) p++;
109 if (*p == '-') error_exit("invalid number '%s'", arg);
112 result = strtoull(p, &p, 0);
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(p, suffixes[i].name)) idx = i;
118 if (idx == -1 || (max/suffixes[idx].val < result))
119 error_exit("invalid number '%s'", arg);
120 result *= suffixes[idx].val;
130 gettimeofday(&now, NULL);
131 seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
132 (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
135 fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
136 TT.in_full, TT.in_part, TT.out_full, TT.out_part);
139 human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
140 fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
141 human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
142 fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
146 static int xmove_fd(int fd)
150 if (fd > STDERR_FILENO) return fd;
151 if ((newfd = fcntl(fd, F_DUPFD, 3) < 0)) perror_exit("dupfd IO");
156 static void setup_inout()
158 /* for C_BS, in/out is done as it is. so only in.sz is enough.
159 * With Single buffer there will be overflow in a read following partial read
161 TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + ((toys.optflags & C_BS)? 0: TT.out.sz));
162 TT.in.bp = TT.out.bp = TT.in.buff;
165 TT.in.name = "stdin";
166 TT.in.fd = STDIN_FILENO;
168 TT.in.fd = xopen(TT.in.name, O_RDONLY);
169 TT.in.fd = xmove_fd(TT.in.fd);
173 TT.out.name = "stdout";
174 TT.out.fd = STDOUT_FILENO;
176 int flags = O_WRONLY|O_CREAT;
177 if (!(toys.optflags&C_NOTRUNC)) flags |= O_TRUNC;
178 TT.out.fd = xcreate(TT.out.name, flags, 0666);
179 TT.out.fd = xmove_fd(TT.out.fd);
183 if (lseek(TT.in.fd, (off_t)(TT.in.offset * TT.in.sz), SEEK_CUR) < 0) {
184 while (TT.in.offset--) {
185 ssize_t n = read(TT.in.fd, TT.in.bp, TT.in.sz);
188 if (toys.optflags & C_NOERROR) { //warn message and status
189 error_msg("%s: read error", TT.in.name);
191 } else perror_exit("%s: read error", TT.in.name);
193 xprintf("%s: Can't skip\n", TT.in.name);
200 if (TT.out.offset) xlseek(TT.out.fd, (off_t)(TT.out.offset * TT.out.sz), SEEK_CUR);
203 static void write_out(int all)
205 TT.out.bp = TT.out.buff;
206 while (TT.out.count) {
207 ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
209 all = 0; //further writes will be on obs
210 if (nw <= 0) perror_exit("%s: write error", TT.out.name);
211 if (nw == TT.out.sz) TT.out_full++;
216 if (TT.out.count < TT.out.sz) break;
218 if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
221 static int comp(const void *a, const void *b) //const to shut compiler up
223 return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name);
228 struct pair *res, key;
232 TT.show_xfer = TT.show_records = 1;
234 TT.in.sz = TT.out.sz = 512; //default io block size
235 while (*toys.optargs) {
236 if (!(arg = strchr(*toys.optargs, '='))) error_exit("unknown arg %s", *toys.optargs);
238 if (!*arg) help_exit(0);
239 key.name = *toys.optargs;
240 if (!(res = bsearch(&key, operands, ARRAY_LEN(operands), sizeof(struct pair),
241 comp))) error_exit("unknown arg %s", key.name);
243 toys.optflags |= res->val;
246 TT.in.sz = TT.out.sz = strsuftoll(arg, 1, LONG_MAX);
249 sz = strsuftoll(arg, 1, LONG_MAX);
250 if (!(toys.optflags & C_BS)) TT.in.sz = sz;
253 sz = strsuftoll(arg, 1, LONG_MAX);
254 if (!(toys.optflags & C_BS)) TT.out.sz = sz;
257 TT.c_count = strsuftoll(arg, 0, ULLONG_MAX);
266 TT.out.offset = strsuftoll(arg, 0, ULLONG_MAX);
269 TT.in.offset = strsuftoll(arg, 0, ULLONG_MAX);
272 if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
273 else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
274 else error_exit("unknown status '%s'", arg);
278 key.name = strsep(&arg, ",");
279 if (!(res = bsearch(&key, clist, ARRAY_LEN(clist),
280 sizeof(struct pair), comp)))
281 error_exit("unknown conversion %s", key.name);
283 toys.optflags |= res->val;
290 signal(SIGINT, generic_signal);
291 signal(SIGUSR1, generic_signal);
294 gettimeofday(&TT.start, NULL);
296 if ((toys.optflags&C_SEEK) && !(toys.optflags & C_NOTRUNC))
297 ftruncate(TT.out.fd, TT.out.offset * TT.out.sz);
299 while (!(toys.optflags & C_COUNT) || (TT.in_full + TT.in_part) < TT.c_count) {
303 // Show progress and exit on SIGINT or just continue on SIGUSR1.
305 if (toys.signal == SIGINT) exit_signal(toys.signal);
309 TT.in.bp = TT.in.buff + TT.in.count;
310 if (toys.optflags & C_SYNC) memset(TT.in.bp, 0, TT.in.sz);
311 if (!(n = read(TT.in.fd, TT.in.bp, TT.in.sz))) break;
313 if (errno == EINTR) continue;
315 perror_msg("%s: read error", TT.in.name);
316 if (!(toys.optflags & C_NOERROR)) exit(1);
318 xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
319 if (!(toys.optflags & C_SYNC)) continue;
320 // if SYNC, then treat as full block of nuls
328 if (toys.optflags & C_SYNC) TT.in.count += TT.in.sz;
329 else TT.in.count += n;
332 TT.out.count = TT.in.count;
333 if (toys.optflags & C_BS) {
339 if (TT.in.count >= TT.out.sz) {
341 TT.in.count = TT.out.count;
344 if (TT.out.count) write_out(1); //write any remaining input blocks
345 if (toys.optflags & C_FSYNC && fsync(TT.out.fd) < 0)
346 perror_exit("%s: fsync fail", TT.out.name);
350 if (TT.in.buff) free(TT.in.buff);