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