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