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