xref: /openbsd-src/sbin/restore/interactive.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: interactive.c,v 1.12 2001/07/07 18:26:19 deraadt 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)interactive.c	8.3 (Berkeley) 9/13/94";
40 #else
41 static char rcsid[] = "$OpenBSD: interactive.c,v 1.12 2001/07/07 18:26:19 deraadt 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/ffs/fs.h>
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ufs/dir.h>
52 #include <protocols/dumprestore.h>
53 
54 #include <setjmp.h>
55 #include <glob.h>
56 #include <stdio.h>
57 #include <errno.h>
58 #include <stdlib.h>
59 #include <string.h>
60 
61 #include "restore.h"
62 #include "extern.h"
63 
64 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
65 
66 /*
67  * Things to handle interruptions.
68  */
69 static int runshell;
70 static jmp_buf reset;
71 static char *nextarg = NULL;
72 
73 /*
74  * Structure and routines associated with listing directories.
75  */
76 struct afile {
77 	ino_t	fnum;		/* inode number of file */
78 	char	*fname;		/* file name */
79 	short	len;		/* name length */
80 	char	prefix;		/* prefix character */
81 	char	postfix;	/* postfix character */
82 };
83 struct arglist {
84 	int	freeglob;	/* glob structure needs to be freed */
85 	int	argcnt;		/* next globbed argument to return */
86 	glob_t	glob;		/* globbing information */
87 	char	*cmd;		/* the current command */
88 };
89 
90 static char	*copynext __P((char *, char *));
91 static int	 fcmp __P((const void *, const void *));
92 static void	 formatf __P((struct afile *, int));
93 static void	 getcmd __P((char *, char *, char *, struct arglist *));
94 struct dirent	*glob_readdir __P((RST_DIR *dirp));
95 static int	 glob_stat __P((const char *, struct stat *));
96 static void	 mkentry __P((char *, struct direct *, struct afile *));
97 static void	 printlist __P((char *, char *));
98 
99 /*
100  * Read and execute commands from the terminal.
101  */
102 void
103 runcmdshell()
104 {
105 	register struct entry *np;
106 	ino_t ino;
107 	struct arglist arglist;
108 	char curdir[MAXPATHLEN];
109 	char name[MAXPATHLEN];
110 	char cmd[BUFSIZ];
111 
112 	arglist.freeglob = 0;
113 	arglist.argcnt = 0;
114 	arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
115 	arglist.glob.gl_opendir = (void *)rst_opendir;
116 	arglist.glob.gl_readdir = (void *)glob_readdir;
117 	arglist.glob.gl_closedir = (void *)rst_closedir;
118 	arglist.glob.gl_lstat = glob_stat;
119 	arglist.glob.gl_stat = glob_stat;
120 	canon("/", curdir);
121 loop:
122 	if (setjmp(reset) != 0) {
123 		if (arglist.freeglob != 0) {
124 			arglist.freeglob = 0;
125 			arglist.argcnt = 0;
126 			globfree(&arglist.glob);
127 		}
128 		nextarg = NULL;
129 		volno = 0;
130 	}
131 	runshell = 1;
132 	getcmd(curdir, cmd, name, &arglist);
133 	switch (cmd[0]) {
134 	/*
135 	 * Add elements to the extraction list.
136 	 */
137 	case 'a':
138 		if (strncmp(cmd, "add", strlen(cmd)) != 0)
139 			goto bad;
140 		ino = dirlookup(name);
141 		if (ino == 0)
142 			break;
143 		if (mflag)
144 			pathcheck(name);
145 		treescan(name, ino, addfile);
146 		break;
147 	/*
148 	 * Change working directory.
149 	 */
150 	case 'c':
151 		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
152 			goto bad;
153 		ino = dirlookup(name);
154 		if (ino == 0)
155 			break;
156 		if (inodetype(ino) == LEAF) {
157 			fprintf(stderr, "%s: not a directory\n", name);
158 			break;
159 		}
160 		(void)strlcpy(curdir, name, sizeof curdir);
161 		break;
162 	/*
163 	 * Delete elements from the extraction list.
164 	 */
165 	case 'd':
166 		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
167 			goto bad;
168 		np = lookupname(name);
169 		if (np == NULL || (np->e_flags & NEW) == 0) {
170 			fprintf(stderr, "%s: not on extraction list\n", name);
171 			break;
172 		}
173 		treescan(name, np->e_ino, deletefile);
174 		break;
175 	/*
176 	 * Extract the requested list.
177 	 */
178 	case 'e':
179 		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
180 			goto bad;
181 		createfiles();
182 		createlinks();
183 		setdirmodes(0);
184 		if (dflag)
185 			checkrestore();
186 		volno = 0;
187 		break;
188 	/*
189 	 * List available commands.
190 	 */
191 	case 'h':
192 		if (strncmp(cmd, "help", strlen(cmd)) != 0)
193 			goto bad;
194 	case '?':
195 		fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
196 			"Available commands are:\n",
197 			"\tls [arg] - list directory\n",
198 			"\tcd arg - change directory\n",
199 			"\tpwd - print current directory\n",
200 			"\tadd [arg] - add `arg' to list of",
201 			" files to be extracted\n",
202 			"\tdelete [arg] - delete `arg' from",
203 			" list of files to be extracted\n",
204 			"\textract - extract requested files\n",
205 			"\tsetmodes - set modes of requested directories\n",
206 			"\tquit - immediately exit program\n",
207 			"\twhat - list dump header information\n",
208 			"\tverbose - toggle verbose flag",
209 			" (useful with ``ls'')\n",
210 			"\thelp or `?' - print this list\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 	register 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)strcpy(cmd, "quit");
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);
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);
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 	register 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 	register 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)strlcat(canonname, rawname, MAXPATHLEN);
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 	register struct afile *fp, *list, *listp = NULL;
521 	register struct direct *dp;
522 	struct afile single;
523 	RST_DIR *dirp;
524 	int entries, len, namelen;
525 	char locname[MAXPATHLEN];
526 
527 	dp = pathsearch(name);
528 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
529 	    (!vflag && dp->d_ino == WINO))
530 		return;
531 	if ((dirp = rst_opendir(name)) == NULL) {
532 		entries = 1;
533 		list = &single;
534 		mkentry(name, dp, list);
535 		len = strlen(basename) + 1;
536 		if (strlen(name) - len > single.len) {
537 			freename(single.fname);
538 			single.fname = savename(&name[len]);
539 			single.len = strlen(single.fname);
540 		}
541 	} else {
542 		entries = 0;
543 		while ((dp = rst_readdir(dirp)))
544 			entries++;
545 		rst_closedir(dirp);
546 		list = (struct afile *)malloc(entries * sizeof(struct afile));
547 		if (list == NULL) {
548 			fprintf(stderr, "ls: out of memory\n");
549 			return;
550 		}
551 		if ((dirp = rst_opendir(name)) == NULL)
552 			panic("directory reopen failed\n");
553 		fprintf(stderr, "%s:\n", name);
554 		entries = 0;
555 		listp = list;
556 		namelen = snprintf(locname, sizeof(locname), "%s/", name);
557 		if (namelen >= sizeof(locname))
558 			namelen = sizeof(locname) - 1;
559 		while ((dp = rst_readdir(dirp))) {
560 			if (dp == NULL)
561 				break;
562 			if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
563 				continue;
564 			if (!vflag && (dp->d_ino == WINO ||
565 			     strcmp(dp->d_name, ".") == 0 ||
566 			     strcmp(dp->d_name, "..") == 0))
567 				continue;
568 			locname[namelen] = '\0';
569 			if (namelen + dp->d_namlen >= MAXPATHLEN) {
570 				fprintf(stderr, "%s%s: name exceeds %d char\n",
571 					locname, dp->d_name, MAXPATHLEN);
572 			} else {
573 				(void)strncat(locname, dp->d_name,
574 				    (int)dp->d_namlen);
575 				mkentry(locname, dp, listp++);
576 				entries++;
577 			}
578 		}
579 		rst_closedir(dirp);
580 		if (entries == 0) {
581 			fprintf(stderr, "\n");
582 			free(list);
583 			return;
584 		}
585 		qsort((char *)list, entries, sizeof(struct afile), fcmp);
586 	}
587 	formatf(list, entries);
588 	if (dirp != NULL) {
589 		for (fp = listp - 1; fp >= list; fp--)
590 			freename(fp->fname);
591 		fprintf(stderr, "\n");
592 		free(list);
593 	}
594 }
595 
596 /*
597  * Read the contents of a directory.
598  */
599 static void
600 mkentry(name, dp, fp)
601 	char *name;
602 	struct direct *dp;
603 	register struct afile *fp;
604 {
605 	char *cp;
606 	struct entry *np;
607 
608 	fp->fnum = dp->d_ino;
609 	fp->fname = savename(dp->d_name);
610 	for (cp = fp->fname; *cp; cp++)
611 		if (!vflag && (*cp < ' ' || *cp >= 0177))
612 			*cp = '?';
613 	fp->len = cp - fp->fname;
614 	if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
615 		fp->prefix = '^';
616 	else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
617 		fp->prefix = '*';
618 	else
619 		fp->prefix = ' ';
620 	switch(dp->d_type) {
621 
622 	default:
623 		fprintf(stderr, "Warning: undefined file type %d\n",
624 		    dp->d_type);
625 		/* fall through */
626 	case DT_REG:
627 		fp->postfix = ' ';
628 		break;
629 
630 	case DT_LNK:
631 		fp->postfix = '@';
632 		break;
633 
634 	case DT_FIFO:
635 	case DT_SOCK:
636 		fp->postfix = '=';
637 		break;
638 
639 	case DT_CHR:
640 	case DT_BLK:
641 		fp->postfix = '#';
642 		break;
643 
644 	case DT_WHT:
645 		fp->postfix = '%';
646 		break;
647 
648 	case DT_UNKNOWN:
649 	case DT_DIR:
650 		if (inodetype(dp->d_ino) == NODE)
651 			fp->postfix = '/';
652 		else
653 			fp->postfix = ' ';
654 		break;
655 	}
656 	return;
657 }
658 
659 /*
660  * Print out a pretty listing of a directory
661  */
662 static void
663 formatf(list, nentry)
664 	register struct afile *list;
665 	int nentry;
666 {
667 	register struct afile *fp, *endlist;
668 	int width, bigino, haveprefix, havepostfix;
669 	int i, j, w, precision = 0, columns, lines;
670 
671 	width = 0;
672 	haveprefix = 0;
673 	havepostfix = 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 	memcpy(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 	register 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 	register 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 	int save_errno = errno;
796 
797 	if (command == 'i' && runshell)
798 		longjmp(reset, 1);	/* XXX signal/longjmp reentrancy */
799 	if (reply("restore interrupted, continue") == FAIL)	/* XXX signal race */
800 		_exit(1);
801 	errno = save_errno;
802 }
803