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