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