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