1 /* $NetBSD: main.c,v 1.53 2002/11/17 04:49:18 itojun 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.53 2002/11/17 04:49:18 itojun 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 if (strlcpy(labelstr, optarg, sizeof(labelstr)) 206 >= sizeof(labelstr)) { 207 msg( 208 "WARNING Label `%s' is larger than limit of %lu characters.\n", 209 optarg, 210 (unsigned long)sizeof(labelstr) - 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, sizeof(spcl.c_filesys), 406 "a subset of %s", mountpoint); 407 else 408 (void)strlcpy(spcl.c_filesys, mountpoint, 409 sizeof(spcl.c_filesys)); 410 } else if (Fflag) { 411 (void)strlcpy(spcl.c_filesys, "a file system image", 412 sizeof(spcl.c_filesys)); 413 } else { 414 (void)strlcpy(spcl.c_filesys, "an unlisted file system", 415 sizeof(spcl.c_filesys)); 416 } 417 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev)); 418 (void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label)); 419 (void)gethostname(spcl.c_host, sizeof(spcl.c_host)); 420 spcl.c_host[sizeof(spcl.c_host) - 1] = '\0'; 421 422 if ((diskfd = open(disk, O_RDONLY)) < 0) { 423 msg("Cannot open %s\n", disk); 424 exit(X_STARTUP); 425 } 426 sync(); 427 428 needswap = fs_read_sblock(sblock_buf); 429 430 spcl.c_level = iswap32(level - '0'); 431 spcl.c_type = iswap32(TS_TAPE); 432 spcl.c_date = iswap32(spcl.c_date); 433 spcl.c_ddate = iswap32(spcl.c_ddate); 434 if (!Tflag) 435 getdumptime(); /* /etc/dumpdates snarfed */ 436 437 date = iswap32(spcl.c_date); 438 msg("Date of this level %c dump: %s", level, 439 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 440 date = iswap32(spcl.c_ddate); 441 msg("Date of last level %c dump: %s", lastlevel, 442 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date)); 443 msg("Dumping "); 444 if (dirc != 0) 445 msgtail("a subset of "); 446 msgtail("%s (%s) ", disk, spcl.c_filesys); 447 if (host) 448 msgtail("to %s on host %s\n", tape, host); 449 else 450 msgtail("to %s\n", tape); 451 msg("Label: %s\n", labelstr); 452 453 ufsib = fs_parametrize(); 454 455 dev_bshift = ffs(dev_bsize) - 1; 456 if (dev_bsize != (1 << dev_bshift)) 457 quit("dev_bsize (%ld) is not a power of 2", dev_bsize); 458 tp_bshift = ffs(TP_BSIZE) - 1; 459 if (TP_BSIZE != (1 << tp_bshift)) 460 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 461 maxino = fs_maxino(); 462 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 463 usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 464 dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 465 dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char)); 466 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 467 468 nonodump = iswap32(spcl.c_level) < honorlevel; 469 470 initcache(readcache, readblksize); 471 472 (void)signal(SIGINFO, statussig); 473 474 msg("mapping (Pass I) [regular files]\n"); 475 anydirskipped = mapfiles(maxino, &tapesize, mountpoint, 476 (dirc ? argv : NULL)); 477 478 msg("mapping (Pass II) [directories]\n"); 479 while (anydirskipped) { 480 anydirskipped = mapdirs(maxino, &tapesize); 481 } 482 483 if (pipeout || unlimited) { 484 tapesize += 10; /* 10 trailer blocks */ 485 msg("estimated %ld tape blocks.\n", tapesize); 486 } else { 487 double fetapes; 488 489 if (blocksperfile) 490 fetapes = (double) tapesize / blocksperfile; 491 else if (cartridge) { 492 /* Estimate number of tapes, assuming streaming stops at 493 the end of each block written, and not in mid-block. 494 Assume no erroneous blocks; this can be compensated 495 for with an artificially low tape size. */ 496 fetapes = 497 ( (double) tapesize /* blocks */ 498 * TP_BSIZE /* bytes/block */ 499 * (1.0/density) /* 0.1" / byte */ 500 + 501 (double) tapesize /* blocks */ 502 * (1.0/ntrec) /* streaming-stops per block */ 503 * 15.48 /* 0.1" / streaming-stop */ 504 ) * (1.0 / tsize ); /* tape / 0.1" */ 505 } else { 506 /* Estimate number of tapes, for old fashioned 9-track 507 tape */ 508 int tenthsperirg = (density == 625) ? 3 : 7; 509 fetapes = 510 ( tapesize /* blocks */ 511 * TP_BSIZE /* bytes / block */ 512 * (1.0/density) /* 0.1" / byte */ 513 + 514 tapesize /* blocks */ 515 * (1.0/ntrec) /* IRG's / block */ 516 * tenthsperirg /* 0.1" / IRG */ 517 ) * (1.0 / tsize ); /* tape / 0.1" */ 518 } 519 etapes = fetapes; /* truncating assignment */ 520 etapes++; 521 /* count the dumped inodes map on each additional tape */ 522 tapesize += (etapes - 1) * 523 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 524 tapesize += etapes + 10; /* headers + 10 trailer blks */ 525 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 526 tapesize, fetapes); 527 } 528 /* 529 * If the user only wants an estimate of the number of 530 * tapes, exit now. 531 */ 532 if (just_estimate) 533 exit(X_FINOK); 534 535 /* 536 * Allocate tape buffer. 537 */ 538 if (!alloctape()) 539 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 540 541 startnewtape(1); 542 (void)time((time_t *)&(tstart_writing)); 543 xferrate = 0; 544 dumpmap(usedinomap, TS_CLRI, maxino - 1); 545 546 msg("dumping (Pass III) [directories]\n"); 547 dirty = 0; /* XXX just to get gcc to shut up */ 548 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 549 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 550 dirty = *map++; 551 else 552 dirty >>= 1; 553 if ((dirty & 1) == 0) 554 continue; 555 /* 556 * Skip directory inodes deleted and maybe reallocated 557 */ 558 dp = getino(ino); 559 if ((dp->di_mode & IFMT) != IFDIR) 560 continue; 561 (void)dumpino(dp, ino); 562 } 563 564 msg("dumping (Pass IV) [regular files]\n"); 565 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 566 int mode; 567 568 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 569 dirty = *map++; 570 else 571 dirty >>= 1; 572 if ((dirty & 1) == 0) 573 continue; 574 /* 575 * Skip inodes deleted and reallocated as directories. 576 */ 577 dp = getino(ino); 578 mode = dp->di_mode & IFMT; 579 if (mode == IFDIR) 580 continue; 581 (void)dumpino(dp, ino); 582 } 583 584 spcl.c_type = iswap32(TS_END); 585 for (i = 0; i < ntrec; i++) 586 writeheader(maxino - 1); 587 if (pipeout) 588 msg("%d tape blocks\n",iswap32(spcl.c_tapea)); 589 else 590 msg("%d tape blocks on %d volume%s\n", 591 iswap32(spcl.c_tapea), iswap32(spcl.c_volume), 592 (iswap32(spcl.c_volume) == 1) ? "" : "s"); 593 tnow = do_stats(); 594 date = iswap32(spcl.c_date); 595 msg("Date of this level %c dump: %s", level, 596 spcl.c_date == 0 ? "the epoch\n" : ctime(&date)); 597 msg("Date this dump completed: %s", ctime(&tnow)); 598 msg("Average transfer rate: %d KB/s\n", xferrate / tapeno); 599 putdumptime(); 600 trewind(0); 601 broadcast("DUMP IS DONE!\a\a\n"); 602 msg("DUMP IS DONE\n"); 603 Exit(X_FINOK); 604 /* NOTREACHED */ 605 exit(X_FINOK); /* XXX: to satisfy gcc */ 606 } 607 608 static void 609 usage(void) 610 { 611 const char *prog = getprogname(); 612 613 (void)fprintf(stderr, 614 "usage: %s [-0123456789aceFnStu] [-B records] [-b blocksize]\n" 615 " [-d density] [-f file] [-h level] [-k read-blocksize]\n" 616 " [-L label] [-l timeout] [-r read-cache] [-s feet] [-T date] files-to-dump\n" 617 " %s [-W | -w]\n", prog, prog); 618 exit(X_STARTUP); 619 } 620 621 /* 622 * Pick up a numeric argument. It must be nonnegative and in the given 623 * range (except that a vmax of 0 means unlimited). 624 */ 625 static long 626 numarg(char *meaning, long vmin, long vmax) 627 { 628 char *p; 629 long val; 630 631 val = strtol(optarg, &p, 10); 632 if (*p) 633 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg); 634 if (val < vmin || (vmax && val > vmax)) 635 errx(X_STARTUP, "%s must be between %ld and %ld", 636 meaning, vmin, vmax); 637 return (val); 638 } 639 640 void 641 sig(int signo) 642 { 643 644 switch(signo) { 645 case SIGALRM: 646 case SIGBUS: 647 case SIGFPE: 648 case SIGHUP: 649 case SIGTERM: 650 case SIGTRAP: 651 if (pipeout) 652 quit("Signal on pipe: cannot recover\n"); 653 msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]); 654 (void)fflush(stderr); 655 (void)fflush(stdout); 656 close_rewind(); 657 exit(X_REWRITE); 658 /* NOTREACHED */ 659 case SIGSEGV: 660 msg("SIGSEGV: ABORTING!\n"); 661 (void)signal(SIGSEGV, SIG_DFL); 662 (void)kill(0, SIGSEGV); 663 /* NOTREACHED */ 664 } 665 } 666 667 char * 668 rawname(char *cp) 669 { 670 static char rawbuf[MAXPATHLEN]; 671 char *dp = strrchr(cp, '/'); 672 673 if (dp == NULL) 674 return (NULL); 675 *dp = '\0'; 676 (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1); 677 *dp = '/'; 678 return (rawbuf); 679 } 680 681 /* 682 * obsolete -- 683 * Change set of key letters and ordered arguments into something 684 * getopt(3) will like. 685 */ 686 static void 687 obsolete(int *argcp, char **argvp[]) 688 { 689 int argc, flags; 690 char *ap, **argv, *flagsp, **nargv, *p; 691 692 /* Setup. */ 693 argv = *argvp; 694 argc = *argcp; 695 696 /* Return if no arguments or first argument has leading dash. */ 697 ap = argv[1]; 698 if (argc == 1 || *ap == '-') 699 return; 700 701 /* Allocate space for new arguments. */ 702 *argvp = nargv = xmalloc((argc + 1) * sizeof(char *)); 703 p = flagsp = xmalloc(strlen(ap) + 2); 704 705 *nargv++ = *argv; 706 argv += 2; 707 708 for (flags = 0; *ap; ++ap) { 709 switch (*ap) { 710 case 'B': 711 case 'b': 712 case 'd': 713 case 'f': 714 case 'h': 715 case 's': 716 case 'T': 717 if (*argv == NULL) { 718 warnx("option requires an argument -- %c", *ap); 719 usage(); 720 } 721 nargv[0] = xmalloc(strlen(*argv) + 2 + 1); 722 nargv[0][0] = '-'; 723 nargv[0][1] = *ap; 724 (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */ 725 ++argv; 726 ++nargv; 727 break; 728 default: 729 if (!flags) { 730 *p++ = '-'; 731 flags = 1; 732 } 733 *p++ = *ap; 734 break; 735 } 736 } 737 738 /* Terminate flags. */ 739 if (flags) { 740 *p = '\0'; 741 *nargv++ = flagsp; 742 } 743 744 /* Copy remaining arguments. */ 745 while ((*nargv++ = *argv++) != NULL) 746 ; 747 748 /* Update argument count. */ 749 *argcp = nargv - *argvp - 1; 750 } 751 752 753 void * 754 xcalloc(size_t number, size_t size) 755 { 756 void *p; 757 758 p = calloc(number, size); 759 if (p == NULL) 760 quit("%s\n", strerror(errno)); 761 return (p); 762 } 763 764 void * 765 xmalloc(size_t size) 766 { 767 void *p; 768 769 p = malloc(size); 770 if (p == NULL) 771 quit("%s\n", strerror(errno)); 772 return (p); 773 } 774 775 char * 776 xstrdup(const char *str) 777 { 778 char *p; 779 780 p = strdup(str); 781 if (p == NULL) 782 quit("%s\n", strerror(errno)); 783 return (p); 784 } 785