3e1024a609b4288ae8d291576780424f222b3fe0
[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 <features.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
37 #include <inttypes.h>
38 #else
39 typedef unsigned long long int uintmax_t;
40 #endif
41
42 static const char dd_usage[] =
43 "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n"
44 "Copy a file, converting and formatting according to options\n\n"
45 "\tif=FILE\tread from FILE instead of stdin\n"
46 "\tof=FILE\twrite to FILE instead of stdout\n"
47 "\tbs=n\tread and write n bytes at a time\n"
48 "\tcount=n\tcopy only n input blocks\n"
49 "\tskip=n\tskip n input blocks\n"
50 "\tseek=n\tskip n output blocks\n"
51 "\n"
52 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
53
54
55
56 extern int dd_main (int argc, char **argv)
57 {
58     const char *inFile = NULL;
59     const char *outFile = NULL;
60     char *cp;
61     int inFd;
62     int outFd;
63     int inCc = 0;
64     int outCc;
65     long blockSize = 512;
66     uintmax_t skipBlocks = 0;
67     uintmax_t seekBlocks = 0;
68     uintmax_t count = (uintmax_t)-1;
69     uintmax_t intotal;
70     uintmax_t outTotal;
71     unsigned char *buf;
72
73     argc--;
74     argv++;
75
76     /* Parse any options */
77     while (argc) {
78         if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
79             inFile=((strchr(*argv, '='))+1);
80         else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
81             outFile=((strchr(*argv, '='))+1);
82         else if (strncmp("count", *argv, 5) == 0) {
83             count = getNum ((strchr(*argv, '='))+1);
84             if (count <= 0) {
85                 fprintf (stderr, "Bad count value %s\n", *argv);
86                 goto usage;
87             }
88         }
89         else if (strncmp(*argv, "bs", 2) == 0) {
90             blockSize = getNum ((strchr(*argv, '='))+1);
91             if (blockSize <= 0) {
92                 fprintf (stderr, "Bad block size value %s\n", *argv);
93                 goto usage;
94             }
95         }
96         else if (strncmp(*argv, "skip", 4) == 0) {
97             skipBlocks = getNum ((strchr(*argv, '='))+1);
98             if (skipBlocks <= 0) {
99                 fprintf (stderr, "Bad skip value %s\n", *argv);
100                 goto usage;
101             }
102
103         }
104         else if (strncmp(*argv, "seek", 4) == 0) {
105             seekBlocks = getNum ((strchr(*argv, '='))+1);
106             if (seekBlocks <= 0) {
107                 fprintf (stderr, "Bad seek value %s\n", *argv);
108                 goto usage;
109             }
110
111         }
112         else {
113             goto usage;
114         }
115         argc--;
116         argv++;
117     }
118
119     buf = malloc (blockSize);
120     if (buf == NULL) {
121         fprintf (stderr, "Cannot allocate buffer\n");
122         exit( FALSE);
123     }
124
125     intotal = 0;
126     outTotal = 0;
127
128     if (inFile == NULL)
129         inFd = fileno(stdin);
130     else
131         inFd = open (inFile, 0);
132
133     if (inFd < 0) {
134         perror (inFile);
135         free (buf);
136         exit( FALSE);
137     }
138
139     if (outFile == NULL)
140         outFd = fileno(stdout);
141     else
142         outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
143
144     if (outFd < 0) {
145         perror (outFile);
146         close (inFd);
147         free (buf);
148         exit( FALSE);
149     }
150
151     lseek(inFd, skipBlocks*blockSize, SEEK_SET);
152     lseek(outFd, seekBlocks*blockSize, SEEK_SET);
153     //
154     //TODO: Convert to using fullRead & fullWrite
155     // from utility.c
156     //  -Erik
157     while (outTotal < count * blockSize) {
158         inCc = read (inFd, buf, blockSize);
159         if (inCc < 0) {
160             perror (inFile);
161             goto cleanup;
162         } else if (inCc == 0) {
163             goto cleanup;
164         }
165         intotal += inCc;
166         cp = buf;
167
168         while (intotal > outTotal) {
169             if (outTotal + inCc > count * blockSize)
170                 inCc = count * blockSize - outTotal;
171             outCc = write (outFd, cp, inCc);
172             if (outCc < 0) {
173                 perror (outFile);
174                 goto cleanup;
175             } else if (outCc == 0) {
176                 goto cleanup;
177             }
178
179             inCc -= outCc;
180             cp += outCc;
181             outTotal += outCc;
182         }
183     }
184
185     if (inCc < 0)
186         perror (inFile);
187
188   cleanup:
189     close (inFd);
190     close (outFd);
191     free (buf);
192
193     printf ("%ld+%d records in\n", (long)(intotal / blockSize),
194             (intotal % blockSize) != 0);
195     printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
196             (outTotal % blockSize) != 0);
197     exit( TRUE);
198   usage:
199
200     usage( dd_usage);
201 }
202
203