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 | * |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 12 | #include <sys/stat.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <string.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 18 | #include <signal.h> // For FEATURE_DD_SIGNAL_HANDLING |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
| 20 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 21 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 22 | { "c", 1 }, |
| 23 | { "w", 2 }, |
| 24 | { "b", 512 }, |
| 25 | { "kD", 1000 }, |
| 26 | { "k", 1024 }, |
| 27 | { "MD", 1000000 }, |
| 28 | { "M", 1048576 }, |
| 29 | { "GD", 1000000000 }, |
| 30 | { "G", 1073741824 }, |
| 31 | { NULL, 0 } |
| 32 | }; |
| 33 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 34 | static size_t out_full; |
| 35 | static size_t out_part; |
| 36 | static size_t in_full; |
| 37 | static size_t in_part; |
| 38 | |
| 39 | static void dd_output_status(int cur_signal) |
| 40 | { |
| 41 | fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n", |
| 42 | (long)in_full, (long)in_part, |
| 43 | (long)out_full, (long)out_part); |
| 44 | } |
| 45 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 46 | int dd_main(int argc, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 47 | { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 48 | size_t count = -1, oc = 0, ibs = 512, obs = 512; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 49 | ssize_t n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 50 | off_t seek = 0, skip = 0; |
| 51 | int sync_flag = FALSE, noerror = FALSE, trunc_flag = TRUE, twobufs_flag = 0, |
| 52 | oflag, ifd, ofd, i; |
| 53 | const char *infile = NULL, *outfile = NULL; |
| 54 | char *ibuf, *obuf; |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 55 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 56 | if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) |
| 57 | { |
| 58 | struct sigaction sa; |
| 59 | |
| 60 | memset(&sa, 0, sizeof(sa)); |
| 61 | sa.sa_handler = dd_output_status; |
| 62 | sa.sa_flags = SA_RESTART; |
| 63 | sigemptyset(&sa.sa_mask); |
| 64 | sigaction(SIGUSR1, &sa, 0); |
| 65 | } |
| 66 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 67 | for (i = 1; i < argc; i++) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 68 | if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[i], 4)) { |
| 69 | ibs = bb_xparse_number(argv[i]+4, dd_suffixes); |
| 70 | twobufs_flag++; |
| 71 | } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[i], 4)) { |
| 72 | obs = bb_xparse_number(argv[i]+4, dd_suffixes); |
| 73 | twobufs_flag++; |
| 74 | } else if (!strncmp("bs=", argv[i], 3)) { |
| 75 | ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes); |
| 76 | } else if (!strncmp("count=", argv[i], 6)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | count = bb_xparse_number(argv[i]+6, dd_suffixes); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 78 | else if (!strncmp("seek=", argv[i], 5)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | seek = bb_xparse_number(argv[i]+5, dd_suffixes); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 80 | else if (!strncmp("skip=", argv[i], 5)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | skip = bb_xparse_number(argv[i]+5, dd_suffixes); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 82 | else if (!strncmp("if=", argv[i], 3)) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 83 | infile = argv[i]+3; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 84 | else if (!strncmp("of=", argv[i], 3)) |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 85 | outfile = argv[i]+3; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 86 | else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[i], 5)) { |
| 87 | ibuf = argv[i]+5; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 88 | while (1) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 89 | if (!strncmp("notrunc", ibuf, 7)) { |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 90 | trunc_flag = FALSE; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 91 | ibuf += 7; |
| 92 | } else if (!strncmp("sync", ibuf, 4)) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 93 | sync_flag = TRUE; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 94 | ibuf += 4; |
| 95 | } else if (!strncmp("noerror", ibuf, 7)) { |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 96 | noerror = TRUE; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 97 | ibuf += 7; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 98 | } else { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame^] | 99 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i]+5, "conv"); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 100 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 101 | if (ibuf[0] == '\0') break; |
| 102 | if (ibuf[0] == ',') ibuf++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 104 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | bb_show_usage(); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 106 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 107 | ibuf = xmalloc(ibs); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 108 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 109 | if (twobufs_flag) obuf = xmalloc(obs); |
| 110 | else obuf = ibuf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 112 | if (infile != NULL) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 113 | ifd = bb_xopen(infile, O_RDONLY); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 114 | } else { |
| 115 | ifd = STDIN_FILENO; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 119 | if (outfile != NULL) { |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 120 | oflag = O_WRONLY | O_CREAT; |
| 121 | |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 122 | if (!seek && trunc_flag) { |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 123 | oflag |= O_TRUNC; |
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 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 126 | ofd = bb_xopen3(outfile, oflag, 0666); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 127 | |
Eric Andersen | ab26cc3 | 2004-01-30 22:24:32 +0000 | [diff] [blame] | 128 | if (seek && trunc_flag) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 129 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 130 | struct stat st; |
| 131 | |
| 132 | if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 133 | S_ISDIR (st.st_mode)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 135 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 136 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 137 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 138 | } else { |
| 139 | ofd = STDOUT_FILENO; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 143 | if (skip) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 144 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
| 145 | while (skip-- > 0) { |
| 146 | n = safe_read(ifd, ibuf, ibs); |
| 147 | if (n < 0) |
| 148 | bb_perror_msg_and_die("%s", infile); |
| 149 | if (n == 0) |
| 150 | break; |
| 151 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 152 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 153 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 154 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 155 | if (seek) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 156 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 157 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 158 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 159 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 160 | |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 161 | while (in_full + in_part != count) { |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 162 | if (noerror) { |
| 163 | /* Pre-zero the buffer when doing the noerror thing */ |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 164 | memset(ibuf, '\0', ibs); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 165 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 166 | |
| 167 | n = safe_read(ifd, ibuf, ibs); |
| 168 | if (n == 0) { |
| 169 | break; |
| 170 | } |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 171 | if (n < 0) { |
| 172 | if (noerror) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 173 | n = ibs; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 174 | bb_perror_msg("%s", infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 175 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_perror_msg_and_die("%s", infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 179 | if ((size_t)n == ibs) { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 180 | in_full++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 181 | } else { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 182 | in_part++; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 183 | if (sync_flag) { |
| 184 | memset(ibuf + n, '\0', ibs - n); |
| 185 | n = ibs; |
| 186 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 187 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 188 | if (twobufs_flag) { |
| 189 | char *tmp = ibuf; |
| 190 | while (n) { |
| 191 | size_t d = obs - oc; |
| 192 | |
| 193 | if (d > n) d = n; |
| 194 | memcpy(obuf + oc, tmp, d); |
| 195 | n -= d; |
| 196 | tmp += d; |
| 197 | oc += d; |
| 198 | if (oc == obs) { |
| 199 | if (bb_full_write(ofd, obuf, obs) < 0) { |
| 200 | bb_perror_msg_and_die("%s", outfile); |
| 201 | } |
| 202 | out_full++; |
| 203 | oc = 0; |
| 204 | } |
| 205 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 206 | } else { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 207 | if ((n = bb_full_write(ofd, ibuf, n)) < 0) { |
| 208 | bb_perror_msg_and_die("%s", outfile); |
| 209 | } |
| 210 | if (n == ibs) out_full++; |
| 211 | else out_part++; |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 212 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 213 | } |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 214 | |
| 215 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
| 216 | if (bb_full_write(ofd, obuf, oc) < 0) { |
| 217 | bb_perror_msg_and_die("%s", outfile); |
| 218 | } |
| 219 | out_part++; |
| 220 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 221 | if (close (ifd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 222 | bb_perror_msg_and_die("%s", infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 223 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 224 | |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 225 | if (close (ofd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_perror_msg_and_die("%s", outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 227 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 228 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 229 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 230 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 231 | return EXIT_SUCCESS; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 232 | } |