xref: /netbsd-src/sbin/dump/main.c (revision a5a68ff5f29de57339ca14f6c671c0a87714f1f8)
1 /*	$NetBSD: main.c,v 1.17 1997/09/18 03:03:56 lukem 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.17 1997/09/18 03:03:56 lukem 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 #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 <signal.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <time.h>
77 #include <unistd.h>
78 
79 #include "dump.h"
80 #include "pathnames.h"
81 
82 #ifndef SBOFF
83 #define SBOFF (SBLOCK * DEV_BSIZE)
84 #endif
85 
86 int	notify = 0;		/* notify operator flag */
87 int	blockswritten = 0;	/* number of blocks written on current tape */
88 int	tapeno = 0;		/* current tape number */
89 int	density = 0;		/* density in bytes/0.1" */
90 int	ntrec = NTREC;		/* # tape blocks in each tape record */
91 int	cartridge = 0;		/* Assume non-cartridge tape */
92 long	dev_bsize = 1;		/* recalculated below */
93 long	blocksperfile = 0;	/* output blocks per file */
94 char	*host = NULL;		/* remote host (if any) */
95 
96 int	main __P((int, char *[]));
97 static long numarg __P((char *, long, long));
98 static void obsolete __P((int *, char **[]));
99 static void usage __P((void));
100 
101 int
102 main(argc, argv)
103 	int argc;
104 	char *argv[];
105 {
106 	ino_t ino;
107 	int dirty;
108 	struct dinode *dp;
109 	struct	fstab *dt;
110 	char *map;
111 	int ch;
112 	int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
113 	ino_t maxino;
114 	time_t tnow;
115 	int dirlist;
116 	char *toplevel;
117 	int just_estimate = 0;
118 
119 	spcl.c_date = 0;
120 	(void)time((time_t *)&spcl.c_date);
121 
122 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
123 	if ((tape = getenv("TAPE")) == NULL)
124 		tape = _PATH_DEFTAPE;
125 	dumpdates = _PATH_DUMPDATES;
126 	temp = _PATH_DTMP;
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:f:h:ns: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 'f':		/* output file */
164 			tape = optarg;
165 			break;
166 
167 		case 'h':
168 			honorlevel = numarg("honor level", 0L, 10L);
169 			break;
170 
171 		case 'n':		/* notify operators */
172 			notify = 1;
173 			break;
174 
175 		case 's':		/* tape size, feet */
176 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
177 			break;
178 
179 		case 'S':		/* exit after estimating # of tapes */
180 			just_estimate = 1;
181 			break;
182 
183 		case 'T':		/* time of last dump */
184 			spcl.c_ddate = unctime(optarg);
185 			if (spcl.c_ddate < 0) {
186 				(void)fprintf(stderr, "bad time \"%s\"\n",
187 				    optarg);
188 				exit(X_ABORT);
189 			}
190 			Tflag = 1;
191 			lastlevel = '?';
192 			break;
193 
194 		case 'u':		/* update /etc/dumpdates */
195 			uflag = 1;
196 			break;
197 
198 		case 'W':		/* what to do */
199 		case 'w':
200 			lastdump(ch);
201 			exit(0);	/* do nothing else */
202 
203 		default:
204 			usage();
205 		}
206 	argc -= optind;
207 	argv += optind;
208 
209 	if (argc < 1) {
210 		(void)fprintf(stderr, "Must specify disk or filesystem\n");
211 		exit(X_ABORT);
212 	}
213 
214 	/*
215 	 *	determine if disk is a subdirectory, and setup appropriately
216 	 */
217 	dirlist = 0;
218 	toplevel = NULL;
219 	for (i = 0; i < argc; i++) {
220 		struct stat sb;
221 		struct statfs fsbuf;
222 
223 		if (lstat(argv[i], &sb) == -1) {
224 			msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
225 			exit(X_ABORT);
226 		}
227 		if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
228 			break;
229 		if (statfs(argv[i], &fsbuf) == -1) {
230 			msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
231 			exit(X_ABORT);
232 		}
233 		if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
234 			if (dirlist != 0) {
235 				msg("Can't dump a mountpoint and a filelist\n");
236 				exit(X_ABORT);
237 			}
238 			break;		/* exit if sole mountpoint */
239 		}
240 		if (!disk) {
241 			if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
242 				msg("Cannot malloc diskname\n");
243 				exit(X_ABORT);
244 			}
245 			disk = toplevel;
246 			if (uflag) {
247 				msg("Ignoring u flag for subdir dump\n");
248 				uflag = 0;
249 			}
250 			if (level > '0') {
251 				msg("Subdir dump is done at level 0\n");
252 				level = '0';
253 			}
254 			msg("Dumping sub files/directories from %s\n", disk);
255 		} else {
256 			if (strcmp(disk, fsbuf.f_mntonname) != 0) {
257 				msg("%s is not on %s\n", argv[i], disk);
258 				exit(X_ABORT);
259 			}
260 		}
261 		msg("Dumping file/directory %s\n", argv[i]);
262 		dirlist++;
263 	}
264 	if (dirlist == 0) {
265 		disk = *argv++;
266 		if (argc != 1) {
267 			(void)fprintf(stderr, "Excess arguments to dump:");
268 			while (--argc)
269 				(void)fprintf(stderr, " %s", *argv++);
270 			(void)fprintf(stderr, "\n");
271 			exit(X_ABORT);
272 		}
273 	}
274 	if (Tflag && uflag) {
275 	        (void)fprintf(stderr,
276 		    "You cannot use the T and u flags together.\n");
277 		exit(X_ABORT);
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 {
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_ABORT);
309 #else
310 		(void)fprintf(stderr, "remote dump not enabled\n");
311 		exit(X_ABORT);
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 	set_operators();	/* /etc/group snarfed */
331 	getfstab();		/* /etc/fstab snarfed */
332 
333 	/*
334 	 *	disk can be either the full special file name,
335 	 *	the suffix of the special file name,
336 	 *	the special name missing the leading '/',
337 	 *	the file system name with or without the leading '/'.
338 	 */
339 	dt = fstabsearch(disk);
340 	if (dt != NULL) {
341 		disk = rawname(dt->fs_spec);
342 		(void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
343 		if (dirlist != 0)
344 			(void)snprintf(spcl.c_filesys, NAMELEN,
345 			    "a subset of %s", dt->fs_file);
346 		else
347 			(void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
348 	} else {
349 		(void)strncpy(spcl.c_dev, disk, NAMELEN);
350 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
351 		    NAMELEN);
352 	}
353 	(void)strncpy(spcl.c_label, "none", sizeof(spcl.c_label) - 1);
354 	(void)gethostname(spcl.c_host, NAMELEN);
355 	spcl.c_level = level - '0';
356 	spcl.c_type = TS_TAPE;
357 	if (!Tflag)
358 	        getdumptime();		/* /etc/dumpdates snarfed */
359 
360 	msg("Date of this level %c dump: %s", level,
361 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
362  	msg("Date of last level %c dump: %s", lastlevel,
363 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
364 	msg("Dumping ");
365 	if (dt != NULL && dirlist != 0)
366 		msgtail("a subset of ");
367 	msgtail("%s (%s) ", disk, spcl.c_filesys);
368 	if (host)
369 		msgtail("to %s on host %s\n", tape, host);
370 	else
371 		msgtail("to %s\n", tape);
372 
373 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
374 		msg("Cannot open %s\n", disk);
375 		exit(X_ABORT);
376 	}
377 	sync();
378 	sblock = (struct fs *)sblock_buf;
379 	bread(SBOFF, (char *) sblock, SBSIZE);
380 	if (sblock->fs_magic != FS_MAGIC)
381 		quit("bad sblock magic number\n");
382 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
383 	dev_bshift = ffs(dev_bsize) - 1;
384 	if (dev_bsize != (1 << dev_bshift))
385 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
386 	tp_bshift = ffs(TP_BSIZE) - 1;
387 	if (TP_BSIZE != (1 << tp_bshift))
388 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
389 #ifdef FS_44INODEFMT
390 	if (sblock->fs_inodefmt >= FS_44INODEFMT)
391 		spcl.c_flags |= DR_NEWINODEFMT;
392 #endif
393 	maxino = sblock->fs_ipg * sblock->fs_ncg;
394 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
395 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
396 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
397 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
398 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
399 
400 	nonodump = spcl.c_level < honorlevel;
401 
402 	(void)signal(SIGINFO, statussig);
403 
404 	msg("mapping (Pass I) [regular files]\n");
405 	anydirskipped = mapfiles(maxino, &tapesize, toplevel,
406 	    (dirlist ? argv : NULL));
407 
408 	msg("mapping (Pass II) [directories]\n");
409 	while (anydirskipped) {
410 		anydirskipped = mapdirs(maxino, &tapesize);
411 	}
412 
413 	if (pipeout) {
414 		tapesize += 10;	/* 10 trailer blocks */
415 		msg("estimated %ld tape blocks.\n", tapesize);
416 	} else {
417 		double fetapes;
418 
419 		if (blocksperfile)
420 			fetapes = (double) tapesize / blocksperfile;
421 		else if (cartridge) {
422 			/* Estimate number of tapes, assuming streaming stops at
423 			   the end of each block written, and not in mid-block.
424 			   Assume no erroneous blocks; this can be compensated
425 			   for with an artificially low tape size. */
426 			fetapes =
427 			(	  tapesize	/* blocks */
428 				* TP_BSIZE	/* bytes/block */
429 				* (1.0/density)	/* 0.1" / byte */
430 			  +
431 				  tapesize	/* blocks */
432 				* (1.0/ntrec)	/* streaming-stops per block */
433 				* 15.48		/* 0.1" / streaming-stop */
434 			) * (1.0 / tsize );	/* tape / 0.1" */
435 		} else {
436 			/* Estimate number of tapes, for old fashioned 9-track
437 			   tape */
438 			int tenthsperirg = (density == 625) ? 3 : 7;
439 			fetapes =
440 			(	  tapesize	/* blocks */
441 				* TP_BSIZE	/* bytes / block */
442 				* (1.0/density)	/* 0.1" / byte */
443 			  +
444 				  tapesize	/* blocks */
445 				* (1.0/ntrec)	/* IRG's / block */
446 				* tenthsperirg	/* 0.1" / IRG */
447 			) * (1.0 / tsize );	/* tape / 0.1" */
448 		}
449 		etapes = fetapes;		/* truncating assignment */
450 		etapes++;
451 		/* count the dumped inodes map on each additional tape */
452 		tapesize += (etapes - 1) *
453 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
454 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
455 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
456 		    tapesize, fetapes);
457 	}
458 	/*
459 	 * If the user only wants an estimate of the number of
460 	 * tapes, exit now.
461 	 */
462 	if (just_estimate)
463 		exit(0);
464 
465 	/*
466 	 * Allocate tape buffer.
467 	 */
468 	if (!alloctape())
469 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
470 
471 	startnewtape(1);
472 	(void)time((time_t *)&(tstart_writing));
473 	xferrate = 0;
474 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
475 
476 	msg("dumping (Pass III) [directories]\n");
477 	dirty = 0;		/* XXX just to get gcc to shut up */
478 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
479 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
480 			dirty = *map++;
481 		else
482 			dirty >>= 1;
483 		if ((dirty & 1) == 0)
484 			continue;
485 		/*
486 		 * Skip directory inodes deleted and maybe reallocated
487 		 */
488 		dp = getino(ino);
489 		if ((dp->di_mode & IFMT) != IFDIR)
490 			continue;
491 		(void)dumpino(dp, ino);
492 	}
493 
494 	msg("dumping (Pass IV) [regular files]\n");
495 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
496 		int mode;
497 
498 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
499 			dirty = *map++;
500 		else
501 			dirty >>= 1;
502 		if ((dirty & 1) == 0)
503 			continue;
504 		/*
505 		 * Skip inodes deleted and reallocated as directories.
506 		 */
507 		dp = getino(ino);
508 		mode = dp->di_mode & IFMT;
509 		if (mode == IFDIR)
510 			continue;
511 		(void)dumpino(dp, ino);
512 	}
513 
514 	spcl.c_type = TS_END;
515 	for (i = 0; i < ntrec; i++)
516 		writeheader(maxino - 1);
517 	if (pipeout)
518 		msg("%ld tape blocks\n",spcl.c_tapea);
519 	else
520 		msg("%ld tape blocks on %d volume%s\n",
521 		    spcl.c_tapea, spcl.c_volume,
522 		    (spcl.c_volume == 1) ? "" : "s");
523 	tnow = do_stats();
524 	msg("Date of this level %c dump: %s", level,
525 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
526 	msg("Date this dump completed:  %s", ctime(&tnow));
527 	msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
528 	putdumptime();
529 	trewind();
530 	broadcast("DUMP IS DONE!\7\7\n");
531 	msg("DUMP IS DONE\n");
532 	Exit(X_FINOK);
533 	/* NOTREACHED */
534 	exit(X_FINOK);		/* XXX: to satisfy gcc */
535 }
536 
537 static void
538 usage()
539 {
540 
541 	(void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n            [-h level] [-s feet] [-T date] filesystem\n");
542 	(void)fprintf(stderr, "       dump [-W | -w]\n");
543 	exit(1);
544 }
545 
546 /*
547  * Pick up a numeric argument.  It must be nonnegative and in the given
548  * range (except that a vmax of 0 means unlimited).
549  */
550 static long
551 numarg(meaning, vmin, vmax)
552 	char *meaning;
553 	long vmin, vmax;
554 {
555 	char *p;
556 	long val;
557 
558 	val = strtol(optarg, &p, 10);
559 	if (*p)
560 		errx(1, "illegal %s -- %s", meaning, optarg);
561 	if (val < vmin || (vmax && val > vmax))
562 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
563 	return (val);
564 }
565 
566 void
567 sig(signo)
568 	int signo;
569 {
570 	switch(signo) {
571 	case SIGALRM:
572 	case SIGBUS:
573 	case SIGFPE:
574 	case SIGHUP:
575 	case SIGTERM:
576 	case SIGTRAP:
577 		if (pipeout)
578 			quit("Signal on pipe: cannot recover\n");
579 		msg("Rewriting attempted as response to unknown signal.\n");
580 		(void)fflush(stderr);
581 		(void)fflush(stdout);
582 		close_rewind();
583 		exit(X_REWRITE);
584 		/* NOTREACHED */
585 	case SIGSEGV:
586 		msg("SIGSEGV: ABORTING!\n");
587 		(void)signal(SIGSEGV, SIG_DFL);
588 		(void)kill(0, SIGSEGV);
589 		/* NOTREACHED */
590 	}
591 }
592 
593 char *
594 rawname(cp)
595 	char *cp;
596 {
597 	static char rawbuf[MAXPATHLEN];
598 	char *dp = strrchr(cp, '/');
599 
600 	if (dp == NULL)
601 		return (NULL);
602 	*dp = '\0';
603 	(void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
604 	*dp = '/';
605 	return (rawbuf);
606 }
607 
608 /*
609  * obsolete --
610  *	Change set of key letters and ordered arguments into something
611  *	getopt(3) will like.
612  */
613 static void
614 obsolete(argcp, argvp)
615 	int *argcp;
616 	char **argvp[];
617 {
618 	int argc, flags;
619 	char *ap, **argv, *flagsp, **nargv, *p;
620 
621 	/* Setup. */
622 	argv = *argvp;
623 	argc = *argcp;
624 
625 	/* Return if no arguments or first argument has leading dash. */
626 	ap = argv[1];
627 	if (argc == 1 || *ap == '-')
628 		return;
629 
630 	/* Allocate space for new arguments. */
631 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
632 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
633 		err(1, "malloc");
634 
635 	*nargv++ = *argv;
636 	argv += 2;
637 
638 	for (flags = 0; *ap; ++ap) {
639 		switch (*ap) {
640 		case 'B':
641 		case 'b':
642 		case 'd':
643 		case 'f':
644 		case 'h':
645 		case 's':
646 		case 'T':
647 			if (*argv == NULL) {
648 				warnx("option requires an argument -- %c", *ap);
649 				usage();
650 			}
651 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
652 				err(1, "malloc");
653 			nargv[0][0] = '-';
654 			nargv[0][1] = *ap;
655 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
656 			++argv;
657 			++nargv;
658 			break;
659 		default:
660 			if (!flags) {
661 				*p++ = '-';
662 				flags = 1;
663 			}
664 			*p++ = *ap;
665 			break;
666 		}
667 	}
668 
669 	/* Terminate flags. */
670 	if (flags) {
671 		*p = '\0';
672 		*nargv++ = flagsp;
673 	}
674 
675 	/* Copy remaining arguments. */
676 	while ((*nargv++ = *argv++) != NULL)
677 		;
678 
679 	/* Update argument count. */
680 	*argcp = nargv - *argvp - 1;
681 }
682