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 | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 6 | * Copyright (C) 2000,2001 Matt Kraai |
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> |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 25 | #include <sys/stat.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <unistd.h> |
| 29 | #include <string.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 31 | #include "busybox.h" |
| 32 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 34 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 35 | { "c", 1 }, |
| 36 | { "w", 2 }, |
| 37 | { "b", 512 }, |
| 38 | { "kD", 1000 }, |
| 39 | { "k", 1024 }, |
| 40 | { "MD", 1000000 }, |
| 41 | { "M", 1048576 }, |
| 42 | { "GD", 1000000000 }, |
| 43 | { "G", 1073741824 }, |
| 44 | { NULL, 0 } |
| 45 | }; |
| 46 | |
| 47 | int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 48 | { |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 49 | size_t out_full = 0; |
| 50 | size_t out_part = 0; |
| 51 | size_t in_full = 0; |
| 52 | size_t in_part = 0; |
| 53 | size_t count = -1; |
| 54 | size_t bs = 512; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 55 | ssize_t n; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 56 | off_t seek = 0; |
| 57 | off_t skip = 0; |
| 58 | int sync_flag = FALSE; |
| 59 | int noerror = FALSE; |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 60 | int trunc_flag = TRUE; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 61 | int oflag; |
| 62 | int ifd; |
| 63 | int ofd; |
| 64 | int i; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | const char *infile = NULL; |
| 66 | const char *outfile = NULL; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 67 | char *buf; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 68 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 69 | for (i = 1; i < argc; i++) { |
| 70 | if (strncmp("bs=", argv[i], 3) == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | bs = bb_xparse_number(argv[i]+3, dd_suffixes); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 72 | else if (strncmp("count=", argv[i], 6) == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | count = bb_xparse_number(argv[i]+6, dd_suffixes); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 74 | else if (strncmp("seek=", argv[i], 5) == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | seek = bb_xparse_number(argv[i]+5, dd_suffixes); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 76 | else if (strncmp("skip=", argv[i], 5) == 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | skip = bb_xparse_number(argv[i]+5, dd_suffixes); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 78 | else if (strncmp("if=", argv[i], 3) == 0) |
| 79 | infile = argv[i]+3; |
| 80 | else if (strncmp("of=", argv[i], 3) == 0) |
| 81 | outfile = argv[i]+3; |
| 82 | else if (strncmp("conv=", argv[i], 5) == 0) { |
| 83 | buf = argv[i]+5; |
| 84 | while (1) { |
| 85 | if (strncmp("notrunc", buf, 7) == 0) { |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 86 | trunc_flag = FALSE; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 87 | buf += 7; |
| 88 | } else if (strncmp("sync", buf, 4) == 0) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 89 | sync_flag = TRUE; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 90 | buf += 4; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 91 | } else if (strncmp("noerror", buf, 7) == 0) { |
| 92 | noerror = TRUE; |
| 93 | buf += 7; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 94 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 96 | } |
| 97 | if (buf[0] == '\0') |
| 98 | break; |
| 99 | if (buf[0] == ',') |
| 100 | buf++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 102 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | bb_show_usage(); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 104 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 105 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 106 | buf = xmalloc(bs); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 108 | if (infile != NULL) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 109 | ifd = bb_xopen(infile, O_RDONLY); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 110 | } else { |
| 111 | ifd = STDIN_FILENO; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 115 | if (outfile != NULL) { |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 116 | oflag = O_WRONLY | O_CREAT; |
| 117 | |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 118 | if (!seek && trunc_flag) { |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 119 | oflag |= O_TRUNC; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 120 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 121 | |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 122 | if ((ofd = open(outfile, oflag, 0666)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 124 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 125 | |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 126 | if (seek && trunc_flag) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 127 | if (ftruncate(ofd, seek * bs) < 0) { |
| 128 | struct stat st; |
| 129 | |
| 130 | if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 131 | S_ISDIR (st.st_mode)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 133 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 134 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 135 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 136 | } else { |
| 137 | ofd = STDOUT_FILENO; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 141 | if (skip) { |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 142 | if (lseek(ifd, skip * bs, SEEK_CUR) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | bb_perror_msg_and_die("%s", infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 144 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 146 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 147 | if (seek) { |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 148 | if (lseek(ofd, seek * bs, SEEK_CUR) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 150 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 151 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 152 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 153 | while (in_full + in_part != count) { |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 154 | if (noerror) { |
| 155 | /* Pre-zero the buffer when doing the noerror thing */ |
| 156 | memset(buf, '\0', bs); |
| 157 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 158 | n = safe_read(ifd, buf, bs); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 159 | if (n < 0) { |
| 160 | if (noerror) { |
| 161 | n = bs; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 162 | bb_perror_msg("%s", infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 163 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 164 | bb_perror_msg_and_die("%s", infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 167 | if (n == 0) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 168 | break; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 169 | } |
"Vladimir N. Oleynik" | 57545c8 | 2006-01-31 12:06:57 +0000 | [diff] [blame] | 170 | if ((size_t)n == bs) { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 171 | in_full++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 172 | } else { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 173 | in_part++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 174 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 175 | if (sync_flag) { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 176 | memset(buf + n, '\0', bs - n); |
| 177 | n = bs; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 178 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 179 | n = bb_full_write(ofd, buf, n); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 180 | if (n < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 181 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 182 | } |
"Vladimir N. Oleynik" | 57545c8 | 2006-01-31 12:06:57 +0000 | [diff] [blame] | 183 | if ((size_t)n == bs) { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 184 | out_full++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 185 | } else { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 186 | out_part++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 187 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 188 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 189 | |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 190 | if (close (ifd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 191 | bb_perror_msg_and_die("%s", infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 192 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 193 | |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 194 | if (close (ofd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 195 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 196 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 197 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 198 | fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n", |
| 199 | (long)in_full, (long)in_part, |
| 200 | (long)out_full, (long)out_part); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 201 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 202 | return EXIT_SUCCESS; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 203 | } |