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