64948571c0d533f54fca26b7a30473a8f0805d69
[oweals/busybox.git] / coreutils / dd.c
1 /*
2  * Mini dd implementation for busybox
3  *
4  * Copyright (C) 1999 by Lineo, inc.
5  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6  * based in part on code taken from sash. 
7  *
8  * Copyright (c) 1999 by David I. Bell
9  * Permission is granted to use, distribute, or modify this source,
10  * provided that this copyright notice remains intact.
11  *
12  * Permission to distribute this code under the GPL has been granted.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  */
29
30
31 #include "internal.h"
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <inttypes.h>
36
37 static const char dd_usage[] =
38 "dd [if=name] [of=name] [bs=n] [count=n]\n\n"
39 "Copy a file, converting and formatting according to options\n\n"
40 "\tif=FILE\tread from FILE instead of stdin\n"
41 "\tof=FILE\twrite to FILE instead of stout\n"
42 "\tbs=n\tread and write N BYTES at a time\n"
43 "\tcount=n\tcopy only n input blocks\n"
44 //"\tskip=n\tskip n input blocks\n"
45 "\n"
46 "BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\n";
47
48
49
50
51 /*
52  * Read a number with a possible multiplier.
53  * Returns -1 if the number format is illegal.
54  */
55 static long getNum (const char *cp)
56 {
57     long value;
58
59     if (!isDecimal (*cp))
60         return -1;
61
62     value = 0;
63
64     while (isDecimal (*cp))
65         value = value * 10 + *cp++ - '0';
66
67     switch (*cp++) {
68     case 'k':
69         value *= 1024;
70         break;
71
72     case 'b':
73         value *= 512;
74         break;
75
76     case 'w':
77         value *= 2;
78         break;
79
80     case '\0':
81         return value;
82
83     default:
84         return -1;
85     }
86
87     if (*cp)
88         return -1;
89
90     return value;
91 }
92
93
94 extern int dd_main (int argc, char **argv)
95 {
96     const char *inFile = NULL;
97     const char *outFile = NULL;
98     char *cp;
99     int inFd;
100     int outFd;
101     int inCc = 0;
102     int outCc;
103     size_t blockSize = 512;
104     //uintmax_t skipBlocks = 0;
105     uintmax_t count = (uintmax_t)-1;
106     uintmax_t intotal;
107     uintmax_t outTotal;
108     unsigned char *buf;
109
110     argc--;
111     argv++;
112
113     /* Parse any options */
114     while (argc) {
115         if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
116             inFile=((strchr(*argv, '='))+1);
117         else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
118             outFile=((strchr(*argv, '='))+1);
119         else if (strncmp("count", *argv, 5) == 0) {
120             count = getNum ((strchr(*argv, '='))+1);
121             if (count <= 0) {
122                 fprintf (stderr, "Bad count value %s\n", *argv);
123                 goto usage;
124             }
125         }
126         else if (strncmp(*argv, "bs", 2) == 0) {
127             blockSize = getNum ((strchr(*argv, '='))+1);
128             if (blockSize <= 0) {
129                 fprintf (stderr, "Bad block size value %s\n", *argv);
130                 goto usage;
131             }
132         }
133 #if 0
134         else if (strncmp(*argv, "skip", 4) == 0) {
135             skipBlocks = atoi( *argv); 
136             if (skipBlocks <= 0) {
137                 fprintf (stderr, "Bad skip value %d\n", skipBlocks);
138                 goto usage;
139             }
140
141         }
142 #endif
143         else {
144             goto usage;
145         }
146         argc--;
147         argv++;
148     }
149
150     buf = malloc (blockSize);
151     if (buf == NULL) {
152         fprintf (stderr, "Cannot allocate buffer\n");
153         exit( FALSE);
154     }
155
156     intotal = 0;
157     outTotal = 0;
158
159     if (inFile == NULL)
160         inFd = fileno(stdin);
161     else
162         inFd = open (inFile, 0);
163
164     if (inFd < 0) {
165         perror (inFile);
166         free (buf);
167         exit( FALSE);
168     }
169
170     if (outFile == NULL)
171         outFd = fileno(stdout);
172     else
173         outFd = creat (outFile, 0666);
174
175     if (outFd < 0) {
176         perror (outFile);
177         close (inFd);
178         free (buf);
179         exit( FALSE);
180     }
181
182     //lseek(inFd, skipBlocks*blockSize, SEEK_SET);
183     while (outTotal < count * blockSize) {
184         inCc = read (inFd, buf, blockSize);
185         if (inCc < 0) {
186             perror (inFile);
187             goto cleanup;
188         } else if (inCc == 0) {
189             goto cleanup;
190         }
191         intotal += inCc;
192         cp = buf;
193
194         while (intotal > outTotal) {
195             if (outTotal + inCc > count * blockSize)
196                 inCc = count * blockSize - outTotal;
197             outCc = write (outFd, cp, inCc);
198             if (outCc < 0) {
199                 perror (outFile);
200                 goto cleanup;
201             } else if (outCc == 0) {
202                 goto cleanup;
203             }
204
205             inCc -= outCc;
206             cp += outCc;
207             outTotal += outCc;
208         }
209     }
210
211     if (inCc < 0)
212         perror (inFile);
213
214   cleanup:
215     close (inFd);
216     close (outFd);
217     free (buf);
218
219     printf ("%ld+%d records in\n", (long)(intotal / blockSize),
220             (intotal % blockSize) != 0);
221     printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
222             (outTotal % blockSize) != 0);
223     exit( TRUE);
224   usage:
225
226     usage( dd_usage);
227 }
228
229