1 /* $NetBSD: dd.c,v 1.7 1997/07/20 21:58:39 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Keith Muller of the University of California, San Diego and Lance 9 * Visser of Convex Computer Corporation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 #ifndef lint 42 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\ 43 The Regents of the University of California. All rights reserved.\n"); 44 #endif /* not lint */ 45 46 #ifndef lint 47 #if 0 48 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; 49 #else 50 __RCSID("$NetBSD: dd.c,v 1.7 1997/07/20 21:58:39 christos Exp $"); 51 #endif 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/stat.h> 56 #include <sys/ioctl.h> 57 #include <sys/mtio.h> 58 59 #include <ctype.h> 60 #include <err.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <signal.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include "dd.h" 70 #include "extern.h" 71 72 static void dd_close __P((void)); 73 static void dd_in __P((void)); 74 static void getfdtype __P((IO *)); 75 static void setup __P((void)); 76 77 int main __P((int, char *[])); 78 79 IO in, out; /* input/output state */ 80 STAT st; /* statistics */ 81 void (*cfunc) __P((void)); /* conversion function */ 82 u_long cpy_cnt; /* # of blocks to copy */ 83 u_int ddflags; /* conversion options */ 84 u_int cbsz; /* conversion block size */ 85 u_int files_cnt = 1; /* # of files to copy */ 86 const u_char *ctab; /* conversion table */ 87 88 int 89 main(argc, argv) 90 int argc; 91 char *argv[]; 92 { 93 jcl(argv); 94 setup(); 95 96 (void)signal(SIGINFO, summaryx); 97 (void)signal(SIGINT, terminate); 98 99 atexit(summary); 100 101 while (files_cnt--) 102 dd_in(); 103 104 dd_close(); 105 exit(0); 106 } 107 108 static void 109 setup() 110 { 111 u_int cnt; 112 113 if (in.name == NULL) { 114 in.name = "stdin"; 115 in.fd = STDIN_FILENO; 116 } else { 117 in.fd = open(in.name, O_RDONLY, 0); 118 if (in.fd < 0) 119 err(1, "%s", in.name); 120 } 121 122 getfdtype(&in); 123 124 if (files_cnt > 1 && !(in.flags & ISTAPE)) 125 errx(1, "files is not supported for non-tape devices"); 126 127 if (out.name == NULL) { 128 /* No way to check for read access here. */ 129 out.fd = STDOUT_FILENO; 130 out.name = "stdout"; 131 } else { 132 #define OFLAGS \ 133 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) 134 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); 135 /* 136 * May not have read access, so try again with write only. 137 * Without read we may have a problem if output also does 138 * not support seeks. 139 */ 140 if (out.fd < 0) { 141 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); 142 out.flags |= NOREAD; 143 } 144 if (out.fd < 0) 145 err(1, "%s", out.name); 146 } 147 148 getfdtype(&out); 149 150 /* 151 * Allocate space for the input and output buffers. If not doing 152 * record oriented I/O, only need a single buffer. 153 */ 154 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { 155 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) 156 err(1, "%s", ""); 157 out.db = in.db; 158 } else if ((in.db = 159 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || 160 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) 161 err(1, "%s", ""); 162 in.dbp = in.db; 163 out.dbp = out.db; 164 165 /* Position the input/output streams. */ 166 if (in.offset) 167 pos_in(); 168 if (out.offset) 169 pos_out(); 170 171 /* 172 * Truncate the output file; ignore errors because it fails on some 173 * kinds of output files, tapes, for example. 174 */ 175 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) 176 (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz); 177 178 /* 179 * If converting case at the same time as another conversion, build a 180 * table that does both at once. If just converting case, use the 181 * built-in tables. 182 */ 183 if (ddflags & (C_LCASE|C_UCASE)) { 184 #ifdef NO_CONV 185 /* Should not get here, but just in case... */ 186 errx(1, "case conv and -DNO_CONV"); 187 #else /* NO_CONV */ 188 if (ddflags & C_ASCII || ddflags & C_EBCDIC) { 189 if (ddflags & C_LCASE) { 190 for (cnt = 0; cnt < 0377; ++cnt) 191 casetab[cnt] = tolower(ctab[cnt]); 192 } else { 193 for (cnt = 0; cnt < 0377; ++cnt) 194 casetab[cnt] = toupper(ctab[cnt]); 195 } 196 } else { 197 if (ddflags & C_LCASE) { 198 for (cnt = 0; cnt < 0377; ++cnt) 199 casetab[cnt] = tolower(cnt); 200 } else { 201 for (cnt = 0; cnt < 0377; ++cnt) 202 casetab[cnt] = toupper(cnt); 203 } 204 } 205 206 ctab = casetab; 207 #endif /* NO_CONV */ 208 } 209 210 (void)time(&st.start); /* Statistics timestamp. */ 211 } 212 213 static void 214 getfdtype(io) 215 IO *io; 216 { 217 struct mtget mt; 218 struct stat sb; 219 220 if (fstat(io->fd, &sb)) 221 err(1, "%s", io->name); 222 if (S_ISCHR(sb.st_mode)) 223 io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; 224 else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) 225 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ 226 } 227 228 static void 229 dd_in() 230 { 231 int flags, n; 232 233 for (flags = ddflags;;) { 234 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) 235 return; 236 237 /* 238 * Zero the buffer first if trying to recover from errors so 239 * lose the minimum amount of data. If doing block operations 240 * use spaces. 241 */ 242 if ((flags & (C_NOERROR|C_SYNC)) == (C_NOERROR|C_SYNC)) 243 if (flags & (C_BLOCK|C_UNBLOCK)) 244 memset(in.dbp, ' ', in.dbsz); 245 else 246 memset(in.dbp, 0, in.dbsz); 247 248 n = read(in.fd, in.dbp, in.dbsz); 249 if (n == 0) { 250 in.dbrcnt = 0; 251 return; 252 } 253 254 /* Read error. */ 255 if (n < 0) { 256 /* 257 * If noerror not specified, die. POSIX requires that 258 * the warning message be followed by an I/O display. 259 */ 260 if (!(flags & C_NOERROR)) 261 err(1, "%s", in.name); 262 warn("%s", in.name); 263 summary(); 264 265 /* 266 * If it's not a tape drive or a pipe, seek past the 267 * error. If your OS doesn't do the right thing for 268 * raw disks this section should be modified to re-read 269 * in sector size chunks. 270 */ 271 if (!(in.flags & (ISPIPE|ISTAPE)) && 272 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) 273 warn("%s", in.name); 274 275 /* If sync not specified, omit block and continue. */ 276 if (!(ddflags & C_SYNC)) 277 continue; 278 279 /* Read errors count as full blocks. */ 280 in.dbcnt += in.dbrcnt = in.dbsz; 281 ++st.in_full; 282 283 /* Handle full input blocks. */ 284 } else if (n == in.dbsz) { 285 in.dbcnt += in.dbrcnt = n; 286 ++st.in_full; 287 288 /* Handle partial input blocks. */ 289 } else { 290 /* If sync, use the entire block. */ 291 if (ddflags & C_SYNC) 292 in.dbcnt += in.dbrcnt = in.dbsz; 293 else 294 in.dbcnt += in.dbrcnt = n; 295 ++st.in_part; 296 } 297 298 /* 299 * POSIX states that if bs is set and no other conversions 300 * than noerror, notrunc or sync are specified, the block 301 * is output without buffering as it is read. 302 */ 303 if (ddflags & C_BS) { 304 out.dbcnt = in.dbcnt; 305 dd_out(1); 306 in.dbcnt = 0; 307 continue; 308 } 309 310 if (ddflags & C_SWAB) { 311 if ((n = in.dbcnt) & 1) { 312 ++st.swab; 313 --n; 314 } 315 swab(in.dbp, in.dbp, n); 316 } 317 318 in.dbp += in.dbrcnt; 319 (*cfunc)(); 320 } 321 } 322 323 /* 324 * Cleanup any remaining I/O and flush output. If necesssary, output file 325 * is truncated. 326 */ 327 static void 328 dd_close() 329 { 330 if (cfunc == def) 331 def_close(); 332 else if (cfunc == block) 333 block_close(); 334 else if (cfunc == unblock) 335 unblock_close(); 336 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) { 337 memset(out.dbp, 0, out.dbsz - out.dbcnt); 338 out.dbcnt = out.dbsz; 339 } 340 if (out.dbcnt) 341 dd_out(1); 342 } 343 344 void 345 dd_out(force) 346 int force; 347 { 348 static int warned; 349 int cnt, n, nw; 350 u_char *outp; 351 352 /* 353 * Write one or more blocks out. The common case is writing a full 354 * output block in a single write; increment the full block stats. 355 * Otherwise, we're into partial block writes. If a partial write, 356 * and it's a character device, just warn. If a tape device, quit. 357 * 358 * The partial writes represent two cases. 1: Where the input block 359 * was less than expected so the output block was less than expected. 360 * 2: Where the input block was the right size but we were forced to 361 * write the block in multiple chunks. The original versions of dd(1) 362 * never wrote a block in more than a single write, so the latter case 363 * never happened. 364 * 365 * One special case is if we're forced to do the write -- in that case 366 * we play games with the buffer size, and it's usually a partial write. 367 */ 368 outp = out.db; 369 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 370 for (cnt = n;; cnt -= nw) { 371 nw = write(out.fd, outp, cnt); 372 if (nw <= 0) { 373 if (nw == 0) 374 errx(1, "%s: end of device", out.name); 375 if (errno != EINTR) 376 err(1, "%s", out.name); 377 nw = 0; 378 } 379 outp += nw; 380 st.bytes += nw; 381 if (nw == n) { 382 if (n != out.dbsz) 383 ++st.out_part; 384 else 385 ++st.out_full; 386 break; 387 } 388 ++st.out_part; 389 if (nw == cnt) 390 break; 391 if (out.flags & ISCHR && !warned) { 392 warned = 1; 393 warnx("%s: short write on character device", 394 out.name); 395 } 396 if (out.flags & ISTAPE) 397 errx(1, "%s: short write on tape device", out.name); 398 } 399 if ((out.dbcnt -= n) < out.dbsz) 400 break; 401 } 402 403 /* Reassemble the output block. */ 404 if (out.dbcnt) 405 memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 406 out.dbp = out.db + out.dbcnt; 407 } 408