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