Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.19
      1 /*	$NetBSD: main.c,v 1.19 1998/07/06 06:50:02 mrg 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.19 1998/07/06 06:50:02 mrg 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 	spcl.c_level = iswap32(level - '0');
    373 	spcl.c_type = iswap32(TS_TAPE);
    374 	spcl.c_date = iswap32(spcl.c_date);
    375 	spcl.c_ddate = iswap32(spcl.c_ddate);
    376 	if (!Tflag)
    377 	        getdumptime();		/* /etc/dumpdates snarfed */
    378 
    379 	date = iswap32(spcl.c_date);
    380 	msg("Date of this level %c dump: %s", level,
    381 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    382 	date = iswap32(spcl.c_ddate);
    383  	msg("Date of last level %c dump: %s", lastlevel,
    384 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
    385 	msg("Dumping ");
    386 	if (dt != NULL && dirlist != 0)
    387 		msgtail("a subset of ");
    388 	msgtail("%s (%s) ", disk, spcl.c_filesys);
    389 	if (host)
    390 		msgtail("to %s on host %s\n", tape, host);
    391 	else
    392 		msgtail("to %s\n", tape);
    393 
    394 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    395 	dev_bshift = ffs(dev_bsize) - 1;
    396 	if (dev_bsize != (1 << dev_bshift))
    397 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
    398 	tp_bshift = ffs(TP_BSIZE) - 1;
    399 	if (TP_BSIZE != (1 << tp_bshift))
    400 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    401 #ifdef FS_44INODEFMT
    402 	if (sblock->fs_inodefmt >= FS_44INODEFMT)
    403 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
    404 #endif
    405 	maxino = sblock->fs_ipg * sblock->fs_ncg;
    406 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    407 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    408 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
    409 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    410 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    411 
    412 	nonodump = iswap32(spcl.c_level) < honorlevel;
    413 
    414 	(void)signal(SIGINFO, statussig);
    415 
    416 	msg("mapping (Pass I) [regular files]\n");
    417 	anydirskipped = mapfiles(maxino, &tapesize, toplevel,
    418 	    (dirlist ? argv : NULL));
    419 
    420 	msg("mapping (Pass II) [directories]\n");
    421 	while (anydirskipped) {
    422 		anydirskipped = mapdirs(maxino, &tapesize);
    423 	}
    424 
    425 	if (pipeout) {
    426 		tapesize += 10;	/* 10 trailer blocks */
    427 		msg("estimated %ld tape blocks.\n", tapesize);
    428 	} else {
    429 		double fetapes;
    430 
    431 		if (blocksperfile)
    432 			fetapes = (double) tapesize / blocksperfile;
    433 		else if (cartridge) {
    434 			/* Estimate number of tapes, assuming streaming stops at
    435 			   the end of each block written, and not in mid-block.
    436 			   Assume no erroneous blocks; this can be compensated
    437 			   for with an artificially low tape size. */
    438 			fetapes =
    439 			(	  tapesize	/* blocks */
    440 				* TP_BSIZE	/* bytes/block */
    441 				* (1.0/density)	/* 0.1" / byte */
    442 			  +
    443 				  tapesize	/* blocks */
    444 				* (1.0/ntrec)	/* streaming-stops per block */
    445 				* 15.48		/* 0.1" / streaming-stop */
    446 			) * (1.0 / tsize );	/* tape / 0.1" */
    447 		} else {
    448 			/* Estimate number of tapes, for old fashioned 9-track
    449 			   tape */
    450 			int tenthsperirg = (density == 625) ? 3 : 7;
    451 			fetapes =
    452 			(	  tapesize	/* blocks */
    453 				* TP_BSIZE	/* bytes / block */
    454 				* (1.0/density)	/* 0.1" / byte */
    455 			  +
    456 				  tapesize	/* blocks */
    457 				* (1.0/ntrec)	/* IRG's / block */
    458 				* tenthsperirg	/* 0.1" / IRG */
    459 			) * (1.0 / tsize );	/* tape / 0.1" */
    460 		}
    461 		etapes = fetapes;		/* truncating assignment */
    462 		etapes++;
    463 		/* count the dumped inodes map on each additional tape */
    464 		tapesize += (etapes - 1) *
    465 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    466 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    467 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
    468 		    tapesize, fetapes);
    469 	}
    470 	/*
    471 	 * If the user only wants an estimate of the number of
    472 	 * tapes, exit now.
    473 	 */
    474 	if (just_estimate)
    475 		exit(0);
    476 
    477 	/*
    478 	 * Allocate tape buffer.
    479 	 */
    480 	if (!alloctape())
    481 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    482 
    483 	startnewtape(1);
    484 	(void)time((time_t *)&(tstart_writing));
    485 	xferrate = 0;
    486 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    487 
    488 	msg("dumping (Pass III) [directories]\n");
    489 	dirty = 0;		/* XXX just to get gcc to shut up */
    490 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    491 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    492 			dirty = *map++;
    493 		else
    494 			dirty >>= 1;
    495 		if ((dirty & 1) == 0)
    496 			continue;
    497 		/*
    498 		 * Skip directory inodes deleted and maybe reallocated
    499 		 */
    500 		dp = getino(ino);
    501 		if ((dp->di_mode & IFMT) != IFDIR)
    502 			continue;
    503 		(void)dumpino(dp, ino);
    504 	}
    505 
    506 	msg("dumping (Pass IV) [regular files]\n");
    507 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    508 		int mode;
    509 
    510 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    511 			dirty = *map++;
    512 		else
    513 			dirty >>= 1;
    514 		if ((dirty & 1) == 0)
    515 			continue;
    516 		/*
    517 		 * Skip inodes deleted and reallocated as directories.
    518 		 */
    519 		dp = getino(ino);
    520 		mode = dp->di_mode & IFMT;
    521 		if (mode == IFDIR)
    522 			continue;
    523 		(void)dumpino(dp, ino);
    524 	}
    525 
    526 	spcl.c_type = iswap32(TS_END);
    527 	for (i = 0; i < ntrec; i++)
    528 		writeheader(maxino - 1);
    529 	if (pipeout)
    530 		msg("%ld tape blocks\n",iswap32(spcl.c_tapea));
    531 	else
    532 		msg("%ld tape blocks on %d volume%s\n",
    533 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
    534 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
    535 	tnow = do_stats();
    536 	date = iswap32(spcl.c_date);
    537 	msg("Date of this level %c dump: %s", level,
    538 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    539 	msg("Date this dump completed:  %s", ctime(&tnow));
    540 	msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
    541 	putdumptime();
    542 	trewind();
    543 	broadcast("DUMP IS DONE!\7\7\n");
    544 	msg("DUMP IS DONE\n");
    545 	Exit(X_FINOK);
    546 	/* NOTREACHED */
    547 	exit(X_FINOK);		/* XXX: to satisfy gcc */
    548 }
    549 
    550 static void
    551 usage()
    552 {
    553 
    554 	(void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n            [-h level] [-s feet] [-T date] filesystem\n");
    555 	(void)fprintf(stderr, "       dump [-W | -w]\n");
    556 	exit(1);
    557 }
    558 
    559 /*
    560  * Pick up a numeric argument.  It must be nonnegative and in the given
    561  * range (except that a vmax of 0 means unlimited).
    562  */
    563 static long
    564 numarg(meaning, vmin, vmax)
    565 	char *meaning;
    566 	long vmin, vmax;
    567 {
    568 	char *p;
    569 	long val;
    570 
    571 	val = strtol(optarg, &p, 10);
    572 	if (*p)
    573 		errx(1, "illegal %s -- %s", meaning, optarg);
    574 	if (val < vmin || (vmax && val > vmax))
    575 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
    576 	return (val);
    577 }
    578 
    579 void
    580 sig(signo)
    581 	int signo;
    582 {
    583 	switch(signo) {
    584 	case SIGALRM:
    585 	case SIGBUS:
    586 	case SIGFPE:
    587 	case SIGHUP:
    588 	case SIGTERM:
    589 	case SIGTRAP:
    590 		if (pipeout)
    591 			quit("Signal on pipe: cannot recover\n");
    592 		msg("Rewriting attempted as response to unknown signal.\n");
    593 		(void)fflush(stderr);
    594 		(void)fflush(stdout);
    595 		close_rewind();
    596 		exit(X_REWRITE);
    597 		/* NOTREACHED */
    598 	case SIGSEGV:
    599 		msg("SIGSEGV: ABORTING!\n");
    600 		(void)signal(SIGSEGV, SIG_DFL);
    601 		(void)kill(0, SIGSEGV);
    602 		/* NOTREACHED */
    603 	}
    604 }
    605 
    606 char *
    607 rawname(cp)
    608 	char *cp;
    609 {
    610 	static char rawbuf[MAXPATHLEN];
    611 	char *dp = strrchr(cp, '/');
    612 
    613 	if (dp == NULL)
    614 		return (NULL);
    615 	*dp = '\0';
    616 	(void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
    617 	*dp = '/';
    618 	return (rawbuf);
    619 }
    620 
    621 /*
    622  * obsolete --
    623  *	Change set of key letters and ordered arguments into something
    624  *	getopt(3) will like.
    625  */
    626 static void
    627 obsolete(argcp, argvp)
    628 	int *argcp;
    629 	char **argvp[];
    630 {
    631 	int argc, flags;
    632 	char *ap, **argv, *flagsp, **nargv, *p;
    633 
    634 	/* Setup. */
    635 	argv = *argvp;
    636 	argc = *argcp;
    637 
    638 	/* Return if no arguments or first argument has leading dash. */
    639 	ap = argv[1];
    640 	if (argc == 1 || *ap == '-')
    641 		return;
    642 
    643 	/* Allocate space for new arguments. */
    644 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
    645 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
    646 		err(1, "malloc");
    647 
    648 	*nargv++ = *argv;
    649 	argv += 2;
    650 
    651 	for (flags = 0; *ap; ++ap) {
    652 		switch (*ap) {
    653 		case 'B':
    654 		case 'b':
    655 		case 'd':
    656 		case 'f':
    657 		case 'h':
    658 		case 's':
    659 		case 'T':
    660 			if (*argv == NULL) {
    661 				warnx("option requires an argument -- %c", *ap);
    662 				usage();
    663 			}
    664 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
    665 				err(1, "malloc");
    666 			nargv[0][0] = '-';
    667 			nargv[0][1] = *ap;
    668 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
    669 			++argv;
    670 			++nargv;
    671 			break;
    672 		default:
    673 			if (!flags) {
    674 				*p++ = '-';
    675 				flags = 1;
    676 			}
    677 			*p++ = *ap;
    678 			break;
    679 		}
    680 	}
    681 
    682 	/* Terminate flags. */
    683 	if (flags) {
    684 		*p = '\0';
    685 		*nargv++ = flagsp;
    686 	}
    687 
    688 	/* Copy remaining arguments. */
    689 	while ((*nargv++ = *argv++) != NULL)
    690 		;
    691 
    692 	/* Update argument count. */
    693 	*argcp = nargv - *argvp - 1;
    694 }
    695