blob: 9c7d872b195511354a2f59982cbccd455844b841 [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 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * based in part on code taken from sash.
8 *
Eric Andersencc8ed391999-10-05 16:24:54 +00009 * 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 *
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 Andersene49d5ec2000-02-08 19:58:47 +000044 "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n"
45 "Copy a file, converting and formatting according to options\n\n"
46 "\tif=FILE\tread from FILE instead of stdin\n"
47 "\tof=FILE\twrite to FILE instead of stdout\n"
48 "\tbs=n\tread and write n bytes at a time\n"
49 "\tcount=n\tcopy only n input blocks\n"
50 "\tskip=n\tskip n input blocks\n"
51 "\tseek=n\tskip n output blocks\n"
52
53 "\n"
54 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000055
56
Eric Andersencc8ed391999-10-05 16:24:54 +000057
Erik Andersene49d5ec2000-02-08 19:58:47 +000058extern int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000059{
Erik Andersen1ad302a2000-03-24 00:54:46 +000060 char *inFile = NULL;
61 char *outFile = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 char *cp;
63 int inFd;
64 int outFd;
65 int inCc = 0;
66 int outCc;
67 long blockSize = 512;
68 uintmax_t skipBlocks = 0;
69 uintmax_t seekBlocks = 0;
70 uintmax_t count = (uintmax_t) - 1;
71 uintmax_t intotal;
72 uintmax_t outTotal;
73 unsigned char *buf;
Eric Andersen3cf52d11999-10-12 22:26:06 +000074
Eric Andersen3cf52d11999-10-12 22:26:06 +000075 argc--;
76 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +000077
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 /* Parse any options */
79 while (argc) {
80 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
81 inFile = ((strchr(*argv, '=')) + 1);
82 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
83 outFile = ((strchr(*argv, '=')) + 1);
84 else if (strncmp("count", *argv, 5) == 0) {
85 count = getNum((strchr(*argv, '=')) + 1);
86 if (count <= 0) {
87 fprintf(stderr, "Bad count value %s\n", *argv);
88 goto usage;
89 }
90 } else if (strncmp(*argv, "bs", 2) == 0) {
91 blockSize = getNum((strchr(*argv, '=')) + 1);
92 if (blockSize <= 0) {
93 fprintf(stderr, "Bad block size value %s\n", *argv);
94 goto usage;
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 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000102
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 } else if (strncmp(*argv, "seek", 4) == 0) {
104 seekBlocks = getNum((strchr(*argv, '=')) + 1);
105 if (seekBlocks <= 0) {
106 fprintf(stderr, "Bad seek value %s\n", *argv);
107 goto usage;
108 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000109
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 } else {
111 goto usage;
112 }
113 argc--;
114 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000116
Erik Andersen0d068a22000-03-21 22:32:57 +0000117 buf = xmalloc(blockSize);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 intotal = 0;
120 outTotal = 0;
121
122 if (inFile == NULL)
123 inFd = fileno(stdin);
124 else
125 inFd = open(inFile, 0);
126
127 if (inFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000128 /* Note that we are not freeing buf or closing
129 * files here to save a few bytes. This exits
130 * here anyways... */
131
132 /* free(buf); */
133 fatalError( inFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
135
136 if (outFile == NULL)
137 outFd = fileno(stdout);
138 else
139 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
140
141 if (outFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000142 /* Note that we are not freeing buf or closing
143 * files here to save a few bytes. This exits
144 * here anyways... */
145
146 /* close(inFd);
147 free(buf); */
148 fatalError( outFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 }
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);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000187
188 cleanup:
Erik Andersen298854f2000-03-23 01:09:18 +0000189 /* Note that we are not freeing memory or closing
190 * files here, to save a few bytes. */
191#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 close(inFd);
193 close(outFd);
194 free(buf);
Erik Andersen298854f2000-03-23 01:09:18 +0000195#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000196
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197 printf("%ld+%d records in\n", (long) (intotal / blockSize),
198 (intotal % blockSize) != 0);
199 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
200 (outTotal % blockSize) != 0);
201 exit(TRUE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000202 usage:
203
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 usage(dd_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000205}