147082Smckusick /*-
266502Sbostic * Copyright (c) 1980, 1991, 1993, 1994
361484Sbostic * The Regents of the University of California. All rights reserved.
447082Smckusick *
547082Smckusick * %sccs.include.redist.c%
622037Sdist */
722037Sdist
822037Sdist #ifndef lint
961484Sbostic static char copyright[] =
1066502Sbostic "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
1161484Sbostic The Regents of the University of California. All rights reserved.\n";
1246586Storek #endif /* not lint */
1322037Sdist
1447082Smckusick #ifndef lint
15*69157Smckusick static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 05/01/95";
1647082Smckusick #endif /* not lint */
1747082Smckusick
1857725Smckusick #include <sys/param.h>
1957725Smckusick #include <sys/time.h>
2050496Smckusick #ifdef sunos
2157725Smckusick #include <sys/vnode.h>
2257725Smckusick
2357725Smckusick #include <ufs/inode.h>
2454042Smckusick #include <ufs/fs.h>
2550496Smckusick #else
26*69157Smckusick #include <ufs/ufs/dinode.h>
2754042Smckusick #include <ufs/ffs/fs.h>
2854042Smckusick #endif
2957725Smckusick
3046795Sbostic #include <protocols/dumprestore.h>
3157725Smckusick
3259926Storek #include <ctype.h>
33*69157Smckusick #include <err.h>
3457725Smckusick #include <errno.h>
3546795Sbostic #include <fcntl.h>
3646795Sbostic #include <fstab.h>
3757725Smckusick #include <signal.h>
3846795Sbostic #include <stdio.h>
3946795Sbostic #include <stdlib.h>
4046795Sbostic #include <string.h>
4157725Smckusick #include <unistd.h>
4257725Smckusick
431423Sroot #include "dump.h"
4437266Sbostic #include "pathnames.h"
451423Sroot
4659926Storek #ifndef SBOFF
4759926Storek #define SBOFF (SBLOCK * DEV_BSIZE)
4859926Storek #endif
4959926Storek
501423Sroot int notify = 0; /* notify operator flag */
511423Sroot int blockswritten = 0; /* number of blocks written on current tape */
521423Sroot int tapeno = 0; /* current tape number */
5310910Ssam int density = 0; /* density in bytes/0.1" */
5410910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */
5510910Ssam int cartridge = 0; /* Assume non-cartridge tape */
5630560Smckusick long dev_bsize = 1; /* recalculated below */
5746614Smckusick long blocksperfile; /* output blocks per file */
5850495Smckusick char *host = NULL; /* remote host (if any) */
591423Sroot
60*69157Smckusick static long numarg __P((char *, long, long));
61*69157Smckusick static void obsolete __P((int *, char **[]));
62*69157Smckusick static void usage __P((void));
6359926Storek
6457725Smckusick int
main(argc,argv)651423Sroot main(argc, argv)
6646794Smckusick int argc;
67*69157Smckusick char *argv[];
681423Sroot {
6946794Smckusick register ino_t ino;
7047058Smckusick register int dirty;
7146794Smckusick register struct dinode *dp;
7246794Smckusick register struct fstab *dt;
7346794Smckusick register char *map;
74*69157Smckusick register int ch;
7559926Storek int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
7646794Smckusick ino_t maxino;
771423Sroot
7850495Smckusick spcl.c_date = 0;
7959926Storek (void)time((time_t *)&spcl.c_date);
801423Sroot
8110910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */
8237946Sbostic tape = _PATH_DEFTAPE;
8346794Smckusick dumpdates = _PATH_DUMPDATES;
8437266Sbostic temp = _PATH_DTMP;
8546586Storek if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
8646586Storek quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
8746794Smckusick level = '0';
88*69157Smckusick
89*69157Smckusick if (argc < 2)
90*69157Smckusick usage();
91*69157Smckusick
92*69157Smckusick obsolete(&argc, &argv);
93*69157Smckusick while ((ch = getopt(argc, argv, "0123456789B:b:cd:f:h:ns:T:uWw")) != -1)
94*69157Smckusick switch (ch) {
95*69157Smckusick /* dump level */
96*69157Smckusick case '0': case '1': case '2': case '3': case '4':
97*69157Smckusick case '5': case '6': case '7': case '8': case '9':
98*69157Smckusick level = ch;
9959926Storek break;
1001423Sroot
101*69157Smckusick case 'B': /* blocks per output file */
102*69157Smckusick blocksperfile = numarg("blocks per file", 1L, 0L);
103*69157Smckusick break;
10446794Smckusick
105*69157Smckusick case 'b': /* blocks per tape write */
106*69157Smckusick ntrec = numarg("blocks per write", 1L, 1000L);
107*69157Smckusick break;
10846794Smckusick
109*69157Smckusick case 'c': /* Tape is cart. not 9-track */
110*69157Smckusick cartridge = 1;
11159926Storek break;
1121423Sroot
11346794Smckusick case 'd': /* density, in bits per inch */
114*69157Smckusick density = numarg("density", 10L, 327670L) / 10;
11518494Smckusick if (density >= 625 && !bflag)
11618494Smckusick ntrec = HIGHDENSITYTREC;
11759926Storek break;
1181423Sroot
119*69157Smckusick case 'f': /* output file */
120*69157Smckusick tape = optarg;
121*69157Smckusick break;
122*69157Smckusick
123*69157Smckusick case 'h':
124*69157Smckusick honorlevel = numarg("honor level", 0L, 10L);
125*69157Smckusick break;
126*69157Smckusick
127*69157Smckusick case 'n': /* notify operators */
128*69157Smckusick notify = 1;
129*69157Smckusick break;
130*69157Smckusick
13146794Smckusick case 's': /* tape size, feet */
132*69157Smckusick tsize = numarg("tape size", 1L, 0L) * 12 * 10;
13359926Storek break;
1341423Sroot
13554042Smckusick case 'T': /* time of last dump */
136*69157Smckusick spcl.c_ddate = unctime(optarg);
13754042Smckusick if (spcl.c_ddate < 0) {
13859926Storek (void)fprintf(stderr, "bad time \"%s\"\n",
139*69157Smckusick optarg);
14059926Storek exit(X_ABORT);
14154042Smckusick }
14259926Storek Tflag = 1;
14354042Smckusick lastlevel = '?';
14459926Storek break;
14554042Smckusick
14646794Smckusick case 'u': /* update /etc/dumpdates */
14759926Storek uflag = 1;
14859926Storek break;
1491423Sroot
150*69157Smckusick case 'W': /* what to do */
151*69157Smckusick case 'w':
152*69157Smckusick lastdump(ch);
153*69157Smckusick exit(0); /* do nothing else */
1541423Sroot
15546794Smckusick default:
156*69157Smckusick usage();
15746794Smckusick }
158*69157Smckusick argc -= optind;
159*69157Smckusick argv += optind;
160*69157Smckusick
16146794Smckusick if (argc < 1) {
16259926Storek (void)fprintf(stderr, "Must specify disk or filesystem\n");
16359926Storek exit(X_ABORT);
1641423Sroot }
16559926Storek disk = *argv++;
16659926Storek argc--;
16746794Smckusick if (argc >= 1) {
16859926Storek (void)fprintf(stderr, "Unknown arguments to dump:");
16946794Smckusick while (argc--)
17059926Storek (void)fprintf(stderr, " %s", *argv++);
17159926Storek (void)fprintf(stderr, "\n");
17259926Storek exit(X_ABORT);
17346794Smckusick }
17454042Smckusick if (Tflag && uflag) {
17559926Storek (void)fprintf(stderr,
17654042Smckusick "You cannot use the T and u flags together.\n");
17759926Storek exit(X_ABORT);
17854042Smckusick }
17912331Smckusick if (strcmp(tape, "-") == 0) {
18012331Smckusick pipeout++;
18112331Smckusick tape = "standard output";
18212331Smckusick }
18310910Ssam
18446614Smckusick if (blocksperfile)
18546614Smckusick blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
18646614Smckusick else {
18746614Smckusick /*
18846614Smckusick * Determine how to default tape size and density
18946614Smckusick *
19046614Smckusick * density tape size
19146614Smckusick * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
19246614Smckusick * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
19346614Smckusick * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
19446614Smckusick * (450*4 - slop)
19546614Smckusick */
19646614Smckusick if (density == 0)
19746614Smckusick density = cartridge ? 100 : 160;
19846614Smckusick if (tsize == 0)
19946614Smckusick tsize = cartridge ? 1700L*120L : 2300L*120L;
20046614Smckusick }
20110910Ssam
20268991Sbostic if (strchr(tape, ':')) {
20350495Smckusick host = tape;
20468991Sbostic tape = strchr(host, ':');
20559926Storek *tape++ = '\0';
2066886Ssam #ifdef RDUMP
20750495Smckusick if (rmthost(host) == 0)
20857725Smckusick exit(X_ABORT);
20950495Smckusick #else
21059926Storek (void)fprintf(stderr, "remote dump not enabled\n");
21157725Smckusick exit(X_ABORT);
21250495Smckusick #endif
2136886Ssam }
21459926Storek (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
21550495Smckusick
21646614Smckusick if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
21755288Sbostic signal(SIGHUP, sig);
21846614Smckusick if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
21955288Sbostic signal(SIGTRAP, sig);
22046614Smckusick if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
22155288Sbostic signal(SIGFPE, sig);
22246614Smckusick if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
22355288Sbostic signal(SIGBUS, sig);
22446614Smckusick if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
22555288Sbostic signal(SIGSEGV, sig);
22646614Smckusick if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
22755288Sbostic signal(SIGTERM, sig);
2281423Sroot if (signal(SIGINT, interrupt) == SIG_IGN)
2291423Sroot signal(SIGINT, SIG_IGN);
2301423Sroot
2311423Sroot set_operators(); /* /etc/group snarfed */
2321423Sroot getfstab(); /* /etc/fstab snarfed */
2331423Sroot /*
2341423Sroot * disk can be either the full special file name,
2351423Sroot * the suffix of the special file name,
2361423Sroot * the special name missing the leading '/',
2371423Sroot * the file system name with or without the leading '/'.
2381423Sroot */
2391423Sroot dt = fstabsearch(disk);
24059926Storek if (dt != NULL) {
2411423Sroot disk = rawname(dt->fs_spec);
24259926Storek (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
24359926Storek (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
24429900Smckusick } else {
24559926Storek (void)strncpy(spcl.c_dev, disk, NAMELEN);
24659926Storek (void)strncpy(spcl.c_filesys, "an unlisted file system",
24754042Smckusick NAMELEN);
24829900Smckusick }
24959926Storek (void)strcpy(spcl.c_label, "none");
25059926Storek (void)gethostname(spcl.c_host, NAMELEN);
25146794Smckusick spcl.c_level = level - '0';
25229900Smckusick spcl.c_type = TS_TAPE;
25354042Smckusick if (!Tflag)
25454042Smckusick getdumptime(); /* /etc/dumpdates snarfed */
2551423Sroot
25646794Smckusick msg("Date of this level %c dump: %s", level,
25746794Smckusick spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
25846794Smckusick msg("Date of last level %c dump: %s", lastlevel,
25946794Smckusick spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
2601423Sroot msg("Dumping %s ", disk);
26159926Storek if (dt != NULL)
2621423Sroot msgtail("(%s) ", dt->fs_file);
26350495Smckusick if (host)
26450495Smckusick msgtail("to %s on host %s\n", tape, host);
26550495Smckusick else
26650495Smckusick msgtail("to %s\n", tape);
2671423Sroot
26846794Smckusick if ((diskfd = open(disk, O_RDONLY)) < 0) {
2691423Sroot msg("Cannot open %s\n", disk);
27059926Storek exit(X_ABORT);
2711423Sroot }
27246794Smckusick sync();
27354042Smckusick sblock = (struct fs *)sblock_buf;
27454042Smckusick bread(SBOFF, (char *) sblock, SBSIZE);
27546586Storek if (sblock->fs_magic != FS_MAGIC)
27646586Storek quit("bad sblock magic number\n");
27730560Smckusick dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
27846586Storek dev_bshift = ffs(dev_bsize) - 1;
27946586Storek if (dev_bsize != (1 << dev_bshift))
28046586Storek quit("dev_bsize (%d) is not a power of 2", dev_bsize);
28146586Storek tp_bshift = ffs(TP_BSIZE) - 1;
28246586Storek if (TP_BSIZE != (1 << tp_bshift))
28346586Storek quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
28457725Smckusick #ifdef FS_44INODEFMT
28554165Smckusick if (sblock->fs_inodefmt >= FS_44INODEFMT)
28654165Smckusick spcl.c_flags |= DR_NEWINODEFMT;
28757725Smckusick #endif
28859926Storek maxino = sblock->fs_ipg * sblock->fs_ncg;
28959926Storek mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
29054042Smckusick usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
29154042Smckusick dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
29254042Smckusick dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
29346794Smckusick tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
2941423Sroot
29559926Storek nonodump = spcl.c_level < honorlevel;
29659926Storek
2971423Sroot msg("mapping (Pass I) [regular files]\n");
29846794Smckusick anydirskipped = mapfiles(maxino, &tapesize);
2991423Sroot
30046794Smckusick msg("mapping (Pass II) [directories]\n");
30146794Smckusick while (anydirskipped) {
30246794Smckusick anydirskipped = mapdirs(maxino, &tapesize);
30346794Smckusick }
3041423Sroot
30559926Storek if (pipeout) {
30646794Smckusick tapesize += 10; /* 10 trailer blocks */
30759926Storek msg("estimated %ld tape blocks.\n", tapesize);
30859926Storek } else {
30959926Storek double fetapes;
31059926Storek
31146614Smckusick if (blocksperfile)
31259926Storek fetapes = (double) tapesize / blocksperfile;
31346614Smckusick else if (cartridge) {
31446614Smckusick /* Estimate number of tapes, assuming streaming stops at
31546614Smckusick the end of each block written, and not in mid-block.
31646614Smckusick Assume no erroneous blocks; this can be compensated
31746614Smckusick for with an artificially low tape size. */
31846614Smckusick fetapes =
31946794Smckusick ( tapesize /* blocks */
32046614Smckusick * TP_BSIZE /* bytes/block */
32146614Smckusick * (1.0/density) /* 0.1" / byte */
32246614Smckusick +
32346794Smckusick tapesize /* blocks */
32446614Smckusick * (1.0/ntrec) /* streaming-stops per block */
32546614Smckusick * 15.48 /* 0.1" / streaming-stop */
32646614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */
32746614Smckusick } else {
32846614Smckusick /* Estimate number of tapes, for old fashioned 9-track
32946614Smckusick tape */
33046614Smckusick int tenthsperirg = (density == 625) ? 3 : 7;
33146614Smckusick fetapes =
33246794Smckusick ( tapesize /* blocks */
33346614Smckusick * TP_BSIZE /* bytes / block */
33446614Smckusick * (1.0/density) /* 0.1" / byte */
33546614Smckusick +
33646794Smckusick tapesize /* blocks */
33746614Smckusick * (1.0/ntrec) /* IRG's / block */
33846614Smckusick * tenthsperirg /* 0.1" / IRG */
33946614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */
34046614Smckusick }
34146614Smckusick etapes = fetapes; /* truncating assignment */
34246614Smckusick etapes++;
34346794Smckusick /* count the dumped inodes map on each additional tape */
34446794Smckusick tapesize += (etapes - 1) *
34546794Smckusick (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
34646794Smckusick tapesize += etapes + 10; /* headers + 10 trailer blks */
34746614Smckusick msg("estimated %ld tape blocks on %3.2f tape(s).\n",
34846794Smckusick tapesize, fetapes);
34959926Storek }
3501423Sroot
35154042Smckusick /*
35259926Storek * Allocate tape buffer.
35354042Smckusick */
35454042Smckusick if (!alloctape())
35554042Smckusick quit("can't allocate tape buffers - try a smaller blocking factor.\n");
35610910Ssam
35750495Smckusick startnewtape(1);
35859926Storek (void)time((time_t *)&(tstart_writing));
35959926Storek dumpmap(usedinomap, TS_CLRI, maxino - 1);
3601423Sroot
3611423Sroot msg("dumping (Pass III) [directories]\n");
36259926Storek dirty = 0; /* XXX just to get gcc to shut up */
36359926Storek for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
36459926Storek if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
36547058Smckusick dirty = *map++;
36646794Smckusick else
36747058Smckusick dirty >>= 1;
36847058Smckusick if ((dirty & 1) == 0)
36946794Smckusick continue;
37046794Smckusick /*
37146794Smckusick * Skip directory inodes deleted and maybe reallocated
37246794Smckusick */
37346794Smckusick dp = getino(ino);
37446794Smckusick if ((dp->di_mode & IFMT) != IFDIR)
37546794Smckusick continue;
37659926Storek (void)dumpino(dp, ino);
37746794Smckusick }
3781423Sroot
3791423Sroot msg("dumping (Pass IV) [regular files]\n");
38059926Storek for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
38157725Smckusick int mode;
38257725Smckusick
38359926Storek if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
38447058Smckusick dirty = *map++;
38546794Smckusick else
38647058Smckusick dirty >>= 1;
38747058Smckusick if ((dirty & 1) == 0)
38846794Smckusick continue;
38946794Smckusick /*
39046794Smckusick * Skip inodes deleted and reallocated as directories.
39146794Smckusick */
39246794Smckusick dp = getino(ino);
39357725Smckusick mode = dp->di_mode & IFMT;
39457725Smckusick if (mode == IFDIR)
39546794Smckusick continue;
39659926Storek (void)dumpino(dp, ino);
39746794Smckusick }
3981423Sroot
3991423Sroot spcl.c_type = TS_END;
40046794Smckusick for (i = 0; i < ntrec; i++)
40159926Storek writeheader(maxino - 1);
40246614Smckusick if (pipeout)
40346614Smckusick msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
40446614Smckusick else
40546614Smckusick msg("DUMP: %ld tape blocks on %d volumes(s)\n",
40646614Smckusick spcl.c_tapea, spcl.c_volume);
40746794Smckusick putdumptime();
40846239Storek trewind();
4091423Sroot broadcast("DUMP IS DONE!\7\7\n");
41050495Smckusick msg("DUMP IS DONE\n");
4111423Sroot Exit(X_FINOK);
41246586Storek /* NOTREACHED */
4131423Sroot }
4141423Sroot
415*69157Smckusick static void
usage()416*69157Smckusick usage()
417*69157Smckusick {
418*69157Smckusick
419*69157Smckusick (void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n [-h level] [-s feet] [-T date] filesystem\n");
420*69157Smckusick (void)fprintf(stderr, " dump [-W | -w]\n");
421*69157Smckusick exit(1);
422*69157Smckusick }
423*69157Smckusick
42459926Storek /*
42559926Storek * Pick up a numeric argument. It must be nonnegative and in the given
42659926Storek * range (except that a vmax of 0 means unlimited).
42759926Storek */
42859926Storek static long
numarg(meaning,vmin,vmax)429*69157Smckusick numarg(meaning, vmin, vmax)
43059926Storek char *meaning;
43159926Storek long vmin, vmax;
43259926Storek {
433*69157Smckusick char *p;
43459926Storek long val;
43559926Storek
436*69157Smckusick val = strtol(optarg, &p, 10);
437*69157Smckusick if (*p)
438*69157Smckusick errx(1, "illegal %s -- %s", meaning, optarg);
43959926Storek if (val < vmin || (vmax && val > vmax))
440*69157Smckusick errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
44159926Storek return (val);
44259926Storek }
44359926Storek
44446586Storek void
sig(signo)44555288Sbostic sig(signo)
44655288Sbostic int signo;
4471423Sroot {
44855288Sbostic switch(signo) {
44955288Sbostic case SIGALRM:
45055288Sbostic case SIGBUS:
45155288Sbostic case SIGFPE:
45255288Sbostic case SIGHUP:
45355288Sbostic case SIGTERM:
45455288Sbostic case SIGTRAP:
45555288Sbostic if (pipeout)
45655288Sbostic quit("Signal on pipe: cannot recover\n");
45755288Sbostic msg("Rewriting attempted as response to unknown signal.\n");
45859926Storek (void)fflush(stderr);
45959926Storek (void)fflush(stdout);
46055288Sbostic close_rewind();
46157725Smckusick exit(X_REWRITE);
46255288Sbostic /* NOTREACHED */
46355288Sbostic case SIGSEGV:
46455288Sbostic msg("SIGSEGV: ABORTING!\n");
46559926Storek (void)signal(SIGSEGV, SIG_DFL);
46659926Storek (void)kill(0, SIGSEGV);
46755288Sbostic /* NOTREACHED */
46855288Sbostic }
4691423Sroot }
4701423Sroot
47146586Storek char *
rawname(cp)47246586Storek rawname(cp)
4731423Sroot char *cp;
4741423Sroot {
47565540Smckusick static char rawbuf[MAXPATHLEN];
47668991Sbostic char *dp = strrchr(cp, '/');
4771423Sroot
47859926Storek if (dp == NULL)
47959926Storek return (NULL);
48059926Storek *dp = '\0';
48159926Storek (void)strcpy(rawbuf, cp);
4821423Sroot *dp = '/';
48359926Storek (void)strcat(rawbuf, "/r");
48459926Storek (void)strcat(rawbuf, dp + 1);
4851423Sroot return (rawbuf);
4861423Sroot }
48750496Smckusick
488*69157Smckusick /*
489*69157Smckusick * obsolete --
490*69157Smckusick * Change set of key letters and ordered arguments into something
491*69157Smckusick * getopt(3) will like.
492*69157Smckusick */
493*69157Smckusick static void
obsolete(argcp,argvp)494*69157Smckusick obsolete(argcp, argvp)
495*69157Smckusick int *argcp;
496*69157Smckusick char **argvp[];
49750496Smckusick {
498*69157Smckusick int argc, flags;
499*69157Smckusick char *ap, **argv, *flagsp, **nargv, *p;
50050496Smckusick
501*69157Smckusick /* Setup. */
502*69157Smckusick argv = *argvp;
503*69157Smckusick argc = *argcp;
504*69157Smckusick
505*69157Smckusick /* Return if no arguments or first argument has leading dash. */
506*69157Smckusick ap = argv[1];
507*69157Smckusick if (argc == 1 || *ap == '-')
508*69157Smckusick return;
509*69157Smckusick
510*69157Smckusick /* Allocate space for new arguments. */
511*69157Smckusick if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
512*69157Smckusick (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
513*69157Smckusick err(1, NULL);
514*69157Smckusick
515*69157Smckusick *nargv++ = *argv;
516*69157Smckusick argv += 2;
517*69157Smckusick
518*69157Smckusick for (flags = 0; *ap; ++ap) {
519*69157Smckusick switch (*ap) {
520*69157Smckusick case 'B':
521*69157Smckusick case 'b':
522*69157Smckusick case 'd':
523*69157Smckusick case 'f':
524*69157Smckusick case 'h':
525*69157Smckusick case 's':
526*69157Smckusick case 'T':
527*69157Smckusick if (*argv == NULL) {
528*69157Smckusick warnx("option requires an argument -- %c", *ap);
529*69157Smckusick usage();
530*69157Smckusick }
531*69157Smckusick if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
532*69157Smckusick err(1, NULL);
533*69157Smckusick nargv[0][0] = '-';
534*69157Smckusick nargv[0][1] = *ap;
535*69157Smckusick (void)strcpy(&nargv[0][2], *argv);
536*69157Smckusick ++argv;
537*69157Smckusick ++nargv;
538*69157Smckusick break;
539*69157Smckusick default:
540*69157Smckusick if (!flags) {
541*69157Smckusick *p++ = '-';
542*69157Smckusick flags = 1;
543*69157Smckusick }
544*69157Smckusick *p++ = *ap;
545*69157Smckusick break;
546*69157Smckusick }
547*69157Smckusick }
548*69157Smckusick
549*69157Smckusick /* Terminate flags. */
550*69157Smckusick if (flags) {
551*69157Smckusick *p = '\0';
552*69157Smckusick *nargv++ = flagsp;
553*69157Smckusick }
554*69157Smckusick
555*69157Smckusick /* Copy remaining arguments. */
556*69157Smckusick while (*nargv++ = *argv++);
557*69157Smckusick
558*69157Smckusick /* Update argument count. */
559*69157Smckusick *argcp = nargv - *argvp - 1;
56050496Smckusick }
561