interactive.c revision 118526
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1985, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3537906Scharnier#if 0 3623685Speterstatic char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95"; 3737906Scharnier#endif 3837906Scharnierstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/restore/interactive.c 118526 2003-08-06 08:46:21Z ache $"; 401558Srgrimes#endif /* not lint */ 411558Srgrimes 421558Srgrimes#include <sys/param.h> 431558Srgrimes#include <sys/stat.h> 441558Srgrimes 451558Srgrimes#include <ufs/ufs/dinode.h> 461558Srgrimes#include <ufs/ufs/dir.h> 471558Srgrimes#include <protocols/dumprestore.h> 481558Srgrimes 49118526Sache#include <ctype.h> 501558Srgrimes#include <glob.h> 51103949Smike#include <limits.h> 52118526Sache#include <setjmp.h> 531558Srgrimes#include <stdio.h> 541558Srgrimes#include <stdlib.h> 551558Srgrimes#include <string.h> 561558Srgrimes 571558Srgrimes#include "restore.h" 581558Srgrimes#include "extern.h" 591558Srgrimes 601558Srgrimes#define round(a, b) (((a) + (b) - 1) / (b) * (b)) 611558Srgrimes 621558Srgrimes/* 631558Srgrimes * Things to handle interruptions. 641558Srgrimes */ 651558Srgrimesstatic int runshell; 661558Srgrimesstatic jmp_buf reset; 671558Srgrimesstatic char *nextarg = NULL; 681558Srgrimes 691558Srgrimes/* 701558Srgrimes * Structure and routines associated with listing directories. 711558Srgrimes */ 721558Srgrimesstruct afile { 731558Srgrimes ino_t fnum; /* inode number of file */ 741558Srgrimes char *fname; /* file name */ 751558Srgrimes short len; /* name length */ 761558Srgrimes char prefix; /* prefix character */ 771558Srgrimes char postfix; /* postfix character */ 781558Srgrimes}; 791558Srgrimesstruct arglist { 801558Srgrimes int freeglob; /* glob structure needs to be freed */ 811558Srgrimes int argcnt; /* next globbed argument to return */ 821558Srgrimes glob_t glob; /* globbing information */ 831558Srgrimes char *cmd; /* the current command */ 841558Srgrimes}; 851558Srgrimes 8692837Simpstatic char *copynext(char *, char *); 8792837Simpstatic int fcmp(const void *, const void *); 8892837Simpstatic void formatf(struct afile *, int); 8992837Simpstatic void getcmd(char *, char *, char *, int, struct arglist *); 9092837Simpstruct dirent *glob_readdir(RST_DIR *dirp); 9192837Simpstatic int glob_stat(const char *, struct stat *); 9292837Simpstatic void mkentry(char *, struct direct *, struct afile *); 9392837Simpstatic void printlist(char *, char *); 941558Srgrimes 951558Srgrimes/* 961558Srgrimes * Read and execute commands from the terminal. 971558Srgrimes */ 981558Srgrimesvoid 9992837Simpruncmdshell(void) 1001558Srgrimes{ 10192806Sobrien struct entry *np; 1021558Srgrimes ino_t ino; 1031558Srgrimes struct arglist arglist; 1041558Srgrimes char curdir[MAXPATHLEN]; 1051558Srgrimes char name[MAXPATHLEN]; 1061558Srgrimes char cmd[BUFSIZ]; 1071558Srgrimes 1081558Srgrimes arglist.freeglob = 0; 1091558Srgrimes arglist.argcnt = 0; 1101558Srgrimes arglist.glob.gl_flags = GLOB_ALTDIRFUNC; 1111558Srgrimes arglist.glob.gl_opendir = (void *)rst_opendir; 1121558Srgrimes arglist.glob.gl_readdir = (void *)glob_readdir; 1131558Srgrimes arglist.glob.gl_closedir = (void *)rst_closedir; 1141558Srgrimes arglist.glob.gl_lstat = glob_stat; 1151558Srgrimes arglist.glob.gl_stat = glob_stat; 11621174Sguido canon("/", curdir, sizeof(curdir)); 1171558Srgrimesloop: 1181558Srgrimes if (setjmp(reset) != 0) { 1191558Srgrimes if (arglist.freeglob != 0) { 1201558Srgrimes arglist.freeglob = 0; 1211558Srgrimes arglist.argcnt = 0; 1221558Srgrimes globfree(&arglist.glob); 1231558Srgrimes } 1241558Srgrimes nextarg = NULL; 1251558Srgrimes volno = 0; 1261558Srgrimes } 1271558Srgrimes runshell = 1; 12821997Simp getcmd(curdir, cmd, name, sizeof(name), &arglist); 1291558Srgrimes switch (cmd[0]) { 1301558Srgrimes /* 1311558Srgrimes * Add elements to the extraction list. 1321558Srgrimes */ 1331558Srgrimes case 'a': 1341558Srgrimes if (strncmp(cmd, "add", strlen(cmd)) != 0) 1351558Srgrimes goto bad; 1361558Srgrimes ino = dirlookup(name); 1371558Srgrimes if (ino == 0) 1381558Srgrimes break; 1391558Srgrimes if (mflag) 1401558Srgrimes pathcheck(name); 1411558Srgrimes treescan(name, ino, addfile); 1421558Srgrimes break; 1431558Srgrimes /* 1441558Srgrimes * Change working directory. 1451558Srgrimes */ 1461558Srgrimes case 'c': 1471558Srgrimes if (strncmp(cmd, "cd", strlen(cmd)) != 0) 1481558Srgrimes goto bad; 1491558Srgrimes ino = dirlookup(name); 1501558Srgrimes if (ino == 0) 1511558Srgrimes break; 1521558Srgrimes if (inodetype(ino) == LEAF) { 1531558Srgrimes fprintf(stderr, "%s: not a directory\n", name); 1541558Srgrimes break; 1551558Srgrimes } 1561558Srgrimes (void) strcpy(curdir, name); 1571558Srgrimes break; 1581558Srgrimes /* 1591558Srgrimes * Delete elements from the extraction list. 1601558Srgrimes */ 1611558Srgrimes case 'd': 1621558Srgrimes if (strncmp(cmd, "delete", strlen(cmd)) != 0) 1631558Srgrimes goto bad; 1641558Srgrimes np = lookupname(name); 1651558Srgrimes if (np == NULL || (np->e_flags & NEW) == 0) { 1661558Srgrimes fprintf(stderr, "%s: not on extraction list\n", name); 1671558Srgrimes break; 1681558Srgrimes } 1691558Srgrimes treescan(name, np->e_ino, deletefile); 1701558Srgrimes break; 1711558Srgrimes /* 1721558Srgrimes * Extract the requested list. 1731558Srgrimes */ 1741558Srgrimes case 'e': 1751558Srgrimes if (strncmp(cmd, "extract", strlen(cmd)) != 0) 1761558Srgrimes goto bad; 1771558Srgrimes createfiles(); 1781558Srgrimes createlinks(); 1791558Srgrimes setdirmodes(0); 1801558Srgrimes if (dflag) 1811558Srgrimes checkrestore(); 1821558Srgrimes volno = 0; 1831558Srgrimes break; 1841558Srgrimes /* 1851558Srgrimes * List available commands. 1861558Srgrimes */ 1871558Srgrimes case 'h': 1881558Srgrimes if (strncmp(cmd, "help", strlen(cmd)) != 0) 1891558Srgrimes goto bad; 1901558Srgrimes case '?': 1911558Srgrimes fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 1921558Srgrimes "Available commands are:\n", 1931558Srgrimes "\tls [arg] - list directory\n", 1941558Srgrimes "\tcd arg - change directory\n", 1951558Srgrimes "\tpwd - print current directory\n", 1961558Srgrimes "\tadd [arg] - add `arg' to list of", 1971558Srgrimes " files to be extracted\n", 1981558Srgrimes "\tdelete [arg] - delete `arg' from", 1991558Srgrimes " list of files to be extracted\n", 2001558Srgrimes "\textract - extract requested files\n", 2011558Srgrimes "\tsetmodes - set modes of requested directories\n", 2021558Srgrimes "\tquit - immediately exit program\n", 2031558Srgrimes "\twhat - list dump header information\n", 2041558Srgrimes "\tverbose - toggle verbose flag", 2051558Srgrimes " (useful with ``ls'')\n", 2061558Srgrimes "\thelp or `?' - print this list\n", 2071558Srgrimes "If no `arg' is supplied, the current", 2081558Srgrimes " directory is used\n"); 2091558Srgrimes break; 2101558Srgrimes /* 2111558Srgrimes * List a directory. 2121558Srgrimes */ 2131558Srgrimes case 'l': 2141558Srgrimes if (strncmp(cmd, "ls", strlen(cmd)) != 0) 2151558Srgrimes goto bad; 2161558Srgrimes printlist(name, curdir); 2171558Srgrimes break; 2181558Srgrimes /* 2191558Srgrimes * Print current directory. 2201558Srgrimes */ 2211558Srgrimes case 'p': 2221558Srgrimes if (strncmp(cmd, "pwd", strlen(cmd)) != 0) 2231558Srgrimes goto bad; 2241558Srgrimes if (curdir[1] == '\0') 2251558Srgrimes fprintf(stderr, "/\n"); 2261558Srgrimes else 2271558Srgrimes fprintf(stderr, "%s\n", &curdir[1]); 2281558Srgrimes break; 2291558Srgrimes /* 2301558Srgrimes * Quit. 2311558Srgrimes */ 2321558Srgrimes case 'q': 2331558Srgrimes if (strncmp(cmd, "quit", strlen(cmd)) != 0) 2341558Srgrimes goto bad; 2351558Srgrimes return; 2361558Srgrimes case 'x': 2371558Srgrimes if (strncmp(cmd, "xit", strlen(cmd)) != 0) 2381558Srgrimes goto bad; 2391558Srgrimes return; 2401558Srgrimes /* 2411558Srgrimes * Toggle verbose mode. 2421558Srgrimes */ 2431558Srgrimes case 'v': 2441558Srgrimes if (strncmp(cmd, "verbose", strlen(cmd)) != 0) 2451558Srgrimes goto bad; 2461558Srgrimes if (vflag) { 2471558Srgrimes fprintf(stderr, "verbose mode off\n"); 2481558Srgrimes vflag = 0; 2491558Srgrimes break; 2501558Srgrimes } 2511558Srgrimes fprintf(stderr, "verbose mode on\n"); 2521558Srgrimes vflag++; 2531558Srgrimes break; 2541558Srgrimes /* 2551558Srgrimes * Just restore requested directory modes. 2561558Srgrimes */ 2571558Srgrimes case 's': 2581558Srgrimes if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) 2591558Srgrimes goto bad; 2601558Srgrimes setdirmodes(FORCE); 2611558Srgrimes break; 2621558Srgrimes /* 2631558Srgrimes * Print out dump header information. 2641558Srgrimes */ 2651558Srgrimes case 'w': 2661558Srgrimes if (strncmp(cmd, "what", strlen(cmd)) != 0) 2671558Srgrimes goto bad; 2681558Srgrimes printdumpinfo(); 2691558Srgrimes break; 2701558Srgrimes /* 2711558Srgrimes * Turn on debugging. 2721558Srgrimes */ 2731558Srgrimes case 'D': 2741558Srgrimes if (strncmp(cmd, "Debug", strlen(cmd)) != 0) 2751558Srgrimes goto bad; 2761558Srgrimes if (dflag) { 2771558Srgrimes fprintf(stderr, "debugging mode off\n"); 2781558Srgrimes dflag = 0; 2791558Srgrimes break; 2801558Srgrimes } 2811558Srgrimes fprintf(stderr, "debugging mode on\n"); 2821558Srgrimes dflag++; 2831558Srgrimes break; 2841558Srgrimes /* 2851558Srgrimes * Unknown command. 2861558Srgrimes */ 2871558Srgrimes default: 2881558Srgrimes bad: 2891558Srgrimes fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); 2901558Srgrimes break; 2911558Srgrimes } 2921558Srgrimes goto loop; 2931558Srgrimes} 2941558Srgrimes 2951558Srgrimes/* 2961558Srgrimes * Read and parse an interactive command. 2971558Srgrimes * The first word on the line is assigned to "cmd". If 2981558Srgrimes * there are no arguments on the command line, then "curdir" 2991558Srgrimes * is returned as the argument. If there are arguments 3001558Srgrimes * on the line they are returned one at a time on each 3011558Srgrimes * successive call to getcmd. Each argument is first assigned 3021558Srgrimes * to "name". If it does not start with "/" the pathname in 3031558Srgrimes * "curdir" is prepended to it. Finally "canon" is called to 3041558Srgrimes * eliminate any embedded ".." components. 3051558Srgrimes */ 3061558Srgrimesstatic void 30792837Simpgetcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap) 3081558Srgrimes{ 30992806Sobrien char *cp; 3101558Srgrimes static char input[BUFSIZ]; 3111558Srgrimes char output[BUFSIZ]; 3121558Srgrimes# define rawname input /* save space by reusing input buffer */ 3131558Srgrimes 3141558Srgrimes /* 3151558Srgrimes * Check to see if still processing arguments. 3161558Srgrimes */ 3171558Srgrimes if (ap->argcnt > 0) 3181558Srgrimes goto retnext; 3191558Srgrimes if (nextarg != NULL) 3201558Srgrimes goto getnext; 3211558Srgrimes /* 3221558Srgrimes * Read a command line and trim off trailing white space. 3231558Srgrimes */ 3241558Srgrimes do { 3251558Srgrimes fprintf(stderr, "restore > "); 3261558Srgrimes (void) fflush(stderr); 32769906Siedowse if (fgets(input, BUFSIZ, terminal) == NULL) { 32869906Siedowse strcpy(cmd, "quit"); 32969906Siedowse return; 33069906Siedowse } 33169906Siedowse } while (input[0] == '\n'); 3321558Srgrimes for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 3331558Srgrimes /* trim off trailing white space and newline */; 3341558Srgrimes *++cp = '\0'; 3351558Srgrimes /* 3361558Srgrimes * Copy the command into "cmd". 3371558Srgrimes */ 3381558Srgrimes cp = copynext(input, cmd); 3391558Srgrimes ap->cmd = cmd; 3401558Srgrimes /* 3411558Srgrimes * If no argument, use curdir as the default. 3421558Srgrimes */ 3431558Srgrimes if (*cp == '\0') { 34441845Simp (void) strncpy(name, curdir, size); 34541845Simp name[size - 1] = '\0'; 3461558Srgrimes return; 3471558Srgrimes } 3481558Srgrimes nextarg = cp; 3491558Srgrimes /* 3501558Srgrimes * Find the next argument. 3511558Srgrimes */ 3521558Srgrimesgetnext: 3531558Srgrimes cp = copynext(nextarg, rawname); 3541558Srgrimes if (*cp == '\0') 3551558Srgrimes nextarg = NULL; 3561558Srgrimes else 3571558Srgrimes nextarg = cp; 3581558Srgrimes /* 3591558Srgrimes * If it is an absolute pathname, canonicalize it and return it. 3601558Srgrimes */ 3611558Srgrimes if (rawname[0] == '/') { 36221997Simp canon(rawname, name, size); 3631558Srgrimes } else { 3641558Srgrimes /* 3651558Srgrimes * For relative pathnames, prepend the current directory to 3661558Srgrimes * it then canonicalize and return it. 3671558Srgrimes */ 36839430Simp snprintf(output, sizeof(output), "%s/%s", curdir, rawname); 36921997Simp canon(output, name, size); 3701558Srgrimes } 3711558Srgrimes if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0) 3721558Srgrimes fprintf(stderr, "%s: out of memory\n", ap->cmd); 3731558Srgrimes if (ap->glob.gl_pathc == 0) 3741558Srgrimes return; 3751558Srgrimes ap->freeglob = 1; 3761558Srgrimes ap->argcnt = ap->glob.gl_pathc; 3771558Srgrimes 3781558Srgrimesretnext: 37941845Simp strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size); 38041845Simp name[size - 1] = '\0'; 3811558Srgrimes if (--ap->argcnt == 0) { 3821558Srgrimes ap->freeglob = 0; 3831558Srgrimes globfree(&ap->glob); 3841558Srgrimes } 3851558Srgrimes# undef rawname 3861558Srgrimes} 3871558Srgrimes 3881558Srgrimes/* 3891558Srgrimes * Strip off the next token of the input. 3901558Srgrimes */ 3911558Srgrimesstatic char * 39292837Simpcopynext(char *input, char *output) 3931558Srgrimes{ 39492806Sobrien char *cp, *bp; 3951558Srgrimes char quote; 3961558Srgrimes 3971558Srgrimes for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 3981558Srgrimes /* skip to argument */; 3991558Srgrimes bp = output; 4001558Srgrimes while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 4011558Srgrimes /* 4021558Srgrimes * Handle back slashes. 4031558Srgrimes */ 4041558Srgrimes if (*cp == '\\') { 4051558Srgrimes if (*++cp == '\0') { 4061558Srgrimes fprintf(stderr, 4071558Srgrimes "command lines cannot be continued\n"); 4081558Srgrimes continue; 4091558Srgrimes } 4101558Srgrimes *bp++ = *cp++; 4111558Srgrimes continue; 4121558Srgrimes } 4131558Srgrimes /* 4141558Srgrimes * The usual unquoted case. 4151558Srgrimes */ 4161558Srgrimes if (*cp != '\'' && *cp != '"') { 4171558Srgrimes *bp++ = *cp++; 4181558Srgrimes continue; 4191558Srgrimes } 4201558Srgrimes /* 4211558Srgrimes * Handle single and double quotes. 4221558Srgrimes */ 4231558Srgrimes quote = *cp++; 4241558Srgrimes while (*cp != quote && *cp != '\0') 425118526Sache *bp++ = *cp++; 4261558Srgrimes if (*cp++ == '\0') { 4271558Srgrimes fprintf(stderr, "missing %c\n", quote); 4281558Srgrimes cp--; 4291558Srgrimes continue; 4301558Srgrimes } 4311558Srgrimes } 4321558Srgrimes *bp = '\0'; 4331558Srgrimes return (cp); 4341558Srgrimes} 4351558Srgrimes 4361558Srgrimes/* 4371558Srgrimes * Canonicalize file names to always start with ``./'' and 43837906Scharnier * remove any embedded "." and ".." components. 4391558Srgrimes */ 4401558Srgrimesvoid 44192837Simpcanon(char *rawname, char *canonname, int len) 4421558Srgrimes{ 44392806Sobrien char *cp, *np; 4441558Srgrimes 4451558Srgrimes if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 4461558Srgrimes (void) strcpy(canonname, ""); 4471558Srgrimes else if (rawname[0] == '/') 4481558Srgrimes (void) strcpy(canonname, "."); 4491558Srgrimes else 4501558Srgrimes (void) strcpy(canonname, "./"); 45121174Sguido if (strlen(canonname) + strlen(rawname) >= len) { 45237906Scharnier fprintf(stderr, "canonname: not enough buffer space\n"); 45321174Sguido done(1); 45421174Sguido } 45521174Sguido 4561558Srgrimes (void) strcat(canonname, rawname); 4571558Srgrimes /* 4581558Srgrimes * Eliminate multiple and trailing '/'s 4591558Srgrimes */ 4601558Srgrimes for (cp = np = canonname; *np != '\0'; cp++) { 4611558Srgrimes *cp = *np++; 4621558Srgrimes while (*cp == '/' && *np == '/') 4631558Srgrimes np++; 4641558Srgrimes } 4651558Srgrimes *cp = '\0'; 4661558Srgrimes if (*--cp == '/') 4671558Srgrimes *cp = '\0'; 4681558Srgrimes /* 4691558Srgrimes * Eliminate extraneous "." and ".." from pathnames. 4701558Srgrimes */ 4711558Srgrimes for (np = canonname; *np != '\0'; ) { 4721558Srgrimes np++; 4731558Srgrimes cp = np; 4741558Srgrimes while (*np != '/' && *np != '\0') 4751558Srgrimes np++; 4761558Srgrimes if (np - cp == 1 && *cp == '.') { 4771558Srgrimes cp--; 4781558Srgrimes (void) strcpy(cp, np); 4791558Srgrimes np = cp; 4801558Srgrimes } 4811558Srgrimes if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 4821558Srgrimes cp--; 4831558Srgrimes while (cp > &canonname[1] && *--cp != '/') 4841558Srgrimes /* find beginning of name */; 4851558Srgrimes (void) strcpy(cp, np); 4861558Srgrimes np = cp; 4871558Srgrimes } 4881558Srgrimes } 4891558Srgrimes} 4901558Srgrimes 4911558Srgrimes/* 4921558Srgrimes * Do an "ls" style listing of a directory 4931558Srgrimes */ 4941558Srgrimesstatic void 49592837Simpprintlist(char *name, char *basename) 4961558Srgrimes{ 49792806Sobrien struct afile *fp, *list, *listp; 49892806Sobrien struct direct *dp; 4991558Srgrimes struct afile single; 5001558Srgrimes RST_DIR *dirp; 50123685Speter int entries, len, namelen; 50223685Speter char locname[MAXPATHLEN + 1]; 5031558Srgrimes 5041558Srgrimes dp = pathsearch(name); 50523685Speter if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 50623685Speter (!vflag && dp->d_ino == WINO)) 5071558Srgrimes return; 5081558Srgrimes if ((dirp = rst_opendir(name)) == NULL) { 5091558Srgrimes entries = 1; 5101558Srgrimes list = &single; 51123685Speter mkentry(name, dp, list); 5121558Srgrimes len = strlen(basename) + 1; 5131558Srgrimes if (strlen(name) - len > single.len) { 5141558Srgrimes freename(single.fname); 5151558Srgrimes single.fname = savename(&name[len]); 5161558Srgrimes single.len = strlen(single.fname); 5171558Srgrimes } 5181558Srgrimes } else { 5191558Srgrimes entries = 0; 52037906Scharnier while ((dp = rst_readdir(dirp))) 5211558Srgrimes entries++; 5221558Srgrimes rst_closedir(dirp); 5231558Srgrimes list = (struct afile *)malloc(entries * sizeof(struct afile)); 5241558Srgrimes if (list == NULL) { 5251558Srgrimes fprintf(stderr, "ls: out of memory\n"); 5261558Srgrimes return; 5271558Srgrimes } 5281558Srgrimes if ((dirp = rst_opendir(name)) == NULL) 5291558Srgrimes panic("directory reopen failed\n"); 5301558Srgrimes fprintf(stderr, "%s:\n", name); 5311558Srgrimes entries = 0; 5321558Srgrimes listp = list; 53323685Speter (void) strncpy(locname, name, MAXPATHLEN); 53423685Speter (void) strncat(locname, "/", MAXPATHLEN); 53523685Speter namelen = strlen(locname); 53637906Scharnier while ((dp = rst_readdir(dirp))) { 53723685Speter if (dp == NULL) 5381558Srgrimes break; 5391558Srgrimes if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) 5401558Srgrimes continue; 54123685Speter if (!vflag && (dp->d_ino == WINO || 54223685Speter strcmp(dp->d_name, ".") == 0 || 5431558Srgrimes strcmp(dp->d_name, "..") == 0)) 5441558Srgrimes continue; 54523685Speter locname[namelen] = '\0'; 54623685Speter if (namelen + dp->d_namlen >= MAXPATHLEN) { 54723685Speter fprintf(stderr, "%s%s: name exceeds %d char\n", 54823685Speter locname, dp->d_name, MAXPATHLEN); 54923685Speter } else { 55023685Speter (void) strncat(locname, dp->d_name, 55123685Speter (int)dp->d_namlen); 55223685Speter mkentry(locname, dp, listp++); 55323685Speter entries++; 55423685Speter } 5551558Srgrimes } 5561558Srgrimes rst_closedir(dirp); 5571558Srgrimes if (entries == 0) { 5581558Srgrimes fprintf(stderr, "\n"); 5591558Srgrimes free(list); 5601558Srgrimes return; 5611558Srgrimes } 5621558Srgrimes qsort((char *)list, entries, sizeof(struct afile), fcmp); 5631558Srgrimes } 5641558Srgrimes formatf(list, entries); 5651558Srgrimes if (dirp != NULL) { 5661558Srgrimes for (fp = listp - 1; fp >= list; fp--) 5671558Srgrimes freename(fp->fname); 5681558Srgrimes fprintf(stderr, "\n"); 5691558Srgrimes free(list); 5701558Srgrimes } 5711558Srgrimes} 5721558Srgrimes 5731558Srgrimes/* 5741558Srgrimes * Read the contents of a directory. 5751558Srgrimes */ 5761558Srgrimesstatic void 57792837Simpmkentry(char *name, struct direct *dp, struct afile *fp) 5781558Srgrimes{ 5791558Srgrimes char *cp; 5801558Srgrimes struct entry *np; 5811558Srgrimes 5821558Srgrimes fp->fnum = dp->d_ino; 5831558Srgrimes fp->fname = savename(dp->d_name); 5841558Srgrimes for (cp = fp->fname; *cp; cp++) 585118526Sache if (!vflag && !isprint((unsigned char)*cp)) 5861558Srgrimes *cp = '?'; 5871558Srgrimes fp->len = cp - fp->fname; 5881558Srgrimes if (dflag && TSTINO(fp->fnum, dumpmap) == 0) 5891558Srgrimes fp->prefix = '^'; 59023685Speter else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW)) 5911558Srgrimes fp->prefix = '*'; 5921558Srgrimes else 5931558Srgrimes fp->prefix = ' '; 5941558Srgrimes switch(dp->d_type) { 5951558Srgrimes 5961558Srgrimes default: 5971558Srgrimes fprintf(stderr, "Warning: undefined file type %d\n", 5981558Srgrimes dp->d_type); 599102411Scharnier /* FALLTHROUGH */ 6001558Srgrimes case DT_REG: 6011558Srgrimes fp->postfix = ' '; 6021558Srgrimes break; 6031558Srgrimes 6041558Srgrimes case DT_LNK: 6051558Srgrimes fp->postfix = '@'; 6061558Srgrimes break; 6071558Srgrimes 6081558Srgrimes case DT_FIFO: 6091558Srgrimes case DT_SOCK: 6101558Srgrimes fp->postfix = '='; 6111558Srgrimes break; 6121558Srgrimes 6131558Srgrimes case DT_CHR: 6141558Srgrimes case DT_BLK: 6151558Srgrimes fp->postfix = '#'; 6161558Srgrimes break; 6171558Srgrimes 61823685Speter case DT_WHT: 61923685Speter fp->postfix = '%'; 62023685Speter break; 62123685Speter 6221558Srgrimes case DT_UNKNOWN: 6231558Srgrimes case DT_DIR: 6241558Srgrimes if (inodetype(dp->d_ino) == NODE) 6251558Srgrimes fp->postfix = '/'; 6261558Srgrimes else 6271558Srgrimes fp->postfix = ' '; 6281558Srgrimes break; 6291558Srgrimes } 6301558Srgrimes return; 6311558Srgrimes} 6321558Srgrimes 6331558Srgrimes/* 6341558Srgrimes * Print out a pretty listing of a directory 6351558Srgrimes */ 6361558Srgrimesstatic void 63792837Simpformatf(struct afile *list, int nentry) 6381558Srgrimes{ 63992806Sobrien struct afile *fp, *endlist; 6401558Srgrimes int width, bigino, haveprefix, havepostfix; 6411558Srgrimes int i, j, w, precision, columns, lines; 6421558Srgrimes 6431558Srgrimes width = 0; 6441558Srgrimes haveprefix = 0; 6451558Srgrimes havepostfix = 0; 6461558Srgrimes bigino = ROOTINO; 6471558Srgrimes endlist = &list[nentry]; 6481558Srgrimes for (fp = &list[0]; fp < endlist; fp++) { 6491558Srgrimes if (bigino < fp->fnum) 6501558Srgrimes bigino = fp->fnum; 6511558Srgrimes if (width < fp->len) 6521558Srgrimes width = fp->len; 6531558Srgrimes if (fp->prefix != ' ') 6541558Srgrimes haveprefix = 1; 6551558Srgrimes if (fp->postfix != ' ') 6561558Srgrimes havepostfix = 1; 6571558Srgrimes } 6581558Srgrimes if (haveprefix) 6591558Srgrimes width++; 6601558Srgrimes if (havepostfix) 6611558Srgrimes width++; 6621558Srgrimes if (vflag) { 6631558Srgrimes for (precision = 0, i = bigino; i > 0; i /= 10) 6641558Srgrimes precision++; 6651558Srgrimes width += precision + 1; 6661558Srgrimes } 6671558Srgrimes width++; 6681558Srgrimes columns = 81 / width; 6691558Srgrimes if (columns == 0) 6701558Srgrimes columns = 1; 6711558Srgrimes lines = (nentry + columns - 1) / columns; 6721558Srgrimes for (i = 0; i < lines; i++) { 6731558Srgrimes for (j = 0; j < columns; j++) { 6741558Srgrimes fp = &list[j * lines + i]; 6751558Srgrimes if (vflag) { 6761558Srgrimes fprintf(stderr, "%*d ", precision, fp->fnum); 6771558Srgrimes fp->len += precision + 1; 6781558Srgrimes } 6791558Srgrimes if (haveprefix) { 6801558Srgrimes putc(fp->prefix, stderr); 6811558Srgrimes fp->len++; 6821558Srgrimes } 6831558Srgrimes fprintf(stderr, "%s", fp->fname); 6841558Srgrimes if (havepostfix) { 6851558Srgrimes putc(fp->postfix, stderr); 6861558Srgrimes fp->len++; 6871558Srgrimes } 6881558Srgrimes if (fp + lines >= endlist) { 6891558Srgrimes fprintf(stderr, "\n"); 6901558Srgrimes break; 6911558Srgrimes } 6921558Srgrimes for (w = fp->len; w < width; w++) 6931558Srgrimes putc(' ', stderr); 6941558Srgrimes } 6951558Srgrimes } 6961558Srgrimes} 6971558Srgrimes 6981558Srgrimes/* 6991558Srgrimes * Skip over directory entries that are not on the tape 7001558Srgrimes * 7011558Srgrimes * First have to get definition of a dirent. 7021558Srgrimes */ 7031558Srgrimes#undef DIRBLKSIZ 7041558Srgrimes#include <dirent.h> 7051558Srgrimes#undef d_ino 7061558Srgrimes 7071558Srgrimesstruct dirent * 70892837Simpglob_readdir(RST_DIR *dirp) 7091558Srgrimes{ 7101558Srgrimes struct direct *dp; 7111558Srgrimes static struct dirent adirent; 7121558Srgrimes 7131558Srgrimes while ((dp = rst_readdir(dirp)) != NULL) { 71423685Speter if (!vflag && dp->d_ino == WINO) 7151558Srgrimes continue; 7161558Srgrimes if (dflag || TSTINO(dp->d_ino, dumpmap)) 7171558Srgrimes break; 7181558Srgrimes } 7191558Srgrimes if (dp == NULL) 7201558Srgrimes return (NULL); 7211558Srgrimes adirent.d_fileno = dp->d_ino; 7221558Srgrimes adirent.d_namlen = dp->d_namlen; 72323685Speter memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1); 7241558Srgrimes return (&adirent); 7251558Srgrimes} 7261558Srgrimes 7271558Srgrimes/* 7281558Srgrimes * Return st_mode information in response to stat or lstat calls 7291558Srgrimes */ 7301558Srgrimesstatic int 73192837Simpglob_stat(const char *name, struct stat *stp) 7321558Srgrimes{ 73392806Sobrien struct direct *dp; 7341558Srgrimes 7351558Srgrimes dp = pathsearch(name); 73623685Speter if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 73723685Speter (!vflag && dp->d_ino == WINO)) 7381558Srgrimes return (-1); 7391558Srgrimes if (inodetype(dp->d_ino) == NODE) 7401558Srgrimes stp->st_mode = IFDIR; 7411558Srgrimes else 7421558Srgrimes stp->st_mode = IFREG; 7431558Srgrimes return (0); 7441558Srgrimes} 7451558Srgrimes 7461558Srgrimes/* 7471558Srgrimes * Comparison routine for qsort. 7481558Srgrimes */ 7491558Srgrimesstatic int 75092837Simpfcmp(const void *f1, const void *f2) 7511558Srgrimes{ 752118526Sache return (strcoll(((struct afile *)f1)->fname, 7531558Srgrimes ((struct afile *)f2)->fname)); 7541558Srgrimes} 7551558Srgrimes 7561558Srgrimes/* 7571558Srgrimes * respond to interrupts 7581558Srgrimes */ 7591558Srgrimesvoid 76092837Simponintr(int signo) 7611558Srgrimes{ 7621558Srgrimes if (command == 'i' && runshell) 7631558Srgrimes longjmp(reset, 1); 7641558Srgrimes if (reply("restore interrupted, continue") == FAIL) 7651558Srgrimes done(1); 7661558Srgrimes} 767