suppress warnings about easch <applet>_main() having
[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 int dd_main(int argc, char **argv)
55 {
56         enum {
57                 sync_flag    = 1 << 0,
58                 noerror      = 1 << 1,
59                 trunc_flag   = 1 << 2,
60                 twobufs_flag = 1 << 3,
61         };
62         int flags = trunc_flag;
63         size_t oc = 0, ibs = 512, obs = 512;
64         ssize_t n, w;
65         off_t seek = 0, skip = 0, count = OFF_T_MAX;
66         int oflag, ifd, ofd;
67         const char *infile = NULL, *outfile = NULL;
68         char *ibuf, *obuf;
69
70         if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
71                 struct sigaction sa;
72
73                 memset(&sa, 0, sizeof(sa));
74                 sa.sa_handler = dd_output_status;
75                 sa.sa_flags = SA_RESTART;
76                 sigemptyset(&sa.sa_mask);
77                 sigaction(SIGUSR1, &sa, 0);
78         }
79
80         for (n = 1; n < argc; n++) {
81                 char *arg = argv[n];
82                 /* Must fit into positive ssize_t */
83                 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
84                         ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
85                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
86                         obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
87                 else if (!strncmp("bs=", arg, 3))
88                         ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
89                 /* These can be large: */
90                 else if (!strncmp("count=", arg, 6))
91                         count = XATOU_SFX(arg+6, dd_suffixes);
92                 else if (!strncmp("seek=", arg, 5))
93                         seek = XATOU_SFX(arg+5, dd_suffixes);
94                 else if (!strncmp("skip=", arg, 5))
95                         skip = XATOU_SFX(arg+5, dd_suffixes);
96
97                 else if (!strncmp("if=", arg, 3))
98                         infile = arg+3;
99                 else if (!strncmp("of=", arg, 3))
100                         outfile = arg+3;
101                 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
102                         arg += 5;
103                         while (1) {
104                                 if (!strncmp("notrunc", arg, 7)) {
105                                         flags &= ~trunc_flag;
106                                         arg += 7;
107                                 } else if (!strncmp("sync", arg, 4)) {
108                                         flags |= sync_flag;
109                                         arg += 4;
110                                 } else if (!strncmp("noerror", arg, 7)) {
111                                         flags |= noerror;
112                                         arg += 7;
113                                 } else {
114                                         bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
115                                 }
116                                 if (arg[0] == '\0') break;
117                                 if (*arg++ != ',') bb_show_usage();
118                         }
119                 } else
120                         bb_show_usage();
121         }
122
123         ibuf = obuf = xmalloc(ibs);
124         if (ibs != obs) {
125                 flags |= twobufs_flag;
126                 obuf = xmalloc(obs);
127         }
128
129         if (infile != NULL)
130                 ifd = xopen(infile, O_RDONLY);
131         else {
132                 ifd = STDIN_FILENO;
133                 infile = bb_msg_standard_input;
134         }
135
136         if (outfile != NULL) {
137                 oflag = O_WRONLY | O_CREAT;
138
139                 if (!seek && (flags & trunc_flag))
140                         oflag |= O_TRUNC;
141
142                 ofd = xopen(outfile, oflag);
143
144                 if (seek && (flags & trunc_flag)) {
145                         if (ftruncate(ofd, seek * obs) < 0) {
146                                 struct stat st;
147
148                                 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
149                                                 S_ISDIR(st.st_mode))
150                                         goto die_outfile;
151                         }
152                 }
153         } else {
154                 ofd = STDOUT_FILENO;
155                 outfile = bb_msg_standard_output;
156         }
157
158         if (skip) {
159                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
160                         while (skip-- > 0) {
161                                 n = safe_read(ifd, ibuf, ibs);
162                                 if (n < 0)
163                                         bb_perror_msg_and_die("%s", infile);
164                                 if (n == 0)
165                                         break;
166                         }
167                 }
168         }
169
170         if (seek) {
171                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
172                         goto die_outfile;
173         }
174
175         while (in_full + in_part != count) {
176                 if (flags & noerror) {
177                         /* Pre-zero the buffer when doing the noerror thing */
178                         memset(ibuf, '\0', ibs);
179                 }
180
181                 n = safe_read(ifd, ibuf, ibs);
182                 if (n == 0)
183                         break;
184                 if (n < 0) {
185                         if (flags & noerror) {
186                                 n = ibs;
187                                 bb_perror_msg("%s", infile);
188                         } else
189                                 bb_perror_msg_and_die("%s", infile);
190                 }
191                 if ((size_t)n == ibs)
192                         in_full++;
193                 else {
194                         in_part++;
195                         if (flags & sync_flag) {
196                                 memset(ibuf + n, '\0', ibs - n);
197                                 n = ibs;
198                         }
199                 }
200                 if (flags & twobufs_flag) {
201                         char *tmp = ibuf;
202                         while (n) {
203                                 size_t d = obs - oc;
204
205                                 if (d > n)
206                                         d = n;
207                                 memcpy(obuf + oc, tmp, d);
208                                 n -= d;
209                                 tmp += d;
210                                 oc += d;
211                                 if (oc == obs) {
212                                         w = full_write_or_warn(ofd, obuf, obs, outfile);
213                                         if (w < 0) goto out_status;
214                                         if (w == obs)
215                                                 out_full++;
216                                         else if (w > 0)
217                                                 out_part++;
218                                         oc = 0;
219                                 }
220                         }
221                 } else {
222                         w = full_write_or_warn(ofd, ibuf, n, outfile);
223                         if (w < 0) goto out_status;
224                         if (w == obs)
225                                 out_full++;
226                         else if (w > 0)
227                                 out_part++;
228                 }
229         }
230
231         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
232                 w = full_write_or_warn(ofd, obuf, oc, outfile);
233                 if (w < 0) goto out_status;
234                 if (w > 0)
235                         out_part++;
236         }
237         if (close(ifd) < 0) {
238                 bb_perror_msg_and_die("%s", infile);
239         }
240
241         if (close(ofd) < 0) {
242  die_outfile:
243                 bb_perror_msg_and_die("%s", outfile);
244         }
245  out_status:
246         dd_output_status(0);
247
248         return EXIT_SUCCESS;
249 }