blob: bc01eedbfaab9e4dfc5df26387ac05ae3dcdc3b9 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Eric Andersenc4996011999-10-20 22:08:37 +00002 * Mini dd implementation for busybox
3 *
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 * based in part on code taken from sash.
7 *
Eric Andersencc8ed391999-10-05 16:24:54 +00008 * Copyright (c) 1999 by David I. Bell
9 * Permission is granted to use, distribute, or modify this source,
10 * provided that this copyright notice remains intact.
11 *
Eric Andersencc8ed391999-10-05 16:24:54 +000012 * Permission to distribute this code under the GPL has been granted.
Eric Andersenc4996011999-10-20 22:08:37 +000013 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
Eric Andersencc8ed391999-10-05 16:24:54 +000028 */
29
Eric Andersenc4996011999-10-20 22:08:37 +000030
Eric Andersencc8ed391999-10-05 16:24:54 +000031#include "internal.h"
Eric Andersencb41c2e1999-11-22 07:41:00 +000032#include <features.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000033#include <stdio.h>
34#include <fcntl.h>
35#include <errno.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000036#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Eric Andersen6a76e651999-11-19 05:31:45 +000037#include <inttypes.h>
Eric Andersencb41c2e1999-11-22 07:41:00 +000038#else
39typedef unsigned long long int uintmax_t;
40#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Eric Andersene77ae3a1999-10-19 20:03:34 +000042static const char dd_usage[] =
Eric Andersend73dc5b1999-11-10 23:13:02 +000043"dd [if=name] [of=name] [bs=n] [count=n]\n\n"
Eric Andersenc4996011999-10-20 22:08:37 +000044"Copy a file, converting and formatting according to options\n\n"
45"\tif=FILE\tread from FILE instead of stdin\n"
46"\tof=FILE\twrite to FILE instead of stout\n"
47"\tbs=n\tread and write N BYTES at a time\n"
48"\tcount=n\tcopy only n input blocks\n"
49//"\tskip=n\tskip n input blocks\n"
50"\n"
Eric Andersen1792f8c1999-12-09 06:11:36 +000051"BYTES may be suffixed by w (x2), k (x1024), b (x512), or m (x1024^2).\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000052
53
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Eric Andersen3cf52d11999-10-12 22:26:06 +000055extern int dd_main (int argc, char **argv)
56{
Eric Andersen6a76e651999-11-19 05:31:45 +000057 const char *inFile = NULL;
58 const char *outFile = NULL;
Eric Andersen3cf52d11999-10-12 22:26:06 +000059 char *cp;
60 int inFd;
61 int outFd;
62 int inCc = 0;
63 int outCc;
Eric Andersen6a76e651999-11-19 05:31:45 +000064 size_t blockSize = 512;
65 //uintmax_t skipBlocks = 0;
66 uintmax_t count = (uintmax_t)-1;
67 uintmax_t intotal;
68 uintmax_t outTotal;
Eric Andersen3cf52d11999-10-12 22:26:06 +000069 unsigned char *buf;
70
Eric Andersen3cf52d11999-10-12 22:26:06 +000071 argc--;
72 argv++;
73
74 /* Parse any options */
75 while (argc) {
Eric Andersen5de30651999-10-13 00:53:55 +000076 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
77 inFile=((strchr(*argv, '='))+1);
78 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
79 outFile=((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000080 else if (strncmp("count", *argv, 5) == 0) {
Eric Andersen5de30651999-10-13 00:53:55 +000081 count = getNum ((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000082 if (count <= 0) {
Eric Andersen6a76e651999-11-19 05:31:45 +000083 fprintf (stderr, "Bad count value %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000084 goto usage;
85 }
86 }
Eric Andersen5de30651999-10-13 00:53:55 +000087 else if (strncmp(*argv, "bs", 2) == 0) {
88 blockSize = getNum ((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000089 if (blockSize <= 0) {
Eric Andersen6a76e651999-11-19 05:31:45 +000090 fprintf (stderr, "Bad block size value %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000091 goto usage;
92 }
93 }
Eric Andersenc4996011999-10-20 22:08:37 +000094#if 0
Eric Andersen5de30651999-10-13 00:53:55 +000095 else if (strncmp(*argv, "skip", 4) == 0) {
Eric Andersen3cf52d11999-10-12 22:26:06 +000096 skipBlocks = atoi( *argv);
97 if (skipBlocks <= 0) {
98 fprintf (stderr, "Bad skip value %d\n", skipBlocks);
99 goto usage;
100 }
101
102 }
Eric Andersenc4996011999-10-20 22:08:37 +0000103#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000104 else {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000105 goto usage;
Eric Andersen5de30651999-10-13 00:53:55 +0000106 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000107 argc--;
108 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000109 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000110
111 buf = malloc (blockSize);
112 if (buf == NULL) {
113 fprintf (stderr, "Cannot allocate buffer\n");
Eric Andersen5de30651999-10-13 00:53:55 +0000114 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115 }
116
117 intotal = 0;
118 outTotal = 0;
119
Eric Andersen6a76e651999-11-19 05:31:45 +0000120 if (inFile == NULL)
Eric Andersen08b10341999-11-19 02:38:58 +0000121 inFd = fileno(stdin);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000122 else
123 inFd = open (inFile, 0);
124
125 if (inFd < 0) {
126 perror (inFile);
127 free (buf);
Eric Andersen5de30651999-10-13 00:53:55 +0000128 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000129 }
130
Eric Andersen5de30651999-10-13 00:53:55 +0000131 if (outFile == NULL)
Eric Andersen08b10341999-11-19 02:38:58 +0000132 outFd = fileno(stdout);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000133 else
134 outFd = creat (outFile, 0666);
135
136 if (outFd < 0) {
137 perror (outFile);
138 close (inFd);
139 free (buf);
Eric Andersen5de30651999-10-13 00:53:55 +0000140 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000141 }
142
Eric Andersen5de30651999-10-13 00:53:55 +0000143 //lseek(inFd, skipBlocks*blockSize, SEEK_SET);
Eric Andersenb186d981999-12-03 09:19:54 +0000144 //
145 //TODO: Convert to using fullRead & fullWrite
146 // from utilitity.c
147 // -Erik
Eric Andersen3cf52d11999-10-12 22:26:06 +0000148 while (outTotal < count * blockSize) {
149 inCc = read (inFd, buf, blockSize);
150 if (inCc < 0) {
151 perror (inFile);
152 goto cleanup;
Eric Andersen08b10341999-11-19 02:38:58 +0000153 } else if (inCc == 0) {
154 goto cleanup;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000155 }
156 intotal += inCc;
157 cp = buf;
158
159 while (intotal > outTotal) {
160 if (outTotal + inCc > count * blockSize)
161 inCc = count * blockSize - outTotal;
162 outCc = write (outFd, cp, inCc);
163 if (outCc < 0) {
164 perror (outFile);
165 goto cleanup;
Eric Andersen08b10341999-11-19 02:38:58 +0000166 } else if (outCc == 0) {
167 goto cleanup;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000168 }
169
170 inCc -= outCc;
171 cp += outCc;
172 outTotal += outCc;
173 }
174 }
175
176 if (inCc < 0)
177 perror (inFile);
178
179 cleanup:
180 close (inFd);
181 close (outFd);
182 free (buf);
183
Eric Andersen6a76e651999-11-19 05:31:45 +0000184 printf ("%ld+%d records in\n", (long)(intotal / blockSize),
Eric Andersen3cf52d11999-10-12 22:26:06 +0000185 (intotal % blockSize) != 0);
Eric Andersen6a76e651999-11-19 05:31:45 +0000186 printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
Eric Andersen3cf52d11999-10-12 22:26:06 +0000187 (outTotal % blockSize) != 0);
188 exit( TRUE);
189 usage:
190
Eric Andersend73dc5b1999-11-10 23:13:02 +0000191 usage( dd_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000192}
193
Eric Andersencc8ed391999-10-05 16:24:54 +0000194