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