Style
[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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include "busybox.h"
32
33
34 static const struct suffix_mult dd_suffixes[] = {
35         { "c", 1 },
36         { "w", 2 },
37         { "b", 512 },
38         { "kD", 1000 },
39         { "k", 1024 },
40         { "MD", 1000000 },
41         { "M", 1048576 },
42         { "GD", 1000000000 },
43         { "G", 1073741824 },
44         { NULL, 0 }
45 };
46
47 int dd_main(int argc, char **argv)
48 {
49         size_t out_full = 0;
50         size_t out_part = 0;
51         size_t in_full = 0;
52         size_t in_part = 0;
53         size_t count = -1;
54         size_t bs = 512;
55         ssize_t n;
56         off_t seek = 0;
57         off_t skip = 0;
58         int sync_flag = FALSE;
59         int noerror = FALSE;
60         int trunc = TRUE;
61         int oflag;
62         int ifd;
63         int ofd;
64         int i;
65         char *infile = NULL;
66         char *outfile = NULL;
67         char *buf;
68
69         for (i = 1; i < argc; i++) {
70                 if (strncmp("bs=", argv[i], 3) == 0)
71                         bs = parse_number(argv[i]+3, dd_suffixes);
72                 else if (strncmp("count=", argv[i], 6) == 0)
73                         count = parse_number(argv[i]+6, dd_suffixes);
74                 else if (strncmp("seek=", argv[i], 5) == 0)
75                         seek = parse_number(argv[i]+5, dd_suffixes);
76                 else if (strncmp("skip=", argv[i], 5) == 0)
77                         skip = parse_number(argv[i]+5, dd_suffixes);
78                 else if (strncmp("if=", argv[i], 3) == 0)
79                         infile = argv[i]+3;
80                 else if (strncmp("of=", argv[i], 3) == 0)
81                         outfile = argv[i]+3;
82                 else if (strncmp("conv=", argv[i], 5) == 0) {
83                         buf = argv[i]+5;
84                         while (1) {
85                                 if (strncmp("notrunc", buf, 7) == 0) {
86                                         trunc = FALSE;
87                                         buf += 7;
88                                 } else if (strncmp("sync", buf, 4) == 0) {
89                                         sync_flag = TRUE;
90                                         buf += 4;
91                                 } else if (strncmp("noerror", buf, 7) == 0) {
92                                         noerror = TRUE;
93                                         buf += 7;
94                                 } else {
95                                         error_msg_and_die("invalid conversion `%s'", argv[i]+5);
96                                 }
97                                 if (buf[0] == '\0')
98                                         break;
99                                 if (buf[0] == ',')
100                                         buf++;
101                         }
102                 } else
103                         show_usage();
104         }
105
106         buf = xmalloc(bs);
107
108         if (infile != NULL) {
109                 if ((ifd = open(infile, O_RDONLY)) < 0) {
110                         perror_msg_and_die("%s", infile);
111                 }
112         } else {
113                 ifd = STDIN_FILENO;
114                 infile = "standard input";
115         }
116
117         if (outfile != NULL) {
118                 oflag = O_WRONLY | O_CREAT;
119
120                 if (!seek && trunc) {
121                         oflag |= O_TRUNC;
122                 }
123
124                 if ((ofd = open(outfile, oflag, 0666)) < 0) {
125                         perror_msg_and_die("%s", outfile);
126                 }
127
128                 if (seek && trunc) {
129                         if (ftruncate(ofd, seek * bs) < 0) {
130                                 struct stat st;
131
132                                 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
133                                                 S_ISDIR (st.st_mode)) {
134                                         perror_msg_and_die("%s", outfile);
135                                 }
136                         }
137                 }
138         } else {
139                 ofd = STDOUT_FILENO;
140                 outfile = "standard output";
141         }
142
143         if (skip) {
144                 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
145                         perror_msg_and_die("%s", infile);
146                 }
147         }
148
149         if (seek) {
150                 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
151                         perror_msg_and_die("%s", outfile);
152                 }
153         }
154
155         while (in_full + in_part != count) {
156                 if (noerror) {
157                         /* Pre-zero the buffer when doing the noerror thing */
158                         memset(buf, '\0', bs);
159                 }
160                 n = safe_read(ifd, buf, bs);
161                 if (n < 0) {
162                         if (noerror) {
163                                 n = bs;
164                                 perror_msg("%s", infile);
165                         } else {
166                                 perror_msg_and_die("%s", infile);
167                         }
168                 }
169                 if (n == 0) {
170                         break;
171                 }
172                 if (n == bs) {
173                         in_full++;
174                 } else {
175                         in_part++;
176                 }
177                 if (sync_flag) {
178                         memset(buf + n, '\0', bs - n);
179                         n = bs;
180                 }
181                 n = full_write(ofd, buf, n);
182                 if (n < 0) {
183                         perror_msg_and_die("%s", outfile);
184                 }
185                 if (n == bs) {
186                         out_full++;
187                 } else {
188                         out_part++;
189                 }
190         }
191
192         if (close (ifd) < 0) {
193                 perror_msg_and_die("%s", infile);
194         }
195
196         if (close (ofd) < 0) {
197                 perror_msg_and_die("%s", outfile);
198         }
199
200         fprintf(stderr, "%ld+%ld records in\n", (long)in_full, (long)in_part);
201         fprintf(stderr, "%ld+%ld records out\n", (long)out_full, (long)out_part);
202
203         return EXIT_SUCCESS;
204 }