xref: /openbsd-src/sbin/restore/interactive.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: interactive.c,v 1.25 2007/09/25 09:51:48 chl 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.25 2007/09/25 09:51:48 chl 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(void)
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(char *curdir, char *cmd, size_t cmdlen, char *name, size_t namelen,
309        struct arglist *ap)
310 {
311 	char *cp;
312 	static char input[BUFSIZ];
313 	char output[BUFSIZ];
314 #	define rawname input	/* save space by reusing input buffer */
315 	int globretval;
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 		(void)fprintf(stderr, "%s > ", __progname);
329 		(void)fflush(stderr);
330 		if (fgets(input, sizeof input, terminal) == NULL) {
331 			(void)strlcpy(cmd, "quit", cmdlen);
332 			return;
333 		}
334 	} while (input[0] == '\n' || input[0] == '\0');
335 	for (cp = &input[strlen(input) - 1];
336 	     cp >= input && (*cp == ' ' || *cp == '\t' || *cp == '\n'); 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)strlcpy(name, curdir, MAXPATHLEN);
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, namelen);
366 	} else {
367 		/*
368 		 * For relative pathnames, prepend the current directory to
369 		 * it then canonicalize and return it.
370 		 */
371 		snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
372 		canon(output, name, namelen);
373 	}
374 	if ((globretval = glob(name, GLOB_ALTDIRFUNC | GLOB_NOESCAPE,
375 	    NULL, &ap->glob)) < 0) {
376 		fprintf(stderr, "%s: %s: ", ap->cmd, name);
377 		switch (globretval) {
378 		case GLOB_NOSPACE:
379 			fprintf(stderr, "out of memory\n");
380 			break;
381 		case GLOB_NOMATCH:
382 			fprintf(stderr, "no filename match.\n");
383 			break;
384 		case GLOB_ABORTED:
385 			fprintf(stderr, "glob() aborted.\n");
386 			break;
387 		default:
388 			fprintf(stderr, "unknown error!\n");
389 			break;
390 		}
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 	strlcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt],
400 	    MAXPATHLEN);
401 	if (--ap->argcnt == 0) {
402 		ap->freeglob = 0;
403 		globfree(&ap->glob);
404 	}
405 #	undef rawname
406 }
407 
408 /*
409  * Strip off the next token of the input.
410  */
411 static char *
412 copynext(char *input, char *output)
413 {
414 	char *cp, *bp;
415 	char quote;
416 
417 	for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
418 		/* skip to argument */;
419 	bp = output;
420 	while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
421 		/*
422 		 * Handle back slashes.
423 		 */
424 		if (*cp == '\\') {
425 			if (*++cp == '\0') {
426 				fprintf(stderr,
427 					"command lines cannot be continued\n");
428 				continue;
429 			}
430 			*bp++ = *cp++;
431 			continue;
432 		}
433 		/*
434 		 * The usual unquoted case.
435 		 */
436 		if (*cp != '\'' && *cp != '"') {
437 			*bp++ = *cp++;
438 			continue;
439 		}
440 		/*
441 		 * Handle single and double quotes.
442 		 */
443 		quote = *cp++;
444 		while (*cp != quote && *cp != '\0')
445 			*bp++ = *cp++;
446 		if (*cp++ == '\0') {
447 			fprintf(stderr, "missing %c\n", quote);
448 			cp--;
449 			continue;
450 		}
451 	}
452 	*bp = '\0';
453 	return (cp);
454 }
455 
456 /*
457  * Canonicalize file names to always start with ``./'' and
458  * remove any imbedded "." and ".." components.
459  */
460 void
461 canon(char *rawname, char *canonname, size_t canonnamelen)
462 {
463 	char *cp, *np;
464 
465 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
466 		(void)strlcpy(canonname, "", canonnamelen);
467 	else if (rawname[0] == '/')
468 		(void)strlcpy(canonname, ".", canonnamelen);
469 	else
470 		(void)strlcpy(canonname, "./", canonnamelen);
471 	(void)strlcat(canonname, rawname, canonnamelen);
472 	/*
473 	 * Eliminate multiple and trailing '/'s
474 	 */
475 	for (cp = np = canonname; *np != '\0'; cp++) {
476 		*cp = *np++;
477 		while (*cp == '/' && *np == '/')
478 			np++;
479 	}
480 	*cp = '\0';
481 	if (*--cp == '/')
482 		*cp = '\0';
483 	/*
484 	 * Eliminate extraneous "." and ".." from pathnames.
485 	 */
486 	for (np = canonname; *np != '\0'; ) {
487 		np++;
488 		cp = np;
489 		while (*np != '/' && *np != '\0')
490 			np++;
491 		if (np - cp == 1 && *cp == '.') {
492 			cp--;
493 			(void)strlcpy(cp, np, canonname + canonnamelen - cp);
494 			np = cp;
495 		}
496 		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
497 			cp--;
498 			while (cp > &canonname[1] && *--cp != '/')
499 				/* find beginning of name */;
500 			(void)strlcpy(cp, np, canonname + canonnamelen - cp);
501 			np = cp;
502 		}
503 	}
504 }
505 
506 /*
507  * Do an "ls" style listing of a directory
508  */
509 static void
510 printlist(char *name, char *basename)
511 {
512 	struct afile *fp, *list, *listp = NULL;
513 	struct direct *dp;
514 	struct afile single;
515 	RST_DIR *dirp;
516 	size_t namelen;
517 	int entries, len;
518 	char locname[MAXPATHLEN];
519 
520 	dp = pathsearch(name);
521 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0))
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)))
536 			entries++;
537 		rst_closedir(dirp);
538 		list = (struct afile *)calloc(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 		namelen = strlcpy(locname, name, sizeof(locname));
549 		if (namelen >= sizeof(locname) - 1)
550 			namelen = sizeof(locname) - 2;
551 		locname[namelen++] = '/';
552 		locname[namelen] = '\0';
553 		while ((dp = rst_readdir(dirp))) {
554 			if (dp == NULL)
555 				break;
556 			if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
557 				continue;
558 			if (!vflag && (strcmp(dp->d_name, ".") == 0 ||
559 			     strcmp(dp->d_name, "..") == 0))
560 				continue;
561 			locname[namelen] = '\0';
562 			if (namelen + dp->d_namlen >= MAXPATHLEN) {
563 				fprintf(stderr, "%s%s: name exceeds %d char\n",
564 					locname, dp->d_name, MAXPATHLEN);
565 			} else {
566 				(void)strncat(locname, dp->d_name,
567 				    (int)dp->d_namlen);
568 				mkentry(locname, dp, listp++);
569 				entries++;
570 			}
571 		}
572 		rst_closedir(dirp);
573 		if (entries == 0) {
574 			fprintf(stderr, "\n");
575 			free(list);
576 			return;
577 		}
578 		qsort((char *)list, entries, sizeof(struct afile), fcmp);
579 	}
580 	formatf(list, entries);
581 	if (dirp != NULL) {
582 		for (fp = listp - 1; fp >= list; fp--)
583 			freename(fp->fname);
584 		fprintf(stderr, "\n");
585 		free(list);
586 	}
587 }
588 
589 /*
590  * Read the contents of a directory.
591  */
592 static void
593 mkentry(char *name, struct direct *dp, struct afile *fp)
594 {
595 	char *cp;
596 	struct entry *np;
597 
598 	fp->fnum = dp->d_ino;
599 	fp->fname = savename(dp->d_name);
600 	for (cp = fp->fname; *cp; cp++)
601 		if (!vflag && (*cp < ' ' || *cp >= 0177))
602 			*cp = '?';
603 	fp->len = cp - fp->fname;
604 	if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
605 		fp->prefix = '^';
606 	else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
607 		fp->prefix = '*';
608 	else
609 		fp->prefix = ' ';
610 	switch(dp->d_type) {
611 
612 	default:
613 		fprintf(stderr, "Warning: undefined file type %d\n",
614 		    dp->d_type);
615 		/* fall through */
616 	case DT_REG:
617 		fp->postfix = ' ';
618 		break;
619 
620 	case DT_LNK:
621 		fp->postfix = '@';
622 		break;
623 
624 	case DT_FIFO:
625 	case DT_SOCK:
626 		fp->postfix = '=';
627 		break;
628 
629 	case DT_CHR:
630 	case DT_BLK:
631 		fp->postfix = '#';
632 		break;
633 
634 	case DT_UNKNOWN:
635 	case DT_DIR:
636 		if (inodetype(dp->d_ino) == NODE)
637 			fp->postfix = '/';
638 		else
639 			fp->postfix = ' ';
640 		break;
641 	}
642 	return;
643 }
644 
645 /*
646  * Print out a pretty listing of a directory
647  */
648 static void
649 formatf(struct afile *list, int nentry)
650 {
651 	struct afile *fp, *endlist;
652 	int width, bigino, haveprefix, havepostfix;
653 	int i, j, w, precision = 0, columns, lines;
654 
655 	width = 0;
656 	haveprefix = 0;
657 	havepostfix = 0;
658 	bigino = ROOTINO;
659 	endlist = &list[nentry];
660 	for (fp = &list[0]; fp < endlist; fp++) {
661 		if (bigino < fp->fnum)
662 			bigino = fp->fnum;
663 		if (width < fp->len)
664 			width = fp->len;
665 		if (fp->prefix != ' ')
666 			haveprefix = 1;
667 		if (fp->postfix != ' ')
668 			havepostfix = 1;
669 	}
670 	if (haveprefix)
671 		width++;
672 	if (havepostfix)
673 		width++;
674 	if (vflag) {
675 		for (precision = 0, i = bigino; i > 0; i /= 10)
676 			precision++;
677 		width += precision + 1;
678 	}
679 	width++;
680 	columns = 81 / width;
681 	if (columns == 0)
682 		columns = 1;
683 	lines = (nentry + columns - 1) / columns;
684 	for (i = 0; i < lines; i++) {
685 		for (j = 0; j < columns; j++) {
686 			fp = &list[j * lines + i];
687 			if (vflag) {
688 				fprintf(stderr, "%*d ", precision, fp->fnum);
689 				fp->len += precision + 1;
690 			}
691 			if (haveprefix) {
692 				putc(fp->prefix, stderr);
693 				fp->len++;
694 			}
695 			fprintf(stderr, "%s", fp->fname);
696 			if (havepostfix) {
697 				putc(fp->postfix, stderr);
698 				fp->len++;
699 			}
700 			if (fp + lines >= endlist) {
701 				fprintf(stderr, "\n");
702 				break;
703 			}
704 			for (w = fp->len; w < width; w++)
705 				putc(' ', stderr);
706 		}
707 	}
708 }
709 
710 /*
711  * Skip over directory entries that are not on the tape
712  *
713  * First have to get definition of a dirent.
714  */
715 #undef DIRBLKSIZ
716 #include <dirent.h>
717 #undef d_ino
718 
719 struct dirent *
720 glob_readdir(RST_DIR *dirp)
721 {
722 	struct direct *dp;
723 	static struct dirent adirent;
724 
725 	while ((dp = rst_readdir(dirp)) != NULL) {
726 		if (dflag || TSTINO(dp->d_ino, dumpmap))
727 			break;
728 	}
729 	if (dp == NULL)
730 		return (NULL);
731 	adirent.d_fileno = dp->d_ino;
732 	adirent.d_namlen = dp->d_namlen;
733 	memcpy(adirent.d_name, dp->d_name, dp->d_namlen + 1);
734 	return (&adirent);
735 }
736 
737 /*
738  * Return st_mode information in response to stat or lstat calls
739  */
740 static int
741 glob_stat(const char *name, struct stat *stp)
742 {
743 	struct direct *dp;
744 
745 	dp = pathsearch(name);
746 	if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0))
747 		return (-1);
748 	if (inodetype(dp->d_ino) == NODE)
749 		stp->st_mode = S_IFDIR;
750 	else
751 		stp->st_mode = S_IFREG;
752 	return (0);
753 }
754 
755 /*
756  * Comparison routine for qsort.
757  */
758 static int
759 fcmp(const void *f1, const void *f2)
760 {
761 	return (strcmp(((struct afile *)f1)->fname,
762 	    ((struct afile *)f2)->fname));
763 }
764 
765 /*
766  * respond to interrupts
767  */
768 void
769 onintr(int signo)
770 {
771 	int save_errno = errno;
772 
773 	if (command == 'i' && runshell)
774 		longjmp(reset, 1);	/* XXX signal/longjmp reentrancy */
775 	if (reply("restore interrupted, continue") == FAIL)	/* XXX signal race */
776 		_exit(1);
777 	errno = save_errno;
778 }
779