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 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 6 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 25 | |
| 26 | #include <sys/types.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 29 | static struct suffix_mult dd_suffixes[] = { |
| 30 | { "c", 1 }, |
| 31 | { "w", 2 }, |
| 32 | { "b", 512 }, |
| 33 | { "kD", 1000 }, |
| 34 | { "k", 1024 }, |
| 35 | { "MD", 1000000 }, |
| 36 | { "M", 1048576 }, |
| 37 | { "GD", 1000000000 }, |
| 38 | { "G", 1073741824 }, |
| 39 | { NULL, 0 } |
| 40 | }; |
| 41 | |
| 42 | int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 43 | { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 44 | int i, ifd, ofd, sync = FALSE, trunc = TRUE; |
| 45 | size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0; |
| 46 | size_t bs = 512, count = -1; |
| 47 | ssize_t n; |
| 48 | off_t seek = 0, skip = 0; |
| 49 | FILE *statusfp; |
| 50 | char *infile = NULL, *outfile = NULL, *buf; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 51 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 52 | for (i = 1; i < argc; i++) { |
| 53 | if (strncmp("bs=", argv[i], 3) == 0) |
| 54 | bs = parse_number(argv[i]+3, dd_suffixes); |
| 55 | else if (strncmp("count=", argv[i], 6) == 0) |
| 56 | count = parse_number(argv[i]+6, dd_suffixes); |
| 57 | else if (strncmp("seek=", argv[i], 5) == 0) |
| 58 | seek = parse_number(argv[i]+5, dd_suffixes); |
| 59 | else if (strncmp("skip=", argv[i], 5) == 0) |
| 60 | skip = parse_number(argv[i]+5, dd_suffixes); |
| 61 | else if (strncmp("if=", argv[i], 3) == 0) |
| 62 | infile = argv[i]+3; |
| 63 | else if (strncmp("of=", argv[i], 3) == 0) |
| 64 | outfile = argv[i]+3; |
| 65 | else if (strncmp("conv=", argv[i], 5) == 0) { |
| 66 | buf = argv[i]+5; |
| 67 | while (1) { |
| 68 | if (strncmp("notrunc", buf, 7) == 0) { |
| 69 | trunc = FALSE; |
| 70 | buf += 7; |
| 71 | } else if (strncmp("sync", buf, 4) == 0) { |
| 72 | sync = TRUE; |
| 73 | buf += 4; |
| 74 | } else { |
| 75 | error_msg_and_die("invalid conversion `%s'\n", argv[i]+5); |
| 76 | } |
| 77 | if (buf[0] == '\0') |
| 78 | break; |
| 79 | if (buf[0] == ',') |
| 80 | buf++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 82 | } else |
| 83 | usage(dd_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 84 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 85 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 86 | buf = xmalloc(bs); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 88 | if (infile != NULL) { |
| 89 | if ((ifd = open(infile, O_RDONLY)) < 0) |
| 90 | perror_msg_and_die("%s", infile); |
| 91 | } else { |
| 92 | ifd = STDIN_FILENO; |
| 93 | infile = "standard input"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 96 | if (outfile != NULL) { |
| 97 | if ((ofd = open(outfile, O_WRONLY | O_CREAT, 0666)) < 0) |
| 98 | perror_msg_and_die("%s", outfile); |
| 99 | statusfp = stdout; |
| 100 | } else { |
| 101 | ofd = STDOUT_FILENO; |
| 102 | outfile = "standard output"; |
| 103 | statusfp = stderr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 106 | if (skip) { |
| 107 | if (lseek(ifd, skip * bs, SEEK_CUR) < 0) |
| 108 | perror_msg_and_die("%s", infile); |
| 109 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 110 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 111 | if (seek) { |
| 112 | if (lseek(ofd, seek * bs, SEEK_CUR) < 0) |
| 113 | perror_msg_and_die("%s", outfile); |
| 114 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 115 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 116 | if (trunc) { |
| 117 | if (ftruncate(ofd, seek * bs) < 0) |
| 118 | perror_msg_and_die("%s", outfile); |
| 119 | } |
| 120 | |
| 121 | while (in_full + in_part != count) { |
| 122 | n = safe_read(ifd, buf, bs); |
| 123 | if (n < 0) |
| 124 | perror_msg_and_die("%s", infile); |
| 125 | if (n == 0) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 126 | break; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 127 | if (n == bs) |
| 128 | in_full++; |
| 129 | else |
| 130 | in_part++; |
| 131 | if (sync) { |
| 132 | memset(buf + n, '\0', bs - n); |
| 133 | n = bs; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 134 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 135 | n = full_write(ofd, buf, n); |
| 136 | if (n < 0) |
| 137 | perror_msg_and_die("%s", outfile); |
| 138 | if (n == bs) |
| 139 | out_full++; |
| 140 | else |
| 141 | out_part++; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 142 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 143 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 144 | fprintf(statusfp, "%d+%d records in\n", in_full, in_part); |
| 145 | fprintf(statusfp, "%d+%d records out\n", out_full, out_part); |
| 146 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 147 | return EXIT_SUCCESS; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 148 | } |