Fix a namespace aliasing problem wereby du and dutmp, or
[oweals/busybox.git] / 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]\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 stout\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 "\n"
51 "BYTES may be suffixed by w (x2), k (x1024), b (x512), or m (x1024^2).\n";
52
53
54
55 extern int dd_main (int argc, char **argv)
56 {
57     const char *inFile = NULL;
58     const char *outFile = NULL;
59     char *cp;
60     int inFd;
61     int outFd;
62     int inCc = 0;
63     int outCc;
64     size_t blockSize = 512;
65     //uintmax_t skipBlocks = 0;
66     uintmax_t count = (uintmax_t)-1;
67     uintmax_t intotal;
68     uintmax_t outTotal;
69     unsigned char *buf;
70
71     argc--;
72     argv++;
73
74     /* Parse any options */
75     while (argc) {
76         if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
77             inFile=((strchr(*argv, '='))+1);
78         else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
79             outFile=((strchr(*argv, '='))+1);
80         else if (strncmp("count", *argv, 5) == 0) {
81             count = getNum ((strchr(*argv, '='))+1);
82             if (count <= 0) {
83                 fprintf (stderr, "Bad count value %s\n", *argv);
84                 goto usage;
85             }
86         }
87         else if (strncmp(*argv, "bs", 2) == 0) {
88             blockSize = getNum ((strchr(*argv, '='))+1);
89             if (blockSize <= 0) {
90                 fprintf (stderr, "Bad block size value %s\n", *argv);
91                 goto usage;
92             }
93         }
94 #if 0
95         else if (strncmp(*argv, "skip", 4) == 0) {
96             skipBlocks = atoi( *argv); 
97             if (skipBlocks <= 0) {
98                 fprintf (stderr, "Bad skip value %d\n", skipBlocks);
99                 goto usage;
100             }
101
102         }
103 #endif
104         else {
105             goto usage;
106         }
107         argc--;
108         argv++;
109     }
110
111     buf = malloc (blockSize);
112     if (buf == NULL) {
113         fprintf (stderr, "Cannot allocate buffer\n");
114         exit( FALSE);
115     }
116
117     intotal = 0;
118     outTotal = 0;
119
120     if (inFile == NULL)
121         inFd = fileno(stdin);
122     else
123         inFd = open (inFile, 0);
124
125     if (inFd < 0) {
126         perror (inFile);
127         free (buf);
128         exit( FALSE);
129     }
130
131     if (outFile == NULL)
132         outFd = fileno(stdout);
133     else
134         outFd = creat (outFile, 0666);
135
136     if (outFd < 0) {
137         perror (outFile);
138         close (inFd);
139         free (buf);
140         exit( FALSE);
141     }
142
143     //lseek(inFd, skipBlocks*blockSize, SEEK_SET);
144     //
145     //TODO: Convert to using fullRead & fullWrite
146     // from utilitity.c
147     //  -Erik
148     while (outTotal < count * blockSize) {
149         inCc = read (inFd, buf, blockSize);
150         if (inCc < 0) {
151             perror (inFile);
152             goto cleanup;
153         } else if (inCc == 0) {
154             goto cleanup;
155         }
156         intotal += inCc;
157         cp = buf;
158
159         while (intotal > outTotal) {
160             if (outTotal + inCc > count * blockSize)
161                 inCc = count * blockSize - outTotal;
162             outCc = write (outFd, cp, inCc);
163             if (outCc < 0) {
164                 perror (outFile);
165                 goto cleanup;
166             } else if (outCc == 0) {
167                 goto cleanup;
168             }
169
170             inCc -= outCc;
171             cp += outCc;
172             outTotal += outCc;
173         }
174     }
175
176     if (inCc < 0)
177         perror (inFile);
178
179   cleanup:
180     close (inFd);
181     close (outFd);
182     free (buf);
183
184     printf ("%ld+%d records in\n", (long)(intotal / blockSize),
185             (intotal % blockSize) != 0);
186     printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
187             (outTotal % blockSize) != 0);
188     exit( TRUE);
189   usage:
190
191     usage( dd_usage);
192 }
193
194