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