Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | * 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 12 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 13 | * |
| 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 30 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | #include "internal.h" |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 36 | static const char dd_usage[] = |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 37 | "dd [if=name] [of=name] [bs=n] [count=n]\n\n" |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 38 | "Copy a file, converting and formatting according to options\n\n" |
| 39 | "\tif=FILE\tread from FILE instead of stdin\n" |
| 40 | "\tof=FILE\twrite to FILE instead of stout\n" |
| 41 | "\tbs=n\tread and write N BYTES at a time\n" |
| 42 | "\tcount=n\tcopy only n input blocks\n" |
| 43 | //"\tskip=n\tskip n input blocks\n" |
| 44 | "\n" |
| 45 | "BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | |
| 47 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /* |
| 51 | * Read a number with a possible multiplier. |
| 52 | * Returns -1 if the number format is illegal. |
| 53 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 54 | static long getNum (const char *cp) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 56 | long value; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 58 | if (!isDecimal (*cp)) |
| 59 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 61 | value = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 63 | while (isDecimal (*cp)) |
| 64 | value = value * 10 + *cp++ - '0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 65 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 66 | switch (*cp++) { |
| 67 | case 'k': |
| 68 | value *= 1024; |
| 69 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 71 | case 'b': |
| 72 | value *= 512; |
| 73 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 74 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 75 | case 'w': |
| 76 | value *= 2; |
| 77 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 79 | case '\0': |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | return value; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 81 | |
| 82 | default: |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | if (*cp) |
| 87 | return -1; |
| 88 | |
| 89 | return value; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 92 | |
| 93 | extern int dd_main (int argc, char **argv) |
| 94 | { |
| 95 | const char *inFile; |
| 96 | const char *outFile; |
| 97 | char *cp; |
| 98 | int inFd; |
| 99 | int outFd; |
| 100 | int inCc = 0; |
| 101 | int outCc; |
| 102 | int skipBlocks; |
| 103 | int blockSize; |
| 104 | long count; |
| 105 | long intotal; |
| 106 | long outTotal; |
| 107 | unsigned char *buf; |
| 108 | |
| 109 | inFile = NULL; |
| 110 | outFile = NULL; |
| 111 | blockSize = 512; |
| 112 | skipBlocks = 0; |
| 113 | count = 1; |
| 114 | |
| 115 | |
| 116 | argc--; |
| 117 | argv++; |
| 118 | |
| 119 | /* Parse any options */ |
| 120 | while (argc) { |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 121 | if (inFile == NULL && (strncmp(*argv, "if", 2) == 0)) |
| 122 | inFile=((strchr(*argv, '='))+1); |
| 123 | else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0)) |
| 124 | outFile=((strchr(*argv, '='))+1); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 125 | else if (strncmp("count", *argv, 5) == 0) { |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 126 | count = getNum ((strchr(*argv, '='))+1); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 127 | if (count <= 0) { |
| 128 | fprintf (stderr, "Bad count value %ld\n", count); |
| 129 | goto usage; |
| 130 | } |
| 131 | } |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 132 | else if (strncmp(*argv, "bs", 2) == 0) { |
| 133 | blockSize = getNum ((strchr(*argv, '='))+1); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 134 | if (blockSize <= 0) { |
| 135 | fprintf (stderr, "Bad block size value %d\n", blockSize); |
| 136 | goto usage; |
| 137 | } |
| 138 | } |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 139 | #if 0 |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 140 | else if (strncmp(*argv, "skip", 4) == 0) { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 141 | skipBlocks = atoi( *argv); |
| 142 | if (skipBlocks <= 0) { |
| 143 | fprintf (stderr, "Bad skip value %d\n", skipBlocks); |
| 144 | goto usage; |
| 145 | } |
| 146 | |
| 147 | } |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 148 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 149 | else { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 150 | goto usage; |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 151 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 152 | argc--; |
| 153 | argv++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 154 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 155 | |
| 156 | buf = malloc (blockSize); |
| 157 | if (buf == NULL) { |
| 158 | fprintf (stderr, "Cannot allocate buffer\n"); |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 159 | exit( FALSE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | intotal = 0; |
| 163 | outTotal = 0; |
| 164 | |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 165 | if (inFile == NULL) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 166 | inFd = STDIN; |
| 167 | else |
| 168 | inFd = open (inFile, 0); |
| 169 | |
| 170 | if (inFd < 0) { |
| 171 | perror (inFile); |
| 172 | free (buf); |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 173 | exit( FALSE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 176 | if (outFile == NULL) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 177 | outFd = STDOUT; |
| 178 | else |
| 179 | outFd = creat (outFile, 0666); |
| 180 | |
| 181 | if (outFd < 0) { |
| 182 | perror (outFile); |
| 183 | close (inFd); |
| 184 | free (buf); |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 185 | exit( FALSE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Eric Andersen | 5de3065 | 1999-10-13 00:53:55 +0000 | [diff] [blame] | 188 | //lseek(inFd, skipBlocks*blockSize, SEEK_SET); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 189 | while (outTotal < count * blockSize) { |
| 190 | inCc = read (inFd, buf, blockSize); |
| 191 | if (inCc < 0) { |
| 192 | perror (inFile); |
| 193 | goto cleanup; |
| 194 | } |
| 195 | intotal += inCc; |
| 196 | cp = buf; |
| 197 | |
| 198 | while (intotal > outTotal) { |
| 199 | if (outTotal + inCc > count * blockSize) |
| 200 | inCc = count * blockSize - outTotal; |
| 201 | outCc = write (outFd, cp, inCc); |
| 202 | if (outCc < 0) { |
| 203 | perror (outFile); |
| 204 | goto cleanup; |
| 205 | } |
| 206 | |
| 207 | inCc -= outCc; |
| 208 | cp += outCc; |
| 209 | outTotal += outCc; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (inCc < 0) |
| 214 | perror (inFile); |
| 215 | |
| 216 | cleanup: |
| 217 | close (inFd); |
| 218 | close (outFd); |
| 219 | free (buf); |
| 220 | |
| 221 | printf ("%ld+%d records in\n", intotal / blockSize, |
| 222 | (intotal % blockSize) != 0); |
| 223 | printf ("%ld+%d records out\n", outTotal / blockSize, |
| 224 | (outTotal % blockSize) != 0); |
| 225 | exit( TRUE); |
| 226 | usage: |
| 227 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 228 | usage( dd_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | |