1 /* $OpenBSD: main.c,v 1.30 2001/11/05 07:39:16 mpech 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 4/15/94"; 46 #else 47 static char rcsid[] = "$NetBSD: main.c,v 1.8 1996/03/15 22:39:32 scottr Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 #include <sys/param.h> 52 #include <sys/mount.h> 53 #include <sys/stat.h> 54 #include <sys/time.h> 55 #ifdef sunos 56 #include <sys/vnode.h> 57 58 #include <ufs/inode.h> 59 #include <ufs/fs.h> 60 #else 61 #include <ufs/ffs/fs.h> 62 #include <ufs/ufs/dinode.h> 63 #endif 64 65 #include <protocols/dumprestore.h> 66 67 #include <ctype.h> 68 #include <err.h> 69 #include <errno.h> 70 #include <fcntl.h> 71 #include <fstab.h> 72 #include <paths.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 #ifndef SBOFF 84 #define SBOFF (SBLOCK * DEV_BSIZE) 85 #endif 86 87 int notify = 0; /* notify operator flag */ 88 int blockswritten = 0; /* number of blocks written on current tape */ 89 int tapeno = 0; /* current tape number */ 90 int density = 0; /* density in bytes/0.1" */ 91 int ntrec = NTREC; /* # tape blocks in each tape record */ 92 int cartridge = 0; /* Assume non-cartridge tape */ 93 long dev_bsize = 1; /* recalculated below */ 94 long blocksperfile; /* output blocks per file */ 95 char *host = NULL; /* remote host (if any) */ 96 int maxbsize = 64*1024; /* XXX MAXBSIZE from sys/param.h */ 97 98 static long numarg __P((char *, long, long)); 99 static void obsolete __P((int *, char **[])); 100 static void usage __P((void)); 101 102 int 103 main(argc, argv) 104 int argc; 105 char *argv[]; 106 { 107 ino_t ino; 108 int dirty; 109 struct dinode *dp; 110 struct fstab *dt; 111 char *map; 112 int ch; 113 struct tm then; 114 struct statfs fsbuf; 115 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 116 ino_t maxino; 117 time_t tnow; 118 int dirlist; 119 char *toplevel, *str, *mount_point = NULL; 120 121 spcl.c_date = 0; 122 (void)time((time_t *)&spcl.c_date); 123 124 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 125 if ((tape = getenv("TAPE")) == NULL) 126 tape = _PATH_DEFTAPE; 127 dumpdates = _PATH_DUMPDATES; 128 temp = _PATH_DTMP; 129 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 130 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 131 level = '0'; 132 133 if (argc < 2) 134 usage(); 135 136 obsolete(&argc, &argv); 137 while ((ch = getopt(argc, argv, "0123456789aB:b:cd:f:h:ns:T:uWw")) != -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 'B': /* blocks per output file */ 146 blocksperfile = numarg("blocks per file", 1L, 0L); 147 break; 148 149 case 'b': /* blocks per tape write */ 150 ntrec = numarg("blocks per write", 1L, 1000L); 151 if (ntrec > maxbsize/1024) { 152 msg("Please choose a blocksize <= %dKB\n", 153 maxbsize/1024); 154 exit(X_STARTUP); 155 } 156 bflag = 1; 157 break; 158 159 case 'c': /* Tape is cart. not 9-track */ 160 cartridge = 1; 161 break; 162 163 case 'd': /* density, in bits per inch */ 164 density = numarg("density", 10L, 327670L) / 10; 165 if (density >= 625 && !bflag) 166 ntrec = HIGHDENSITYTREC; 167 break; 168 169 case 'f': /* output file */ 170 tape = optarg; 171 break; 172 173 case 'h': 174 honorlevel = numarg("honor level", 0L, 10L); 175 break; 176 177 case 'n': /* notify operators */ 178 notify = 1; 179 break; 180 181 case 's': /* tape size, feet */ 182 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 183 break; 184 185 case 'T': /* time of last dump */ 186 str = strptime(optarg, "%a %b %e %H:%M:%S %Y", &then); 187 then.tm_isdst = -1; 188 if (str == NULL || (*str != '\n' && *str != '\0')) 189 spcl.c_ddate = (time_t) -1; 190 else 191 spcl.c_ddate = mktime(&then); 192 if (spcl.c_ddate < 0) { 193 (void)fprintf(stderr, "bad time \"%s\"\n", 194 optarg); 195 exit(X_STARTUP); 196 } 197 Tflag = 1; 198 lastlevel = '?'; 199 break; 200 201 case 'u': /* update /etc/dumpdates */ 202 uflag = 1; 203 break; 204 205 case 'W': /* what to do */ 206 case 'w': 207 lastdump(ch); 208 exit(X_FINOK); /* do nothing else */ 209 210 case 'a': /* `auto-size', Write to EOM. */ 211 unlimited = 1; 212 break; 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 filesystem\n"); 222 exit(X_STARTUP); 223 } 224 225 /* 226 * determine if disk is a subdirectory, and setup appropriately 227 */ 228 dirlist = 0; 229 toplevel = NULL; 230 for (i = 0; i < argc; i++) { 231 struct stat sb; 232 233 if (lstat(argv[i], &sb) == -1) { 234 msg("Cannot lstat %s: %s\n", argv[i], strerror(errno)); 235 exit(X_STARTUP); 236 } 237 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) 238 break; 239 if (statfs(argv[i], &fsbuf) == -1) { 240 msg("Cannot statfs %s: %s\n", argv[i], strerror(errno)); 241 exit(X_STARTUP); 242 } 243 if (strcmp(argv[i], fsbuf.f_mntonname) == 0) { 244 if (dirlist != 0) { 245 msg("Can't dump a mountpoint and a filelist\n"); 246 exit(X_STARTUP); 247 } 248 break; /* exit if sole mountpoint */ 249 } 250 if (!disk) { 251 if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) { 252 msg("Cannot malloc diskname\n"); 253 exit(X_STARTUP); 254 } 255 disk = toplevel; 256 if (uflag) { 257 msg("Ignoring u flag for subdir dump\n"); 258 uflag = 0; 259 } 260 if (level > '0') { 261 msg("Subdir dump is done at level 0\n"); 262 level = '0'; 263 } 264 msg("Dumping sub files/directories from %s\n", disk); 265 } else { 266 if (strcmp(disk, fsbuf.f_mntonname) != 0) { 267 msg("%s is not on %s\n", argv[i], disk); 268 exit(X_STARTUP); 269 } 270 } 271 msg("Dumping file/directory %s\n", argv[i]); 272 dirlist++; 273 } 274 if (dirlist == 0) { 275 disk = *argv++; 276 if (argc != 1) { 277 (void)fputs("Excess arguments to dump:", stderr); 278 while (--argc) { 279 (void)putc(' ', stderr); 280 (void)fputs(*argv++, stderr); 281 } 282 (void)putc('\n', stderr); 283 exit(X_STARTUP); 284 } 285 } 286 if (Tflag && uflag) { 287 (void)fprintf(stderr, 288 "You cannot use the T and u flags together.\n"); 289 exit(X_STARTUP); 290 } 291 if (strcmp(tape, "-") == 0) { 292 pipeout++; 293 tape = "standard output"; 294 } 295 296 if (blocksperfile) 297 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 298 else if (!unlimited) { 299 /* 300 * Determine how to default tape size and density 301 * 302 * density tape size 303 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 304 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 305 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 306 * (450*4 - slop) 307 */ 308 if (density == 0) 309 density = cartridge ? 100 : 160; 310 if (tsize == 0) 311 tsize = cartridge ? 1700L*120L : 2300L*120L; 312 } 313 314 if (strchr(tape, ':')) { 315 host = tape; 316 tape = strchr(host, ':'); 317 *tape++ = '\0'; 318 #ifdef RDUMP 319 if (rmthost(host) == 0) 320 exit(X_STARTUP); 321 #else 322 (void)fprintf(stderr, "remote dump not enabled\n"); 323 exit(X_STARTUP); 324 #endif 325 } 326 327 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 328 signal(SIGHUP, sig); 329 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 330 signal(SIGTRAP, sig); 331 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 332 signal(SIGFPE, sig); 333 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 334 signal(SIGBUS, sig); 335 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 336 signal(SIGSEGV, sig); 337 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 338 signal(SIGTERM, sig); 339 if (signal(SIGINT, interrupt) == SIG_IGN) 340 signal(SIGINT, SIG_IGN); 341 342 getfstab(); /* /etc/fstab snarfed */ 343 344 /* 345 * disk can be either the full special file name, 346 * the suffix of the special file name, 347 * the special name missing the leading '/', 348 * the file system name with or without the leading '/'. 349 */ 350 if (!statfs(disk, &fsbuf) && !strcmp(fsbuf.f_mntonname, disk)) { 351 /* mounted disk? */ 352 disk = rawname(fsbuf.f_mntfromname); 353 if (!disk) { 354 (void)fprintf(stderr, "cannot get raw name for %s\n", 355 fsbuf.f_mntfromname); 356 exit(X_STARTUP); 357 } 358 mount_point = fsbuf.f_mntonname; 359 (void)strlcpy(spcl.c_dev, fsbuf.f_mntfromname, 360 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 if ((dt = fstabsearch(disk)) != NULL) { 369 /* in fstab? */ 370 disk = rawname(dt->fs_spec); 371 mount_point = dt->fs_file; 372 (void)strlcpy(spcl.c_dev, dt->fs_spec, sizeof(spcl.c_dev)); 373 if (dirlist != 0) { 374 (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys), 375 "a subset of %s", mount_point); 376 } else { 377 (void)strlcpy(spcl.c_filesys, mount_point, 378 sizeof(spcl.c_filesys)); 379 } 380 } else { 381 /* must be a device */ 382 (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev)); 383 (void)strlcpy(spcl.c_filesys, "an unlisted file system", 384 sizeof(spcl.c_filesys)); 385 } 386 (void)strlcpy(spcl.c_label, "none", sizeof(spcl.c_label)); 387 (void)gethostname(spcl.c_host, sizeof(spcl.c_host)); 388 spcl.c_level = level - '0'; 389 spcl.c_type = TS_TAPE; 390 if (!Tflag) 391 getdumptime(); /* /etc/dumpdates snarfed */ 392 393 msg("Date of this level %c dump: %s", level, 394 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 395 msg("Date of last level %c dump: %s", lastlevel, 396 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 397 msg("Dumping %s ", disk); 398 if (mount_point != NULL) 399 msgtail("(%s) ", mount_point); 400 if (host) 401 msgtail("to %s on host %s\n", tape, host); 402 else 403 msgtail("to %s\n", tape); 404 405 if ((diskfd = open(disk, O_RDONLY)) < 0) { 406 msg("Cannot open %s\n", disk); 407 exit(X_STARTUP); 408 } 409 sync(); 410 sblock = (struct fs *)sblock_buf; 411 bread(SBOFF, (char *) sblock, SBSIZE); 412 if (sblock->fs_magic != FS_MAGIC) 413 quit("bad sblock magic number\n"); 414 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 415 dev_bshift = ffs(dev_bsize) - 1; 416 if (dev_bsize != (1 << dev_bshift)) 417 quit("dev_bsize (%d) is not a power of 2\n", dev_bsize); 418 tp_bshift = ffs(TP_BSIZE) - 1; 419 if (TP_BSIZE != (1 << tp_bshift)) 420 quit("TP_BSIZE (%d) is not a power of 2\n", TP_BSIZE); 421 #ifdef FS_44INODEFMT 422 if (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 %qd 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 %qd 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); 515 if ((dp->di_mode & IFMT) != 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 int mode; 523 524 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 525 dirty = *map++; 526 else 527 dirty >>= 1; 528 if ((dirty & 1) == 0) 529 continue; 530 /* 531 * Skip inodes deleted and reallocated as directories. 532 */ 533 dp = getino(ino); 534 mode = dp->di_mode & IFMT; 535 if (mode == IFDIR) 536 continue; 537 (void)dumpino(dp, ino); 538 } 539 540 spcl.c_type = TS_END; 541 for (i = 0; i < ntrec; i++) 542 writeheader(maxino - 1); 543 if (pipeout) 544 msg("%ld tape blocks\n", spcl.c_tapea); 545 else 546 msg("%ld tape blocks on %d volume%s\n", 547 spcl.c_tapea, spcl.c_volume, 548 (spcl.c_volume == 1) ? "" : "s"); 549 tnow = do_stats(); 550 msg("Date of this level %c dump: %s", level, 551 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 552 msg("Date this dump completed: %s", ctime(&tnow)); 553 msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno); 554 putdumptime(); 555 trewind(); 556 broadcast("DUMP IS DONE!\7\7\n"); 557 msg("DUMP IS DONE\n"); 558 Exit(X_FINOK); 559 /* NOTREACHED */ 560 } 561 562 static void 563 usage() 564 { 565 extern char *__progname; 566 567 (void)fprintf(stderr, "usage: %s [-0123456789acnu] [-B records]" 568 "[-b blocksize] [-d density] [-f file]\n" 569 " [-h level] [-s feet] [-T date] filesystem\n" 570 " %s [-W | -w]\n", __progname, __progname); 571 exit(X_STARTUP); 572 } 573 574 /* 575 * Pick up a numeric argument. It must be nonnegative and in the given 576 * range (except that a vmax of 0 means unlimited). 577 */ 578 static long 579 numarg(meaning, vmin, vmax) 580 char *meaning; 581 long vmin, vmax; 582 { 583 char *p; 584 long val; 585 586 val = strtol(optarg, &p, 10); 587 if (*p) 588 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg); 589 if (val < vmin || (vmax && val > vmax)) 590 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax); 591 return (val); 592 } 593 594 void 595 sig(signo) 596 int signo; 597 { 598 switch(signo) { 599 case SIGALRM: 600 case SIGBUS: 601 case SIGFPE: 602 case SIGHUP: 603 case SIGTERM: 604 case SIGTRAP: 605 if (pipeout) 606 quit("Signal on pipe: cannot recover\n"); 607 msg("Rewriting attempted as response to unknown signal.\n"); 608 (void)fflush(stderr); 609 (void)fflush(stdout); 610 close_rewind(); 611 exit(X_REWRITE); 612 /* NOTREACHED */ 613 case SIGSEGV: 614 msg("SIGSEGV: ABORTING!\n"); 615 (void)signal(SIGSEGV, SIG_DFL); 616 (void)kill(0, SIGSEGV); 617 /* NOTREACHED */ 618 } 619 } 620 621 char * 622 rawname(cp) 623 char *cp; 624 { 625 static char rawbuf[MAXPATHLEN]; 626 char *dp = strrchr(cp, '/'); 627 628 if (dp == NULL) 629 return (NULL); 630 *dp = '\0'; 631 (void)snprintf(rawbuf, sizeof(rawbuf), "%s/r%s", cp, dp + 1); 632 *dp = '/'; 633 return (rawbuf); 634 } 635 636 /* 637 * obsolete -- 638 * Change set of key letters and ordered arguments into something 639 * getopt(3) will like. 640 */ 641 static void 642 obsolete(argcp, argvp) 643 int *argcp; 644 char **argvp[]; 645 { 646 int argc, flags; 647 char *ap, **argv, *flagsp, **nargv, *p; 648 649 /* Setup. */ 650 argv = *argvp; 651 argc = *argcp; 652 653 /* Return if no arguments or first argument has leading dash. */ 654 ap = argv[1]; 655 if (argc == 1 || *ap == '-') 656 return; 657 658 /* Allocate space for new arguments. */ 659 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 660 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 661 err(1, NULL); 662 663 *nargv++ = *argv; 664 argv += 2; 665 666 for (flags = 0; *ap; ++ap) { 667 switch (*ap) { 668 case 'B': 669 case 'b': 670 case 'd': 671 case 'f': 672 case 'h': 673 case 's': 674 case 'T': 675 if (*argv == NULL) { 676 warnx("option requires an argument -- %c", *ap); 677 usage(); 678 } 679 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 680 err(1, NULL); 681 nargv[0][0] = '-'; 682 nargv[0][1] = *ap; 683 (void)strcpy(&nargv[0][2], *argv); 684 ++argv; 685 ++nargv; 686 break; 687 default: 688 if (!flags) { 689 *p++ = '-'; 690 flags = 1; 691 } 692 *p++ = *ap; 693 break; 694 } 695 } 696 697 /* Terminate flags. */ 698 if (flags) { 699 *p = '\0'; 700 *nargv++ = flagsp; 701 } 702 703 /* Copy remaining arguments. */ 704 while ((*nargv++ = *argv++)) 705 ; 706 707 /* Update argument count. */ 708 *argcp = nargv - *argvp - 1; 709 } 710