dd: use unsigned printf specifier for # of blocks
[platform/upstream/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 <signal.h>  /* For FEATURE_DD_SIGNAL_HANDLING */
12 #include "libbb.h"
13
14 /* This is a NOEXEC applet. Be very careful! */
15
16
17 static const struct suffix_mult dd_suffixes[] = {
18         { "c", 1 },
19         { "w", 2 },
20         { "b", 512 },
21         { "kD", 1000 },
22         { "k", 1024 },
23         { "K", 1024 },  /* compat with coreutils dd */
24         { "MD", 1000000 },
25         { "M", 1048576 },
26         { "GD", 1000000000 },
27         { "G", 1073741824 },
28         { }
29 };
30
31 struct globals {
32         off_t out_full, out_part, in_full, in_part;
33 };
34 #define G (*(struct globals*)&bb_common_bufsiz1)
35 /* We have to zero it out because of NOEXEC */
36 #define INIT_G() memset(&G, 0, sizeof(G))
37
38
39 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
40 {
41         /* Deliberately using %u, not %d */
42         fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
43                         "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
44                         G.in_full, G.in_part,
45                         G.out_full, G.out_part);
46 }
47
48 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
49         const char * const filename)
50 {
51         ssize_t n = full_write(fd, buf, len);
52         if (n < 0)
53                 bb_perror_msg("writing '%s'", filename);
54         return n;
55 }
56
57 static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
58         const char *filename)
59 {
60         ssize_t n = full_write_or_warn(fd, buf, len, filename);
61         if (n < 0)
62                 return 1;
63         if (n == obs)
64                 G.out_full++;
65         else if (n) /* > 0 */
66                 G.out_part++;
67         return 0;
68 }
69
70 #if ENABLE_LFS
71 #define XATOU_SFX xatoull_sfx
72 #else
73 #define XATOU_SFX xatoul_sfx
74 #endif
75
76 int dd_main(int argc, char **argv);
77 int dd_main(int argc, char **argv)
78 {
79         enum {
80                 FLAG_SYNC    = 1 << 0,
81                 FLAG_NOERROR = 1 << 1,
82                 FLAG_NOTRUNC = 1 << 2,
83                 FLAG_TWOBUFS = 1 << 3,
84                 FLAG_COUNT   = 1 << 4,
85         };
86         static const char keywords[] =
87                 "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
88 #if ENABLE_FEATURE_DD_IBS_OBS
89                 "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
90 #endif
91                 ;
92         enum {
93                 OP_bs = 1,
94                 OP_count,
95                 OP_seek,
96                 OP_skip,
97                 OP_if,
98                 OP_of,
99 #if ENABLE_FEATURE_DD_IBS_OBS
100                 OP_ibs,
101                 OP_obs,
102                 OP_conv,
103                 OP_conv_notrunc,
104                 OP_conv_sync,
105                 OP_conv_noerror,
106 #endif
107         };
108         size_t ibs = 512, obs = 512;
109         ssize_t n, w;
110         char *ibuf, *obuf;
111         /* And these are all zeroed at once! */
112         struct {
113                 int flags;
114                 int ifd, ofd;
115                 size_t oc;
116                 off_t count;
117                 off_t seek, skip;
118                 const char *infile, *outfile;
119 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
120                 struct sigaction sigact;
121 #endif
122         } Z;
123 #define flags   (Z.flags  )
124 #define ifd     (Z.ifd    )
125 #define ofd     (Z.ofd    )
126 #define oc      (Z.oc     )
127 #define count   (Z.count  )
128 #define seek    (Z.seek   )
129 #define skip    (Z.skip   )
130 #define infile  (Z.infile )
131 #define outfile (Z.outfile)
132 #define sigact  (Z.sigact )
133
134         memset(&Z, 0, sizeof(Z));
135         INIT_G();
136
137 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
138         sigact.sa_handler = dd_output_status;
139         sigact.sa_flags = SA_RESTART;
140         sigemptyset(&sigact.sa_mask);
141         sigaction(SIGUSR1, &sigact, NULL);
142 #endif
143
144         for (n = 1; n < argc; n++) {
145                 smalluint key_len;
146                 smalluint what;
147                 char *key;
148                 char *arg = argv[n];
149
150 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
151 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
152                 key = strstr(arg, "=");
153                 if (key == NULL)
154                         bb_show_usage();
155                 key_len = key - arg + 1;
156                 key = xstrndup(arg, key_len);
157                 what = index_in_strings(keywords, key) + 1;
158                 if (ENABLE_FEATURE_CLEAN_UP)
159                         free(key);
160                 if (what == 0)
161                         bb_show_usage();
162                 arg += key_len;
163                 /* Must fit into positive ssize_t */
164 #if ENABLE_FEATURE_DD_IBS_OBS
165                         if (what == OP_ibs) {
166                                 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
167                                 continue;
168                         }
169                         if (what == OP_obs) {
170                                 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
171                                 continue;
172                         }
173                         if (what == OP_conv) {
174                                 while (1) {
175                                         /* find ',', replace them with nil so we can use arg for
176                                          * index_in_strings() without copying.
177                                          * We rely on arg being non-null, else strchr would fault.
178                                          */
179                                         key = strchr(arg, ',');
180                                         if (key)
181                                                 *key = '\0';
182                                         what = index_in_strings(keywords, arg) + 1;
183                                         if (what < OP_conv_notrunc)
184                                                 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
185                                         if (what == OP_conv_notrunc)
186                                                 flags |= FLAG_NOTRUNC;
187                                         if (what == OP_conv_sync)
188                                                 flags |= FLAG_SYNC;
189                                         if (what == OP_conv_noerror)
190                                                 flags |= FLAG_NOERROR;
191                                         if (!key) /* no ',' left, so this was the last specifier */
192                                                 break;
193                                         arg = key + 1; /* skip this keyword and ',' */
194                                 }
195                                 continue;
196                         }
197 #endif
198                 if (what == OP_bs) {
199                         ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
200                         continue;
201                 }
202                 /* These can be large: */
203                 if (what == OP_count) {
204                         flags |= FLAG_COUNT;
205                         count = XATOU_SFX(arg, dd_suffixes);
206                         continue;
207                 }
208                 if (what == OP_seek) {
209                         seek = XATOU_SFX(arg, dd_suffixes);
210                         continue;
211                 }
212                 if (what == OP_skip) {
213                         skip = XATOU_SFX(arg, dd_suffixes);
214                         continue;
215                 }
216                 if (what == OP_if) {
217                         infile = arg;
218                         continue;
219                 }
220                 if (what == OP_of)
221                         outfile = arg;
222         }
223 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
224         ibuf = obuf = xmalloc(ibs);
225         if (ibs != obs) {
226                 flags |= FLAG_TWOBUFS;
227                 obuf = xmalloc(obs);
228         }
229         if (infile != NULL)
230                 ifd = xopen(infile, O_RDONLY);
231         else {
232                 /* ifd = STDIN_FILENO; - it's zero and it's already there */
233                 infile = bb_msg_standard_input;
234         }
235         if (outfile != NULL) {
236                 int oflag = O_WRONLY | O_CREAT;
237
238                 if (!seek && !(flags & FLAG_NOTRUNC))
239                         oflag |= O_TRUNC;
240
241                 ofd = xopen(outfile, oflag);
242
243                 if (seek && !(flags & FLAG_NOTRUNC)) {
244                         if (ftruncate(ofd, seek * obs) < 0) {
245                                 struct stat st;
246
247                                 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
248                                                 S_ISDIR(st.st_mode))
249                                         goto die_outfile;
250                         }
251                 }
252         } else {
253                 ofd = STDOUT_FILENO;
254                 outfile = bb_msg_standard_output;
255         }
256         if (skip) {
257                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
258                         while (skip-- > 0) {
259                                 n = safe_read(ifd, ibuf, ibs);
260                                 if (n < 0)
261                                         goto die_infile;
262                                 if (n == 0)
263                                         break;
264                         }
265                 }
266         }
267         if (seek) {
268                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
269                         goto die_outfile;
270         }
271
272         while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
273                 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
274                         memset(ibuf, 0, ibs);
275                 n = safe_read(ifd, ibuf, ibs);
276                 if (n == 0)
277                         break;
278                 if (n < 0) {
279                         if (flags & FLAG_NOERROR) {
280                                 n = ibs;
281                                 bb_perror_msg("%s", infile);
282                         } else
283                                 goto die_infile;
284                 }
285                 if ((size_t)n == ibs)
286                         G.in_full++;
287                 else {
288                         G.in_part++;
289                         if (flags & FLAG_SYNC) {
290                                 memset(ibuf + n, '\0', ibs - n);
291                                 n = ibs;
292                         }
293                 }
294                 if (flags & FLAG_TWOBUFS) {
295                         char *tmp = ibuf;
296                         while (n) {
297                                 size_t d = obs - oc;
298
299                                 if (d > n)
300                                         d = n;
301                                 memcpy(obuf + oc, tmp, d);
302                                 n -= d;
303                                 tmp += d;
304                                 oc += d;
305                                 if (oc == obs) {
306                                         if (write_and_stats(ofd, obuf, obs, obs, outfile))
307                                                 goto out_status;
308                                         oc = 0;
309                                 }
310                         }
311                 } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
312                         goto out_status;
313         }
314
315         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
316                 w = full_write_or_warn(ofd, obuf, oc, outfile);
317                 if (w < 0) goto out_status;
318                 if (w > 0)
319                         G.out_part++;
320         }
321         if (close(ifd) < 0) {
322  die_infile:
323                 bb_perror_msg_and_die("%s", infile);
324         }
325
326         if (close(ofd) < 0) {
327  die_outfile:
328                 bb_perror_msg_and_die("%s", outfile);
329         }
330  out_status:
331         dd_output_status(0);
332
333         return EXIT_SUCCESS;
334 }