84f3211fb0c5c5c19305827d29b06cc1c36d2df6
[oweals/busybox.git] / coreutils / dd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dd implementation for busybox
4  *
5  * Copyright (C) 1999, 2000 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * Based in part on code taken from sash. 
9  *   Copyright (c) 1999 by David I. Bell
10  *   Permission is granted to use, distribute, or modify this source,
11  *   provided that this copyright notice remains intact.
12  *
13  * Permission to distribute this code under the GPL has been granted.
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  */
30
31
32 #include "internal.h"
33 #include <features.h>
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
38 #include <inttypes.h>
39 #else
40 typedef unsigned long long int uintmax_t;
41 #endif
42
43 extern int dd_main(int argc, char **argv)
44 {
45         char *inFile = NULL;
46         char *outFile = NULL;
47         int inFd;
48         int outFd;
49         int inCc = 0;
50         int outCc;
51         long blockSize = 512;
52         uintmax_t skipBlocks = 0;
53         uintmax_t seekBlocks = 0;
54         uintmax_t count = (uintmax_t) - 1;
55         uintmax_t inTotal = 0;
56         uintmax_t outTotal = 0;
57         uintmax_t totalSize;
58         uintmax_t readSize;
59         unsigned char buf[BUFSIZ];
60
61         argc--;
62         argv++;
63
64         /* Parse any options */
65         while (argc) {
66                 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
67                         inFile = ((strchr(*argv, '=')) + 1);
68                 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
69                         outFile = ((strchr(*argv, '=')) + 1);
70                 else if (strncmp("count", *argv, 5) == 0) {
71                         count = getNum((strchr(*argv, '=')) + 1);
72                         if (count <= 0) {
73                                 errorMsg("Bad count value %s\n", *argv);
74                                 goto usage;
75                         }
76                 } else if (strncmp(*argv, "bs", 2) == 0) {
77                         blockSize = getNum((strchr(*argv, '=')) + 1);
78                         if (blockSize <= 0) {
79                                 errorMsg("Bad block size value %s\n", *argv);
80                                 goto usage;
81                         }
82                 } else if (strncmp(*argv, "skip", 4) == 0) {
83                         skipBlocks = getNum((strchr(*argv, '=')) + 1);
84                         if (skipBlocks <= 0) {
85                                 errorMsg("Bad skip value %s\n", *argv);
86                                 goto usage;
87                         }
88
89                 } else if (strncmp(*argv, "seek", 4) == 0) {
90                         seekBlocks = getNum((strchr(*argv, '=')) + 1);
91                         if (seekBlocks <= 0) {
92                                 errorMsg("Bad seek value %s\n", *argv);
93                                 goto usage;
94                         }
95
96                 } else {
97                         goto usage;
98                 }
99                 argc--;
100                 argv++;
101         }
102
103         if (inFile == NULL)
104                 inFd = fileno(stdin);
105         else
106                 inFd = open(inFile, 0);
107
108         if (inFd < 0) {
109                 /* Note that we are not freeing buf or closing
110                  * files here to save a few bytes. This exits
111                  * here anyways... */
112
113                 /* free(buf); */
114                 fatalError( inFile);
115         }
116
117         if (outFile == NULL)
118                 outFd = fileno(stdout);
119         else
120                 outFd = open(outFile, O_WRONLY | O_CREAT, 0666);
121
122         if (outFd < 0) {
123                 /* Note that we are not freeing buf or closing
124                  * files here to save a few bytes. This exits
125                  * here anyways... */
126
127                 /* close(inFd);
128                    free(buf); */
129                 fatalError( outFile);
130         }
131
132         lseek(inFd, (off_t) (skipBlocks * blockSize), SEEK_SET);
133         lseek(outFd, (off_t) (seekBlocks * blockSize), SEEK_SET);
134         totalSize=count*blockSize;
135         while ((readSize = totalSize - inTotal) > 0) {
136                 if (readSize > BUFSIZ)
137                         readSize=BUFSIZ;
138                 inCc = read(inFd, buf, readSize);
139                 inTotal += inCc;
140                 if ((outCc = fullWrite(outFd, buf, inCc)) < 0)
141                         break;
142                 outTotal += outCc;
143         }
144
145         /* Note that we are not freeing memory or closing
146          * files here, to save a few bytes. */
147 #ifdef BB_FEATURE_CLEAN_UP
148         close(inFd);
149         close(outFd);
150 #endif
151
152         printf("%ld+%d records in\n", (long) (inTotal / blockSize),
153                    (inTotal % blockSize) != 0);
154         printf("%ld+%d records out\n", (long) (outTotal / blockSize),
155                    (outTotal % blockSize) != 0);
156         exit(TRUE);
157   usage:
158
159         usage(dd_usage);
160 }