fix subtle bug inherited from dash
[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 "busybox.h"
12 #include <signal.h>  /* For FEATURE_DD_SIGNAL_HANDLING */
13
14 static const struct suffix_mult dd_suffixes[] = {
15         { "c", 1 },
16         { "w", 2 },
17         { "b", 512 },
18         { "kD", 1000 },
19         { "k", 1024 },
20         { "K", 1024 },  // compat with coreutils dd
21         { "MD", 1000000 },
22         { "M", 1048576 },
23         { "GD", 1000000000 },
24         { "G", 1073741824 },
25         { NULL, 0 }
26 };
27
28 static off_t out_full, out_part, in_full, in_part;
29
30 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
31 {
32         fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
33                         "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
34                         in_full, in_part,
35                         out_full, out_part);
36 }
37
38 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
39                 const char* filename)
40 {
41         ssize_t n = full_write(fd, buf, len);
42         if (n < 0)
43                 bb_perror_msg("writing '%s'", filename);
44         return n;
45 }
46
47 #if ENABLE_LFS
48 #define XATOU_SFX xatoull_sfx
49 #else
50 #define XATOU_SFX xatoul_sfx
51 #endif
52
53 int dd_main(int argc, char **argv)
54 {
55         enum {
56                 sync_flag    = 1 << 0,
57                 noerror      = 1 << 1,
58                 trunc_flag   = 1 << 2,
59                 twobufs_flag = 1 << 3,
60         };
61         int flags = trunc_flag;
62         size_t oc = 0, ibs = 512, obs = 512;
63         ssize_t n, w;
64         off_t seek = 0, skip = 0, count = OFF_T_MAX;
65         int oflag, ifd, ofd;
66         const char *infile = NULL, *outfile = NULL;
67         char *ibuf, *obuf;
68
69         if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
70                 struct sigaction sa;
71
72                 memset(&sa, 0, sizeof(sa));
73                 sa.sa_handler = dd_output_status;
74                 sa.sa_flags = SA_RESTART;
75                 sigemptyset(&sa.sa_mask);
76                 sigaction(SIGUSR1, &sa, 0);
77         }
78
79         for (n = 1; n < argc; n++) {
80                 char *arg = argv[n];
81                 /* Must fit into positive ssize_t */
82                 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
83                         ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
84                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
85                         obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
86                 else if (!strncmp("bs=", arg, 3))
87                         ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
88                 /* These can be large: */
89                 else if (!strncmp("count=", arg, 6))
90                         count = XATOU_SFX(arg+6, dd_suffixes);
91                 else if (!strncmp("seek=", arg, 5))
92                         seek = XATOU_SFX(arg+5, dd_suffixes);
93                 else if (!strncmp("skip=", arg, 5))
94                         skip = XATOU_SFX(arg+5, dd_suffixes);
95
96                 else if (!strncmp("if=", arg, 3))
97                         infile = arg+3;
98                 else if (!strncmp("of=", arg, 3))
99                         outfile = arg+3;
100                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
101                         arg += 5;
102                         while (1) {
103                                 if (!strncmp("notrunc", arg, 7)) {
104                                         flags &= ~trunc_flag;
105                                         arg += 7;
106                                 } else if (!strncmp("sync", arg, 4)) {
107                                         flags |= sync_flag;
108                                         arg += 4;
109                                 } else if (!strncmp("noerror", arg, 7)) {
110                                         flags |= noerror;
111                                         arg += 7;
112                                 } else {
113                                         bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
114                                 }
115                                 if (arg[0] == '\0') break;
116                                 if (*arg++ != ',') bb_show_usage();
117                         }
118                 } else
119                         bb_show_usage();
120         }
121
122         ibuf = obuf = xmalloc(ibs);
123         if (ibs != obs) {
124                 flags |= twobufs_flag;
125                 obuf = xmalloc(obs);
126         }
127
128         if (infile != NULL)
129                 ifd = xopen(infile, O_RDONLY);
130         else {
131                 ifd = STDIN_FILENO;
132                 infile = bb_msg_standard_input;
133         }
134
135         if (outfile != NULL) {
136                 oflag = O_WRONLY | O_CREAT;
137
138                 if (!seek && (flags & trunc_flag))
139                         oflag |= O_TRUNC;
140
141                 ofd = xopen(outfile, oflag);
142
143                 if (seek && (flags & trunc_flag)) {
144                         if (ftruncate(ofd, seek * obs) < 0) {
145                                 struct stat st;
146
147                                 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
148                                                 S_ISDIR(st.st_mode))
149                                         goto die_outfile;
150                         }
151                 }
152         } else {
153                 ofd = STDOUT_FILENO;
154                 outfile = bb_msg_standard_output;
155         }
156
157         if (skip) {
158                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
159                         while (skip-- > 0) {
160                                 n = safe_read(ifd, ibuf, ibs);
161                                 if (n < 0)
162                                         bb_perror_msg_and_die("%s", infile);
163                                 if (n == 0)
164                                         break;
165                         }
166                 }
167         }
168
169         if (seek) {
170                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
171                         goto die_outfile;
172         }
173
174         while (in_full + in_part != count) {
175                 if (flags & noerror) {
176                         /* Pre-zero the buffer when doing the noerror thing */
177                         memset(ibuf, '\0', ibs);
178                 }
179
180                 n = safe_read(ifd, ibuf, ibs);
181                 if (n == 0)
182                         break;
183                 if (n < 0) {
184                         if (flags & noerror) {
185                                 n = ibs;
186                                 bb_perror_msg("%s", infile);
187                         } else
188                                 bb_perror_msg_and_die("%s", infile);
189                 }
190                 if ((size_t)n == ibs)
191                         in_full++;
192                 else {
193                         in_part++;
194                         if (flags & sync_flag) {
195                                 memset(ibuf + n, '\0', ibs - n);
196                                 n = ibs;
197                         }
198                 }
199                 if (flags & twobufs_flag) {
200                         char *tmp = ibuf;
201                         while (n) {
202                                 size_t d = obs - oc;
203
204                                 if (d > n)
205                                         d = n;
206                                 memcpy(obuf + oc, tmp, d);
207                                 n -= d;
208                                 tmp += d;
209                                 oc += d;
210                                 if (oc == obs) {
211                                         w = full_write_or_warn(ofd, obuf, obs, outfile);
212                                         if (w < 0) goto out_status;
213                                         if (w == obs)
214                                                 out_full++;
215                                         else if (w > 0)
216                                                 out_part++;
217                                         oc = 0;
218                                 }
219                         }
220                 } else {
221                         w = full_write_or_warn(ofd, ibuf, n, outfile);
222                         if (w < 0) goto out_status;
223                         if (w == obs)
224                                 out_full++;
225                         else if (w > 0)
226                                 out_part++;
227                 }
228         }
229
230         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
231                 w = full_write_or_warn(ofd, obuf, oc, outfile);
232                 if (w < 0) goto out_status;
233                 if (w > 0)
234                         out_part++;
235         }
236         if (close(ifd) < 0) {
237                 bb_perror_msg_and_die("%s", infile);
238         }
239
240         if (close(ofd) < 0) {
241  die_outfile:
242                 bb_perror_msg_and_die("%s", outfile);
243         }
244  out_status:
245         dd_output_status(0);
246
247         return EXIT_SUCCESS;
248 }