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