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