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