Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 3 | * Mini dd implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 7 | * based in part on code taken from sash. |
| 8 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 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. |
| 12 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 13 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 14 | * |
| 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include "internal.h" |
Eric Andersen | cb41c2e | 1999-11-22 07:41:00 +0000 | [diff] [blame] | 33 | #include <features.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <errno.h> |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 37 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Eric Andersen | 6a76e65 | 1999-11-19 05:31:45 +0000 | [diff] [blame] | 38 | #include <inttypes.h> |
Eric Andersen | cb41c2e | 1999-11-22 07:41:00 +0000 | [diff] [blame] | 39 | #else |
| 40 | typedef unsigned long long int uintmax_t; |
| 41 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 43 | static const char dd_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n" |
| 45 | "Copy a file, converting and formatting according to options\n\n" |
| 46 | "\tif=FILE\tread from FILE instead of stdin\n" |
| 47 | "\tof=FILE\twrite to FILE instead of stdout\n" |
| 48 | "\tbs=n\tread and write n bytes at a time\n" |
| 49 | "\tcount=n\tcopy only n input blocks\n" |
| 50 | "\tskip=n\tskip n input blocks\n" |
| 51 | "\tseek=n\tskip n output blocks\n" |
| 52 | |
| 53 | "\n" |
| 54 | "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | |
| 56 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | extern int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 59 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | const char *inFile = NULL; |
| 61 | const char *outFile = NULL; |
| 62 | char *cp; |
| 63 | int inFd; |
| 64 | int outFd; |
| 65 | int inCc = 0; |
| 66 | int outCc; |
| 67 | long blockSize = 512; |
| 68 | uintmax_t skipBlocks = 0; |
| 69 | uintmax_t seekBlocks = 0; |
| 70 | uintmax_t count = (uintmax_t) - 1; |
| 71 | uintmax_t intotal; |
| 72 | uintmax_t outTotal; |
| 73 | unsigned char *buf; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 74 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 75 | argc--; |
| 76 | argv++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 77 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | /* Parse any options */ |
| 79 | while (argc) { |
| 80 | if (inFile == NULL && (strncmp(*argv, "if", 2) == 0)) |
| 81 | inFile = ((strchr(*argv, '=')) + 1); |
| 82 | else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0)) |
| 83 | outFile = ((strchr(*argv, '=')) + 1); |
| 84 | else if (strncmp("count", *argv, 5) == 0) { |
| 85 | count = getNum((strchr(*argv, '=')) + 1); |
| 86 | if (count <= 0) { |
| 87 | fprintf(stderr, "Bad count value %s\n", *argv); |
| 88 | goto usage; |
| 89 | } |
| 90 | } else if (strncmp(*argv, "bs", 2) == 0) { |
| 91 | blockSize = getNum((strchr(*argv, '=')) + 1); |
| 92 | if (blockSize <= 0) { |
| 93 | fprintf(stderr, "Bad block size value %s\n", *argv); |
| 94 | goto usage; |
| 95 | } |
| 96 | } else if (strncmp(*argv, "skip", 4) == 0) { |
| 97 | skipBlocks = getNum((strchr(*argv, '=')) + 1); |
| 98 | if (skipBlocks <= 0) { |
| 99 | fprintf(stderr, "Bad skip value %s\n", *argv); |
| 100 | goto usage; |
| 101 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 102 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | } else if (strncmp(*argv, "seek", 4) == 0) { |
| 104 | seekBlocks = getNum((strchr(*argv, '=')) + 1); |
| 105 | if (seekBlocks <= 0) { |
| 106 | fprintf(stderr, "Bad seek value %s\n", *argv); |
| 107 | goto usage; |
| 108 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 109 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | } else { |
| 111 | goto usage; |
| 112 | } |
| 113 | argc--; |
| 114 | argv++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 115 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 116 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | buf = malloc(blockSize); |
| 118 | if (buf == NULL) { |
| 119 | fprintf(stderr, "Cannot allocate buffer\n"); |
| 120 | exit(FALSE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 121 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | intotal = 0; |
| 124 | outTotal = 0; |
| 125 | |
| 126 | if (inFile == NULL) |
| 127 | inFd = fileno(stdin); |
| 128 | else |
| 129 | inFd = open(inFile, 0); |
| 130 | |
| 131 | if (inFd < 0) { |
| 132 | perror(inFile); |
| 133 | free(buf); |
| 134 | exit(FALSE); |
| 135 | } |
| 136 | |
| 137 | if (outFile == NULL) |
| 138 | outFd = fileno(stdout); |
| 139 | else |
| 140 | outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 141 | |
| 142 | if (outFd < 0) { |
| 143 | perror(outFile); |
| 144 | close(inFd); |
| 145 | free(buf); |
| 146 | exit(FALSE); |
| 147 | } |
| 148 | |
| 149 | lseek(inFd, skipBlocks * blockSize, SEEK_SET); |
| 150 | lseek(outFd, seekBlocks * blockSize, SEEK_SET); |
| 151 | // |
| 152 | //TODO: Convert to using fullRead & fullWrite |
| 153 | // from utility.c |
| 154 | // -Erik |
| 155 | while (outTotal < count * blockSize) { |
| 156 | inCc = read(inFd, buf, blockSize); |
| 157 | if (inCc < 0) { |
| 158 | perror(inFile); |
| 159 | goto cleanup; |
| 160 | } else if (inCc == 0) { |
| 161 | goto cleanup; |
| 162 | } |
| 163 | intotal += inCc; |
| 164 | cp = buf; |
| 165 | |
| 166 | while (intotal > outTotal) { |
| 167 | if (outTotal + inCc > count * blockSize) |
| 168 | inCc = count * blockSize - outTotal; |
| 169 | outCc = write(outFd, cp, inCc); |
| 170 | if (outCc < 0) { |
| 171 | perror(outFile); |
| 172 | goto cleanup; |
| 173 | } else if (outCc == 0) { |
| 174 | goto cleanup; |
| 175 | } |
| 176 | |
| 177 | inCc -= outCc; |
| 178 | cp += outCc; |
| 179 | outTotal += outCc; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (inCc < 0) |
| 184 | perror(inFile); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 185 | |
| 186 | cleanup: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | close(inFd); |
| 188 | close(outFd); |
| 189 | free(buf); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 190 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | printf("%ld+%d records in\n", (long) (intotal / blockSize), |
| 192 | (intotal % blockSize) != 0); |
| 193 | printf("%ld+%d records out\n", (long) (outTotal / blockSize), |
| 194 | (outTotal % blockSize) != 0); |
| 195 | exit(TRUE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 196 | usage: |
| 197 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | usage(dd_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 199 | } |