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