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