Home | History | Annotate | Line # | Download | only in restore
interactive.c revision 1.21
      1 /*	$NetBSD: interactive.c,v 1.21 2005/02/17 15:00:33 xtraeme Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1993
      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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)interactive.c	8.5 (Berkeley) 5/1/95";
     36 #else
     37 __RCSID("$NetBSD: interactive.c,v 1.21 2005/02/17 15:00:33 xtraeme Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 #include <sys/stat.h>
     44 
     45 #include <ufs/ufs/dinode.h>
     46 #include <ufs/ufs/dir.h>
     47 #include <ufs/ffs/fs.h>
     48 #include <protocols/dumprestore.h>
     49 
     50 #include <setjmp.h>
     51 #include <glob.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 
     56 #include "restore.h"
     57 #include "extern.h"
     58 
     59 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
     60 
     61 /*
     62  * Things to handle interruptions.
     63  */
     64 static int runshell;
     65 static jmp_buf reset;
     66 static char *nextarg = NULL;
     67 
     68 /*
     69  * Structure and routines associated with listing directories.
     70  */
     71 struct afile {
     72 	ino_t	fnum;		/* inode number of file */
     73 	char	*fname;		/* file name */
     74 	short	len;		/* name length */
     75 	char	prefix;		/* prefix character */
     76 	char	postfix;	/* postfix character */
     77 };
     78 struct arglist {
     79 	int	freeglob;	/* glob structure needs to be freed */
     80 	int	argcnt;		/* next globbed argument to return */
     81 	glob_t	glob;		/* globbing information */
     82 	char	*cmd;		/* the current command */
     83 };
     84 
     85 static char	*copynext(char *, char *);
     86 static int	 fcmp(const void *, const void *);
     87 static void	 formatf(struct afile *, int);
     88 static void	 getcmd(char *, char *, char *, struct arglist *);
     89 struct dirent	*glob_readdir(RST_DIR *dirp);
     90 static int	 glob_stat(const char *, struct stat *);
     91 static void	 mkentry(char *, struct direct *, struct afile *);
     92 static void	 printlist(char *, char *);
     93 
     94 /*
     95  * Read and execute commands from the terminal.
     96  */
     97 void
     98 runcmdshell(void)
     99 {
    100 	struct entry *np;
    101 	ino_t ino;
    102 	struct arglist arglist;
    103 	char curdir[MAXPATHLEN];
    104 	char name[MAXPATHLEN];
    105 	char cmd[BUFSIZ];
    106 
    107 	arglist.freeglob = 0;
    108 	arglist.argcnt = 0;
    109 	arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
    110 	arglist.glob.gl_opendir = (void *)rst_opendir;
    111 	arglist.glob.gl_readdir = (void *)glob_readdir;
    112 	arglist.glob.gl_closedir = (void *)rst_closedir;
    113 	arglist.glob.gl_lstat = glob_stat;
    114 	arglist.glob.gl_stat = glob_stat;
    115 	canon("/", curdir);
    116 loop:
    117 	if (setjmp(reset) != 0) {
    118 		if (arglist.freeglob != 0) {
    119 			arglist.freeglob = 0;
    120 			arglist.argcnt = 0;
    121 			globfree(&arglist.glob);
    122 		}
    123 		nextarg = NULL;
    124 		volno = 0;
    125 	}
    126 	runshell = 1;
    127 	getcmd(curdir, cmd, name, &arglist);
    128 	switch (cmd[0]) {
    129 	/*
    130 	 * Add elements to the extraction list.
    131 	 */
    132 	case 'a':
    133 		if (strncmp(cmd, "add", strlen(cmd)) != 0)
    134 			goto bad;
    135 		ino = dirlookup(name);
    136 		if (ino == 0)
    137 			break;
    138 		if (ino == ROOTINO)
    139 			dotflag = 1;
    140 		if (mflag)
    141 			pathcheck(name);
    142 		treescan(name, ino, addfile);
    143 		break;
    144 	/*
    145 	 * Change working directory.
    146 	 */
    147 	case 'c':
    148 		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
    149 			goto bad;
    150 		ino = dirlookup(name);
    151 		if (ino == 0)
    152 			break;
    153 		if (inodetype(ino) == LEAF) {
    154 			fprintf(stderr, "%s: not a directory\n", name);
    155 			break;
    156 		}
    157 		(void) strcpy(curdir, name);
    158 		break;
    159 	/*
    160 	 * Delete elements from the extraction list.
    161 	 */
    162 	case 'd':
    163 		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
    164 			goto bad;
    165 		np = lookupname(name);
    166 		if (np == NULL || (np->e_flags & NEW) == 0) {
    167 			fprintf(stderr, "%s: not on extraction list\n", name);
    168 			break;
    169 		}
    170 		treescan(name, np->e_ino, deletefile);
    171 		break;
    172 	/*
    173 	 * Extract the requested list.
    174 	 */
    175 	case 'e':
    176 		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
    177 			goto bad;
    178 		createfiles();
    179 		createlinks();
    180 		setdirmodes(0);
    181 		if (dflag)
    182 			checkrestore();
    183 		volno = 0;
    184 		break;
    185 	/*
    186 	 * List available commands.
    187 	 */
    188 	case 'h':
    189 		if (strncmp(cmd, "help", strlen(cmd)) != 0)
    190 			goto bad;
    191 	case '?':
    192 		fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
    193 			"Available commands are:\n",
    194 			"\tls [arg] - list directory\n",
    195 			"\tcd arg - change directory\n",
    196 			"\tpwd - print current directory\n",
    197 			"\tadd [arg] - add `arg' to list of",
    198 			" files to be extracted\n",
    199 			"\tdelete [arg] - delete `arg' from",
    200 			" list of files to be extracted\n",
    201 			"\textract - extract requested files\n",
    202 			"\tsetmodes - set modes of requested directories\n",
    203 			"\tquit or xit - immediately exit program\n",
    204 			"\twhat - list dump header information\n",
    205 			"\tverbose - toggle verbose flag",
    206 			" (useful with ``ls'')\n",
    207 			"\thelp or `?' - print this list\n",
    208 			"\tDebug - turn on debugging\n",
    209 			"If no `arg' is supplied, the current",
    210 			" directory is used\n");
    211 		break;
    212 	/*
    213 	 * List a directory.
    214 	 */
    215 	case 'l':
    216 		if (strncmp(cmd, "ls", strlen(cmd)) != 0)
    217 			goto bad;
    218 		printlist(name, curdir);
    219 		break;
    220 	/*
    221 	 * Print current directory.
    222 	 */
    223 	case 'p':
    224 		if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
    225 			goto bad;
    226 		if (curdir[1] == '\0')
    227 			fprintf(stderr, "/\n");
    228 		else
    229 			fprintf(stderr, "%s\n", &curdir[1]);
    230 		break;
    231 	/*
    232 	 * Quit.
    233 	 */
    234 	case 'q':
    235 		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
    236 			goto bad;
    237 		return;
    238 	case 'x':
    239 		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
    240 			goto bad;
    241 		return;
    242 	/*
    243 	 * Toggle verbose mode.
    244 	 */
    245 	case 'v':
    246 		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
    247 			goto bad;
    248 		if (vflag) {
    249 			fprintf(stderr, "verbose mode off\n");
    250 			vflag = 0;
    251 			break;
    252 		}
    253 		fprintf(stderr, "verbose mode on\n");
    254 		vflag++;
    255 		break;
    256 	/*
    257 	 * Just restore requested directory modes.
    258 	 */
    259 	case 's':
    260 		if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
    261 			goto bad;
    262 		setdirmodes(FORCE);
    263 		break;
    264 	/*
    265 	 * Print out dump header information.
    266 	 */
    267 	case 'w':
    268 		if (strncmp(cmd, "what", strlen(cmd)) != 0)
    269 			goto bad;
    270 		printdumpinfo();
    271 		break;
    272 	/*
    273 	 * Turn on debugging.
    274 	 */
    275 	case 'D':
    276 		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
    277 			goto bad;
    278 		if (dflag) {
    279 			fprintf(stderr, "debugging mode off\n");
    280 			dflag = 0;
    281 			break;
    282 		}
    283 		fprintf(stderr, "debugging mode on\n");
    284 		dflag++;
    285 		break;
    286 	/*
    287 	 * Unknown command.
    288 	 */
    289 	default:
    290 	bad:
    291 		fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
    292 		break;
    293 	}
    294 	goto loop;
    295 }
    296 
    297 /*
    298  * Read and parse an interactive command.
    299  * The first word on the line is assigned to "cmd". If
    300  * there are no arguments on the command line, then "curdir"
    301  * is returned as the argument. If there are arguments
    302  * on the line they are returned one at a time on each
    303  * successive call to getcmd. Each argument is first assigned
    304  * to "name". If it does not start with "/" the pathname in
    305  * "curdir" is prepended to it. Finally "canon" is called to
    306  * eliminate any embedded ".." components.
    307  */
    308 static void
    309 getcmd(char *curdir, char *cmd, char *name, struct arglist *ap)
    310 {
    311 	char *cp;
    312 	static char input[BUFSIZ];
    313 	char output[BUFSIZ];
    314 	int globretval;
    315 #	define rawname input	/* save space by reusing input buffer */
    316 
    317 	/*
    318 	 * Check to see if still processing arguments.
    319 	 */
    320 	if (ap->argcnt > 0)
    321 		goto retnext;
    322 	if (nextarg != NULL)
    323 		goto getnext;
    324 	/*
    325 	 * Read a command line and trim off trailing white space.
    326 	 */
    327 	do	{
    328 		fprintf(stderr, "%s > ", getprogname());
    329 		(void) fflush(stderr);
    330 		(void) fgets(input, BUFSIZ, terminal);
    331 	} while (!feof(terminal) && input[0] == '\n');
    332 	if (feof(terminal)) {
    333 		(void) strcpy(cmd, "quit");
    334 		return;
    335 	}
    336 	for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
    337 		/* trim off trailing white space and newline */;
    338 	*++cp = '\0';
    339 	/*
    340 	 * Copy the command into "cmd".
    341 	 */
    342 	cp = copynext(input, cmd);
    343 	ap->cmd = cmd;
    344 	/*
    345 	 * If no argument, use curdir as the default.
    346 	 */
    347 	if (*cp == '\0') {
    348 		(void) strcpy(name, curdir);
    349 		return;
    350 	}
    351 	nextarg = cp;
    352 	/*
    353 	 * Find the next argument.
    354 	 */
    355 getnext:
    356 	cp = copynext(nextarg, rawname);
    357 	if (*cp == '\0')
    358 		nextarg = NULL;
    359 	else
    360 		nextarg = cp;
    361 	/*
    362 	 * If it is an absolute pathname, canonicalize it and return it.
    363 	 */
    364 	if (rawname[0] == '/') {
    365 		canon(rawname, name);
    366 	} else {
    367 		/*
    368 		 * For relative pathnames, prepend the current directory to
    369 		 * it then canonicalize and return it.
    370 		 */
    371 		(void) strcpy(output, curdir);
    372 		(void) strcat(output, "/");
    373 		(void) strcat(output, rawname);
    374 		canon(output, name);
    375 	}
    376 	if ((globretval = glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob)) < 0) {
    377 		fprintf(stderr, "%s: %s: ", ap->cmd, name);
    378 		switch (globretval) {
    379 		case GLOB_NOSPACE:
    380 			fprintf(stderr, "out of memory\n");
    381 			break;
    382 		case GLOB_NOMATCH:
    383 			fprintf(stderr, "no filename match.\n");
    384 			break;
    385 		case GLOB_ABORTED:
    386 			fprintf(stderr, "glob() aborted.\n");
    387 			break;
    388 		default:
    389 			fprintf(stderr, "unknown error!\n");
    390 			break;
    391 		}
    392 	}
    393 	if (ap->glob.gl_pathc == 0)
    394 		return;
    395 	ap->freeglob = 1;
    396 	ap->argcnt = ap->glob.gl_pathc;
    397 
    398 retnext:
    399 	strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
    400 	if (--ap->argcnt == 0) {
    401 		ap->freeglob = 0;
    402 		globfree(&ap->glob);
    403 	}
    404 #	undef rawname
    405 }
    406 
    407 /*
    408  * Strip off the next token of the input.
    409  */
    410 static char *
    411 copynext(char *input, char *output)
    412 {
    413 	char *cp, *bp;
    414 	char quote;
    415 
    416 	for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
    417 		/* skip to argument */;
    418 	bp = output;
    419 	while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
    420 		/*
    421 		 * Handle back slashes.
    422 		 */
    423 		if (*cp == '\\') {
    424 			if (*++cp == '\0') {
    425 				fprintf(stderr,
    426 					"command lines cannot be continued\n");
    427 				continue;
    428 			}
    429 			*bp++ = *cp++;
    430 			continue;
    431 		}
    432 		/*
    433 		 * The usual unquoted case.
    434 		 */
    435 		if (*cp != '\'' && *cp != '"') {
    436 			*bp++ = *cp++;
    437 			continue;
    438 		}
    439 		/*
    440 		 * Handle single and double quotes.
    441 		 */
    442 		quote = *cp++;
    443 		while (*cp != quote && *cp != '\0')
    444 			*bp++ = *cp++;
    445 		if (*cp++ == '\0') {
    446 			fprintf(stderr, "missing %c\n", quote);
    447 			cp--;
    448 			continue;
    449 		}
    450 	}
    451 	*bp = '\0';
    452 	return (cp);
    453 }
    454 
    455 /*
    456  * Canonicalize file names to always start with ``./'' and
    457  * remove any imbedded "." and ".." components.
    458  */
    459 void
    460 canon(char *rawname, char *canonname)
    461 {
    462 	char *cp, *np;
    463 
    464 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
    465 		(void) strcpy(canonname, "");
    466 	else if (rawname[0] == '/')
    467 		(void) strcpy(canonname, ".");
    468 	else
    469 		(void) strcpy(canonname, "./");
    470 	(void) strcat(canonname, rawname);
    471 	/*
    472 	 * Eliminate multiple and trailing '/'s
    473 	 */
    474 	for (cp = np = canonname; *np != '\0'; cp++) {
    475 		*cp = *np++;
    476 		while (*cp == '/' && *np == '/')
    477 			np++;
    478 	}
    479 	*cp = '\0';
    480 	if (*--cp == '/')
    481 		*cp = '\0';
    482 	/*
    483 	 * Eliminate extraneous "." and ".." from pathnames.
    484 	 */
    485 	for (np = canonname; *np != '\0'; ) {
    486 		np++;
    487 		cp = np;
    488 		while (*np != '/' && *np != '\0')
    489 			np++;
    490 		if (np - cp == 1 && *cp == '.') {
    491 			cp--;
    492 			(void) strcpy(cp, np);
    493 			np = cp;
    494 		}
    495 		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
    496 			cp--;
    497 			while (cp > &canonname[1] && *--cp != '/')
    498 				/* find beginning of name */;
    499 			(void) strcpy(cp, np);
    500 			np = cp;
    501 		}
    502 	}
    503 }
    504 
    505 /*
    506  * Do an "ls" style listing of a directory
    507  */
    508 static void
    509 printlist(char *name, char *basename)
    510 {
    511 	struct afile *fp, *list, *listp;
    512 	struct direct *dp;
    513 	struct afile single;
    514 	RST_DIR *dirp;
    515 	int entries, len, namelen;
    516 	char locname[MAXPATHLEN + 1];
    517 
    518 	dp = pathsearch(name);
    519 	listp = NULL;
    520 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
    521 	    (!vflag && dp->d_ino == WINO))
    522 		return;
    523 	if ((dirp = rst_opendir(name)) == NULL) {
    524 		entries = 1;
    525 		list = &single;
    526 		mkentry(name, dp, list);
    527 		len = strlen(basename) + 1;
    528 		if (strlen(name) - len > single.len) {
    529 			freename(single.fname);
    530 			single.fname = savename(&name[len]);
    531 			single.len = strlen(single.fname);
    532 		}
    533 	} else {
    534 		entries = 0;
    535 		while ((dp = rst_readdir(dirp)) != NULL)
    536 			entries++;
    537 		rst_closedir(dirp);
    538 		list = (struct afile *)malloc(entries * sizeof(struct afile));
    539 		if (list == NULL) {
    540 			fprintf(stderr, "ls: out of memory\n");
    541 			return;
    542 		}
    543 		if ((dirp = rst_opendir(name)) == NULL)
    544 			panic("directory reopen failed\n");
    545 		fprintf(stderr, "%s:\n", name);
    546 		entries = 0;
    547 		listp = list;
    548 		(void) strncpy(locname, name, MAXPATHLEN);
    549 		(void) strncat(locname, "/", MAXPATHLEN);
    550 		namelen = strlen(locname);
    551 		while ((dp = rst_readdir(dirp)) != NULL) {
    552 			if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
    553 				continue;
    554 			if (!vflag && (dp->d_ino == WINO ||
    555 			     strcmp(dp->d_name, ".") == 0 ||
    556 			     strcmp(dp->d_name, "..") == 0))
    557 				continue;
    558 			locname[namelen] = '\0';
    559 			if (namelen + dp->d_namlen >= MAXPATHLEN) {
    560 				fprintf(stderr, "%s%s: name exceeds %d char\n",
    561 					locname, dp->d_name, MAXPATHLEN);
    562 			} else {
    563 				(void) strncat(locname, dp->d_name,
    564 				    (int)dp->d_namlen);
    565 				mkentry(locname, dp, listp++);
    566 				entries++;
    567 			}
    568 		}
    569 		rst_closedir(dirp);
    570 		if (entries == 0) {
    571 			fprintf(stderr, "\n");
    572 			free(list);
    573 			return;
    574 		}
    575 		qsort((char *)list, entries, sizeof(struct afile), fcmp);
    576 	}
    577 	formatf(list, entries);
    578 	if (dirp != NULL) {
    579 		for (fp = listp - 1; fp >= list; fp--)
    580 			freename(fp->fname);
    581 		fprintf(stderr, "\n");
    582 		free(list);
    583 	}
    584 }
    585 
    586 /*
    587  * Read the contents of a directory.
    588  */
    589 static void
    590 mkentry(char *name, struct direct *dp, struct afile *fp)
    591 {
    592 	char *cp;
    593 	struct entry *np;
    594 
    595 	fp->fnum = dp->d_ino;
    596 	fp->fname = savename(dp->d_name);
    597 	for (cp = fp->fname; *cp; cp++)
    598 		if (!vflag && (*cp < ' ' || *cp >= 0177))
    599 			*cp = '?';
    600 	fp->len = cp - fp->fname;
    601 	if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
    602 		fp->prefix = '^';
    603 	else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
    604 		fp->prefix = '*';
    605 	else
    606 		fp->prefix = ' ';
    607 	switch(dp->d_type) {
    608 
    609 	default:
    610 		fprintf(stderr, "Warning: undefined file type %d\n",
    611 		    dp->d_type);
    612 		/* fall through */
    613 	case DT_REG:
    614 		fp->postfix = ' ';
    615 		break;
    616 
    617 	case DT_LNK:
    618 		fp->postfix = '@';
    619 		break;
    620 
    621 	case DT_FIFO:
    622 	case DT_SOCK:
    623 		fp->postfix = '=';
    624 		break;
    625 
    626 	case DT_CHR:
    627 	case DT_BLK:
    628 		fp->postfix = '#';
    629 		break;
    630 
    631 	case DT_WHT:
    632 		fp->postfix = '%';
    633 		break;
    634 
    635 	case DT_UNKNOWN:
    636 	case DT_DIR:
    637 		if (inodetype(dp->d_ino) == NODE)
    638 			fp->postfix = '/';
    639 		else
    640 			fp->postfix = ' ';
    641 		break;
    642 	}
    643 	return;
    644 }
    645 
    646 /*
    647  * Print out a pretty listing of a directory
    648  */
    649 static void
    650 formatf(struct afile *list, int nentry)
    651 {
    652 	struct afile *fp, *endlist;
    653 	int width, bigino, haveprefix, havepostfix;
    654 	int i, j, w, precision, columns, lines;
    655 
    656 	width = 0;
    657 	haveprefix = 0;
    658 	havepostfix = 0;
    659 	precision = 0;
    660 	bigino = ROOTINO;
    661 	endlist = &list[nentry];
    662 	for (fp = &list[0]; fp < endlist; fp++) {
    663 		if (bigino < fp->fnum)
    664 			bigino = fp->fnum;
    665 		if (width < fp->len)
    666 			width = fp->len;
    667 		if (fp->prefix != ' ')
    668 			haveprefix = 1;
    669 		if (fp->postfix != ' ')
    670 			havepostfix = 1;
    671 	}
    672 	if (haveprefix)
    673 		width++;
    674 	if (havepostfix)
    675 		width++;
    676 	if (vflag) {
    677 		for (precision = 0, i = bigino; i > 0; i /= 10)
    678 			precision++;
    679 		width += precision + 1;
    680 	}
    681 	width++;
    682 	columns = 81 / width;
    683 	if (columns == 0)
    684 		columns = 1;
    685 	lines = (nentry + columns - 1) / columns;
    686 	for (i = 0; i < lines; i++) {
    687 		for (j = 0; j < columns; j++) {
    688 			fp = &list[j * lines + i];
    689 			if (vflag) {
    690 				fprintf(stderr, "%*d ", precision, fp->fnum);
    691 				fp->len += precision + 1;
    692 			}
    693 			if (haveprefix) {
    694 				putc(fp->prefix, stderr);
    695 				fp->len++;
    696 			}
    697 			fprintf(stderr, "%s", fp->fname);
    698 			if (havepostfix) {
    699 				putc(fp->postfix, stderr);
    700 				fp->len++;
    701 			}
    702 			if (fp + lines >= endlist) {
    703 				fprintf(stderr, "\n");
    704 				break;
    705 			}
    706 			for (w = fp->len; w < width; w++)
    707 				putc(' ', stderr);
    708 		}
    709 	}
    710 }
    711 
    712 /*
    713  * Skip over directory entries that are not on the tape
    714  *
    715  * First have to get definition of a dirent.
    716  */
    717 #undef DIRBLKSIZ
    718 #include <dirent.h>
    719 #undef d_ino
    720 
    721 struct dirent *
    722 glob_readdir(RST_DIR *dirp)
    723 {
    724 	struct direct *dp;
    725 	static struct dirent adirent;
    726 
    727 	while ((dp = rst_readdir(dirp)) != NULL) {
    728 		if (!vflag && dp->d_ino == WINO)
    729 			continue;
    730 		if (dflag || TSTINO(dp->d_ino, dumpmap))
    731 			break;
    732 	}
    733 	if (dp == NULL)
    734 		return (NULL);
    735 	adirent.d_fileno = dp->d_ino;
    736 	adirent.d_namlen = dp->d_namlen;
    737 	memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
    738 	return (&adirent);
    739 }
    740 
    741 /*
    742  * Return st_mode information in response to stat or lstat calls
    743  */
    744 static int
    745 glob_stat(const char *name, struct stat *stp)
    746 {
    747 	struct direct *dp;
    748 
    749 	dp = pathsearch(name);
    750 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
    751 	    (!vflag && dp->d_ino == WINO))
    752 		return (-1);
    753 	if (inodetype(dp->d_ino) == NODE)
    754 		stp->st_mode = S_IFDIR;
    755 	else
    756 		stp->st_mode = S_IFREG;
    757 	return (0);
    758 }
    759 
    760 /*
    761  * Comparison routine for qsort.
    762  */
    763 static int
    764 fcmp(const void *f1, const void *f2)
    765 {
    766 	return (strcmp(((struct afile *)f1)->fname,
    767 	    ((struct afile *)f2)->fname));
    768 }
    769 
    770 /*
    771  * respond to interrupts
    772  */
    773 void
    774 onintr(int signo)
    775 {
    776 	if (command == 'i' && runshell)
    777 		longjmp(reset, 1);
    778 	if (reply("restore interrupted, continue") == FAIL)
    779 		exit(1);
    780 }
    781