dd: support conv=swab
[oweals/busybox.git] / coreutils / dd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dd implementation for busybox
4  *
5  *
6  * Copyright (C) 2000,2001  Matt Kraai
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 //usage:#define dd_trivial_usage
12 //usage:       "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
13 //usage:       "        [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]")
14 //usage:#define dd_full_usage "\n\n"
15 //usage:       "Copy a file with converting and formatting\n"
16 //usage:     "\n        if=FILE         Read from FILE instead of stdin"
17 //usage:     "\n        of=FILE         Write to FILE instead of stdout"
18 //usage:     "\n        bs=N            Read and write N bytes at a time"
19 //usage:        IF_FEATURE_DD_IBS_OBS(
20 //usage:     "\n        ibs=N           Read N bytes at a time"
21 //usage:        )
22 //usage:        IF_FEATURE_DD_IBS_OBS(
23 //usage:     "\n        obs=N           Write N bytes at a time"
24 //usage:        )
25 //usage:     "\n        count=N         Copy only N input blocks"
26 //usage:     "\n        skip=N          Skip N input blocks"
27 //usage:     "\n        seek=N          Skip N output blocks"
28 //usage:        IF_FEATURE_DD_IBS_OBS(
29 //usage:     "\n        conv=notrunc    Don't truncate output file"
30 //usage:     "\n        conv=noerror    Continue after read errors"
31 //usage:     "\n        conv=sync       Pad blocks with zeros"
32 //usage:     "\n        conv=fsync      Physically write data out before finishing"
33 //usage:     "\n        conv=swab       Swap every pair of bytes"
34 //usage:        )
35 //usage:     "\n"
36 //usage:     "\nN may be suffixed by c (1), w (2), b (512), kD (1000), k (1024), MD, M, GD, G"
37 //usage:
38 //usage:#define dd_example_usage
39 //usage:       "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
40 //usage:       "4+0 records in\n"
41 //usage:       "4+0 records out\n"
42
43 #include "libbb.h"
44
45 /* This is a NOEXEC applet. Be very careful! */
46
47
48 enum {
49         ifd = STDIN_FILENO,
50         ofd = STDOUT_FILENO,
51 };
52
53 static const struct suffix_mult dd_suffixes[] = {
54         { "c", 1 },
55         { "w", 2 },
56         { "b", 512 },
57         { "kD", 1000 },
58         { "k", 1024 },
59         { "K", 1024 },  /* compat with coreutils dd */
60         { "MD", 1000000 },
61         { "M", 1048576 },
62         { "GD", 1000000000 },
63         { "G", 1073741824 },
64         { "", 0 }
65 };
66
67 struct globals {
68         off_t out_full, out_part, in_full, in_part;
69 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
70         unsigned long long total_bytes;
71         unsigned long long begin_time_us;
72 #endif
73 } FIX_ALIASING;
74 #define G (*(struct globals*)&bb_common_bufsiz1)
75 #define INIT_G() do { \
76         /* we have to zero it out because of NOEXEC */ \
77         memset(&G, 0, sizeof(G)); \
78 } while (0)
79
80
81 static void dd_output_status(int UNUSED_PARAM cur_signal)
82 {
83 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
84         double seconds;
85         unsigned long long bytes_sec;
86         unsigned long long now_us = monotonic_us(); /* before fprintf */
87 #endif
88
89         /* Deliberately using %u, not %d */
90         fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
91                         "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
92                         G.in_full, G.in_part,
93                         G.out_full, G.out_part);
94
95 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
96         fprintf(stderr, "%llu bytes (%sB) copied, ",
97                         G.total_bytes,
98                         /* show fractional digit, use suffixes */
99                         make_human_readable_str(G.total_bytes, 1, 0)
100         );
101         /* Corner cases:
102          * ./busybox dd </dev/null >/dev/null
103          * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
104          * (echo DONE) | ./busybox dd >/dev/null
105          * (sleep 1; echo DONE) | ./busybox dd >/dev/null
106          */
107         seconds = (now_us - G.begin_time_us) / 1000000.0;
108         bytes_sec = G.total_bytes / seconds;
109         fprintf(stderr, "%f seconds, %sB/s\n",
110                         seconds,
111                         /* show fractional digit, use suffixes */
112                         make_human_readable_str(bytes_sec, 1, 0)
113         );
114 #endif
115 }
116
117 static ssize_t full_write_or_warn(const void *buf, size_t len,
118         const char *const filename)
119 {
120         ssize_t n = full_write(ofd, buf, len);
121         if (n < 0)
122                 bb_perror_msg("writing '%s'", filename);
123         return n;
124 }
125
126 static bool write_and_stats(const void *buf, size_t len, size_t obs,
127         const char *filename)
128 {
129         ssize_t n = full_write_or_warn(buf, len, filename);
130         if (n < 0)
131                 return 1;
132         if ((size_t)n == obs)
133                 G.out_full++;
134         else if (n) /* > 0 */
135                 G.out_part++;
136 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
137         G.total_bytes += n;
138 #endif
139         return 0;
140 }
141
142 #if ENABLE_LFS
143 # define XATOU_SFX xatoull_sfx
144 #else
145 # define XATOU_SFX xatoul_sfx
146 #endif
147
148 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 int dd_main(int argc UNUSED_PARAM, char **argv)
150 {
151         enum {
152                 /* Must be in the same order as OP_conv_XXX! */
153                 /* (see "flags |= (1 << what)" below) */
154                 FLAG_NOTRUNC = 1 << 0,
155                 FLAG_SYNC    = 1 << 1,
156                 FLAG_NOERROR = 1 << 2,
157                 FLAG_FSYNC   = 1 << 3,
158                 FLAG_SWAB    = 1 << 4,
159                 /* end of conv flags */
160                 FLAG_TWOBUFS = 1 << 5,
161                 FLAG_COUNT   = 1 << 6,
162         };
163         static const char keywords[] ALIGN1 =
164                 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
165 #if ENABLE_FEATURE_DD_IBS_OBS
166                 "ibs\0""obs\0""conv\0"
167 #endif
168                 ;
169 #if ENABLE_FEATURE_DD_IBS_OBS
170         static const char conv_words[] ALIGN1 =
171                 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
172 #endif
173         enum {
174                 OP_bs = 0,
175                 OP_count,
176                 OP_seek,
177                 OP_skip,
178                 OP_if,
179                 OP_of,
180 #if ENABLE_FEATURE_DD_IBS_OBS
181                 OP_ibs,
182                 OP_obs,
183                 OP_conv,
184                 /* Must be in the same order as FLAG_XXX! */
185                 OP_conv_notrunc = 0,
186                 OP_conv_sync,
187                 OP_conv_noerror,
188                 OP_conv_fsync,
189                 OP_conv_swab,
190         /* Unimplemented conv=XXX: */
191         //nocreat       do not create the output file
192         //excl          fail if the output file already exists
193         //fdatasync     physically write output file data before finishing
194         //lcase         change upper case to lower case
195         //ucase         change lower case to upper case
196         //block         pad newline-terminated records with spaces to cbs-size
197         //unblock       replace trailing spaces in cbs-size records with newline
198         //ascii         from EBCDIC to ASCII
199         //ebcdic        from ASCII to EBCDIC
200         //ibm           from ASCII to alternate EBCDIC
201         /* Partially implemented: */
202         //swab          swap every pair of input bytes: will abort on non-even reads
203 #endif
204         };
205         int exitcode = EXIT_FAILURE;
206         size_t ibs = 512, obs = 512;
207         int i;
208         char *ibuf, *obuf;
209         /* And these are all zeroed at once! */
210         struct {
211                 int flags;
212                 size_t oc;
213                 off_t count;
214                 off_t seek, skip;
215                 const char *infile, *outfile;
216         } Z;
217 #define flags   (Z.flags  )
218 #define oc      (Z.oc     )
219 #define count   (Z.count  )
220 #define seek    (Z.seek   )
221 #define skip    (Z.skip   )
222 #define infile  (Z.infile )
223 #define outfile (Z.outfile)
224
225         memset(&Z, 0, sizeof(Z));
226         INIT_G();
227         //fflush_all(); - is this needed because of NOEXEC?
228
229         for (i = 1; argv[i]; i++) {
230                 int what;
231                 char *val;
232                 char *arg = argv[i];
233
234 #if ENABLE_DESKTOP
235                 /* "dd --". NB: coreutils 6.9 will complain if they see
236                  * more than one of them. We wouldn't. */
237                 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
238                         continue;
239 #endif
240                 val = strchr(arg, '=');
241                 if (val == NULL)
242                         bb_show_usage();
243                 *val = '\0';
244                 what = index_in_strings(keywords, arg);
245                 if (what < 0)
246                         bb_show_usage();
247                 /* *val = '='; - to preserve ps listing? */
248                 val++;
249 #if ENABLE_FEATURE_DD_IBS_OBS
250                 if (what == OP_ibs) {
251                         /* Must fit into positive ssize_t */
252                         ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
253                         /*continue;*/
254                 }
255                 if (what == OP_obs) {
256                         obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
257                         /*continue;*/
258                 }
259                 if (what == OP_conv) {
260                         while (1) {
261                                 /* find ',', replace them with NUL so we can use val for
262                                  * index_in_strings() without copying.
263                                  * We rely on val being non-null, else strchr would fault.
264                                  */
265                                 arg = strchr(val, ',');
266                                 if (arg)
267                                         *arg = '\0';
268                                 what = index_in_strings(conv_words, val);
269                                 if (what < 0)
270                                         bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
271                                 flags |= (1 << what);
272                                 if (!arg) /* no ',' left, so this was the last specifier */
273                                         break;
274                                 /* *arg = ','; - to preserve ps listing? */
275                                 val = arg + 1; /* skip this keyword and ',' */
276                         }
277                         continue; /* we trashed 'what', can't fall through */
278                 }
279 #endif
280                 if (what == OP_bs) {
281                         ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
282                         /*continue;*/
283                 }
284                 /* These can be large: */
285                 if (what == OP_count) {
286                         flags |= FLAG_COUNT;
287                         count = XATOU_SFX(val, dd_suffixes);
288                         /*continue;*/
289                 }
290                 if (what == OP_seek) {
291                         seek = XATOU_SFX(val, dd_suffixes);
292                         /*continue;*/
293                 }
294                 if (what == OP_skip) {
295                         skip = XATOU_SFX(val, dd_suffixes);
296                         /*continue;*/
297                 }
298                 if (what == OP_if) {
299                         infile = val;
300                         /*continue;*/
301                 }
302                 if (what == OP_of) {
303                         outfile = val;
304                         /*continue;*/
305                 }
306         } /* end of "for (argv[i])" */
307
308 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
309         ibuf = obuf = xmalloc(ibs);
310         if (ibs != obs) {
311                 flags |= FLAG_TWOBUFS;
312                 obuf = xmalloc(obs);
313         }
314
315 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
316         signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
317 #endif
318 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
319         G.begin_time_us = monotonic_us();
320 #endif
321
322         if (infile != NULL)
323                 xmove_fd(xopen(infile, O_RDONLY), ifd);
324         else {
325                 infile = bb_msg_standard_input;
326         }
327         if (outfile != NULL) {
328                 int oflag = O_WRONLY | O_CREAT;
329
330                 if (!seek && !(flags & FLAG_NOTRUNC))
331                         oflag |= O_TRUNC;
332
333                 xmove_fd(xopen(outfile, oflag), ofd);
334
335                 if (seek && !(flags & FLAG_NOTRUNC)) {
336                         if (ftruncate(ofd, seek * obs) < 0) {
337                                 struct stat st;
338
339                                 if (fstat(ofd, &st) < 0
340                                  || S_ISREG(st.st_mode)
341                                  || S_ISDIR(st.st_mode)
342                                 ) {
343                                         goto die_outfile;
344                                 }
345                         }
346                 }
347         } else {
348                 outfile = bb_msg_standard_output;
349         }
350         if (skip) {
351                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
352                         while (skip-- > 0) {
353                                 ssize_t n = safe_read(ifd, ibuf, ibs);
354                                 if (n < 0)
355                                         goto die_infile;
356                                 if (n == 0)
357                                         break;
358                         }
359                 }
360         }
361         if (seek) {
362                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
363                         goto die_outfile;
364         }
365
366         while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
367                 ssize_t n;
368
369                 n = safe_read(ifd, ibuf, ibs);
370                 if (n == 0)
371                         break;
372                 if (n < 0) {
373                         /* "Bad block" */
374                         if (!(flags & FLAG_NOERROR))
375                                 goto die_infile;
376                         bb_simple_perror_msg(infile);
377                         /* GNU dd with conv=noerror skips over bad blocks */
378                         xlseek(ifd, ibs, SEEK_CUR);
379                         /* conv=noerror,sync writes NULs,
380                          * conv=noerror just ignores input bad blocks */
381                         n = 0;
382                 }
383                 if (flags & FLAG_SWAB) {
384                         /* If n is odd, last byte is not swapped:
385                          *  echo -n "qwe" | dd conv=swab bs=1
386                          * prints "wqe".
387                          * The code does not handle correctly odd-sized reads
388                          * in the *middle* of the input. FIXME.
389                          */
390                         uint16_t *p16 = (void*) ibuf;
391                         uint16_t *end = (void*) (ibuf + (n & ~(ssize_t)1));
392                         while (p16 < end) {
393                                 *p16 = bswap_16(*p16);
394                                 p16++;
395                         }
396                 }
397                 if ((size_t)n == ibs)
398                         G.in_full++;
399                 else {
400                         G.in_part++;
401                         if (flags & FLAG_SYNC) {
402                                 memset(ibuf + n, 0, ibs - n);
403                                 n = ibs;
404                         }
405                 }
406                 if (flags & FLAG_TWOBUFS) {
407                         char *tmp = ibuf;
408                         while (n) {
409                                 size_t d = obs - oc;
410
411                                 if (d > (size_t)n)
412                                         d = n;
413                                 memcpy(obuf + oc, tmp, d);
414                                 n -= d;
415                                 tmp += d;
416                                 oc += d;
417                                 if (oc == obs) {
418                                         if (write_and_stats(obuf, obs, obs, outfile))
419                                                 goto out_status;
420                                         oc = 0;
421                                 }
422                         }
423                 } else if (write_and_stats(ibuf, n, obs, outfile))
424                         goto out_status;
425
426                 if (flags & FLAG_FSYNC) {
427                         if (fsync(ofd) < 0)
428                                 goto die_outfile;
429                 }
430         }
431
432         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
433                 ssize_t w = full_write_or_warn(obuf, oc, outfile);
434                 if (w < 0) goto out_status;
435                 if (w > 0) G.out_part++;
436         }
437         if (close(ifd) < 0) {
438  die_infile:
439                 bb_simple_perror_msg_and_die(infile);
440         }
441
442         if (close(ofd) < 0) {
443  die_outfile:
444                 bb_simple_perror_msg_and_die(outfile);
445         }
446
447         exitcode = EXIT_SUCCESS;
448  out_status:
449         dd_output_status(0);
450
451         if (ENABLE_FEATURE_CLEAN_UP) {
452                 free(obuf);
453                 if (flags & FLAG_TWOBUFS)
454                         free(ibuf);
455         }
456
457         return exitcode;
458 }