avoid signed<->unsigned warning
[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_flag = TRUE;
61         int oflag;
62         int ifd;
63         int ofd;
64         int i;
65         const char *infile = NULL;
66         const char *outfile = NULL;
67         char *buf;
68
69         for (i = 1; i < argc; i++) {
70                 if (strncmp("bs=", argv[i], 3) == 0)
71                         bs = bb_xparse_number(argv[i]+3, dd_suffixes);
72                 else if (strncmp("count=", argv[i], 6) == 0)
73                         count = bb_xparse_number(argv[i]+6, dd_suffixes);
74                 else if (strncmp("seek=", argv[i], 5) == 0)
75                         seek = bb_xparse_number(argv[i]+5, dd_suffixes);
76                 else if (strncmp("skip=", argv[i], 5) == 0)
77                         skip = bb_xparse_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_flag = 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                                         bb_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                         bb_show_usage();
104         }
105
106         buf = xmalloc(bs);
107
108         if (infile != NULL) {
109                 ifd = bb_xopen(infile, O_RDONLY);
110         } else {
111                 ifd = STDIN_FILENO;
112                 infile = bb_msg_standard_input;
113         }
114
115         if (outfile != NULL) {
116                 oflag = O_WRONLY | O_CREAT;
117
118                 if (!seek && trunc_flag) {
119                         oflag |= O_TRUNC;
120                 }
121
122                 if ((ofd = open(outfile, oflag, 0666)) < 0) {
123                         bb_perror_msg_and_die("%s", outfile);
124                 }
125
126                 if (seek && trunc_flag) {
127                         if (ftruncate(ofd, seek * bs) < 0) {
128                                 struct stat st;
129
130                                 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
131                                                 S_ISDIR (st.st_mode)) {
132                                         bb_perror_msg_and_die("%s", outfile);
133                                 }
134                         }
135                 }
136         } else {
137                 ofd = STDOUT_FILENO;
138                 outfile = bb_msg_standard_output;
139         }
140
141         if (skip) {
142                 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
143                         bb_perror_msg_and_die("%s", infile);
144                 }
145         }
146
147         if (seek) {
148                 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
149                         bb_perror_msg_and_die("%s", outfile);
150                 }
151         }
152
153         while (in_full + in_part != count) {
154                 if (noerror) {
155                         /* Pre-zero the buffer when doing the noerror thing */
156                         memset(buf, '\0', bs);
157                 }
158                 n = safe_read(ifd, buf, bs);
159                 if (n < 0) {
160                         if (noerror) {
161                                 n = bs;
162                                 bb_perror_msg("%s", infile);
163                         } else {
164                                 bb_perror_msg_and_die("%s", infile);
165                         }
166                 }
167                 if (n == 0) {
168                         break;
169                 }
170                 if ((size_t)n == bs) {
171                         in_full++;
172                 } else {
173                         in_part++;
174                 }
175                 if (sync_flag) {
176                         memset(buf + n, '\0', bs - n);
177                         n = bs;
178                 }
179                 n = bb_full_write(ofd, buf, n);
180                 if (n < 0) {
181                         bb_perror_msg_and_die("%s", outfile);
182                 }
183                 if ((size_t)n == bs) {
184                         out_full++;
185                 } else {
186                         out_part++;
187                 }
188         }
189
190         if (close (ifd) < 0) {
191                 bb_perror_msg_and_die("%s", infile);
192         }
193
194         if (close (ofd) < 0) {
195                 bb_perror_msg_and_die("%s", outfile);
196         }
197
198         fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
199                         (long)in_full, (long)in_part,
200                         (long)out_full, (long)out_part);
201
202         return EXIT_SUCCESS;
203 }