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