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