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