1 /* $NetBSD: main.c,v 1.55 2003/04/02 10:39:24 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.55 2003/04/02 10:39:24 fvdl Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/stat.h> 53 #include <sys/mount.h> 54 55 #include <ufs/ufs/dinode.h> 56 #include <ufs/ffs/fs.h> 57 #include <ufs/ffs/ffs_extern.h> 58 59 #include <protocols/dumprestore.h> 60 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <fcntl.h> 65 #include <fstab.h> 66 #include <signal.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <time.h> 71 #include <unistd.h> 72 73 #include "dump.h" 74 #include "pathnames.h" 75 76 int timestamp; /* print message timestamps */ 77 int notify; /* notify operator flag */ 78 int blockswritten; /* number of blocks written on current tape */ 79 int tapeno; /* current tape number */ 80 int density; /* density in bytes/0.1" */ 81 int ntrec = NTREC; /* # tape blocks in each tape record */ 82 int cartridge; /* Assume non-cartridge tape */ 83 long dev_bsize = 1; /* recalculated below */ 84 long blocksperfile; /* output blocks per file */ 85 char *host; /* remote host (if any) */ 86 int readcache = -1; /* read cache size (in readblksize blks) */ 87 int readblksize = 32 * 1024; /* read block size */ 88 char default_time_string[] = "%T %Z"; /* default timestamp string */ 89 char *time_string = default_time_string; /* timestamp string */ 90 91 int main(int, char *[]); 92 static long numarg(char *, long, long); 93 static void obsolete(int *, char **[]); 94 static void usage(void); 95 96 int 97 main(int argc, char *argv[]) 98 { 99 ino_t ino; 100 int dirty; 101 union dinode *dp; 102 struct fstab *dt; 103 struct statfs *mntinfo, fsbuf; 104 char *map; 105 int ch; 106 int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1; 107 ino_t maxino; 108 time_t tnow, date; 109 int dirc; 110 char *mountpoint; 111 int just_estimate = 0; 112 char labelstr[LBLSIZE]; 113 char *new_time_format; 114 115 spcl.c_date = 0; 116 (void)time(&tnow); 117 spcl.c_date = tnow; 118 tzset(); /* set up timezone for strftime */ 119 if ((new_time_format = getenv("TIMEFORMAT")) != NULL) 120 time_string = new_time_format; 121 122 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 123 if ((tape = getenv("TAPE")) == NULL) 124 tape = _PATH_DEFTAPE; 125 dumpdates = _PATH_DUMPDATES; 126 temp = _PATH_DTMP; 127 strcpy(labelstr, "none"); /* XXX safe strcpy. */ 128 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 129 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 130 level = '0'; 131 timestamp = 0; 132 133 if (argc < 2) 134 usage(); 135 136 obsolete(&argc, &argv); 137 while ((ch = getopt(argc, argv, 138 "0123456789aB:b:cd:eFf:h:k:l:L:nr:s:StT:uWw")) != -1) 139 switch (ch) { 140 /* dump level */ 141 case '0': case '1': case '2': case '3': case '4': 142 case '5': case '6': case '7': case '8': case '9': 143 level = ch; 144 break; 145 146 case 'a': /* `auto-size', Write to EOM. */ 147 unlimited = 1; 148 break; 149 150 case 'B': /* blocks per output file */ 151 blocksperfile = numarg("blocks per file", 1L, 0L); 152 break; 153 154 case 'b': /* blocks per tape write */ 155 ntrec = numarg("blocks per write", 1L, 1000L); 156 bflag = 1; 157 break; 158 159 case 'c': /* Tape is cart. not 9-track */ 160 cartridge = 1; 161 break; 162 163 case 'd': /* density, in bits per inch */ 164 density = numarg("density", 10L, 327670L) / 10; 165 if (density >= 625 && !bflag) 166 ntrec = HIGHDENSITYTREC; 167 break; 168 169 case 'e': /* eject full tapes */ 170 eflag = 1; 171 break; 172 173 case 'F': /* files-to-dump is an fs image */ 174 Fflag = 1; 175 break; 176 177 case 'f': /* output file */ 178 tape = optarg; 179 break; 180 181 case 'h': 182 honorlevel = numarg("honor level", 0L, 10L); 183 break; 184 185 case 'k': 186 readblksize = numarg("read block size", 0, 64) * 1024; 187 break; 188 189 case 'l': /* autoload after eject full tapes */ 190 eflag = 1; 191 lflag = numarg("timeout (in seconds)", 1, 0); 192 break; 193 194 case 'L': 195 /* 196 * Note that although there are LBLSIZE characters, 197 * the last must be '\0', so the limit on strlen() 198 * is really LBLSIZE-1. 199 */ 200 if (strlcpy(labelstr, optarg, sizeof(labelstr)) 201 >= sizeof(labelstr)) { 202 msg( 203 "WARNING Label `%s' is larger than limit of %lu characters.\n", 204 optarg, 205 (unsigned long)sizeof(labelstr) - 1); 206 msg("WARNING: Using truncated label `%s'.\n", 207 labelstr); 208 } 209 break; 210 case 'n': /* notify operators */ 211 notify = 1; 212 break; 213 214 case 'r': /* read cache size */ 215 readcache = numarg("read cache size", 0, 512); 216 break; 217 218 case 's': /* tape size, feet */ 219 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 220 break; 221 222 case 'S': /* exit after estimating # of tapes */ 223 just_estimate = 1; 224 break; 225 226 case 't': 227 timestamp = 1; 228 break; 229 230 case 'T': /* time of last dump */ 231 spcl.c_ddate = unctime(optarg); 232 if (spcl.c_ddate < 0) { 233 (void)fprintf(stderr, "bad time \"%s\"\n", 234 optarg); 235 exit(X_STARTUP); 236 } 237 Tflag = 1; 238 lastlevel = '?'; 239 break; 240 241 case 'u': /* update /etc/dumpdates */ 242 uflag = 1; 243 break; 244 245 case 'W': /* what to do */ 246 case 'w': 247 lastdump(ch); 248 exit(X_FINOK); /* do nothing else */ 249 250 default: 251 usage(); 252 } 253 argc -= optind; 254 argv += optind; 255 256 if (argc < 1) { 257 (void)fprintf(stderr, 258 "Must specify disk or image, or file list\n"); 259 exit(X_STARTUP); 260 } 261 262 263 /* 264 * determine if disk is a subdirectory, and setup appropriately 265 */ 266 getfstab(); /* /etc/fstab snarfed */ 267 disk = NULL; 268 mountpoint = NULL; 269 dirc = 0; 270 for (i = 0; i < argc; i++) { 271 struct stat sb; 272 273 if (lstat(argv[i], &sb) == -1) 274 quit("Cannot stat %s: %s\n", argv[i], strerror(errno)); 275 if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { 276 disk = argv[i]; 277 multicheck: 278 if (dirc != 0) 279 quit( 280 "Can't dump a disk or image at the same time as a file list\n"); 281 break; 282 } 283 if ((dt = fstabsearch(argv[i])) != NULL) { 284 disk = dt->fs_spec; 285 mountpoint = xstrdup(dt->fs_file); 286 goto multicheck; 287 } 288 if (statfs(argv[i], &fsbuf) == -1) 289 quit("Cannot statfs %s: %s\n", argv[i], 290 strerror(errno)); 291 disk = fsbuf.f_mntfromname; 292 if (strcmp(argv[i], fsbuf.f_mntonname) == 0) 293 goto multicheck; 294 if (mountpoint == NULL) { 295 mountpoint = xstrdup(fsbuf.f_mntonname); 296 if (uflag) { 297 msg("Ignoring u flag for subdir dump\n"); 298 uflag = 0; 299 } 300 if (level > '0') { 301 msg("Subdir dump is done at level 0\n"); 302 level = '0'; 303 } 304 msg("Dumping sub files/directories from %s\n", 305 mountpoint); 306 } else { 307 if (strcmp(mountpoint, fsbuf.f_mntonname) != 0) 308 quit("%s is not on %s\n", argv[i], mountpoint); 309 } 310 msg("Dumping file/directory %s\n", argv[i]); 311 dirc++; 312 } 313 if (mountpoint) 314 free(mountpoint); 315 316 if (dirc == 0) { 317 argv++; 318 if (argc != 1) { 319 (void)fprintf(stderr, "Excess arguments to dump:"); 320 while (--argc) 321 (void)fprintf(stderr, " %s", *argv++); 322 (void)fprintf(stderr, "\n"); 323 exit(X_STARTUP); 324 } 325 } 326 if (Tflag && uflag) { 327 (void)fprintf(stderr, 328 "You cannot use the T and u flags together.\n"); 329 exit(X_STARTUP); 330 } 331 if (strcmp(tape, "-") == 0) { 332 pipeout++; 333 tape = "standard output"; 334 } 335 336 if (blocksperfile) 337 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 338 else if (!unlimited) { 339 /* 340 * Determine how to default tape size and density 341 * 342 * density tape size 343 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 344 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 345 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 346 * (450*4 - slop) 347 */ 348 if (density == 0) 349 density = cartridge ? 100 : 160; 350 if (tsize == 0) 351 tsize = cartridge ? 1700L*120L : 2300L*120L; 352 } 353 354 if (strchr(tape, ':')) { 355 host = tape; 356 tape = strchr(host, ':'); 357 *tape++ = '\0'; 358 #ifdef RDUMP 359 if (rmthost(host) == 0) 360 exit(X_STARTUP); 361 #else 362 (void)fprintf(stderr, "remote dump not enabled\n"); 363 exit(X_STARTUP); 364 #endif 365 } 366 367 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 368 signal(SIGHUP, sig); 369 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 370 signal(SIGTRAP, sig); 371 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 372 signal(SIGFPE, sig); 373 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 374 signal(SIGBUS, sig); 375 #if 0 376 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 377 signal(SIGSEGV, sig); 378 #endif 379 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 380 signal(SIGTERM, sig); 381 if (signal(SIGINT, interrupt) == SIG_IGN) 382 signal(SIGINT, SIG_IGN); 383 384 /* 385 * disk can be either the full special file name, or 386 * the file system name. 387 */ 388 mountpoint = NULL; 389 if ((dt = fstabsearch(disk)) != NULL) { 390 disk = rawname(dt->fs_spec); 391 mountpoint = dt->fs_file; 392 msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB); 393 } else if ((mntinfo = mntinfosearch(disk)) != NULL) { 394 disk = rawname(mntinfo->f_mntfromname); 395 mountpoint = mntinfo->f_mntonname; 396 msg("Found %s on %s in mount table\n", disk, mountpoint); 397 } 398 if (mountpoint != NULL) { 399 if (dirc != 0) 400 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys), 401 "a subset of %s", mountpoint); 402 else 403 (void)strlcpy(spcl.c_filesys, mountpoint, 404 sizeof(spcl.c_filesys)); 405 } else if (Fflag) { 406 (void)strlcpy(spcl.c_filesys, "a file system image", 407 sizeof(spcl.c_filesys)); 408 } else { 409 (void)strlcpy(spcl.c_filesys, "an unlisted file system", 410 sizeof(spcl.c_filesys)); 411 } 412 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev)); 413 (void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label)); 414 (void)gethostname(spcl.c_host, sizeof(spcl.c_host)); 415 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0'; 416 417 if ((diskfd = open(disk, O_RDONLY)) < 0) { 418 msg("Cannot open %s\n", disk); 419 exit(X_STARTUP); 420 } 421 sync(); 422 423 needswap = fs_read_sblock(sblock_buf); 424 425 spcl.c_level = iswap32(level - '0'); 426 spcl.c_type = iswap32(TS_TAPE); 427 spcl.c_date = iswap32(spcl.c_date); 428 spcl.c_ddate = iswap32(spcl.c_ddate); 429 if (!Tflag) 430 getdumptime(); /* /etc/dumpdates snarfed */ 431 432 date = iswap32(spcl.c_date); 433 msg("Date of this level %c dump: %s", level, 434 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 435 date = iswap32(spcl.c_ddate); 436 msg("Date of last level %c dump: %s", lastlevel, 437 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date)); 438 msg("Dumping "); 439 if (dirc != 0) 440 msgtail("a subset of "); 441 msgtail("%s (%s) ", disk, spcl.c_filesys); 442 if (host) 443 msgtail("to %s on host %s\n", tape, host); 444 else 445 msgtail("to %s\n", tape); 446 msg("Label: %s\n", labelstr); 447 448 ufsib = fs_parametrize(); 449 450 dev_bshift = ffs(dev_bsize) - 1; 451 if (dev_bsize != (1 << dev_bshift)) 452 quit("dev_bsize (%ld) is not a power of 2", dev_bsize); 453 tp_bshift = ffs(TP_BSIZE) - 1; 454 if (TP_BSIZE != (1 << tp_bshift)) 455 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 456 maxino = fs_maxino(); 457 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 458 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 459 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 460 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 461 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 462 463 nonodump = iswap32(spcl.c_level) < honorlevel; 464 465 initcache(readcache, readblksize); 466 467 (void)signal(SIGINFO, statussig); 468 469 msg("mapping (Pass I) [regular files]\n"); 470 anydirskipped = mapfiles(maxino, &tapesize, mountpoint, 471 (dirc ? argv : NULL)); 472 473 msg("mapping (Pass II) [directories]\n"); 474 while (anydirskipped) { 475 anydirskipped = mapdirs(maxino, &tapesize); 476 } 477 478 if (pipeout || unlimited) { 479 tapesize += 10; /* 10 trailer blocks */ 480 msg("estimated %llu tape blocks.\n", 481 (unsigned long long)tapesize); 482 } else { 483 double fetapes; 484 485 if (blocksperfile) 486 fetapes = (double) tapesize / blocksperfile; 487 else if (cartridge) { 488 /* Estimate number of tapes, assuming streaming stops at 489 the end of each block written, and not in mid-block. 490 Assume no erroneous blocks; this can be compensated 491 for with an artificially low tape size. */ 492 fetapes = 493 ( (double) tapesize /* blocks */ 494 * TP_BSIZE /* bytes/block */ 495 * (1.0/density) /* 0.1" / byte */ 496 + 497 (double) tapesize /* blocks */ 498 * (1.0/ntrec) /* streaming-stops per block */ 499 * 15.48 /* 0.1" / streaming-stop */ 500 ) * (1.0 / tsize ); /* tape / 0.1" */ 501 } else { 502 /* Estimate number of tapes, for old fashioned 9-track 503 tape */ 504 int tenthsperirg = (density == 625) ? 3 : 7; 505 fetapes = 506 ( tapesize /* blocks */ 507 * TP_BSIZE /* bytes / block */ 508 * (1.0/density) /* 0.1" / byte */ 509 + 510 tapesize /* blocks */ 511 * (1.0/ntrec) /* IRG's / block */ 512 * tenthsperirg /* 0.1" / IRG */ 513 ) * (1.0 / tsize ); /* tape / 0.1" */ 514 } 515 etapes = fetapes; /* truncating assignment */ 516 etapes++; 517 /* count the dumped inodes map on each additional tape */ 518 tapesize += (etapes - 1) * 519 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 520 tapesize += etapes + 10; /* headers + 10 trailer blks */ 521 msg("estimated %llu tape blocks on %3.2f tape(s).\n", 522 (unsigned long long)tapesize, fetapes); 523 } 524 /* 525 * If the user only wants an estimate of the number of 526 * tapes, exit now. 527 */ 528 if (just_estimate) 529 exit(X_FINOK); 530 531 /* 532 * Allocate tape buffer. 533 */ 534 if (!alloctape()) 535 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 536 537 startnewtape(1); 538 (void)time((time_t *)&(tstart_writing)); 539 xferrate = 0; 540 dumpmap(usedinomap, TS_CLRI, maxino - 1); 541 542 msg("dumping (Pass III) [directories]\n"); 543 dirty = 0; /* XXX just to get gcc to shut up */ 544 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 545 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 546 dirty = *map++; 547 else 548 dirty >>= 1; 549 if ((dirty & 1) == 0) 550 continue; 551 /* 552 * Skip directory inodes deleted and maybe reallocated 553 */ 554 dp = getino(ino); 555 if ((DIP(dp, mode) & IFMT) != IFDIR) 556 continue; 557 (void)dumpino(dp, ino); 558 } 559 560 msg("dumping (Pass IV) [regular files]\n"); 561 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 562 int mode; 563 564 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 565 dirty = *map++; 566 else 567 dirty >>= 1; 568 if ((dirty & 1) == 0) 569 continue; 570 /* 571 * Skip inodes deleted and reallocated as directories. 572 */ 573 dp = getino(ino); 574 mode = DIP(dp, mode) & IFMT; 575 if (mode == IFDIR) 576 continue; 577 (void)dumpino(dp, ino); 578 } 579 580 spcl.c_type = iswap32(TS_END); 581 for (i = 0; i < ntrec; i++) 582 writeheader(maxino - 1); 583 if (pipeout) 584 msg("%d tape blocks\n",iswap32(spcl.c_tapea)); 585 else 586 msg("%d tape blocks on %d volume%s\n", 587 iswap32(spcl.c_tapea), iswap32(spcl.c_volume), 588 (iswap32(spcl.c_volume) == 1) ? "" : "s"); 589 tnow = do_stats(); 590 date = iswap32(spcl.c_date); 591 msg("Date of this level %c dump: %s", level, 592 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 593 msg("Date this dump completed: %s", ctime(&tnow)); 594 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno); 595 putdumptime(); 596 trewind(0); 597 broadcast("DUMP IS DONE!\a\a\n"); 598 msg("DUMP IS DONE\n"); 599 Exit(X_FINOK); 600 /* NOTREACHED */ 601 exit(X_FINOK); /* XXX: to satisfy gcc */ 602 } 603 604 static void 605 usage(void) 606 { 607 const char *prog = getprogname(); 608 609 (void)fprintf(stderr, 610 "usage: %s [-0123456789aceFnStu] [-B records] [-b blocksize]\n" 611 " [-d density] [-f file] [-h level] [-k read-blocksize]\n" 612 " [-L label] [-l timeout] [-r read-cache] [-s feet] [-T date] files-to-dump\n" 613 " %s [-W | -w]\n", prog, prog); 614 exit(X_STARTUP); 615 } 616 617 /* 618 * Pick up a numeric argument. It must be nonnegative and in the given 619 * range (except that a vmax of 0 means unlimited). 620 */ 621 static long 622 numarg(char *meaning, long vmin, long vmax) 623 { 624 char *p; 625 long val; 626 627 val = strtol(optarg, &p, 10); 628 if (*p) 629 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg); 630 if (val < vmin || (vmax && val > vmax)) 631 errx(X_STARTUP, "%s must be between %ld and %ld", 632 meaning, vmin, vmax); 633 return (val); 634 } 635 636 void 637 sig(int signo) 638 { 639 640 switch(signo) { 641 case SIGALRM: 642 case SIGBUS: 643 case SIGFPE: 644 case SIGHUP: 645 case SIGTERM: 646 case SIGTRAP: 647 if (pipeout) 648 quit("Signal on pipe: cannot recover\n"); 649 msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]); 650 (void)fflush(stderr); 651 (void)fflush(stdout); 652 close_rewind(); 653 exit(X_REWRITE); 654 /* NOTREACHED */ 655 case SIGSEGV: 656 msg("SIGSEGV: ABORTING!\n"); 657 (void)signal(SIGSEGV, SIG_DFL); 658 (void)kill(0, SIGSEGV); 659 /* NOTREACHED */ 660 } 661 } 662 663 char * 664 rawname(char *cp) 665 { 666 static char rawbuf[MAXPATHLEN]; 667 char *dp = strrchr(cp, '/'); 668 669 if (dp == NULL) 670 return (NULL); 671 *dp = '\0'; 672 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1); 673 *dp = '/'; 674 return (rawbuf); 675 } 676 677 /* 678 * obsolete -- 679 * Change set of key letters and ordered arguments into something 680 * getopt(3) will like. 681 */ 682 static void 683 obsolete(int *argcp, char **argvp[]) 684 { 685 int argc, flags; 686 char *ap, **argv, *flagsp, **nargv, *p; 687 688 /* Setup. */ 689 argv = *argvp; 690 argc = *argcp; 691 692 /* Return if no arguments or first argument has leading dash. */ 693 ap = argv[1]; 694 if (argc == 1 || *ap == '-') 695 return; 696 697 /* Allocate space for new arguments. */ 698 *argvp = nargv = xmalloc((argc + 1) * sizeof(char *)); 699 p = flagsp = xmalloc(strlen(ap) + 2); 700 701 *nargv++ = *argv; 702 argv += 2; 703 704 for (flags = 0; *ap; ++ap) { 705 switch (*ap) { 706 case 'B': 707 case 'b': 708 case 'd': 709 case 'f': 710 case 'h': 711 case 's': 712 case 'T': 713 if (*argv == NULL) { 714 warnx("option requires an argument -- %c", *ap); 715 usage(); 716 } 717 nargv[0] = xmalloc(strlen(*argv) + 2 + 1); 718 nargv[0][0] = '-'; 719 nargv[0][1] = *ap; 720 (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */ 721 ++argv; 722 ++nargv; 723 break; 724 default: 725 if (!flags) { 726 *p++ = '-'; 727 flags = 1; 728 } 729 *p++ = *ap; 730 break; 731 } 732 } 733 734 /* Terminate flags. */ 735 if (flags) { 736 *p = '\0'; 737 *nargv++ = flagsp; 738 } 739 740 /* Copy remaining arguments. */ 741 while ((*nargv++ = *argv++) != NULL) 742 ; 743 744 /* Update argument count. */ 745 *argcp = nargv - *argvp - 1; 746 } 747 748 749 void * 750 xcalloc(size_t number, size_t size) 751 { 752 void *p; 753 754 p = calloc(number, size); 755 if (p == NULL) 756 quit("%s\n", strerror(errno)); 757 return (p); 758 } 759 760 void * 761 xmalloc(size_t size) 762 { 763 void *p; 764 765 p = malloc(size); 766 if (p == NULL) 767 quit("%s\n", strerror(errno)); 768 return (p); 769 } 770 771 char * 772 xstrdup(const char *str) 773 { 774 char *p; 775 776 p = strdup(str); 777 if (p == NULL) 778 quit("%s\n", strerror(errno)); 779 return (p); 780 } 781