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 * todo: ctrl-c doesn't work, the read() is restarting.
10 USE_DD(NEWTOY(dd, NULL, TOYFLAG_USR|TOYFLAG_BIN))
16 usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
17 [seek=N] [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
20 if=FILE Read from FILE instead of stdin
21 of=FILE Write to FILE instead of stdout
22 bs=N Read and write N bytes at a time
23 ibs=N Read N bytes at a time
24 obs=N Write N bytes at a time
25 count=N Copy only N input blocks
26 skip=N Skip N input blocks
27 seek=N Skip N output blocks
28 conv=notrunc Don't truncate output file
29 conv=noerror Continue after read errors
30 conv=sync Pad blocks with zeros
31 conv=fsync Physically write data out before finishing
32 status=noxfer Don't show transfer rate
33 status=none Don't show transfer rate or records in/out
35 Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
36 MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
45 unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
50 unsigned char *buff, *bp;
52 unsigned long long offset;
57 #define C_FSYNC 0x0200
58 #define C_NOERROR 0x0400
59 #define C_NOTRUNC 0x0800
66 static struct pair suffixes[] = {
67 { "c", 1 }, { "w", 2 }, { "b", 512 },
68 { "kD", 1000 }, { "k", 1024 }, { "K", 1024 },
69 { "MD", 1000000 }, { "M", 1048576 },
70 { "GD", 1000000000 }, { "G", 1073741824 }
73 static struct pair clist[] = {
75 { "noerror", C_NOERROR },
76 { "notrunc", C_NOTRUNC },
80 static unsigned long long strsuftoll(char *arg, int def, unsigned long long max)
82 unsigned long long result;
86 while (isspace(*p)) p++;
87 if (*p == '-') error_exit("invalid number '%s'", arg);
90 result = strtoull(p, &p, 0);
91 if (errno == ERANGE || result > max || result < def)
92 perror_exit("invalid number '%s'", arg);
94 for (i = 0; i < ARRAY_LEN(suffixes); i++)
95 if (!strcmp(p, suffixes[i].name)) idx = i;
96 if (idx == -1 || (max/suffixes[idx].val < result))
97 error_exit("invalid number '%s'", arg);
98 result *= suffixes[idx].val;
108 gettimeofday(&now, NULL);
109 seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
110 (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
113 fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
114 TT.in_full, TT.in_part, TT.out_full, TT.out_part);
117 human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
118 fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
119 human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
120 fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
124 static void write_out(int all)
126 TT.out.bp = TT.out.buff;
127 while (TT.out.count) {
128 ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
130 all = 0; //further writes will be on obs
131 if (nw <= 0) perror_exit("%s: write error", TT.out.name);
132 if (nw == TT.out.sz) TT.out_full++;
137 if (TT.out.count < TT.out.sz) break;
139 if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
142 int strstarteq(char **a, char *b)
146 if (!strstart(&aa, b)) return 0;
147 if (*aa != '=') return 0;
153 static int comp(const void *a, const void *b) //const to shut compiler up
155 return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name);
160 struct pair *res, key;
162 unsigned long long bs = 0;
165 TT.show_xfer = TT.show_records = 1;
166 TT.c_count = ULLONG_MAX;
168 TT.in.sz = TT.out.sz = 512; //default io block size
169 for (args = toys.optargs; *args; args++) {
172 if (strstarteq(&arg, "bs")) bs = strsuftoll(arg, 1, LONG_MAX);
173 else if (strstarteq(&arg, "ibs")) TT.in.sz = strsuftoll(arg, 1, LONG_MAX);
174 else if (strstarteq(&arg, "obs")) TT.out.sz = strsuftoll(arg, 1, LONG_MAX);
175 else if (strstarteq(&arg, "count")) TT.c_count = strsuftoll(arg, 0, ULLONG_MAX-1);
176 else if (strstarteq(&arg, "if")) TT.in.name = arg;
177 else if (strstarteq(&arg, "of")) TT.out.name = arg;
178 else if (strstarteq(&arg, "seek"))
179 TT.out.offset = strsuftoll(arg, 0, ULLONG_MAX);
180 else if (strstarteq(&arg, "skip"))
181 TT.in.offset = strsuftoll(arg, 0, ULLONG_MAX);
182 else if (strstarteq(&arg, "status")) {
183 if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
184 else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
185 else error_exit("unknown status '%s'", arg);
186 } else if (strstarteq(&arg, "conv")) {
188 key.name = strsep(&arg, ",");
189 if (!(res = bsearch(&key, clist, ARRAY_LEN(clist),
190 sizeof(struct pair), comp)))
191 error_exit("unknown conversion %s", key.name);
193 toys.optflags |= res->val;
195 } else error_exit("bad arg %s", arg);
197 if (bs) TT.in.sz = TT.out.sz = bs;
199 signal(SIGINT, generic_signal);
200 signal(SIGUSR1, generic_signal);
201 gettimeofday(&TT.start, NULL);
203 /* for bs=, in/out is done as it is. so only in.sz is enough.
204 * With Single buffer there will be overflow in a read following partial read
206 TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
207 TT.in.bp = TT.out.bp = TT.in.buff;
209 if (!TT.in.name) TT.in.name = "stdin";
210 else TT.in.fd = xopenro(TT.in.name);
212 if (toys.optflags&C_NOTRUNC) trunc = 0;
216 TT.out.name = "stdout";
218 } else TT.out.fd = xcreate(TT.out.name,
219 O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
223 if (lseek(TT.in.fd, (off_t)(TT.in.offset * TT.in.sz), SEEK_CUR) < 0) {
224 while (TT.in.offset--) {
225 ssize_t n = read(TT.in.fd, TT.in.bp, TT.in.sz);
228 perror_msg("%s", TT.in.name);
229 if (toys.optflags & C_NOERROR) status();
232 xprintf("%s: Can't skip\n", TT.in.name);
239 // seek/truncate as necessary. We handled position zero truncate with
240 // O_TRUNC on open, so output to /dev/null and such doesn't error.
241 if (TT.out.fd!=1 && (bs = TT.out.offset*TT.out.sz)) {
242 xlseek(TT.out.fd, bs, SEEK_CUR);
243 if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
246 while (TT.c_count==ULLONG_MAX || (TT.in_full + TT.in_part) < TT.c_count) {
249 // Show progress and exit on SIGINT or just continue on SIGUSR1.
252 if (toys.signal==SIGINT) exit_signal(toys.signal);
256 TT.in.bp = TT.in.buff + TT.in.count;
257 if (toys.optflags & C_SYNC) memset(TT.in.bp, 0, TT.in.sz);
258 if (!(n = read(TT.in.fd, TT.in.bp, TT.in.sz))) break;
260 if (errno == EINTR) continue;
262 perror_msg("%s: read error", TT.in.name);
263 if (!(toys.optflags & C_NOERROR)) exit(1);
265 xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
266 if (!(toys.optflags & C_SYNC)) continue;
267 // if SYNC, then treat as full block of nuls
275 if (toys.optflags & C_SYNC) TT.in.count += TT.in.sz;
276 else TT.in.count += n;
279 TT.out.count = TT.in.count;
286 if (TT.in.count >= TT.out.sz) {
288 TT.in.count = TT.out.count;
291 if (TT.out.count) write_out(1); //write any remaining input blocks
292 if (toys.optflags & C_FSYNC && fsync(TT.out.fd) < 0)
293 perror_exit("%s: fsync fail", TT.out.name);
297 if (TT.in.buff) free(TT.in.buff);