]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
df9ae507 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999 |
1227625a SP |
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 | ||
df9ae507 SP |
41 | #ifndef lint |
42 | static const char rcsid[] = | |
43 | "$Id: interactive.c,v 1.6 1999/10/13 09:57:20 stelian Exp $"; | |
44 | #endif /* not lint */ | |
45 | ||
1227625a | 46 | #include <sys/param.h> |
1227625a SP |
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> | |
1227625a SP |
55 | #endif /* __linux__ */ |
56 | #include <protocols/dumprestore.h> | |
57 | ||
58 | #include <setjmp.h> | |
ddd2ef55 SP |
59 | #include <compaterr.h> |
60 | #include <errno.h> | |
61 | #include <compatglob.h> | |
1227625a SP |
62 | #include <stdio.h> |
63 | #include <stdlib.h> | |
64 | #include <string.h> | |
65 | ||
66 | #ifdef __linux__ | |
67 | #include <ext2fs/ext2fs.h> | |
ddd2ef55 | 68 | extern char * __progname; |
1227625a SP |
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 | ||
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)); | |
b45f51d6 | 103 | static void getcmd __P((char *, char *, char *, int, struct arglist *)); |
1227625a SP |
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 | |
ddd2ef55 | 113 | runcmdshell(void) |
1227625a SP |
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; | |
b45f51d6 | 130 | canon("/", curdir, sizeof(curdir)); |
1227625a SP |
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; | |
b45f51d6 | 142 | getcmd(curdir, cmd, name, sizeof(name), &arglist); |
1227625a SP |
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 | } | |
ddd2ef55 SP |
170 | (void) strncpy(curdir, name, sizeof(curdir)); |
171 | curdir[sizeof(curdir) - 1] = '\0'; | |
1227625a SP |
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", | |
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 | "\thelp or `?' - print this list\n", | |
222 | "If no `arg' is supplied, the current", | |
223 | " directory is used\n"); | |
224 | break; | |
225 | /* | |
226 | * List a directory. | |
227 | */ | |
228 | case 'l': | |
229 | if (strncmp(cmd, "ls", strlen(cmd)) != 0) | |
230 | goto bad; | |
231 | printlist(name, curdir); | |
232 | break; | |
233 | /* | |
234 | * Print current directory. | |
235 | */ | |
236 | case 'p': | |
237 | if (strncmp(cmd, "pwd", strlen(cmd)) != 0) | |
238 | goto bad; | |
239 | if (curdir[1] == '\0') | |
240 | fprintf(stderr, "/\n"); | |
241 | else | |
242 | fprintf(stderr, "%s\n", &curdir[1]); | |
243 | break; | |
244 | /* | |
245 | * Quit. | |
246 | */ | |
247 | case 'q': | |
248 | if (strncmp(cmd, "quit", strlen(cmd)) != 0) | |
249 | goto bad; | |
250 | return; | |
251 | case 'x': | |
252 | if (strncmp(cmd, "xit", strlen(cmd)) != 0) | |
253 | goto bad; | |
254 | return; | |
255 | /* | |
256 | * Toggle verbose mode. | |
257 | */ | |
258 | case 'v': | |
259 | if (strncmp(cmd, "verbose", strlen(cmd)) != 0) | |
260 | goto bad; | |
261 | if (vflag) { | |
262 | fprintf(stderr, "verbose mode off\n"); | |
263 | vflag = 0; | |
264 | break; | |
265 | } | |
266 | fprintf(stderr, "verbose mode on\n"); | |
267 | vflag++; | |
268 | break; | |
269 | /* | |
270 | * Just restore requested directory modes. | |
271 | */ | |
272 | case 's': | |
273 | if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) | |
274 | goto bad; | |
275 | setdirmodes(FORCE); | |
276 | break; | |
277 | /* | |
278 | * Print out dump header information. | |
279 | */ | |
280 | case 'w': | |
281 | if (strncmp(cmd, "what", strlen(cmd)) != 0) | |
282 | goto bad; | |
283 | printdumpinfo(); | |
284 | break; | |
285 | /* | |
286 | * Turn on debugging. | |
287 | */ | |
288 | case 'D': | |
289 | if (strncmp(cmd, "Debug", strlen(cmd)) != 0) | |
290 | goto bad; | |
291 | if (dflag) { | |
292 | fprintf(stderr, "debugging mode off\n"); | |
293 | dflag = 0; | |
294 | break; | |
295 | } | |
296 | fprintf(stderr, "debugging mode on\n"); | |
297 | dflag++; | |
298 | break; | |
299 | /* | |
300 | * Unknown command. | |
301 | */ | |
302 | default: | |
303 | bad: | |
304 | fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); | |
305 | break; | |
306 | } | |
307 | goto loop; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Read and parse an interactive command. | |
312 | * The first word on the line is assigned to "cmd". If | |
313 | * there are no arguments on the command line, then "curdir" | |
314 | * is returned as the argument. If there are arguments | |
315 | * on the line they are returned one at a time on each | |
316 | * successive call to getcmd. Each argument is first assigned | |
317 | * to "name". If it does not start with "/" the pathname in | |
318 | * "curdir" is prepended to it. Finally "canon" is called to | |
319 | * eliminate any embedded ".." components. | |
320 | */ | |
321 | static void | |
ddd2ef55 | 322 | getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap) |
1227625a SP |
323 | { |
324 | register char *cp; | |
325 | static char input[BUFSIZ]; | |
326 | char output[BUFSIZ]; | |
327 | # define rawname input /* save space by reusing input buffer */ | |
328 | ||
329 | /* | |
330 | * Check to see if still processing arguments. | |
331 | */ | |
332 | if (ap->argcnt > 0) | |
333 | goto retnext; | |
334 | if (nextarg != NULL) | |
335 | goto getnext; | |
336 | /* | |
337 | * Read a command line and trim off trailing white space. | |
338 | */ | |
339 | do { | |
ddd2ef55 | 340 | fprintf(stderr, "%s > ", __progname); |
1227625a SP |
341 | (void) fflush(stderr); |
342 | (void) fgets(input, BUFSIZ, terminal); | |
343 | } while (!feof(terminal) && input[0] == '\n'); | |
344 | if (feof(terminal)) { | |
345 | (void) strcpy(cmd, "quit"); | |
346 | return; | |
347 | } | |
348 | for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) | |
349 | /* trim off trailing white space and newline */; | |
350 | *++cp = '\0'; | |
351 | /* | |
352 | * Copy the command into "cmd". | |
353 | */ | |
354 | cp = copynext(input, cmd); | |
355 | ap->cmd = cmd; | |
356 | /* | |
357 | * If no argument, use curdir as the default. | |
358 | */ | |
359 | if (*cp == '\0') { | |
b45f51d6 SP |
360 | (void) strncpy(name, curdir, size); |
361 | name[size - 1] = '\0'; | |
1227625a SP |
362 | return; |
363 | } | |
364 | nextarg = cp; | |
365 | /* | |
366 | * Find the next argument. | |
367 | */ | |
368 | getnext: | |
369 | cp = copynext(nextarg, rawname); | |
370 | if (*cp == '\0') | |
371 | nextarg = NULL; | |
372 | else | |
373 | nextarg = cp; | |
374 | /* | |
375 | * If it is an absolute pathname, canonicalize it and return it. | |
376 | */ | |
377 | if (rawname[0] == '/') { | |
b45f51d6 | 378 | canon(rawname, name, size); |
1227625a SP |
379 | } else { |
380 | /* | |
381 | * For relative pathnames, prepend the current directory to | |
382 | * it then canonicalize and return it. | |
383 | */ | |
b45f51d6 SP |
384 | snprintf(output, sizeof(output), "%s/%s", curdir, rawname); |
385 | canon(output, name, size); | |
1227625a SP |
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: | |
b45f51d6 SP |
395 | strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size); |
396 | name[size - 1] = '\0'; | |
1227625a SP |
397 | if (--ap->argcnt == 0) { |
398 | ap->freeglob = 0; | |
399 | globfree(&ap->glob); | |
400 | } | |
401 | # undef rawname | |
402 | } | |
403 | ||
404 | /* | |
405 | * Strip off the next token of the input. | |
406 | */ | |
407 | static char * | |
ddd2ef55 | 408 | copynext(char *input, char *output) |
1227625a SP |
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') | |
b45f51d6 | 441 | *bp++ = *cp++ | 0200; |
1227625a SP |
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 | |
b45f51d6 | 454 | * remove any embedded "." and ".." components. |
1227625a SP |
455 | */ |
456 | void | |
ddd2ef55 | 457 | canon(char *rawname, char *canonname, int len) |
1227625a SP |
458 | { |
459 | register char *cp, *np; | |
460 | ||
461 | if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) | |
462 | (void) strcpy(canonname, ""); | |
463 | else if (rawname[0] == '/') | |
464 | (void) strcpy(canonname, "."); | |
465 | else | |
466 | (void) strcpy(canonname, "./"); | |
ddd2ef55 SP |
467 | if (strlen(canonname) + strlen(rawname) >= len) |
468 | errx(1, "canonname: not enough buffer space"); | |
b45f51d6 | 469 | |
1227625a SP |
470 | (void) strcat(canonname, rawname); |
471 | /* | |
472 | * Eliminate multiple and trailing '/'s | |
473 | */ | |
474 | for (cp = np = canonname; *np != '\0'; cp++) { | |
475 | *cp = *np++; | |
476 | while (*cp == '/' && *np == '/') | |
477 | np++; | |
478 | } | |
479 | *cp = '\0'; | |
480 | if (*--cp == '/') | |
481 | *cp = '\0'; | |
482 | /* | |
483 | * Eliminate extraneous "." and ".." from pathnames. | |
484 | */ | |
485 | for (np = canonname; *np != '\0'; ) { | |
486 | np++; | |
487 | cp = np; | |
488 | while (*np != '/' && *np != '\0') | |
489 | np++; | |
490 | if (np - cp == 1 && *cp == '.') { | |
491 | cp--; | |
492 | (void) strcpy(cp, np); | |
493 | np = cp; | |
494 | } | |
495 | if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { | |
496 | cp--; | |
497 | while (cp > &canonname[1] && *--cp != '/') | |
498 | /* find beginning of name */; | |
499 | (void) strcpy(cp, np); | |
500 | np = cp; | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
505 | /* | |
506 | * Do an "ls" style listing of a directory | |
507 | */ | |
508 | static void | |
ddd2ef55 | 509 | printlist(char *name, char *basename) |
1227625a | 510 | { |
ddd2ef55 | 511 | register struct afile *fp, *list, *listp = NULL; |
1227625a SP |
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; | |
b45f51d6 | 534 | while ((dp = rst_readdir(dirp))) |
1227625a SP |
535 | entries++; |
536 | rst_closedir(dirp); | |
537 | list = (struct afile *)malloc(entries * sizeof(struct afile)); | |
538 | if (list == NULL) { | |
539 | fprintf(stderr, "ls: out of memory\n"); | |
540 | return; | |
541 | } | |
542 | if ((dirp = rst_opendir(name)) == NULL) | |
543 | panic("directory reopen failed\n"); | |
544 | fprintf(stderr, "%s:\n", name); | |
545 | entries = 0; | |
546 | listp = list; | |
ddd2ef55 SP |
547 | namelen = snprintf(locname, sizeof(locname), "%s/", name); |
548 | if (namelen >= sizeof(locname)) | |
549 | namelen = sizeof(locname) - 1; | |
b45f51d6 | 550 | while ((dp = rst_readdir(dirp))) { |
1227625a SP |
551 | if (dp == NULL) |
552 | break; | |
553 | if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) | |
554 | continue; | |
555 | if (!vflag && (dp->d_ino == WINO || | |
556 | strcmp(dp->d_name, ".") == 0 || | |
557 | strcmp(dp->d_name, "..") == 0)) | |
558 | continue; | |
559 | locname[namelen] = '\0'; | |
8d4197bb | 560 | if (namelen + strlen(dp->d_name) >= MAXPATHLEN) { |
1227625a SP |
561 | fprintf(stderr, "%s%s: name exceeds %d char\n", |
562 | locname, dp->d_name, MAXPATHLEN); | |
563 | } else { | |
564 | (void) strncat(locname, dp->d_name, | |
8d4197bb | 565 | (int)strlen(dp->d_name)); |
1227625a SP |
566 | mkentry(locname, dp, listp++); |
567 | entries++; | |
568 | } | |
569 | } | |
570 | rst_closedir(dirp); | |
571 | if (entries == 0) { | |
572 | fprintf(stderr, "\n"); | |
573 | free(list); | |
574 | return; | |
575 | } | |
576 | qsort((char *)list, entries, sizeof(struct afile), fcmp); | |
577 | } | |
578 | formatf(list, entries); | |
579 | if (dirp != NULL) { | |
580 | for (fp = listp - 1; fp >= list; fp--) | |
581 | freename(fp->fname); | |
582 | fprintf(stderr, "\n"); | |
583 | free(list); | |
584 | } | |
585 | } | |
586 | ||
587 | /* | |
588 | * Read the contents of a directory. | |
589 | */ | |
590 | static void | |
ddd2ef55 | 591 | mkentry(char *name, struct direct *dp, struct afile *fp) |
1227625a SP |
592 | { |
593 | char *cp; | |
594 | struct entry *np; | |
595 | ||
596 | fp->fnum = dp->d_ino; | |
597 | fp->fname = savename(dp->d_name); | |
598 | for (cp = fp->fname; *cp; cp++) | |
599 | if (!vflag && (*cp < ' ' || *cp >= 0177)) | |
600 | *cp = '?'; | |
601 | fp->len = cp - fp->fname; | |
602 | if (dflag && TSTINO(fp->fnum, dumpmap) == 0) | |
603 | fp->prefix = '^'; | |
604 | else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW)) | |
605 | fp->prefix = '*'; | |
606 | else | |
607 | fp->prefix = ' '; | |
1227625a SP |
608 | switch(dp->d_type) { |
609 | ||
610 | default: | |
611 | fprintf(stderr, "Warning: undefined file type %d\n", | |
612 | dp->d_type); | |
613 | /* fall through */ | |
614 | case DT_REG: | |
615 | fp->postfix = ' '; | |
616 | break; | |
617 | ||
618 | case DT_LNK: | |
619 | fp->postfix = '@'; | |
620 | break; | |
621 | ||
622 | case DT_FIFO: | |
623 | case DT_SOCK: | |
624 | fp->postfix = '='; | |
625 | break; | |
626 | ||
627 | case DT_CHR: | |
628 | case DT_BLK: | |
629 | fp->postfix = '#'; | |
630 | break; | |
631 | ||
8d4197bb SP |
632 | #ifndef __linux__ |
633 | /* no need for this */ | |
1227625a SP |
634 | case DT_WHT: |
635 | fp->postfix = '%'; | |
636 | break; | |
8d4197bb | 637 | #endif |
1227625a SP |
638 | |
639 | case DT_UNKNOWN: | |
640 | case DT_DIR: | |
641 | if (inodetype(dp->d_ino) == NODE) | |
642 | fp->postfix = '/'; | |
643 | else | |
644 | fp->postfix = ' '; | |
645 | break; | |
646 | } | |
1227625a SP |
647 | return; |
648 | } | |
649 | ||
650 | /* | |
651 | * Print out a pretty listing of a directory | |
652 | */ | |
653 | static void | |
ddd2ef55 | 654 | formatf(struct afile *list, int nentry) |
1227625a SP |
655 | { |
656 | register struct afile *fp, *endlist; | |
657 | int width, bigino, haveprefix, havepostfix; | |
ddd2ef55 | 658 | int i, j, w, precision = 0, columns, lines; |
1227625a SP |
659 | |
660 | width = 0; | |
661 | haveprefix = 0; | |
662 | havepostfix = 0; | |
663 | bigino = ROOTINO; | |
664 | endlist = &list[nentry]; | |
665 | for (fp = &list[0]; fp < endlist; fp++) { | |
666 | if (bigino < fp->fnum) | |
667 | bigino = fp->fnum; | |
668 | if (width < fp->len) | |
669 | width = fp->len; | |
670 | if (fp->prefix != ' ') | |
671 | haveprefix = 1; | |
672 | if (fp->postfix != ' ') | |
673 | havepostfix = 1; | |
674 | } | |
675 | if (haveprefix) | |
676 | width++; | |
677 | if (havepostfix) | |
678 | width++; | |
679 | if (vflag) { | |
680 | for (precision = 0, i = bigino; i > 0; i /= 10) | |
681 | precision++; | |
682 | width += precision + 1; | |
683 | } | |
684 | width++; | |
685 | columns = 81 / width; | |
686 | if (columns == 0) | |
687 | columns = 1; | |
688 | lines = (nentry + columns - 1) / columns; | |
689 | for (i = 0; i < lines; i++) { | |
690 | for (j = 0; j < columns; j++) { | |
691 | fp = &list[j * lines + i]; | |
692 | if (vflag) { | |
b45f51d6 | 693 | fprintf(stderr, "%*ld ", precision, fp->fnum); |
1227625a SP |
694 | fp->len += precision + 1; |
695 | } | |
696 | if (haveprefix) { | |
697 | putc(fp->prefix, stderr); | |
698 | fp->len++; | |
699 | } | |
700 | fprintf(stderr, "%s", fp->fname); | |
701 | if (havepostfix) { | |
702 | putc(fp->postfix, stderr); | |
703 | fp->len++; | |
704 | } | |
705 | if (fp + lines >= endlist) { | |
706 | fprintf(stderr, "\n"); | |
707 | break; | |
708 | } | |
709 | for (w = fp->len; w < width; w++) | |
710 | putc(' ', stderr); | |
711 | } | |
712 | } | |
713 | } | |
714 | ||
715 | /* | |
716 | * Skip over directory entries that are not on the tape | |
717 | * | |
718 | * First have to get definition of a dirent. | |
8d4197bb SP |
719 | * |
720 | * For Linux the dirent struct is now included from bsdcompat.h | |
1227625a | 721 | */ |
8d4197bb | 722 | #ifndef __linux__ |
1227625a SP |
723 | #undef DIRBLKSIZ |
724 | #include <dirent.h> | |
725 | #undef d_ino | |
8d4197bb | 726 | #endif /* ! __linux__ */ |
1227625a SP |
727 | |
728 | struct dirent * | |
ddd2ef55 | 729 | glob_readdir(RST_DIR *dirp) |
1227625a SP |
730 | { |
731 | struct direct *dp; | |
8d4197bb | 732 | static struct dirent adirent; |
1227625a SP |
733 | |
734 | while ((dp = rst_readdir(dirp)) != NULL) { | |
735 | if (!vflag && dp->d_ino == WINO) | |
736 | continue; | |
737 | if (dflag || TSTINO(dp->d_ino, dumpmap)) | |
738 | break; | |
739 | } | |
740 | if (dp == NULL) | |
741 | return (NULL); | |
742 | adirent.d_fileno = dp->d_ino; | |
1227625a SP |
743 | memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1); |
744 | return (&adirent); | |
745 | } | |
746 | ||
747 | /* | |
748 | * Return st_mode information in response to stat or lstat calls | |
749 | */ | |
750 | static int | |
ddd2ef55 | 751 | glob_stat(const char *name, struct stat *stp) |
1227625a SP |
752 | { |
753 | register struct direct *dp; | |
1227625a SP |
754 | dp = pathsearch(name); |
755 | if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || | |
756 | (!vflag && dp->d_ino == WINO)) | |
757 | return (-1); | |
758 | if (inodetype(dp->d_ino) == NODE) | |
759 | stp->st_mode = IFDIR; | |
760 | else | |
761 | stp->st_mode = IFREG; | |
762 | return (0); | |
763 | } | |
764 | ||
765 | /* | |
766 | * Comparison routine for qsort. | |
767 | */ | |
768 | static int | |
ddd2ef55 | 769 | fcmp(const void *f1, const void *f2) |
1227625a SP |
770 | { |
771 | return (strcmp(((struct afile *)f1)->fname, | |
772 | ((struct afile *)f2)->fname)); | |
773 | } | |
774 | ||
775 | /* | |
776 | * respond to interrupts | |
777 | */ | |
778 | void | |
ddd2ef55 | 779 | onintr(int signo) |
1227625a | 780 | { |
ddd2ef55 SP |
781 | int save_errno = errno; |
782 | ||
1227625a SP |
783 | if (command == 'i' && runshell) |
784 | longjmp(reset, 1); | |
785 | if (reply("restore interrupted, continue") == FAIL) | |
ddd2ef55 SP |
786 | exit(1); |
787 | errno = save_errno; | |
1227625a | 788 | } |