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