Trim help text a bit more
[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 tarball for details.
9  */
10
11 #include "libbb.h"
12
13 /* This is a NOEXEC applet. Be very careful! */
14
15
16 enum {
17         ifd = STDIN_FILENO,
18         ofd = STDOUT_FILENO,
19 };
20
21 static const struct suffix_mult dd_suffixes[] = {
22         { "c", 1 },
23         { "w", 2 },
24         { "b", 512 },
25         { "kD", 1000 },
26         { "k", 1024 },
27         { "K", 1024 },  /* compat with coreutils dd */
28         { "MD", 1000000 },
29         { "M", 1048576 },
30         { "GD", 1000000000 },
31         { "G", 1073741824 },
32         { "", 0 }
33 };
34
35 struct globals {
36         off_t out_full, out_part, in_full, in_part;
37 };
38 #define G (*(struct globals*)&bb_common_bufsiz1)
39 #define INIT_G() do { \
40         /* we have to zero it out because of NOEXEC */ \
41         memset(&G, 0, sizeof(G)); \
42 } while (0)
43
44
45 static void dd_output_status(int UNUSED_PARAM cur_signal)
46 {
47         /* Deliberately using %u, not %d */
48         fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
49                         "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
50                         G.in_full, G.in_part,
51                         G.out_full, G.out_part);
52 }
53
54 static ssize_t full_write_or_warn(const void *buf, size_t len,
55         const char *const filename)
56 {
57         ssize_t n = full_write(ofd, buf, len);
58         if (n < 0)
59                 bb_perror_msg("writing '%s'", filename);
60         return n;
61 }
62
63 static bool write_and_stats(const void *buf, size_t len, size_t obs,
64         const char *filename)
65 {
66         ssize_t n = full_write_or_warn(buf, len, filename);
67         if (n < 0)
68                 return 1;
69         if ((size_t)n == obs)
70                 G.out_full++;
71         else if (n) /* > 0 */
72                 G.out_part++;
73         return 0;
74 }
75
76 #if ENABLE_LFS
77 #define XATOU_SFX xatoull_sfx
78 #else
79 #define XATOU_SFX xatoul_sfx
80 #endif
81
82 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
83 int dd_main(int argc UNUSED_PARAM, char **argv)
84 {
85         enum {
86                 /* Must be in the same order as OP_conv_XXX! */
87                 /* (see "flags |= (1 << what)" below) */
88                 FLAG_NOTRUNC = 1 << 0,
89                 FLAG_SYNC    = 1 << 1,
90                 FLAG_NOERROR = 1 << 2,
91                 FLAG_FSYNC   = 1 << 3,
92                 /* end of conv flags */
93                 FLAG_TWOBUFS = 1 << 4,
94                 FLAG_COUNT   = 1 << 5,
95         };
96         static const char keywords[] ALIGN1 =
97                 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
98 #if ENABLE_FEATURE_DD_IBS_OBS
99                 "ibs\0""obs\0""conv\0"
100 #endif
101                 ;
102 #if ENABLE_FEATURE_DD_IBS_OBS
103         static const char conv_words[] ALIGN1 =
104                 "notrunc\0""sync\0""noerror\0""fsync\0";
105 #endif
106         enum {
107                 OP_bs = 0,
108                 OP_count,
109                 OP_seek,
110                 OP_skip,
111                 OP_if,
112                 OP_of,
113 #if ENABLE_FEATURE_DD_IBS_OBS
114                 OP_ibs,
115                 OP_obs,
116                 OP_conv,
117                 /* Must be in the same order as FLAG_XXX! */
118                 OP_conv_notrunc = 0,
119                 OP_conv_sync,
120                 OP_conv_noerror,
121                 OP_conv_fsync,
122         /* Unimplemented conv=XXX: */
123         //nocreat       do not create the output file
124         //excl          fail if the output file already exists
125         //fdatasync     physically write output file data before finishing
126         //swab          swap every pair of input bytes
127         //lcase         change upper case to lower case
128         //ucase         change lower case to upper case
129         //block         pad newline-terminated records with spaces to cbs-size
130         //unblock       replace trailing spaces in cbs-size records with newline
131         //ascii         from EBCDIC to ASCII
132         //ebcdic        from ASCII to EBCDIC
133         //ibm           from ASCII to alternate EBCDIC
134 #endif
135         };
136         int exitcode = EXIT_FAILURE;
137         size_t ibs = 512, obs = 512;
138         ssize_t n, w;
139         char *ibuf, *obuf;
140         /* And these are all zeroed at once! */
141         struct {
142                 int flags;
143                 size_t oc;
144                 off_t count;
145                 off_t seek, skip;
146                 const char *infile, *outfile;
147         } Z;
148 #define flags   (Z.flags  )
149 #define oc      (Z.oc     )
150 #define count   (Z.count  )
151 #define seek    (Z.seek   )
152 #define skip    (Z.skip   )
153 #define infile  (Z.infile )
154 #define outfile (Z.outfile)
155
156         memset(&Z, 0, sizeof(Z));
157         INIT_G();
158         //fflush(NULL); - is this needed because of NOEXEC?
159
160 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
161         signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
162 #endif
163
164         for (n = 1; argv[n]; n++) {
165                 int what;
166                 char *val;
167                 char *arg = argv[n];
168
169 #if ENABLE_DESKTOP
170                 /* "dd --". NB: coreutils 6.9 will complain if they see
171                  * more than one of them. We wouldn't. */
172                 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
173                         continue;
174 #endif
175                 val = strchr(arg, '=');
176                 if (val == NULL)
177                         bb_show_usage();
178                 *val = '\0';
179                 what = index_in_strings(keywords, arg);
180                 if (what < 0)
181                         bb_show_usage();
182                 /* *val = '='; - to preserve ps listing? */
183                 val++;
184 #if ENABLE_FEATURE_DD_IBS_OBS
185                 if (what == OP_ibs) {
186                         /* Must fit into positive ssize_t */
187                         ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
188                         /*continue;*/
189                 }
190                 if (what == OP_obs) {
191                         obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
192                         /*continue;*/
193                 }
194                 if (what == OP_conv) {
195                         while (1) {
196                                 /* find ',', replace them with NUL so we can use val for
197                                  * index_in_strings() without copying.
198                                  * We rely on val being non-null, else strchr would fault.
199                                  */
200                                 arg = strchr(val, ',');
201                                 if (arg)
202                                         *arg = '\0';
203                                 what = index_in_strings(conv_words, val);
204                                 if (what < 0)
205                                         bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
206                                 flags |= (1 << what);
207                                 if (!arg) /* no ',' left, so this was the last specifier */
208                                         break;
209                                 /* *arg = ','; - to preserve ps listing? */
210                                 val = arg + 1; /* skip this keyword and ',' */
211                         }
212                         continue; /* we trashed 'what', can't fall through */
213                 }
214 #endif
215                 if (what == OP_bs) {
216                         ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
217                         /*continue;*/
218                 }
219                 /* These can be large: */
220                 if (what == OP_count) {
221                         flags |= FLAG_COUNT;
222                         count = XATOU_SFX(val, dd_suffixes);
223                         /*continue;*/
224                 }
225                 if (what == OP_seek) {
226                         seek = XATOU_SFX(val, dd_suffixes);
227                         /*continue;*/
228                 }
229                 if (what == OP_skip) {
230                         skip = XATOU_SFX(val, dd_suffixes);
231                         /*continue;*/
232                 }
233                 if (what == OP_if) {
234                         infile = val;
235                         /*continue;*/
236                 }
237                 if (what == OP_of) {
238                         outfile = val;
239                         /*continue;*/
240                 }
241         } /* end of "for (argv[n])" */
242
243 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
244         ibuf = obuf = xmalloc(ibs);
245         if (ibs != obs) {
246                 flags |= FLAG_TWOBUFS;
247                 obuf = xmalloc(obs);
248         }
249         if (infile != NULL)
250                 xmove_fd(xopen(infile, O_RDONLY), ifd);
251         else {
252                 infile = bb_msg_standard_input;
253         }
254         if (outfile != NULL) {
255                 int oflag = O_WRONLY | O_CREAT;
256
257                 if (!seek && !(flags & FLAG_NOTRUNC))
258                         oflag |= O_TRUNC;
259
260                 xmove_fd(xopen(outfile, oflag), ofd);
261
262                 if (seek && !(flags & FLAG_NOTRUNC)) {
263                         if (ftruncate(ofd, seek * obs) < 0) {
264                                 struct stat st;
265
266                                 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
267                                                 S_ISDIR(st.st_mode))
268                                         goto die_outfile;
269                         }
270                 }
271         } else {
272                 outfile = bb_msg_standard_output;
273         }
274         if (skip) {
275                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
276                         while (skip-- > 0) {
277                                 n = safe_read(ifd, ibuf, ibs);
278                                 if (n < 0)
279                                         goto die_infile;
280                                 if (n == 0)
281                                         break;
282                         }
283                 }
284         }
285         if (seek) {
286                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
287                         goto die_outfile;
288         }
289
290         while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
291                 n = safe_read(ifd, ibuf, ibs);
292                 if (n == 0)
293                         break;
294                 if (n < 0) {
295                         /* "Bad block" */
296                         if (!(flags & FLAG_NOERROR))
297                                 goto die_infile;
298                         bb_simple_perror_msg(infile);
299                         /* GNU dd with conv=noerror skips over bad blocks */
300                         xlseek(ifd, ibs, SEEK_CUR);
301                         /* conv=noerror,sync writes NULs,
302                          * conv=noerror just ignores input bad blocks */
303                         n = 0;
304                 }
305                 if ((size_t)n == ibs)
306                         G.in_full++;
307                 else {
308                         G.in_part++;
309                         if (flags & FLAG_SYNC) {
310                                 memset(ibuf + n, 0, ibs - n);
311                                 n = ibs;
312                         }
313                 }
314                 if (flags & FLAG_TWOBUFS) {
315                         char *tmp = ibuf;
316                         while (n) {
317                                 size_t d = obs - oc;
318
319                                 if (d > (size_t)n)
320                                         d = n;
321                                 memcpy(obuf + oc, tmp, d);
322                                 n -= d;
323                                 tmp += d;
324                                 oc += d;
325                                 if (oc == obs) {
326                                         if (write_and_stats(obuf, obs, obs, outfile))
327                                                 goto out_status;
328                                         oc = 0;
329                                 }
330                         }
331                 } else if (write_and_stats(ibuf, n, obs, outfile))
332                         goto out_status;
333
334                 if (flags & FLAG_FSYNC) {
335                         if (fsync(ofd) < 0)
336                                 goto die_outfile;
337                 }
338         }
339
340         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
341                 w = full_write_or_warn(obuf, oc, outfile);
342                 if (w < 0) goto out_status;
343                 if (w > 0) G.out_part++;
344         }
345         if (close(ifd) < 0) {
346  die_infile:
347                 bb_simple_perror_msg_and_die(infile);
348         }
349
350         if (close(ofd) < 0) {
351  die_outfile:
352                 bb_simple_perror_msg_and_die(outfile);
353         }
354
355         exitcode = EXIT_SUCCESS;
356  out_status:
357         dd_output_status(0);
358
359         return exitcode;
360 }