xref: /openbsd/sbin/restore/interactive.c (revision db3296cf)
1 /*	$OpenBSD: interactive.c,v 1.19 2003/07/28 06:13:26 tedu Exp $	*/
2 /*	$NetBSD: interactive.c,v 1.10 1997/03/19 08:42:52 lukem Exp $	*/
3 
4 /*
5  * Copyright (c) 1985, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)interactive.c	8.3 (Berkeley) 9/13/94";
36 #else
37 static const char rcsid[] = "$OpenBSD: interactive.c,v 1.19 2003/07/28 06:13:26 tedu 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/ffs/fs.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <protocols/dumprestore.h>
49 
50 #include <setjmp.h>
51 #include <glob.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "restore.h"
59 #include "extern.h"
60 
61 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
62 
63 /*
64  * Things to handle interruptions.
65  */
66 static int runshell;
67 static jmp_buf reset;
68 static char *nextarg = NULL;
69 
70 /*
71  * Structure and routines associated with listing directories.
72  */
73 struct afile {
74 	ino_t	fnum;		/* inode number of file */
75 	char	*fname;		/* file name */
76 	short	len;		/* name length */
77 	char	prefix;		/* prefix character */
78 	char	postfix;	/* postfix character */
79 };
80 struct arglist {
81 	int	freeglob;	/* glob structure needs to be freed */
82 	int	argcnt;		/* next globbed argument to return */
83 	glob_t	glob;		/* globbing information */
84 	char	*cmd;		/* the current command */
85 };
86 
87 static char	*copynext(char *, char *);
88 static int	 fcmp(const void *, const void *);
89 static void	 formatf(struct afile *, int);
90 static void	 getcmd(char *, char *, size_t, char *, size_t, struct arglist *);
91 struct dirent	*glob_readdir(RST_DIR *dirp);
92 static int	 glob_stat(const char *, struct stat *);
93 static void	 mkentry(char *, struct direct *, struct afile *);
94 static void	 printlist(char *, char *);
95 
96 /*
97  * Read and execute commands from the terminal.
98  */
99 void
100 runcmdshell()
101 {
102 	struct entry *np;
103 	ino_t ino;
104 	struct arglist arglist;
105 	char curdir[MAXPATHLEN];
106 	char name[MAXPATHLEN];
107 	char cmd[BUFSIZ];
108 
109 	arglist.freeglob = 0;
110 	arglist.argcnt = 0;
111 	arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
112 	arglist.glob.gl_opendir = (void *)rst_opendir;
113 	arglist.glob.gl_readdir = (void *)glob_readdir;
114 	arglist.glob.gl_closedir = (void *)rst_closedir;
115 	arglist.glob.gl_lstat = glob_stat;
116 	arglist.glob.gl_stat = glob_stat;
117 	canon("/", curdir, sizeof curdir);
118 loop:
119 	if (setjmp(reset) != 0) {
120 		if (arglist.freeglob != 0) {
121 			arglist.freeglob = 0;
122 			arglist.argcnt = 0;
123 			globfree(&arglist.glob);
124 		}
125 		nextarg = NULL;
126 		volno = 0;
127 	}
128 	runshell = 1;
129 	getcmd(curdir, cmd, sizeof cmd, name, sizeof name, &arglist);
130 	switch (cmd[0]) {
131 	/*
132 	 * Add elements to the extraction list.
133 	 */
134 	case 'a':
135 		if (strncmp(cmd, "add", strlen(cmd)) != 0)
136 			goto bad;
137 		ino = dirlookup(name);
138 		if (ino == 0)
139 			break;
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)strlcpy(curdir, name, sizeof curdir);
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",
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 - 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 			"If no `arg' is supplied, the current",
209 			" directory is used\n");
210 		break;
211 	/*
212 	 * List a directory.
213 	 */
214 	case 'l':
215 		if (strncmp(cmd, "ls", strlen(cmd)) != 0)
216 			goto bad;
217 		printlist(name, curdir);
218 		break;
219 	/*
220 	 * Print current directory.
221 	 */
222 	case 'p':
223 		if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
224 			goto bad;
225 		if (curdir[1] == '\0')
226 			fprintf(stderr, "/\n");
227 		else
228 			fprintf(stderr, "%s\n", &curdir[1]);
229 		break;
230 	/*
231 	 * Quit.
232 	 */
233 	case 'q':
234 		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
235 			goto bad;
236 		return;
237 	case 'x':
238 		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
239 			goto bad;
240 		return;
241 	/*
242 	 * Toggle verbose mode.
243 	 */
244 	case 'v':
245 		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
246 			goto bad;
247 		if (vflag) {
248 			fprintf(stderr, "verbose mode off\n");
249 			vflag = 0;
250 			break;
251 		}
252 		fprintf(stderr, "verbose mode on\n");
253 		vflag++;
254 		break;
255 	/*
256 	 * Just restore requested directory modes.
257 	 */
258 	case 's':
259 		if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
260 			goto bad;
261 		setdirmodes(FORCE);
262 		break;
263 	/*
264 	 * Print out dump header information.
265 	 */
266 	case 'w':
267 		if (strncmp(cmd, "what", strlen(cmd)) != 0)
268 			goto bad;
269 		printdumpinfo();
270 		break;
271 	/*
272 	 * Turn on debugging.
273 	 */
274 	case 'D':
275 		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
276 			goto bad;
277 		if (dflag) {
278 			fprintf(stderr, "debugging mode off\n");
279 			dflag = 0;
280 			break;
281 		}
282 		fprintf(stderr, "debugging mode on\n");
283 		dflag++;
284 		break;
285 	/*
286 	 * Unknown command.
287 	 */
288 	default:
289 	bad:
290 		fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
291 		break;
292 	}
293 	goto loop;
294 }
295 
296 /*
297  * Read and parse an interactive command.
298  * The first word on the line is assigned to "cmd". If
299  * there are no arguments on the command line, then "curdir"
300  * is returned as the argument. If there are arguments
301  * on the line they are returned one at a time on each
302  * successive call to getcmd. Each argument is first assigned
303  * to "name". If it does not start with "/" the pathname in
304  * "curdir" is prepended to it. Finally "canon" is called to
305  * eliminate any embedded ".." components.
306  */
307 static void
308 getcmd(curdir, cmd, cmdlen, name, namelen, ap)
309 	char *curdir, *cmd;
310 	size_t cmdlen;
311 	char *name;
312 	size_t namelen;
313 	struct arglist *ap;
314 {
315 	char *cp;
316 	static char input[BUFSIZ];
317 	char output[BUFSIZ];
318 #	define rawname input	/* save space by reusing input buffer */
319 	int globretval;
320 
321 	/*
322 	 * Check to see if still processing arguments.
323 	 */
324 	if (ap->argcnt > 0)
325 		goto retnext;
326 	if (nextarg != NULL)
327 		goto getnext;
328 	/*
329 	 * Read a command line and trim off trailing white space.
330 	 */
331 	do {
332 		(void)fprintf(stderr, "%s > ", __progname);
333 		(void)fflush(stderr);
334 		(void)fgets(input, BUFSIZ, terminal);
335 	} while (!feof(terminal) && input[0] == '\n');
336 	if (feof(terminal)) {
337 		(void)strlcpy(cmd, "quit", cmdlen);
338 		return;
339 	}
340 	for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
341 		/* trim off trailing white space and newline */;
342 	*++cp = '\0';
343 	/*
344 	 * Copy the command into "cmd".
345 	 */
346 	cp = copynext(input, cmd);
347 	ap->cmd = cmd;
348 	/*
349 	 * If no argument, use curdir as the default.
350 	 */
351 	if (*cp == '\0') {
352 		(void)strlcpy(name, curdir, MAXPATHLEN);
353 		return;
354 	}
355 	nextarg = cp;
356 	/*
357 	 * Find the next argument.
358 	 */
359 getnext:
360 	cp = copynext(nextarg, rawname);
361 	if (*cp == '\0')
362 		nextarg = NULL;
363 	else
364 		nextarg = cp;
365 	/*
366 	 * If it is an absolute pathname, canonicalize it and return it.
367 	 */
368 	if (rawname[0] == '/') {
369 		canon(rawname, name, namelen);
370 	} else {
371 		/*
372 		 * For relative pathnames, prepend the current directory to
373 		 * it then canonicalize and return it.
374 		 */
375 		snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
376 		canon(output, name, namelen);
377 	}
378 	if ((globretval = glob(name, GLOB_ALTDIRFUNC | GLOB_NOESCAPE,
379 	    NULL, &ap->glob)) < 0) {
380 		fprintf(stderr, "%s: %s: ", ap->cmd, name);
381 		switch (globretval) {
382 		case GLOB_NOSPACE:
383 			fprintf(stderr, "out of memory\n");
384 			break;
385 		case GLOB_NOMATCH:
386 			fprintf(stderr, "no filename match.\n");
387 			break;
388 		case GLOB_ABORTED:
389 			fprintf(stderr, "glob() aborted.\n");
390 			break;
391 		default:
392 			fprintf(stderr, "unknown error!\n");
393 			break;
394 		}
395 	}
396 
397 	if (ap->glob.gl_pathc == 0)
398 		return;
399 	ap->freeglob = 1;
400 	ap->argcnt = ap->glob.gl_pathc;
401 
402 retnext:
403 	strlcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt],
404 	    MAXPATHLEN);
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, canonnamelen)
467 	char *rawname, *canonname;
468 	size_t canonnamelen;
469 {
470 	char *cp, *np;
471 
472 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
473 		(void)strlcpy(canonname, "", canonnamelen);
474 	else if (rawname[0] == '/')
475 		(void)strlcpy(canonname, ".", canonnamelen);
476 	else
477 		(void)strlcpy(canonname, "./", canonnamelen);
478 	(void)strlcat(canonname, rawname, canonnamelen);
479 	/*
480 	 * Eliminate multiple and trailing '/'s
481 	 */
482 	for (cp = np = canonname; *np != '\0'; cp++) {
483 		*cp = *np++;
484 		while (*cp == '/' && *np == '/')
485 			np++;
486 	}
487 	*cp = '\0';
488 	if (*--cp == '/')
489 		*cp = '\0';
490 	/*
491 	 * Eliminate extraneous "." and ".." from pathnames.
492 	 */
493 	for (np = canonname; *np != '\0'; ) {
494 		np++;
495 		cp = np;
496 		while (*np != '/' && *np != '\0')
497 			np++;
498 		if (np - cp == 1 && *cp == '.') {
499 			cp--;
500 			(void)strlcpy(cp, np, canonname + canonnamelen - cp);
501 			np = cp;
502 		}
503 		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
504 			cp--;
505 			while (cp > &canonname[1] && *--cp != '/')
506 				/* find beginning of name */;
507 			(void)strlcpy(cp, np, canonname + canonnamelen - cp);
508 			np = cp;
509 		}
510 	}
511 }
512 
513 /*
514  * Do an "ls" style listing of a directory
515  */
516 static void
517 printlist(name, basename)
518 	char *name;
519 	char *basename;
520 {
521 	struct afile *fp, *list, *listp = NULL;
522 	struct direct *dp;
523 	struct afile single;
524 	RST_DIR *dirp;
525 	int entries, len, namelen;
526 	char locname[MAXPATHLEN];
527 
528 	dp = pathsearch(name);
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)))
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 		namelen = snprintf(locname, sizeof(locname), "%s/", name);
558 		if (namelen >= sizeof(locname))
559 			namelen = sizeof(locname) - 1;
560 		while ((dp = rst_readdir(dirp))) {
561 			if (dp == NULL)
562 				break;
563 			if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
564 				continue;
565 			if (!vflag && (dp->d_ino == WINO ||
566 			     strcmp(dp->d_name, ".") == 0 ||
567 			     strcmp(dp->d_name, "..") == 0))
568 				continue;
569 			locname[namelen] = '\0';
570 			if (namelen + dp->d_namlen >= MAXPATHLEN) {
571 				fprintf(stderr, "%s%s: name exceeds %d char\n",
572 					locname, dp->d_name, MAXPATHLEN);
573 			} else {
574 				(void)strncat(locname, dp->d_name,
575 				    (int)dp->d_namlen);
576 				mkentry(locname, dp, listp++);
577 				entries++;
578 			}
579 		}
580 		rst_closedir(dirp);
581 		if (entries == 0) {
582 			fprintf(stderr, "\n");
583 			free(list);
584 			return;
585 		}
586 		qsort((char *)list, entries, sizeof(struct afile), fcmp);
587 	}
588 	formatf(list, entries);
589 	if (dirp != NULL) {
590 		for (fp = listp - 1; fp >= list; fp--)
591 			freename(fp->fname);
592 		fprintf(stderr, "\n");
593 		free(list);
594 	}
595 }
596 
597 /*
598  * Read the contents of a directory.
599  */
600 static void
601 mkentry(name, dp, fp)
602 	char *name;
603 	struct direct *dp;
604 	struct afile *fp;
605 {
606 	char *cp;
607 	struct entry *np;
608 
609 	fp->fnum = dp->d_ino;
610 	fp->fname = savename(dp->d_name);
611 	for (cp = fp->fname; *cp; cp++)
612 		if (!vflag && (*cp < ' ' || *cp >= 0177))
613 			*cp = '?';
614 	fp->len = cp - fp->fname;
615 	if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
616 		fp->prefix = '^';
617 	else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
618 		fp->prefix = '*';
619 	else
620 		fp->prefix = ' ';
621 	switch(dp->d_type) {
622 
623 	default:
624 		fprintf(stderr, "Warning: undefined file type %d\n",
625 		    dp->d_type);
626 		/* fall through */
627 	case DT_REG:
628 		fp->postfix = ' ';
629 		break;
630 
631 	case DT_LNK:
632 		fp->postfix = '@';
633 		break;
634 
635 	case DT_FIFO:
636 	case DT_SOCK:
637 		fp->postfix = '=';
638 		break;
639 
640 	case DT_CHR:
641 	case DT_BLK:
642 		fp->postfix = '#';
643 		break;
644 
645 	case DT_WHT:
646 		fp->postfix = '%';
647 		break;
648 
649 	case DT_UNKNOWN:
650 	case DT_DIR:
651 		if (inodetype(dp->d_ino) == NODE)
652 			fp->postfix = '/';
653 		else
654 			fp->postfix = ' ';
655 		break;
656 	}
657 	return;
658 }
659 
660 /*
661  * Print out a pretty listing of a directory
662  */
663 static void
664 formatf(list, nentry)
665 	struct afile *list;
666 	int nentry;
667 {
668 	struct afile *fp, *endlist;
669 	int width, bigino, haveprefix, havepostfix;
670 	int i, j, w, precision = 0, columns, lines;
671 
672 	width = 0;
673 	haveprefix = 0;
674 	havepostfix = 0;
675 	bigino = ROOTINO;
676 	endlist = &list[nentry];
677 	for (fp = &list[0]; fp < endlist; fp++) {
678 		if (bigino < fp->fnum)
679 			bigino = fp->fnum;
680 		if (width < fp->len)
681 			width = fp->len;
682 		if (fp->prefix != ' ')
683 			haveprefix = 1;
684 		if (fp->postfix != ' ')
685 			havepostfix = 1;
686 	}
687 	if (haveprefix)
688 		width++;
689 	if (havepostfix)
690 		width++;
691 	if (vflag) {
692 		for (precision = 0, i = bigino; i > 0; i /= 10)
693 			precision++;
694 		width += precision + 1;
695 	}
696 	width++;
697 	columns = 81 / width;
698 	if (columns == 0)
699 		columns = 1;
700 	lines = (nentry + columns - 1) / columns;
701 	for (i = 0; i < lines; i++) {
702 		for (j = 0; j < columns; j++) {
703 			fp = &list[j * lines + i];
704 			if (vflag) {
705 				fprintf(stderr, "%*d ", precision, fp->fnum);
706 				fp->len += precision + 1;
707 			}
708 			if (haveprefix) {
709 				putc(fp->prefix, stderr);
710 				fp->len++;
711 			}
712 			fprintf(stderr, "%s", fp->fname);
713 			if (havepostfix) {
714 				putc(fp->postfix, stderr);
715 				fp->len++;
716 			}
717 			if (fp + lines >= endlist) {
718 				fprintf(stderr, "\n");
719 				break;
720 			}
721 			for (w = fp->len; w < width; w++)
722 				putc(' ', stderr);
723 		}
724 	}
725 }
726 
727 /*
728  * Skip over directory entries that are not on the tape
729  *
730  * First have to get definition of a dirent.
731  */
732 #undef DIRBLKSIZ
733 #include <dirent.h>
734 #undef d_ino
735 
736 struct dirent *
737 glob_readdir(dirp)
738 	RST_DIR *dirp;
739 {
740 	struct direct *dp;
741 	static struct dirent adirent;
742 
743 	while ((dp = rst_readdir(dirp)) != NULL) {
744 		if (!vflag && dp->d_ino == WINO)
745 			continue;
746 		if (dflag || TSTINO(dp->d_ino, dumpmap))
747 			break;
748 	}
749 	if (dp == NULL)
750 		return (NULL);
751 	adirent.d_fileno = dp->d_ino;
752 	adirent.d_namlen = dp->d_namlen;
753 	memcpy(adirent.d_name, dp->d_name, dp->d_namlen + 1);
754 	return (&adirent);
755 }
756 
757 /*
758  * Return st_mode information in response to stat or lstat calls
759  */
760 static int
761 glob_stat(name, stp)
762 	const char *name;
763 	struct stat *stp;
764 {
765 	struct direct *dp;
766 
767 	dp = pathsearch(name);
768 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
769 	    (!vflag && dp->d_ino == WINO))
770 		return (-1);
771 	if (inodetype(dp->d_ino) == NODE)
772 		stp->st_mode = S_IFDIR;
773 	else
774 		stp->st_mode = S_IFREG;
775 	return (0);
776 }
777 
778 /*
779  * Comparison routine for qsort.
780  */
781 static int
782 fcmp(f1, f2)
783 	const void *f1, *f2;
784 {
785 	return (strcmp(((struct afile *)f1)->fname,
786 	    ((struct afile *)f2)->fname));
787 }
788 
789 /*
790  * respond to interrupts
791  */
792 void
793 onintr(signo)
794 	int signo;
795 {
796 	int save_errno = errno;
797 
798 	if (command == 'i' && runshell)
799 		longjmp(reset, 1);	/* XXX signal/longjmp reentrancy */
800 	if (reply("restore interrupted, continue") == FAIL)	/* XXX signal race */
801 		_exit(1);
802 	errno = save_errno;
803 }
804