1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
5 * Copyright (C) 1999, 2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
13 * Permission to distribute this code under the GPL has been granted.
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.
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.
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
37 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
40 typedef unsigned long long int uintmax_t;
43 extern int dd_main(int argc, char **argv)
53 long blockSize = 512,ibs;
54 uintmax_t skipBlocks = 0;
55 uintmax_t seekBlocks = 0;
56 uintmax_t count = (uintmax_t) - 1;
57 uintmax_t inTotal = 0;
58 uintmax_t outTotal = 0;
61 unsigned char buf[BUFSIZ];
67 /* Parse any options */
69 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
70 inFile = ((strchr(*argv, '=')) + 1);
71 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
72 outFile = ((strchr(*argv, '=')) + 1);
73 else if (strncmp("count", *argv, 5) == 0) {
74 count = atoi_w_units((strchr(*argv, '=')) + 1);
76 error_msg("Bad count value %s\n", *argv);
79 } else if (strncmp(*argv, "bs", 2) == 0) {
80 blockSize = atoi_w_units((strchr(*argv, '=')) + 1);
82 error_msg("Bad block size value %s\n", *argv);
85 } else if (strncmp(*argv, "skip", 4) == 0) {
86 skipBlocks = atoi_w_units((strchr(*argv, '=')) + 1);
87 if (skipBlocks <= 0) {
88 error_msg("Bad skip value %s\n", *argv);
92 } else if (strncmp(*argv, "seek", 4) == 0) {
93 seekBlocks = atoi_w_units((strchr(*argv, '=')) + 1);
94 if (seekBlocks <= 0) {
95 error_msg("Bad seek value %s\n", *argv);
98 } else if (strncmp(*argv, "conv", 4) == 0) {
99 keyword = (strchr(*argv, '=') + 1);
100 if (strcmp(keyword, "notrunc") == 0)
102 if (strcmp(keyword, "sync") == 0)
112 inFd = fileno(stdin);
114 inFd = open(inFile, 0);
117 /* Note that we are not freeing buf or closing
118 * files here to save a few bytes. This exits
122 perror_msg_and_die("%s", inFile);
126 outFd = fileno(stdout);
128 outFd = open(outFile, O_WRONLY | O_CREAT, 0666);
131 /* Note that we are not freeing buf or closing
132 * files here to save a few bytes. This exits
137 perror_msg_and_die("%s", outFile);
140 lseek(inFd, (off_t) (skipBlocks * blockSize), SEEK_SET);
141 lseek(outFd, (off_t) (seekBlocks * blockSize), SEEK_SET);
142 totalSize=count*blockSize;
148 while (totalSize > outTotal) {
149 inCc = full_read(inFd, buf, ibs);
151 if ( (sync==TRUE) && (inCc>0) )
155 if ((outCc = full_write(outFd, buf, inCc)) < 1){
157 perror("Error during write");
164 ftruncate(outFd, lseek(outFd, 0, SEEK_CUR));
166 /* Note that we are not freeing memory or closing
167 * files here, to save a few bytes. */
168 #ifdef BB_FEATURE_CLEAN_UP
173 printf("%ld+%d records in\n", (long) (inTotal / blockSize),
174 (inTotal % blockSize) != 0);
175 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
176 (outTotal % blockSize) != 0);