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 <limits.h> 66 #include <signal.h> 67 #include <stdint.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 char snapname[BUFSIZ], snapcmd[BUFSIZ]; 321 322 snprintf(snapname, sizeof snapname, 323 "%s/.snap/dump_snapshot", mntpt); 324 snprintf(snapcmd, sizeof snapcmd, 325 "mksnap_ffs %s %s", mntpt, snapname); 326 unlink(snapname); 327 if (system(snapcmd) != 0) 328 errx(X_STARTUP, "Cannot create %s: %s\n", 329 snapname, strerror(errno)); 330 if ((diskfd = open(snapname, O_RDONLY)) < 0) { 331 unlink(snapname); 332 errx(X_STARTUP, "Cannot open %s: %s\n", 333 snapname, strerror(errno)); 334 } 335 unlink(snapname); 336 if (fstat(diskfd, &sb) != 0) 337 err(X_STARTUP, "%s: stat", snapname); 338 spcl.c_date = _time_to_time64(sb.st_mtime); 339 } 340 } else if (snapdump != 0) { 341 msg("WARNING: Cannot use -L on an unmounted filesystem.\n"); 342 snapdump = 0; 343 } 344 if (snapdump == 0) { 345 if ((diskfd = open(disk, O_RDONLY)) < 0) 346 err(X_STARTUP, "Cannot open %s", disk); 347 if (fstat(diskfd, &sb) != 0) 348 err(X_STARTUP, "%s: stat", disk); 349 if (S_ISDIR(sb.st_mode)) 350 errx(X_STARTUP, "%s: unknown file system", disk); 351 } 352 353 (void)strcpy(spcl.c_label, "none"); 354 (void)gethostname(spcl.c_host, NAMELEN); 355 spcl.c_level = level - '0'; 356 spcl.c_type = TS_TAPE; 357 358 if (spcl.c_date == 0) { 359 tmsg = "the epoch\n"; 360 } else { 361 time_t t = _time64_to_time(spcl.c_date); 362 tmsg = ctime(&t); 363 } 364 msg("Date of this level %c dump: %s", level, tmsg); 365 366 if (!Tflag) 367 getdumptime(); /* /etc/dumpdates snarfed */ 368 if (spcl.c_ddate == 0) { 369 tmsg = "the epoch\n"; 370 } else { 371 time_t t = _time64_to_time(spcl.c_ddate); 372 tmsg = ctime(&t); 373 } 374 msg("Date of last level %c dump: %s", lastlevel, tmsg); 375 376 msg("Dumping %s%s ", snapdump ? "snapshot of ": "", disk); 377 if (dt != NULL) 378 msgtail("(%s) ", dt->fs_file); 379 if (host) 380 msgtail("to %s on host %s\n", tape, host); 381 else 382 msgtail("to %s\n", tape); 383 384 sync(); 385 sblock = (struct fs *)sblock_buf; 386 for (i = 0; sblock_try[i] != -1; i++) { 387 sblock->fs_fsize = SBLOCKSIZE; /* needed in bread */ 388 bread(sblock_try[i] >> dev_bshift, (char *) sblock, SBLOCKSIZE); 389 if ((sblock->fs_magic == FS_UFS1_MAGIC || 390 (sblock->fs_magic == FS_UFS2_MAGIC && 391 sblock->fs_sblockloc == sblock_try[i])) && 392 sblock->fs_bsize <= MAXBSIZE && 393 sblock->fs_bsize >= sizeof(struct fs)) 394 break; 395 } 396 if (sblock_try[i] == -1) 397 quit("Cannot find file system superblock\n"); 398 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 399 dev_bshift = ffs(dev_bsize) - 1; 400 if (dev_bsize != (1 << dev_bshift)) 401 quit("dev_bsize (%ld) is not a power of 2", dev_bsize); 402 tp_bshift = ffs(TP_BSIZE) - 1; 403 if (TP_BSIZE != (1 << tp_bshift)) 404 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 405 maxino = sblock->fs_ipg * sblock->fs_ncg; 406 mapsize = roundup(howmany(maxino, CHAR_BIT), TP_BSIZE); 407 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 408 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 409 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 410 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 411 412 nonodump = spcl.c_level < honorlevel; 413 414 passno = 1; 415 setproctitle("%s: pass 1: regular files", disk); 416 msg("mapping (Pass I) [regular files]\n"); 417 anydirskipped = mapfiles(maxino, &tapesize); 418 419 passno = 2; 420 setproctitle("%s: pass 2: directories", disk); 421 msg("mapping (Pass II) [directories]\n"); 422 while (anydirskipped) { 423 anydirskipped = mapdirs(maxino, &tapesize); 424 } 425 426 if (pipeout || unlimited) { 427 tapesize += 10; /* 10 trailer blocks */ 428 msg("estimated %ld tape blocks.\n", tapesize); 429 } else { 430 double fetapes; 431 432 if (blocksperfile) 433 fetapes = (double) tapesize / blocksperfile; 434 else if (cartridge) { 435 /* Estimate number of tapes, assuming streaming stops at 436 the end of each block written, and not in mid-block. 437 Assume no erroneous blocks; this can be compensated 438 for with an artificially low tape size. */ 439 fetapes = 440 ( (double) tapesize /* blocks */ 441 * TP_BSIZE /* bytes/block */ 442 * (1.0/density) /* 0.1" / byte " */ 443 + 444 (double) tapesize /* blocks */ 445 * (1.0/ntrec) /* streaming-stops per block */ 446 * 15.48 /* 0.1" / streaming-stop " */ 447 ) * (1.0 / tsize ); /* tape / 0.1" " */ 448 } else { 449 /* Estimate number of tapes, for old fashioned 9-track 450 tape */ 451 int tenthsperirg = (density == 625) ? 3 : 7; 452 fetapes = 453 ( (double) tapesize /* blocks */ 454 * TP_BSIZE /* bytes / block */ 455 * (1.0/density) /* 0.1" / byte " */ 456 + 457 (double) tapesize /* blocks */ 458 * (1.0/ntrec) /* IRG's / block */ 459 * tenthsperirg /* 0.1" / IRG " */ 460 ) * (1.0 / tsize ); /* tape / 0.1" " */ 461 } 462 etapes = fetapes; /* truncating assignment */ 463 etapes++; 464 /* count the dumped inodes map on each additional tape */ 465 tapesize += (etapes - 1) * 466 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 467 tapesize += etapes + 10; /* headers + 10 trailer blks */ 468 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 469 tapesize, fetapes); 470 } 471 472 /* 473 * If the user only wants an estimate of the number of 474 * tapes, exit now. 475 */ 476 if (just_estimate) 477 exit(0); 478 479 /* 480 * Allocate tape buffer. 481 */ 482 if (!alloctape()) 483 quit( 484 "can't allocate tape buffers - try a smaller blocking factor.\n"); 485 486 startnewtape(1); 487 (void)time((time_t *)&(tstart_writing)); 488 dumpmap(usedinomap, TS_CLRI, maxino - 1); 489 490 passno = 3; 491 setproctitle("%s: pass 3: directories", disk); 492 msg("dumping (Pass III) [directories]\n"); 493 dirty = 0; /* XXX just to get gcc to shut up */ 494 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 495 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */ 496 dirty = *map++; 497 else 498 dirty >>= 1; 499 if ((dirty & 1) == 0) 500 continue; 501 /* 502 * Skip directory inodes deleted and maybe reallocated 503 */ 504 dp = getino(ino, &mode); 505 if (mode != IFDIR) 506 continue; 507 (void)dumpino(dp, ino); 508 } 509 510 passno = 4; 511 setproctitle("%s: pass 4: regular files", disk); 512 msg("dumping (Pass IV) [regular files]\n"); 513 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 514 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */ 515 dirty = *map++; 516 else 517 dirty >>= 1; 518 if ((dirty & 1) == 0) 519 continue; 520 /* 521 * Skip inodes deleted and reallocated as directories. 522 */ 523 dp = getino(ino, &mode); 524 if (mode == IFDIR) 525 continue; 526 (void)dumpino(dp, ino); 527 } 528 529 (void)time((time_t *)&(tend_writing)); 530 spcl.c_type = TS_END; 531 for (i = 0; i < ntrec; i++) 532 writeheader(maxino - 1); 533 if (pipeout) 534 msg("DUMP: %jd tape blocks\n", (intmax_t)spcl.c_tapea); 535 else 536 msg("DUMP: %jd tape blocks on %d volume%s\n", 537 (intmax_t)spcl.c_tapea, spcl.c_volume, 538 (spcl.c_volume == 1) ? "" : "s"); 539 540 /* report dump performance, avoid division through zero */ 541 if (tend_writing - tstart_writing == 0) 542 msg("finished in less than a second\n"); 543 else 544 msg("finished in %jd seconds, throughput %jd KBytes/sec\n", 545 (intmax_t)tend_writing - tstart_writing, 546 (intmax_t)(spcl.c_tapea / 547 (tend_writing - tstart_writing))); 548 549 putdumptime(); 550 trewind(); 551 broadcast("DUMP IS DONE!\a\a\n"); 552 msg("DUMP IS DONE\n"); 553 Exit(X_FINOK); 554 /* NOTREACHED */ 555 } 556 557 static void 558 usage(void) 559 { 560 fprintf(stderr, 561 "usage: dump [-0123456789acLnSu] [-B records] [-b blocksize] [-C cachesize]\n" 562 " [-D dumpdates] [-d density] [-f file] [-h level] [-s feet]\n" 563 " [-T date] filesystem\n" 564 " dump -W | -w\n"); 565 exit(X_STARTUP); 566 } 567 568 /* 569 * Check to see if a disk is currently mounted. 570 */ 571 static char * 572 getmntpt(char *name, int *mntflagsp) 573 { 574 long mntsize, i; 575 struct statfs *mntbuf; 576 577 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 578 for (i = 0; i < mntsize; i++) { 579 if (!strcmp(mntbuf[i].f_mntfromname, name)) { 580 *mntflagsp = mntbuf[i].f_flags; 581 return (mntbuf[i].f_mntonname); 582 } 583 } 584 return (0); 585 } 586 587 /* 588 * Pick up a numeric argument. It must be nonnegative and in the given 589 * range (except that a vmax of 0 means unlimited). 590 */ 591 static long 592 numarg(const char *meaning, long vmin, long vmax) 593 { 594 char *p; 595 long val; 596 597 val = strtol(optarg, &p, 10); 598 if (*p) 599 errx(1, "illegal %s -- %s", meaning, optarg); 600 if (val < vmin || (vmax && val > vmax)) 601 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 602 return (val); 603 } 604 605 void 606 sig(int signo) 607 { 608 switch(signo) { 609 case SIGALRM: 610 case SIGBUS: 611 case SIGFPE: 612 case SIGHUP: 613 case SIGTERM: 614 case SIGTRAP: 615 if (pipeout) 616 quit("Signal on pipe: cannot recover\n"); 617 msg("Rewriting attempted as response to unknown signal.\n"); 618 (void)fflush(stderr); 619 (void)fflush(stdout); 620 close_rewind(); 621 exit(X_REWRITE); 622 /* NOTREACHED */ 623 case SIGSEGV: 624 msg("SIGSEGV: ABORTING!\n"); 625 (void)signal(SIGSEGV, SIG_DFL); 626 (void)kill(0, SIGSEGV); 627 /* NOTREACHED */ 628 } 629 } 630 631 char * 632 rawname(char *cp) 633 { 634 static char rawbuf[MAXPATHLEN]; 635 char *dp; 636 struct stat sb; 637 638 if (stat(cp, &sb) == 0) { 639 /* 640 * If the name already refers to a raw device, return 641 * it immediately without tampering. 642 */ 643 if ((sb.st_mode & S_IFMT) == S_IFCHR) 644 return (cp); 645 } 646 647 dp = strrchr(cp, '/'); 648 649 if (dp == NULL) 650 return (NULL); 651 *dp = '\0'; 652 (void)strlcpy(rawbuf, cp, MAXPATHLEN - 1); 653 *dp = '/'; 654 (void)strlcat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 655 (void)strlcat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 656 return (rawbuf); 657 } 658 659 /* 660 * obsolete -- 661 * Change set of key letters and ordered arguments into something 662 * getopt(3) will like. 663 */ 664 static void 665 obsolete(int *argcp, char **argvp[]) 666 { 667 int argc, flags; 668 char *ap, **argv, *flagsp, **nargv, *p; 669 670 /* Setup. */ 671 argv = *argvp; 672 argc = *argcp; 673 674 /* Return if no arguments or first argument has leading dash. */ 675 ap = argv[1]; 676 if (argc == 1 || *ap == '-') 677 return; 678 679 /* Allocate space for new arguments. */ 680 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 681 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 682 err(1, NULL); 683 684 *nargv++ = *argv; 685 argv += 2; 686 687 for (flags = 0; *ap; ++ap) { 688 switch (*ap) { 689 case 'B': 690 case 'b': 691 case 'd': 692 case 'f': 693 case 'D': 694 case 'C': 695 case 'h': 696 case 's': 697 case 'T': 698 if (*argv == NULL) { 699 warnx("option requires an argument -- %c", *ap); 700 usage(); 701 } 702 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 703 err(1, NULL); 704 nargv[0][0] = '-'; 705 nargv[0][1] = *ap; 706 (void)strcpy(&nargv[0][2], *argv); 707 ++argv; 708 ++nargv; 709 break; 710 default: 711 if (!flags) { 712 *p++ = '-'; 713 flags = 1; 714 } 715 *p++ = *ap; 716 break; 717 } 718 } 719 720 /* Terminate flags. */ 721 if (flags) { 722 *p = '\0'; 723 *nargv++ = flagsp; 724 } 725 726 /* Copy remaining arguments. */ 727 while ((*nargv++ = *argv++)); 728 729 /* Update argument count. */ 730 *argcp = nargv - *argvp - 1; 731 } 732