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