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