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