blob: 6df2588ca43238bcbff4e216bbc42a123d2939e8 [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
Erik Andersene49d5ec2000-02-08 19:58:47 +000043extern int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000044{
Erik Andersen1ad302a2000-03-24 00:54:46 +000045 char *inFile = NULL;
46 char *outFile = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 char *cp;
48 int inFd;
49 int outFd;
50 int inCc = 0;
51 int outCc;
52 long blockSize = 512;
53 uintmax_t skipBlocks = 0;
54 uintmax_t seekBlocks = 0;
55 uintmax_t count = (uintmax_t) - 1;
56 uintmax_t intotal;
57 uintmax_t outTotal;
58 unsigned char *buf;
Eric Andersen3cf52d11999-10-12 22:26:06 +000059
Eric Andersen3cf52d11999-10-12 22:26:06 +000060 argc--;
61 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 /* Parse any options */
64 while (argc) {
65 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
66 inFile = ((strchr(*argv, '=')) + 1);
67 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
68 outFile = ((strchr(*argv, '=')) + 1);
69 else if (strncmp("count", *argv, 5) == 0) {
70 count = getNum((strchr(*argv, '=')) + 1);
71 if (count <= 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000072 errorMsg("Bad count value %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 goto usage;
74 }
75 } else if (strncmp(*argv, "bs", 2) == 0) {
76 blockSize = getNum((strchr(*argv, '=')) + 1);
77 if (blockSize <= 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000078 errorMsg("Bad block size value %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 goto usage;
80 }
81 } else if (strncmp(*argv, "skip", 4) == 0) {
82 skipBlocks = getNum((strchr(*argv, '=')) + 1);
83 if (skipBlocks <= 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000084 errorMsg("Bad skip value %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 goto usage;
86 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 } else if (strncmp(*argv, "seek", 4) == 0) {
89 seekBlocks = getNum((strchr(*argv, '=')) + 1);
90 if (seekBlocks <= 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000091 errorMsg("Bad seek value %s\n", *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 goto usage;
93 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000094
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 } else {
96 goto usage;
97 }
98 argc--;
99 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000100 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000101
Erik Andersen0d068a22000-03-21 22:32:57 +0000102 buf = xmalloc(blockSize);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000103
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 intotal = 0;
105 outTotal = 0;
106
107 if (inFile == NULL)
108 inFd = fileno(stdin);
109 else
110 inFd = open(inFile, 0);
111
112 if (inFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000113 /* Note that we are not freeing buf or closing
114 * files here to save a few bytes. This exits
115 * here anyways... */
116
117 /* free(buf); */
118 fatalError( inFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
120
121 if (outFile == NULL)
122 outFd = fileno(stdout);
123 else
124 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
125
126 if (outFd < 0) {
Erik Andersen298854f2000-03-23 01:09:18 +0000127 /* Note that we are not freeing buf or closing
128 * files here to save a few bytes. This exits
129 * here anyways... */
130
131 /* close(inFd);
132 free(buf); */
133 fatalError( outFile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
135
136 lseek(inFd, skipBlocks * blockSize, SEEK_SET);
137 lseek(outFd, seekBlocks * blockSize, SEEK_SET);
138 //
139 //TODO: Convert to using fullRead & fullWrite
140 // from utility.c
141 // -Erik
142 while (outTotal < count * blockSize) {
143 inCc = read(inFd, buf, blockSize);
144 if (inCc < 0) {
145 perror(inFile);
146 goto cleanup;
147 } else if (inCc == 0) {
148 goto cleanup;
149 }
150 intotal += inCc;
151 cp = buf;
152
153 while (intotal > outTotal) {
154 if (outTotal + inCc > count * blockSize)
155 inCc = count * blockSize - outTotal;
156 outCc = write(outFd, cp, inCc);
157 if (outCc < 0) {
158 perror(outFile);
159 goto cleanup;
160 } else if (outCc == 0) {
161 goto cleanup;
162 }
163
164 inCc -= outCc;
165 cp += outCc;
166 outTotal += outCc;
167 }
168 }
169
170 if (inCc < 0)
171 perror(inFile);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000172
173 cleanup:
Erik Andersen298854f2000-03-23 01:09:18 +0000174 /* Note that we are not freeing memory or closing
175 * files here, to save a few bytes. */
Eric Andersenb040d4f2000-07-25 18:01:20 +0000176#ifdef BB_FEATURE_CLEAN_UP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 close(inFd);
178 close(outFd);
179 free(buf);
Erik Andersen298854f2000-03-23 01:09:18 +0000180#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000181
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 printf("%ld+%d records in\n", (long) (intotal / blockSize),
183 (intotal % blockSize) != 0);
184 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
185 (outTotal % blockSize) != 0);
186 exit(TRUE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000187 usage:
188
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 usage(dd_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000190}