blob: 3e1024a609b4288ae8d291576780424f222b3fe0 [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[] =
Erik Andersenfac10d72000-02-07 05:29:42 +000043"dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=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"
Erik Andersenfac10d72000-02-07 05:29:42 +000046"\tof=FILE\twrite to FILE instead of stdout\n"
47"\tbs=n\tread and write n bytes at a time\n"
Eric Andersenc4996011999-10-20 22:08:37 +000048"\tcount=n\tcopy only n input blocks\n"
Erik Andersenfac10d72000-02-07 05:29:42 +000049"\tskip=n\tskip n input blocks\n"
50"\tseek=n\tskip n output blocks\n"
Eric Andersenc4996011999-10-20 22:08:37 +000051"\n"
Erik Andersenfac10d72000-02-07 05:29:42 +000052"Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000053
54
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Eric Andersen3cf52d11999-10-12 22:26:06 +000056extern int dd_main (int argc, char **argv)
57{
Eric Andersen6a76e651999-11-19 05:31:45 +000058 const char *inFile = NULL;
59 const char *outFile = NULL;
Eric Andersen3cf52d11999-10-12 22:26:06 +000060 char *cp;
61 int inFd;
62 int outFd;
63 int inCc = 0;
64 int outCc;
Erik Andersenfac10d72000-02-07 05:29:42 +000065 long blockSize = 512;
66 uintmax_t skipBlocks = 0;
67 uintmax_t seekBlocks = 0;
Eric Andersen6a76e651999-11-19 05:31:45 +000068 uintmax_t count = (uintmax_t)-1;
69 uintmax_t intotal;
70 uintmax_t outTotal;
Eric Andersen3cf52d11999-10-12 22:26:06 +000071 unsigned char *buf;
72
Eric Andersen3cf52d11999-10-12 22:26:06 +000073 argc--;
74 argv++;
75
76 /* Parse any options */
77 while (argc) {
Eric Andersen5de30651999-10-13 00:53:55 +000078 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
79 inFile=((strchr(*argv, '='))+1);
80 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
81 outFile=((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000082 else if (strncmp("count", *argv, 5) == 0) {
Eric Andersen5de30651999-10-13 00:53:55 +000083 count = getNum ((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000084 if (count <= 0) {
Eric Andersen6a76e651999-11-19 05:31:45 +000085 fprintf (stderr, "Bad count value %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000086 goto usage;
87 }
88 }
Eric Andersen5de30651999-10-13 00:53:55 +000089 else if (strncmp(*argv, "bs", 2) == 0) {
90 blockSize = getNum ((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000091 if (blockSize <= 0) {
Eric Andersen6a76e651999-11-19 05:31:45 +000092 fprintf (stderr, "Bad block size value %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000093 goto usage;
94 }
95 }
Eric Andersen5de30651999-10-13 00:53:55 +000096 else if (strncmp(*argv, "skip", 4) == 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000097 skipBlocks = getNum ((strchr(*argv, '='))+1);
Eric Andersen3cf52d11999-10-12 22:26:06 +000098 if (skipBlocks <= 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000099 fprintf (stderr, "Bad skip value %s\n", *argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000100 goto usage;
101 }
102
103 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000104 else if (strncmp(*argv, "seek", 4) == 0) {
105 seekBlocks = getNum ((strchr(*argv, '='))+1);
106 if (seekBlocks <= 0) {
107 fprintf (stderr, "Bad seek value %s\n", *argv);
108 goto usage;
109 }
110
111 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000112 else {
Eric Andersen3cf52d11999-10-12 22:26:06 +0000113 goto usage;
Eric Andersen5de30651999-10-13 00:53:55 +0000114 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115 argc--;
116 argv++;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000117 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118
119 buf = malloc (blockSize);
120 if (buf == NULL) {
121 fprintf (stderr, "Cannot allocate buffer\n");
Eric Andersen5de30651999-10-13 00:53:55 +0000122 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000123 }
124
125 intotal = 0;
126 outTotal = 0;
127
Eric Andersen6a76e651999-11-19 05:31:45 +0000128 if (inFile == NULL)
Eric Andersen08b10341999-11-19 02:38:58 +0000129 inFd = fileno(stdin);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000130 else
131 inFd = open (inFile, 0);
132
133 if (inFd < 0) {
134 perror (inFile);
135 free (buf);
Eric Andersen5de30651999-10-13 00:53:55 +0000136 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000137 }
138
Eric Andersen5de30651999-10-13 00:53:55 +0000139 if (outFile == NULL)
Eric Andersen08b10341999-11-19 02:38:58 +0000140 outFd = fileno(stdout);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000141 else
Erik Andersenfac10d72000-02-07 05:29:42 +0000142 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000143
144 if (outFd < 0) {
145 perror (outFile);
146 close (inFd);
147 free (buf);
Eric Andersen5de30651999-10-13 00:53:55 +0000148 exit( FALSE);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000149 }
150
Erik Andersenfac10d72000-02-07 05:29:42 +0000151 lseek(inFd, skipBlocks*blockSize, SEEK_SET);
152 lseek(outFd, seekBlocks*blockSize, SEEK_SET);
Eric Andersenb186d981999-12-03 09:19:54 +0000153 //
154 //TODO: Convert to using fullRead & fullWrite
Erik Andersenfac10d72000-02-07 05:29:42 +0000155 // from utility.c
Eric Andersenb186d981999-12-03 09:19:54 +0000156 // -Erik
Eric Andersen3cf52d11999-10-12 22:26:06 +0000157 while (outTotal < count * blockSize) {
158 inCc = read (inFd, buf, blockSize);
159 if (inCc < 0) {
160 perror (inFile);
161 goto cleanup;
Eric Andersen08b10341999-11-19 02:38:58 +0000162 } else if (inCc == 0) {
163 goto cleanup;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000164 }
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;
Eric Andersen08b10341999-11-19 02:38:58 +0000175 } else if (outCc == 0) {
176 goto cleanup;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000177 }
178
179 inCc -= outCc;
180 cp += outCc;
181 outTotal += outCc;
182 }
183 }
184
185 if (inCc < 0)
186 perror (inFile);
187
188 cleanup:
189 close (inFd);
190 close (outFd);
191 free (buf);
192
Eric Andersen6a76e651999-11-19 05:31:45 +0000193 printf ("%ld+%d records in\n", (long)(intotal / blockSize),
Eric Andersen3cf52d11999-10-12 22:26:06 +0000194 (intotal % blockSize) != 0);
Eric Andersen6a76e651999-11-19 05:31:45 +0000195 printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
Eric Andersen3cf52d11999-10-12 22:26:06 +0000196 (outTotal % blockSize) != 0);
197 exit( TRUE);
198 usage:
199
Eric Andersend73dc5b1999-11-10 23:13:02 +0000200 usage( dd_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000201}
202
Eric Andersencc8ed391999-10-05 16:24:54 +0000203