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