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