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 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
| 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 33 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 34 | { "c", 1 }, |
| 35 | { "w", 2 }, |
| 36 | { "b", 512 }, |
| 37 | { "kD", 1000 }, |
| 38 | { "k", 1024 }, |
| 39 | { "MD", 1000000 }, |
| 40 | { "M", 1048576 }, |
| 41 | { "GD", 1000000000 }, |
| 42 | { "G", 1073741824 }, |
| 43 | { NULL, 0 } |
| 44 | }; |
| 45 | |
| 46 | int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 47 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 48 | int i, ifd, ofd, oflag, sync_flag = FALSE, trunc = TRUE; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 49 | size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0; |
| 50 | size_t bs = 512, count = -1; |
| 51 | ssize_t n; |
| 52 | off_t seek = 0, skip = 0; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 53 | char *infile = NULL, *outfile = NULL, *buf; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 54 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 55 | for (i = 1; i < argc; i++) { |
| 56 | if (strncmp("bs=", argv[i], 3) == 0) |
| 57 | bs = parse_number(argv[i]+3, dd_suffixes); |
| 58 | else if (strncmp("count=", argv[i], 6) == 0) |
| 59 | count = parse_number(argv[i]+6, dd_suffixes); |
| 60 | else if (strncmp("seek=", argv[i], 5) == 0) |
| 61 | seek = parse_number(argv[i]+5, dd_suffixes); |
| 62 | else if (strncmp("skip=", argv[i], 5) == 0) |
| 63 | skip = parse_number(argv[i]+5, dd_suffixes); |
| 64 | else if (strncmp("if=", argv[i], 3) == 0) |
| 65 | infile = argv[i]+3; |
| 66 | else if (strncmp("of=", argv[i], 3) == 0) |
| 67 | outfile = argv[i]+3; |
| 68 | else if (strncmp("conv=", argv[i], 5) == 0) { |
| 69 | buf = argv[i]+5; |
| 70 | while (1) { |
| 71 | if (strncmp("notrunc", buf, 7) == 0) { |
| 72 | trunc = FALSE; |
| 73 | buf += 7; |
| 74 | } else if (strncmp("sync", buf, 4) == 0) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 75 | sync_flag = TRUE; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 76 | buf += 4; |
| 77 | } else { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 78 | error_msg_and_die("invalid conversion `%s'", argv[i]+5); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 79 | } |
| 80 | if (buf[0] == '\0') |
| 81 | break; |
| 82 | if (buf[0] == ',') |
| 83 | buf++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 85 | } else |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 86 | show_usage(); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 87 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 88 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 89 | buf = xmalloc(bs); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 91 | if (infile != NULL) { |
| 92 | if ((ifd = open(infile, O_RDONLY)) < 0) |
| 93 | perror_msg_and_die("%s", infile); |
| 94 | } else { |
| 95 | ifd = STDIN_FILENO; |
| 96 | infile = "standard input"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 99 | if (outfile != NULL) { |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 100 | oflag = O_WRONLY | O_CREAT; |
| 101 | |
| 102 | if (!seek && trunc) |
| 103 | oflag |= O_TRUNC; |
| 104 | |
| 105 | if ((ofd = open(outfile, oflag, 0666)) < 0) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 106 | perror_msg_and_die("%s", outfile); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 107 | |
| 108 | if (seek && trunc) { |
| 109 | if (ftruncate(ofd, seek * bs) < 0) |
| 110 | perror_msg_and_die("%s", outfile); |
| 111 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 112 | } else { |
| 113 | ofd = STDOUT_FILENO; |
| 114 | outfile = "standard output"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 117 | if (skip) { |
| 118 | if (lseek(ifd, skip * bs, SEEK_CUR) < 0) |
| 119 | perror_msg_and_die("%s", infile); |
| 120 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 121 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 122 | if (seek) { |
| 123 | if (lseek(ofd, seek * bs, SEEK_CUR) < 0) |
| 124 | perror_msg_and_die("%s", outfile); |
| 125 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 126 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 127 | while (in_full + in_part != count) { |
| 128 | n = safe_read(ifd, buf, bs); |
| 129 | if (n < 0) |
| 130 | perror_msg_and_die("%s", infile); |
| 131 | if (n == 0) |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 132 | break; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 133 | if (n == bs) |
| 134 | in_full++; |
| 135 | else |
| 136 | in_part++; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 137 | if (sync_flag) { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 138 | memset(buf + n, '\0', bs - n); |
| 139 | n = bs; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 140 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 141 | n = full_write(ofd, buf, n); |
| 142 | if (n < 0) |
| 143 | perror_msg_and_die("%s", outfile); |
| 144 | if (n == bs) |
| 145 | out_full++; |
| 146 | else |
| 147 | out_part++; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 148 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 149 | |
Matt Kraai | 73f6a1b | 2001-07-31 21:02:19 +0000 | [diff] [blame] | 150 | fprintf(stderr, "%ld+%ld records in\n", (long)in_full, (long)in_part); |
| 151 | fprintf(stderr, "%ld+%ld records out\n", (long)out_full, (long)out_part); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 152 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 153 | return EXIT_SUCCESS; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 154 | } |