1 /* dd.c - program to convert and copy a file.
2  *
3  * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
4  * Copyright 2013 Kyungwan Han <asura321@gmail.com>
5  *
6  * See  http://opengroup.org/onlinepubs/9699919799/utilities/dd.html
7 
8 USE_DD(NEWTOY(dd, 0, TOYFLAG_USR|TOYFLAG_BIN))
9 
10 config DD
11   bool "dd"
12   default n
13   help
14     usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [iflag=FLAGS] [oflag=FLAGS]
15             [bs=N] [count=N] [seek=N] [skip=N]
16             [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
17 
18     Copy/convert files.
19 
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		Input block size
24     obs=N		Output block size
25     count=N		Copy only N input blocks
26     skip=N		Skip N input blocks
27     seek=N		Skip N output blocks
28     iflag=FLAGS	Set input flags
29     oflag=FLAGS	Set output flags
30     conv=notrunc	Don't truncate output file
31     conv=noerror	Continue after read errors
32     conv=sync	Pad blocks with zeros
33     conv=fsync	Physically write data out before finishing
34     status=noxfer	Don't show transfer rate
35     status=none	Don't show transfer rate or records in/out
36 
37     FLAGS is a comma-separated list of:
38 
39     count_bytes	(iflag) interpret count=N in bytes, not blocks
40     seek_bytes	(oflag) interpret seek=N in bytes, not blocks
41     skip_bytes	(iflag) interpret skip=N in bytes, not blocks
42 
43     Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
44     MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
45 */
46 
47 #define FOR_dd
48 #include "toys.h"
49 
50 GLOBALS(
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 
64 struct dd_flag {
65   char *name;
66 };
67 
68 static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
69   {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"},
70 );
71 
72 static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
73   {"count_bytes"}, {"skip_bytes"},
74 );
75 
76 static const struct dd_flag dd_oflag[] = TAGGED_ARRAY(DD_oflag,
77   {"seek_bytes"},
78 );
79 
status()80 static 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 
dd_sigint(int sig)101 static void dd_sigint(int sig)
102 {
103   toys.exitval = sig|128;
104   xexit();
105 }
106 
write_out(int all)107 static 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; //further writes will be on obs
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); //move remainder to front
123 }
124 
parse_flags(char * what,char * arg,const struct dd_flag * flags,int flag_count,unsigned * result)125 static 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 
dd_main()138 void 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; //default io block size
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   sigatexit(status);
178   xsignal(SIGINT, dd_sigint);
179   xsignal(SIGUSR1, status);
180   gettimeofday(&TT.start, NULL);
181 
182   // For bs=, in/out is done as it is. so only in.sz is enough.
183   // With Single buffer there will be overflow in a read following partial read.
184   TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
185   TT.in.bp = TT.out.bp = TT.in.buff;
186 
187   if (!TT.in.name) TT.in.name = "stdin";
188   else TT.in.fd = xopenro(TT.in.name);
189 
190   if (TT.conv & _DD_conv_notrunc) trunc = 0;
191 
192   if (!TT.out.name) {
193     TT.out.name = "stdout";
194     TT.out.fd = 1;
195   } else TT.out.fd = xcreate(TT.out.name,
196     O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
197 
198   // Implement skip=
199   if (TT.in.offset) {
200     off_t off = TT.in.offset;
201 
202     if (!(TT.iflag & _DD_iflag_skip_bytes)) off *= TT.in.sz;
203     if (lseek(TT.in.fd, off, SEEK_CUR) < 0) {
204       while (off > 0) {
205         int chunk = off < TT.in.sz ? off : TT.in.sz;
206         ssize_t n = read(TT.in.fd, TT.in.bp, chunk);
207 
208         if (n < 0) {
209           perror_msg("%s", TT.in.name);
210           if (TT.conv & _DD_conv_noerror) status();
211           else return;
212         } else if (!n) {
213           xprintf("%s: Can't skip\n", TT.in.name);
214           return;
215         }
216         off -= n;
217       }
218     }
219   }
220 
221   // Implement seek= and truncate as necessary. We handled position zero
222   // truncate with O_TRUNC on open, so output to /dev/null and such doesn't
223   // error.
224   bs = TT.out.offset;
225   if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
226   if (bs) {
227     struct stat st;
228 
229     xlseek(TT.out.fd, bs, SEEK_CUR);
230     if (trunc && !fstat(TT.out.fd, &st) && S_ISREG(st.st_mode)
231       && ftruncate(TT.out.fd, bs)) perror_exit("unexpected ftruncate failure");
232   }
233 
234   unsigned long long bytes_left = TT.c_count;
235   if (TT.c_count != ULLONG_MAX && !(TT.iflag & _DD_iflag_count_bytes)) {
236     bytes_left *= TT.in.sz;
237   }
238   while (bytes_left) {
239     int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz;
240     ssize_t n;
241 
242     TT.in.bp = TT.in.buff + TT.in.count;
243     if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz);
244     if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break;
245     if (n < 0) {
246       if (errno == EINTR) continue;
247       //read error case.
248       perror_msg("%s: read error", TT.in.name);
249       if (!(TT.conv & _DD_conv_noerror)) exit(1);
250       status();
251       xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
252       if (!(TT.conv & _DD_conv_sync)) continue;
253       // if SYNC, then treat as full block of nuls
254       n = TT.in.sz;
255     }
256     if (n == TT.in.sz) {
257       TT.in_full++;
258       TT.in.count += n;
259     } else {
260       TT.in_part++;
261       if (TT.conv & _DD_conv_sync) TT.in.count += TT.in.sz;
262       else TT.in.count += n;
263     }
264     bytes_left -= n;
265 
266     TT.out.count = TT.in.count;
267     if (bs) {
268       write_out(1);
269       TT.in.count = 0;
270       continue;
271     }
272 
273     if (TT.in.count >= TT.out.sz) {
274       write_out(0);
275       TT.in.count = TT.out.count;
276     }
277   }
278   if (TT.out.count) write_out(1); //write any remaining input blocks
279   if ((TT.conv & _DD_conv_fsync) && fsync(TT.out.fd)<0)
280     perror_exit("%s: fsync", TT.out.name);
281 
282   close(TT.in.fd);
283   close(TT.out.fd);
284   if (TT.in.buff) free(TT.in.buff);
285 }
286