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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999, 2000 by Lineo, inc. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 8 | * Based in part on code taken from sash. |
| 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. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 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 | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 32 | #include "busybox.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 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | extern int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 44 | { |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 45 | char *inFile = NULL; |
| 46 | char *outFile = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | int inFd; |
| 48 | int outFd; |
| 49 | int inCc = 0; |
| 50 | int outCc; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 51 | int trunc=TRUE; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 52 | int sync=FALSE; |
| 53 | long blockSize = 512,ibs; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | uintmax_t skipBlocks = 0; |
| 55 | uintmax_t seekBlocks = 0; |
| 56 | uintmax_t count = (uintmax_t) - 1; |
Glenn L McGrath | 0ae8e5a | 2000-09-10 01:54:27 +0000 | [diff] [blame] | 57 | uintmax_t inTotal = 0; |
| 58 | uintmax_t outTotal = 0; |
| 59 | uintmax_t totalSize; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 0ae8e5a | 2000-09-10 01:54:27 +0000 | [diff] [blame] | 61 | unsigned char buf[BUFSIZ]; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 62 | char *keyword = NULL; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 64 | argc--; |
| 65 | argv++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 66 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | /* Parse any options */ |
| 68 | while (argc) { |
| 69 | if (inFile == NULL && (strncmp(*argv, "if", 2) == 0)) |
| 70 | inFile = ((strchr(*argv, '=')) + 1); |
| 71 | else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0)) |
| 72 | outFile = ((strchr(*argv, '=')) + 1); |
| 73 | else if (strncmp("count", *argv, 5) == 0) { |
| 74 | count = getNum((strchr(*argv, '=')) + 1); |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 75 | if (count < 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 76 | errorMsg("Bad count value %s\n", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | goto usage; |
| 78 | } |
| 79 | } else if (strncmp(*argv, "bs", 2) == 0) { |
| 80 | blockSize = getNum((strchr(*argv, '=')) + 1); |
| 81 | if (blockSize <= 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 82 | errorMsg("Bad block size value %s\n", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | goto usage; |
| 84 | } |
| 85 | } else if (strncmp(*argv, "skip", 4) == 0) { |
| 86 | skipBlocks = getNum((strchr(*argv, '=')) + 1); |
| 87 | if (skipBlocks <= 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 88 | errorMsg("Bad skip value %s\n", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | goto usage; |
| 90 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 91 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | } else if (strncmp(*argv, "seek", 4) == 0) { |
| 93 | seekBlocks = getNum((strchr(*argv, '=')) + 1); |
| 94 | if (seekBlocks <= 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 95 | errorMsg("Bad seek value %s\n", *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | goto usage; |
| 97 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 98 | } else if (strncmp(*argv, "conv", 4) == 0) { |
| 99 | keyword = (strchr(*argv, '=') + 1); |
| 100 | if (strcmp(keyword, "notrunc") == 0) |
| 101 | trunc=FALSE; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 102 | if (strcmp(keyword, "sync") == 0) |
| 103 | sync=TRUE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | } else { |
| 105 | goto usage; |
| 106 | } |
| 107 | argc--; |
| 108 | argv++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 109 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 110 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | if (inFile == NULL) |
| 112 | inFd = fileno(stdin); |
| 113 | else |
| 114 | inFd = open(inFile, 0); |
| 115 | |
| 116 | if (inFd < 0) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 117 | /* Note that we are not freeing buf or closing |
| 118 | * files here to save a few bytes. This exits |
| 119 | * here anyways... */ |
| 120 | |
| 121 | /* free(buf); */ |
Matt Kraai | 324a778 | 2000-10-25 15:10:08 +0000 | [diff] [blame] | 122 | fatalPerror("%s", inFile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (outFile == NULL) |
| 126 | outFd = fileno(stdout); |
| 127 | else |
Glenn L McGrath | 18310f1 | 2000-09-10 04:39:37 +0000 | [diff] [blame] | 128 | outFd = open(outFile, O_WRONLY | O_CREAT, 0666); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | |
| 130 | if (outFd < 0) { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 131 | /* Note that we are not freeing buf or closing |
| 132 | * files here to save a few bytes. This exits |
| 133 | * here anyways... */ |
| 134 | |
| 135 | /* close(inFd); |
| 136 | free(buf); */ |
Matt Kraai | 324a778 | 2000-10-25 15:10:08 +0000 | [diff] [blame] | 137 | fatalPerror("%s", outFile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Glenn L McGrath | 18310f1 | 2000-09-10 04:39:37 +0000 | [diff] [blame] | 140 | lseek(inFd, (off_t) (skipBlocks * blockSize), SEEK_SET); |
Glenn L McGrath | 729216c | 2000-09-10 04:42:20 +0000 | [diff] [blame] | 141 | lseek(outFd, (off_t) (seekBlocks * blockSize), SEEK_SET); |
Glenn L McGrath | 0ae8e5a | 2000-09-10 01:54:27 +0000 | [diff] [blame] | 142 | totalSize=count*blockSize; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 143 | |
| 144 | ibs=blockSize; |
| 145 | if (ibs > BUFSIZ) |
| 146 | ibs=BUFSIZ; |
| 147 | |
| 148 | while (totalSize > outTotal) { |
| 149 | inCc = fullRead(inFd, buf, ibs); |
Glenn L McGrath | 0ae8e5a | 2000-09-10 01:54:27 +0000 | [diff] [blame] | 150 | inTotal += inCc; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 151 | if ( (sync==TRUE) && (inCc>0) ) |
| 152 | while (inCc<ibs) |
| 153 | buf[inCc++]='\0'; |
| 154 | |
| 155 | if ((outCc = fullWrite(outFd, buf, inCc)) < 1){ |
| 156 | if (outCc < 0 ){ |
| 157 | perror("Error during write"); |
| 158 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 159 | break; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 160 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 161 | outTotal += outCc; |
| 162 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 163 | if (trunc == TRUE) { |
| 164 | ftruncate(outFd, lseek(outFd, 0, SEEK_CUR)); |
| 165 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 166 | /* Note that we are not freeing memory or closing |
| 167 | * files here, to save a few bytes. */ |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 168 | #ifdef BB_FEATURE_CLEAN_UP |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | close(inFd); |
| 170 | close(outFd); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 171 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 172 | |
Glenn L McGrath | 0ae8e5a | 2000-09-10 01:54:27 +0000 | [diff] [blame] | 173 | printf("%ld+%d records in\n", (long) (inTotal / blockSize), |
| 174 | (inTotal % blockSize) != 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | printf("%ld+%d records out\n", (long) (outTotal / blockSize), |
| 176 | (outTotal % blockSize) != 0); |
| 177 | exit(TRUE); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 178 | usage: |
| 179 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 180 | usage(dd_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 181 | } |