blob: 5d9993d8b1ef644a51ff6f95d0b444ee1859303f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999, 2000 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Erik Andersen61677fe2000-04-13 01:18:56 +00008 * 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.
Eric Andersencc8ed391999-10-05 16:24:54 +000012 *
Eric Andersencc8ed391999-10-05 16:24:54 +000013 * Permission to distribute this code under the GPL has been granted.
Eric Andersenc4996011999-10-20 22:08:37 +000014 *
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 *
Eric Andersencc8ed391999-10-05 16:24:54 +000029 */
30
Eric Andersenc4996011999-10-20 22:08:37 +000031
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include "internal.h"
Eric Andersencb41c2e1999-11-22 07:41:00 +000033#include <features.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000034#include <stdio.h>
35#include <fcntl.h>
36#include <errno.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000037#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersen6a76e651999-11-19 05:31:45 +000038#include <inttypes.h>
Eric Andersencb41c2e1999-11-22 07:41:00 +000039#else
40typedef unsigned long long int uintmax_t;
41#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Eric Andersene77ae3a1999-10-19 20:03:34 +000043static const char dd_usage[] =
Erik Andersen59b9e872000-05-10 05:05:45 +000044 "dd [if=FILE] [of=FILE] [bs=N] [count=N] [skip=N] [seek=N]\n"
45#ifndef BB_FEATURE_TRIVIAL_HELP
46 "\nCopy a file, converting and formatting according to options\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 "\tif=FILE\tread from FILE instead of stdin\n"
48 "\tof=FILE\twrite to FILE instead of stdout\n"
Erik Andersen59b9e872000-05-10 05:05:45 +000049 "\tbs=N\tread and write N bytes at a time\n"
50 "\tcount=N\tcopy only N input blocks\n"
51 "\tskip=N\tskip N input blocks\n"
52 "\tseek=N\tskip N output blocks\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 "\n"
Erik Andersen59b9e872000-05-10 05:05:45 +000054 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n"
55#endif
56 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000057
58
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060extern int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000061{
Erik Andersen1ad302a2000-03-24 00:54:46 +000062 char *inFile = NULL;
63 char *outFile = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 char *cp;
65 int inFd;
66 int outFd;
67 int inCc = 0;
68 int outCc;
69 long blockSize = 512;
70 uintmax_t skipBlocks = 0;
71 uintmax_t seekBlocks = 0;
72 uintmax_t count = (uintmax_t) - 1;
73 uintmax_t intotal;
74 uintmax_t outTotal;
75 unsigned char *buf;
Eric Andersen3cf52d11999-10-12 22:26:06 +000076
Eric Andersen3cf52d11999-10-12 22:26:06 +000077 argc--;
78 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +000079
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 /* Parse any options */
81 while (argc) {
82 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
83 inFile = ((strchr(*argv, '=')) + 1);
84 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
85 outFile = ((strchr(*argv, '=')) + 1);
86 else if (strncmp("count", *argv, 5) == 0) {
87 count = getNum((strchr(*argv, '=')) + 1);
88 if (count <= 0) {
89 fprintf(stderr, "Bad count value %s\n", *argv);
90 goto usage;
91 }
92 } else if (strncmp(*argv, "bs", 2) == 0) {
93 blockSize = getNum((strchr(*argv, '=')) + 1);
94 if (blockSize <= 0) {
95 fprintf(stderr, "Bad block size value %s\n", *argv);
96 goto usage;
97 }
98 } else if (strncmp(*argv, "skip", 4) == 0) {
99 skipBlocks = getNum((strchr(*argv, '=')) + 1);
100 if (skipBlocks <= 0) {
101 fprintf(stderr, "Bad skip value %s\n", *argv);
102 goto usage;
103 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000104
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 } else if (strncmp(*argv, "seek", 4) == 0) {
106 seekBlocks = getNum((strchr(*argv, '=')) + 1);
107 if (seekBlocks <= 0) {
108 fprintf(stderr, "Bad seek value %s\n", *argv);
109 goto usage;
110 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000111
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 } else {
113 goto usage;
114 }
115 argc--;
116 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000117 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118
Erik Andersen0d068a22000-03-21 22:32:57 +0000119 buf = xmalloc(blockSize);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000120
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 intotal = 0;
122 outTotal = 0;
123
124 if (inFile == NULL)
125 inFd = fileno(stdin);
126 else
127 inFd = open(inFile, 0);
128
129 if (inFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000130 /* Note that we are not freeing buf or closing
131 * files here to save a few bytes. This exits
132 * here anyways... */
133
134 /* free(buf); */
135 fatalError( inFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 }
137
138 if (outFile == NULL)
139 outFd = fileno(stdout);
140 else
141 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
142
143 if (outFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000144 /* Note that we are not freeing buf or closing
145 * files here to save a few bytes. This exits
146 * here anyways... */
147
148 /* close(inFd);
149 free(buf); */
150 fatalError( outFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 }
152
153 lseek(inFd, skipBlocks * blockSize, SEEK_SET);
154 lseek(outFd, seekBlocks * blockSize, SEEK_SET);
155 //
156 //TODO: Convert to using fullRead & fullWrite
157 // from utility.c
158 // -Erik
159 while (outTotal < count * blockSize) {
160 inCc = read(inFd, buf, blockSize);
161 if (inCc < 0) {
162 perror(inFile);
163 goto cleanup;
164 } else if (inCc == 0) {
165 goto cleanup;
166 }
167 intotal += inCc;
168 cp = buf;
169
170 while (intotal > outTotal) {
171 if (outTotal + inCc > count * blockSize)
172 inCc = count * blockSize - outTotal;
173 outCc = write(outFd, cp, inCc);
174 if (outCc < 0) {
175 perror(outFile);
176 goto cleanup;
177 } else if (outCc == 0) {
178 goto cleanup;
179 }
180
181 inCc -= outCc;
182 cp += outCc;
183 outTotal += outCc;
184 }
185 }
186
187 if (inCc < 0)
188 perror(inFile);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000189
190 cleanup:
Erik Andersen298854f2000-03-23 01:09:18 +0000191 /* Note that we are not freeing memory or closing
192 * files here, to save a few bytes. */
193#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 close(inFd);
195 close(outFd);
196 free(buf);
Erik Andersen298854f2000-03-23 01:09:18 +0000197#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000198
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 printf("%ld+%d records in\n", (long) (intotal / blockSize),
200 (intotal % blockSize) != 0);
201 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
202 (outTotal % blockSize) != 0);
203 exit(TRUE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000204 usage:
205
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 usage(dd_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000207}