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