xref: /onnv-gate/usr/src/cmd/backup/restore/interactive.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1998,2001-2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1985 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #include <setjmp.h>
18*0Sstevel@tonic-gate #include <euc.h>
19*0Sstevel@tonic-gate #include <widec.h>
20*0Sstevel@tonic-gate #include "restore.h"
21*0Sstevel@tonic-gate #include <ctype.h>
22*0Sstevel@tonic-gate #include <limits.h>
23*0Sstevel@tonic-gate #include <sys/wait.h>
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate extern eucwidth_t wp;
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #define	round(a, b) ((((a) + (b) - 1) / (b)) * (b))
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Things to handle interruptions.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate static jmp_buf reset;
33*0Sstevel@tonic-gate static int reset_OK;
34*0Sstevel@tonic-gate static char *nextarg = NULL;
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate static int dontexpand;	/* co-routine state set in getnext, used in expandarg */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #ifdef __STDC__
39*0Sstevel@tonic-gate static void getcmd(char *, char *, size_t, char *, size_t, struct arglist *);
40*0Sstevel@tonic-gate static void expandarg(char *, struct arglist *);
41*0Sstevel@tonic-gate static void printlist(char *, ino_t, char *, int);
42*0Sstevel@tonic-gate static void formatf(struct arglist *);
43*0Sstevel@tonic-gate static char *copynext(char *, char *, size_t);
44*0Sstevel@tonic-gate static int fcmp(struct afile *, struct afile *);
45*0Sstevel@tonic-gate static char *fmtentry(struct afile *);
46*0Sstevel@tonic-gate static void setpagercmd(void);
47*0Sstevel@tonic-gate static uint_t setpagerargs(char **);
48*0Sstevel@tonic-gate #else
49*0Sstevel@tonic-gate static void getcmd();
50*0Sstevel@tonic-gate static void expandarg();
51*0Sstevel@tonic-gate static void printlist();
52*0Sstevel@tonic-gate static void formatf();
53*0Sstevel@tonic-gate static char *copynext();
54*0Sstevel@tonic-gate static int fcmp();
55*0Sstevel@tonic-gate static char *fmtentry();
56*0Sstevel@tonic-gate static void setpagercmd();
57*0Sstevel@tonic-gate static uint_t setpagerargs();
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Read and execute commands from the terminal.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate void
64*0Sstevel@tonic-gate #ifdef __STDC__
runcmdshell(void)65*0Sstevel@tonic-gate runcmdshell(void)
66*0Sstevel@tonic-gate #else
67*0Sstevel@tonic-gate runcmdshell()
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	struct entry *np;
71*0Sstevel@tonic-gate 	ino_t ino;
72*0Sstevel@tonic-gate 	static struct arglist alist = { 0, 0, 0, 0, 0 };
73*0Sstevel@tonic-gate 	char curdir[MAXCOMPLEXLEN];
74*0Sstevel@tonic-gate 	char name[MAXCOMPLEXLEN];
75*0Sstevel@tonic-gate 	char cmd[BUFSIZ];
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #ifdef	lint
78*0Sstevel@tonic-gate 	curdir[0] = '\0';
79*0Sstevel@tonic-gate #endif	/* lint */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	canon("/", curdir, sizeof (curdir));
82*0Sstevel@tonic-gate loop:
83*0Sstevel@tonic-gate 	if (setjmp(reset) != 0) {
84*0Sstevel@tonic-gate 		for (; alist.head < alist.last; alist.head++)
85*0Sstevel@tonic-gate 			freename(alist.head->fname);
86*0Sstevel@tonic-gate 		nextarg = NULL;
87*0Sstevel@tonic-gate 		volno = 0;
88*0Sstevel@tonic-gate 		goto loop;	/* make sure jmpbuf is up-to-date */
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	reset_OK = 1;
91*0Sstevel@tonic-gate 	getcmd(curdir, cmd, sizeof (cmd), name, sizeof (name), &alist);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * Using strncmp() to catch unique prefixes.
95*0Sstevel@tonic-gate 	 */
96*0Sstevel@tonic-gate 	switch (cmd[0]) {
97*0Sstevel@tonic-gate 	/*
98*0Sstevel@tonic-gate 	 * Add elements to the extraction list.
99*0Sstevel@tonic-gate 	 */
100*0Sstevel@tonic-gate 	case 'a':
101*0Sstevel@tonic-gate 		if (strncmp(cmd, "add", strlen(cmd)) != 0)
102*0Sstevel@tonic-gate 			goto bad;
103*0Sstevel@tonic-gate 		if (name[0] == '\0')
104*0Sstevel@tonic-gate 			break;
105*0Sstevel@tonic-gate 		ino = dirlookup(name);
106*0Sstevel@tonic-gate 		if (ino == 0)
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		if (mflag)
109*0Sstevel@tonic-gate 			pathcheck(name);
110*0Sstevel@tonic-gate 		treescan(name, ino, addfile);
111*0Sstevel@tonic-gate 		break;
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * Change working directory.
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 	case 'c':
116*0Sstevel@tonic-gate 		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
117*0Sstevel@tonic-gate 			goto bad;
118*0Sstevel@tonic-gate 		if (name[0] == '\0')
119*0Sstevel@tonic-gate 			break;
120*0Sstevel@tonic-gate 		ino = dirlookup(name);
121*0Sstevel@tonic-gate 		if (ino == 0)
122*0Sstevel@tonic-gate 			break;
123*0Sstevel@tonic-gate 		if (inodetype(ino) == LEAF) {
124*0Sstevel@tonic-gate 			(void) fprintf(stderr,
125*0Sstevel@tonic-gate 				gettext("%s: not a directory\n"), name);
126*0Sstevel@tonic-gate 			break;
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		/* No need to canon(name), getcmd() did it for us */
130*0Sstevel@tonic-gate 		(void) strncpy(curdir, name, sizeof (curdir));
131*0Sstevel@tonic-gate 		curdir[sizeof (curdir) - 1] = '\0';
132*0Sstevel@tonic-gate 		break;
133*0Sstevel@tonic-gate 	/*
134*0Sstevel@tonic-gate 	 * Delete elements from the extraction list.
135*0Sstevel@tonic-gate 	 */
136*0Sstevel@tonic-gate 	case 'd':
137*0Sstevel@tonic-gate 		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
138*0Sstevel@tonic-gate 			goto bad;
139*0Sstevel@tonic-gate 		if (name[0] == '\0')
140*0Sstevel@tonic-gate 			break;
141*0Sstevel@tonic-gate 		np = lookupname(name);
142*0Sstevel@tonic-gate 		if (np == NIL || (np->e_flags & NEW) == 0) {
143*0Sstevel@tonic-gate 			(void) fprintf(stderr,
144*0Sstevel@tonic-gate 				gettext("%s: not on extraction list\n"), name);
145*0Sstevel@tonic-gate 			break;
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 		treescan(name, np->e_ino, deletefile);
148*0Sstevel@tonic-gate 		break;
149*0Sstevel@tonic-gate 	/*
150*0Sstevel@tonic-gate 	 * Extract the requested list.
151*0Sstevel@tonic-gate 	 */
152*0Sstevel@tonic-gate 	case 'e':
153*0Sstevel@tonic-gate 		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
154*0Sstevel@tonic-gate 			goto bad;
155*0Sstevel@tonic-gate 		attrscan(0, addfile);
156*0Sstevel@tonic-gate 		createfiles();
157*0Sstevel@tonic-gate 		createlinks();
158*0Sstevel@tonic-gate 		setdirmodes();
159*0Sstevel@tonic-gate 		if (dflag)
160*0Sstevel@tonic-gate 			checkrestore();
161*0Sstevel@tonic-gate 		volno = 0;
162*0Sstevel@tonic-gate 		break;
163*0Sstevel@tonic-gate 	/*
164*0Sstevel@tonic-gate 	 * List available commands.
165*0Sstevel@tonic-gate 	 */
166*0Sstevel@tonic-gate 	case 'h':
167*0Sstevel@tonic-gate 		if (strncmp(cmd, "help", strlen(cmd)) != 0)
168*0Sstevel@tonic-gate 			goto bad;
169*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
170*0Sstevel@tonic-gate 	case '?':
171*0Sstevel@tonic-gate 		/* ANSI string catenation, to shut cstyle up */
172*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s",
173*0Sstevel@tonic-gate 			gettext("Available commands are:\n"
174*0Sstevel@tonic-gate "\tls [arg] - list directory\n"
175*0Sstevel@tonic-gate "\tmarked [arg] - list items marked for extraction from directory\n"
176*0Sstevel@tonic-gate "\tcd arg - change directory\n"
177*0Sstevel@tonic-gate "\tpwd - print current directory\n"
178*0Sstevel@tonic-gate "\tadd [arg] - add `arg' to list of files to be extracted\n"
179*0Sstevel@tonic-gate "\tdelete [arg] - delete `arg' from list of files to be extracted\n"
180*0Sstevel@tonic-gate "\textract - extract requested files\n"
181*0Sstevel@tonic-gate "\tsetmodes - set modes of requested directories\n"
182*0Sstevel@tonic-gate "\tquit - immediately exit program\n"
183*0Sstevel@tonic-gate "\twhat - list dump header information\n"
184*0Sstevel@tonic-gate "\tverbose - toggle verbose flag (useful with ``ls'')\n"
185*0Sstevel@tonic-gate "\tpaginate - toggle pagination flag (affects ``ls'' and ``marked'')\n"
186*0Sstevel@tonic-gate "\tsetpager - set pagination command and arguments\n"
187*0Sstevel@tonic-gate "\thelp or `?' - print this list\n"
188*0Sstevel@tonic-gate "If no `arg' is supplied, the current directory is used\n"));
189*0Sstevel@tonic-gate 		break;
190*0Sstevel@tonic-gate 	/*
191*0Sstevel@tonic-gate 	 * List a directory.
192*0Sstevel@tonic-gate 	 */
193*0Sstevel@tonic-gate 	case 'l':
194*0Sstevel@tonic-gate 	case 'm':
195*0Sstevel@tonic-gate 		if ((strncmp(cmd, "ls", strlen(cmd)) != 0) &&
196*0Sstevel@tonic-gate 		    (strncmp(cmd, "marked", strlen(cmd)) != 0))
197*0Sstevel@tonic-gate 			goto bad;
198*0Sstevel@tonic-gate 		if (name[0] == '\0')
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 		ino = dirlookup(name);
201*0Sstevel@tonic-gate 		if (ino == 0)
202*0Sstevel@tonic-gate 			break;
203*0Sstevel@tonic-gate 		printlist(name, ino, curdir, *cmd == 'm');
204*0Sstevel@tonic-gate 		break;
205*0Sstevel@tonic-gate 	/*
206*0Sstevel@tonic-gate 	 * Print current directory or enable pagination.
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 	case 'p':
209*0Sstevel@tonic-gate 		if (strlen(cmd) < 2)
210*0Sstevel@tonic-gate 			goto ambiguous;
211*0Sstevel@tonic-gate 		if (strncmp(cmd, "pwd", strlen(cmd)) == 0) {
212*0Sstevel@tonic-gate 			if (curdir[1] == '\0') {
213*0Sstevel@tonic-gate 				(void) fprintf(stderr, "/\n");
214*0Sstevel@tonic-gate 			} else {
215*0Sstevel@tonic-gate 				(void) fprintf(stderr, "%s\n", &curdir[1]);
216*0Sstevel@tonic-gate 			}
217*0Sstevel@tonic-gate 		} else if (strncmp(cmd, "paginate", strlen(cmd)) == 0) {
218*0Sstevel@tonic-gate 			if (paginating) {
219*0Sstevel@tonic-gate 				(void) fprintf(stderr,
220*0Sstevel@tonic-gate 				    gettext("paging disabled\n"));
221*0Sstevel@tonic-gate 				paginating = 0;
222*0Sstevel@tonic-gate 				break;
223*0Sstevel@tonic-gate 			}
224*0Sstevel@tonic-gate 			if (vflag) {
225*0Sstevel@tonic-gate 				(void) fprintf(stderr,
226*0Sstevel@tonic-gate 				    gettext("paging enabled (%s)\n"),
227*0Sstevel@tonic-gate 				    pager_catenated);
228*0Sstevel@tonic-gate 			} else {
229*0Sstevel@tonic-gate 				(void) fprintf(stderr,
230*0Sstevel@tonic-gate 				    gettext("paging enabled\n"));
231*0Sstevel@tonic-gate 			}
232*0Sstevel@tonic-gate 			if (dflag) {
233*0Sstevel@tonic-gate 				int index = 0;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 				while (index < pager_len) {
236*0Sstevel@tonic-gate 					(void) fprintf(stderr,
237*0Sstevel@tonic-gate 					    ">>>pager_vector[%d] = `%s'\n",
238*0Sstevel@tonic-gate 					    index,
239*0Sstevel@tonic-gate 					    pager_vector[index] ?
240*0Sstevel@tonic-gate 						pager_vector[index] : "(null)");
241*0Sstevel@tonic-gate 					index += 1;
242*0Sstevel@tonic-gate 				}
243*0Sstevel@tonic-gate 			}
244*0Sstevel@tonic-gate 			paginating = 1;
245*0Sstevel@tonic-gate 		} else {
246*0Sstevel@tonic-gate 			goto bad;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 		break;
249*0Sstevel@tonic-gate 	/*
250*0Sstevel@tonic-gate 	 * Quit.
251*0Sstevel@tonic-gate 	 */
252*0Sstevel@tonic-gate 	case 'q':
253*0Sstevel@tonic-gate 		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
254*0Sstevel@tonic-gate 			goto bad;
255*0Sstevel@tonic-gate 		reset_OK = 0;
256*0Sstevel@tonic-gate 		return;
257*0Sstevel@tonic-gate 	case 'x':
258*0Sstevel@tonic-gate 		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
259*0Sstevel@tonic-gate 			goto bad;
260*0Sstevel@tonic-gate 		reset_OK = 0;
261*0Sstevel@tonic-gate 		return;
262*0Sstevel@tonic-gate 	/*
263*0Sstevel@tonic-gate 	 * Toggle verbose mode.
264*0Sstevel@tonic-gate 	 */
265*0Sstevel@tonic-gate 	case 'v':
266*0Sstevel@tonic-gate 		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
267*0Sstevel@tonic-gate 			goto bad;
268*0Sstevel@tonic-gate 		if (vflag) {
269*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("verbose mode off\n"));
270*0Sstevel@tonic-gate 			vflag = 0;
271*0Sstevel@tonic-gate 			break;
272*0Sstevel@tonic-gate 		}
273*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("verbose mode on\n"));
274*0Sstevel@tonic-gate 		vflag = 1;
275*0Sstevel@tonic-gate 		break;
276*0Sstevel@tonic-gate 	/*
277*0Sstevel@tonic-gate 	 * Just restore requested directory modes, or set pagination command.
278*0Sstevel@tonic-gate 	 */
279*0Sstevel@tonic-gate 	case 's':
280*0Sstevel@tonic-gate 		if (strlen(cmd) < 4)
281*0Sstevel@tonic-gate 			goto ambiguous;
282*0Sstevel@tonic-gate 		if (strncmp(cmd, "setmodes", strlen(cmd)) == 0) {
283*0Sstevel@tonic-gate 			setdirmodes();
284*0Sstevel@tonic-gate 		} else if (strncmp(cmd, "setpager", strlen(cmd)) == 0) {
285*0Sstevel@tonic-gate 			setpagercmd();
286*0Sstevel@tonic-gate 		} else {
287*0Sstevel@tonic-gate 			goto bad;
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 		break;
290*0Sstevel@tonic-gate 	/*
291*0Sstevel@tonic-gate 	 * Print out dump header information.
292*0Sstevel@tonic-gate 	 */
293*0Sstevel@tonic-gate 	case 'w':
294*0Sstevel@tonic-gate 		if (strncmp(cmd, "what", strlen(cmd)) != 0)
295*0Sstevel@tonic-gate 			goto bad;
296*0Sstevel@tonic-gate 		printdumpinfo();
297*0Sstevel@tonic-gate 		break;
298*0Sstevel@tonic-gate 	/*
299*0Sstevel@tonic-gate 	 * Turn on debugging.
300*0Sstevel@tonic-gate 	 */
301*0Sstevel@tonic-gate 	case 'D':
302*0Sstevel@tonic-gate 		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
303*0Sstevel@tonic-gate 			goto bad;
304*0Sstevel@tonic-gate 		if (dflag) {
305*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("debugging mode off\n"));
306*0Sstevel@tonic-gate 			dflag = 0;
307*0Sstevel@tonic-gate 			break;
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("debugging mode on\n"));
310*0Sstevel@tonic-gate 		dflag++;
311*0Sstevel@tonic-gate 		break;
312*0Sstevel@tonic-gate 	/*
313*0Sstevel@tonic-gate 	 * Unknown command.
314*0Sstevel@tonic-gate 	 */
315*0Sstevel@tonic-gate 	default:
316*0Sstevel@tonic-gate 	bad:
317*0Sstevel@tonic-gate 		(void) fprintf(stderr,
318*0Sstevel@tonic-gate 			gettext("%s: unknown command; type ? for help\n"), cmd);
319*0Sstevel@tonic-gate 		break;
320*0Sstevel@tonic-gate 	ambiguous:
321*0Sstevel@tonic-gate 		(void) fprintf(stderr,
322*0Sstevel@tonic-gate 		    gettext("%s: ambiguous command; type ? for help\n"), cmd);
323*0Sstevel@tonic-gate 		break;
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 	goto loop;
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate static char input[MAXCOMPLEXLEN]; /* shared by getcmd() and setpagercmd() */
329*0Sstevel@tonic-gate #define	rawname input	/* save space by reusing input buffer */
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate /*
332*0Sstevel@tonic-gate  * Read and parse an interactive command.
333*0Sstevel@tonic-gate  * The first word on the line is assigned to "cmd". If
334*0Sstevel@tonic-gate  * there are no arguments on the command line, then "curdir"
335*0Sstevel@tonic-gate  * is returned as the argument. If there are arguments
336*0Sstevel@tonic-gate  * on the line they are returned one at a time on each
337*0Sstevel@tonic-gate  * successive call to getcmd. Each argument is first assigned
338*0Sstevel@tonic-gate  * to "name". If it does not start with "/" the pathname in
339*0Sstevel@tonic-gate  * "curdir" is prepended to it. Finally "canon" is called to
340*0Sstevel@tonic-gate  * eliminate any embedded ".." components.
341*0Sstevel@tonic-gate  */
342*0Sstevel@tonic-gate /* ARGSUSED */
343*0Sstevel@tonic-gate static void
getcmd(curdir,cmd,cmdsiz,name,namesiz,ap)344*0Sstevel@tonic-gate getcmd(curdir, cmd, cmdsiz, name, namesiz, ap)
345*0Sstevel@tonic-gate 	char *curdir, *cmd, *name;
346*0Sstevel@tonic-gate 	size_t cmdsiz, namesiz;
347*0Sstevel@tonic-gate 	struct arglist *ap;
348*0Sstevel@tonic-gate {
349*0Sstevel@tonic-gate 	char *cp;
350*0Sstevel@tonic-gate 	char output[MAXCOMPLEXLEN];
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	/*
353*0Sstevel@tonic-gate 	 * Check to see if still processing arguments.
354*0Sstevel@tonic-gate 	 */
355*0Sstevel@tonic-gate 	if (ap->head != ap->last) {
356*0Sstevel@tonic-gate 		(void) strncpy(name, ap->head->fname, namesiz);
357*0Sstevel@tonic-gate 		name[namesiz - 1] = '\0';
358*0Sstevel@tonic-gate 		/* double null terminate string */
359*0Sstevel@tonic-gate 		if ((strlen(name) + 2) > namesiz) {
360*0Sstevel@tonic-gate 			fprintf(stderr, gettext("name is too long, ignoring"));
361*0Sstevel@tonic-gate 			memset(name, 0, namesiz);
362*0Sstevel@tonic-gate 		} else {
363*0Sstevel@tonic-gate 			name[strlen(name) + 1] = '\0';
364*0Sstevel@tonic-gate 		}
365*0Sstevel@tonic-gate 		freename(ap->head->fname);
366*0Sstevel@tonic-gate 		ap->head++;
367*0Sstevel@tonic-gate 		return;
368*0Sstevel@tonic-gate 	}
369*0Sstevel@tonic-gate 	if (nextarg != NULL)
370*0Sstevel@tonic-gate 		goto getnext;
371*0Sstevel@tonic-gate 	/*
372*0Sstevel@tonic-gate 	 * Read a command line and trim off trailing white space.
373*0Sstevel@tonic-gate 	 */
374*0Sstevel@tonic-gate readagain:
375*0Sstevel@tonic-gate 	do {
376*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s > ", progname);
377*0Sstevel@tonic-gate 		(void) fflush(stderr);
378*0Sstevel@tonic-gate 		(void) fgets(input, sizeof (input), terminal);
379*0Sstevel@tonic-gate 	} while (!feof(terminal) && input[0] == '\n');
380*0Sstevel@tonic-gate 	if (feof(terminal)) {
381*0Sstevel@tonic-gate 		(void) strncpy(cmd, "quit", cmdsiz);
382*0Sstevel@tonic-gate 		return;
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 	/* trim off trailing white space and newline */
385*0Sstevel@tonic-gate 	for (cp = &input[strlen(input) - 2];
386*0Sstevel@tonic-gate 	    cp >= &input[0] && isspace((uchar_t)*cp);
387*0Sstevel@tonic-gate 	    cp--) {
388*0Sstevel@tonic-gate 		continue;
389*0Sstevel@tonic-gate 		/*LINTED [empty loop body]*/
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 	*++cp = '\0';
392*0Sstevel@tonic-gate 	if ((strlen(input) + 2) > MAXCOMPLEXLEN) {
393*0Sstevel@tonic-gate 		fprintf(stderr, gettext("command is too long\n"));
394*0Sstevel@tonic-gate 		goto readagain;
395*0Sstevel@tonic-gate 	} else {
396*0Sstevel@tonic-gate 		/* double null terminate string */
397*0Sstevel@tonic-gate 		*(cp + 1) = '\0';
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	if (cp == &input[0])
401*0Sstevel@tonic-gate 		goto readagain;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	/*
404*0Sstevel@tonic-gate 	 * Copy the command into "cmd".
405*0Sstevel@tonic-gate 	 */
406*0Sstevel@tonic-gate 	cp = copynext(input, cmd, cmdsiz);
407*0Sstevel@tonic-gate 	ap->cmd = cmd;
408*0Sstevel@tonic-gate 	/*
409*0Sstevel@tonic-gate 	 * If no argument, use curdir as the default.
410*0Sstevel@tonic-gate 	 */
411*0Sstevel@tonic-gate 	if (*cp == '\0') {
412*0Sstevel@tonic-gate 		(void) strncpy(name, curdir, namesiz);
413*0Sstevel@tonic-gate 		name[namesiz - 1] = '\0';
414*0Sstevel@tonic-gate 		/* double null terminate string */
415*0Sstevel@tonic-gate 		if ((strlen(name) + 2) > namesiz) {
416*0Sstevel@tonic-gate 			fprintf(stderr, gettext("name is too long, ignoring"));
417*0Sstevel@tonic-gate 			memset(name, 0, namesiz);
418*0Sstevel@tonic-gate 		} else {
419*0Sstevel@tonic-gate 			name[strlen(name) + 1] = '\0';
420*0Sstevel@tonic-gate 		}
421*0Sstevel@tonic-gate 		return;
422*0Sstevel@tonic-gate 	}
423*0Sstevel@tonic-gate 	nextarg = cp;
424*0Sstevel@tonic-gate 	/*
425*0Sstevel@tonic-gate 	 * Find the next argument.
426*0Sstevel@tonic-gate 	 */
427*0Sstevel@tonic-gate getnext:
428*0Sstevel@tonic-gate 	cp = copynext(nextarg, rawname, sizeof (rawname));
429*0Sstevel@tonic-gate 	if (*cp == '\0')
430*0Sstevel@tonic-gate 		nextarg = NULL;
431*0Sstevel@tonic-gate 	else
432*0Sstevel@tonic-gate 		nextarg = cp;
433*0Sstevel@tonic-gate 	/*
434*0Sstevel@tonic-gate 	 * If it an absolute pathname, canonicalize it and return it.
435*0Sstevel@tonic-gate 	 */
436*0Sstevel@tonic-gate 	if (rawname[0] == '/') {
437*0Sstevel@tonic-gate 		canon(rawname, name, namesiz);
438*0Sstevel@tonic-gate 	} else {
439*0Sstevel@tonic-gate 		/*
440*0Sstevel@tonic-gate 		 * For relative pathnames, prepend the current directory to
441*0Sstevel@tonic-gate 		 * it then canonicalize and return it.
442*0Sstevel@tonic-gate 		 */
443*0Sstevel@tonic-gate 		(void) snprintf(output, sizeof (output), "%s/%s",
444*0Sstevel@tonic-gate 		    curdir, rawname);
445*0Sstevel@tonic-gate 		canon(output, name, namesiz);
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 	expandarg(name, ap);
448*0Sstevel@tonic-gate 	/*
449*0Sstevel@tonic-gate 	 * ap->head->fname guaranteed to be double null-terminated and
450*0Sstevel@tonic-gate 	 * no more than MAXCOMPLEXLEN characters long.
451*0Sstevel@tonic-gate 	 */
452*0Sstevel@tonic-gate 	assert(namesiz >= (MAXCOMPLEXLEN));
453*0Sstevel@tonic-gate 	(void) strcpy(name, ap->head->fname);
454*0Sstevel@tonic-gate 	/* double null terminate string */
455*0Sstevel@tonic-gate 	name[strlen(name) + 1] = '\0';
456*0Sstevel@tonic-gate 	freename(ap->head->fname);
457*0Sstevel@tonic-gate 	ap->head++;
458*0Sstevel@tonic-gate #undef	rawname
459*0Sstevel@tonic-gate }
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate /*
462*0Sstevel@tonic-gate  * Strip off the next token of the input.
463*0Sstevel@tonic-gate  */
464*0Sstevel@tonic-gate static char *
copynext(input,output,outsize)465*0Sstevel@tonic-gate copynext(input, output, outsize)
466*0Sstevel@tonic-gate 	char *input, *output;
467*0Sstevel@tonic-gate 	size_t outsize;
468*0Sstevel@tonic-gate {
469*0Sstevel@tonic-gate 	char *cp, *bp, *limit;
470*0Sstevel@tonic-gate 	char quote;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	dontexpand = 0;
473*0Sstevel@tonic-gate 	/* skip to argument */
474*0Sstevel@tonic-gate 	for (cp = input; *cp != '\0' && isspace((uchar_t)*cp); cp++) {
475*0Sstevel@tonic-gate 		continue;
476*0Sstevel@tonic-gate 		/*LINTED [empty loop body]*/
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 	bp = output;
479*0Sstevel@tonic-gate 	limit = output + outsize - 1; /* -1 for the trailing \0 */
480*0Sstevel@tonic-gate 	while (!isspace((uchar_t)*cp) && *cp != '\0' && bp < limit) {
481*0Sstevel@tonic-gate 		/*
482*0Sstevel@tonic-gate 		 * Handle back slashes.
483*0Sstevel@tonic-gate 		 */
484*0Sstevel@tonic-gate 		if (*cp == '\\') {
485*0Sstevel@tonic-gate 			if (*++cp == '\0') {
486*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
487*0Sstevel@tonic-gate 				    "command lines cannot be continued\n"));
488*0Sstevel@tonic-gate 				continue;
489*0Sstevel@tonic-gate 			}
490*0Sstevel@tonic-gate 			*bp++ = *cp++;
491*0Sstevel@tonic-gate 			continue;
492*0Sstevel@tonic-gate 		}
493*0Sstevel@tonic-gate 		/*
494*0Sstevel@tonic-gate 		 * The usual unquoted case.
495*0Sstevel@tonic-gate 		 */
496*0Sstevel@tonic-gate 		if (*cp != '\'' && *cp != '"') {
497*0Sstevel@tonic-gate 			*bp++ = *cp++;
498*0Sstevel@tonic-gate 			continue;
499*0Sstevel@tonic-gate 		}
500*0Sstevel@tonic-gate 		/*
501*0Sstevel@tonic-gate 		 * Handle single and double quotes.
502*0Sstevel@tonic-gate 		 */
503*0Sstevel@tonic-gate 		quote = *cp++;
504*0Sstevel@tonic-gate 		dontexpand = 1;
505*0Sstevel@tonic-gate 		while (*cp != quote && *cp != '\0' && bp < limit)
506*0Sstevel@tonic-gate 			*bp++ = *cp++;
507*0Sstevel@tonic-gate 		if (*cp++ == '\0') {
508*0Sstevel@tonic-gate 			(void) fprintf(stderr,
509*0Sstevel@tonic-gate 			    gettext("missing %c\n"), (uchar_t)quote);
510*0Sstevel@tonic-gate 			cp--;
511*0Sstevel@tonic-gate 			continue;
512*0Sstevel@tonic-gate 		}
513*0Sstevel@tonic-gate 	}
514*0Sstevel@tonic-gate 	*bp = '\0';
515*0Sstevel@tonic-gate 	if ((strlen(output) + 2) > outsize) {
516*0Sstevel@tonic-gate 		fprintf(stderr, gettext(
517*0Sstevel@tonic-gate 		    "name is too long, ignoring"));
518*0Sstevel@tonic-gate 		memset(output, 0, outsize);
519*0Sstevel@tonic-gate 	} else {
520*0Sstevel@tonic-gate 		/* double null terminate string */
521*0Sstevel@tonic-gate 		*(bp + 1) = '\0';
522*0Sstevel@tonic-gate 	}
523*0Sstevel@tonic-gate 	return (cp);
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate /*
527*0Sstevel@tonic-gate  * Canonicalize file names to always start with ``./'' and
528*0Sstevel@tonic-gate  * remove any imbedded "." and ".." components.
529*0Sstevel@tonic-gate  *
530*0Sstevel@tonic-gate  * The pathname "canonname" is returned double null terminated.
531*0Sstevel@tonic-gate  */
532*0Sstevel@tonic-gate void
canon(rawname,canonname,limit)533*0Sstevel@tonic-gate canon(rawname, canonname, limit)
534*0Sstevel@tonic-gate 	char *rawname, *canonname;
535*0Sstevel@tonic-gate 	size_t limit;
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate 	char *cp, *np, *prefix;
538*0Sstevel@tonic-gate 	uint_t len;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	assert(limit > 3);
541*0Sstevel@tonic-gate 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
542*0Sstevel@tonic-gate 		prefix = "";
543*0Sstevel@tonic-gate 	else if (rawname[0] == '/')
544*0Sstevel@tonic-gate 		prefix = ".";
545*0Sstevel@tonic-gate 	else
546*0Sstevel@tonic-gate 		prefix = "./";
547*0Sstevel@tonic-gate 	(void) snprintf(canonname, limit, "%s%s", prefix, rawname);
548*0Sstevel@tonic-gate 	/*
549*0Sstevel@tonic-gate 	 * Eliminate multiple and trailing '/'s
550*0Sstevel@tonic-gate 	 */
551*0Sstevel@tonic-gate 	for (cp = np = canonname; *np != '\0'; cp++) {
552*0Sstevel@tonic-gate 		*cp = *np++;
553*0Sstevel@tonic-gate 		while (*cp == '/' && *np == '/')
554*0Sstevel@tonic-gate 			np++;
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 	*cp = '\0';
557*0Sstevel@tonic-gate 	if ((strlen(canonname) + 2) > limit) {
558*0Sstevel@tonic-gate 		fprintf(stderr,
559*0Sstevel@tonic-gate 		    gettext("canonical name is too long, ignoring name\n"));
560*0Sstevel@tonic-gate 		memset(canonname, 0, limit);
561*0Sstevel@tonic-gate 	} else {
562*0Sstevel@tonic-gate 		/* double null terminate string */
563*0Sstevel@tonic-gate 		*(cp + 1) = '\0';
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	if (*--cp == '/')
567*0Sstevel@tonic-gate 		*cp = '\0';
568*0Sstevel@tonic-gate 	/*
569*0Sstevel@tonic-gate 	 * Eliminate extraneous "." and ".." from pathnames.  Uses
570*0Sstevel@tonic-gate 	 * memmove(), as strcpy() might do the wrong thing for these
571*0Sstevel@tonic-gate 	 * small overlaps.
572*0Sstevel@tonic-gate 	 */
573*0Sstevel@tonic-gate 	np = canonname;
574*0Sstevel@tonic-gate 	while (*np != '\0') {
575*0Sstevel@tonic-gate 		np++;
576*0Sstevel@tonic-gate 		cp = np;
577*0Sstevel@tonic-gate 		while (*np != '/' && *np != '\0')
578*0Sstevel@tonic-gate 			np++;
579*0Sstevel@tonic-gate 		if (np - cp == 1 && *cp == '.') {
580*0Sstevel@tonic-gate 			cp--;
581*0Sstevel@tonic-gate 			len = strlen(np);
582*0Sstevel@tonic-gate 			(void) memmove(cp, np, len);
583*0Sstevel@tonic-gate 			*(cp + len) = '\0';
584*0Sstevel@tonic-gate 			/* double null terminate string */
585*0Sstevel@tonic-gate 			*(cp + len + 1) = '\0';
586*0Sstevel@tonic-gate 			np = cp;
587*0Sstevel@tonic-gate 		}
588*0Sstevel@tonic-gate 		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
589*0Sstevel@tonic-gate 			cp--;
590*0Sstevel@tonic-gate 			/* find beginning of name */
591*0Sstevel@tonic-gate 			while (cp > &canonname[1] && *--cp != '/') {
592*0Sstevel@tonic-gate 				continue;
593*0Sstevel@tonic-gate 				/*LINTED [empty loop body]*/
594*0Sstevel@tonic-gate 			}
595*0Sstevel@tonic-gate 			len = strlen(np);
596*0Sstevel@tonic-gate 			(void) memmove(cp, np, len);
597*0Sstevel@tonic-gate 			*(cp + len) = '\0';
598*0Sstevel@tonic-gate 			/* double null terminate string */
599*0Sstevel@tonic-gate 			*(cp + len + 1) = '\0';
600*0Sstevel@tonic-gate 			np = cp;
601*0Sstevel@tonic-gate 		}
602*0Sstevel@tonic-gate 	}
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate /*
606*0Sstevel@tonic-gate  * globals (file name generation)
607*0Sstevel@tonic-gate  *
608*0Sstevel@tonic-gate  * "*" in params matches r.e ".*"
609*0Sstevel@tonic-gate  * "?" in params matches r.e. "."
610*0Sstevel@tonic-gate  * "[...]" in params matches character class
611*0Sstevel@tonic-gate  * "[...a-z...]" in params matches a through z.
612*0Sstevel@tonic-gate  */
613*0Sstevel@tonic-gate static void
expandarg(arg,ap)614*0Sstevel@tonic-gate expandarg(arg, ap)
615*0Sstevel@tonic-gate 	char *arg;
616*0Sstevel@tonic-gate 	struct arglist *ap;
617*0Sstevel@tonic-gate {
618*0Sstevel@tonic-gate 	static struct afile single;
619*0Sstevel@tonic-gate 	int size;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	ap->head = ap->last = (struct afile *)0;
622*0Sstevel@tonic-gate 	if (dontexpand)
623*0Sstevel@tonic-gate 		size = 0;
624*0Sstevel@tonic-gate 	else
625*0Sstevel@tonic-gate 		size = expand(arg, 0, ap);
626*0Sstevel@tonic-gate 	if (size == 0) {
627*0Sstevel@tonic-gate 		struct entry *ep;
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 		ep = lookupname(arg);
630*0Sstevel@tonic-gate 		single.fnum = ep ? ep->e_ino : 0;
631*0Sstevel@tonic-gate 		single.fname = savename(arg);
632*0Sstevel@tonic-gate 		ap->head = &single;
633*0Sstevel@tonic-gate 		ap->last = ap->head + 1;
634*0Sstevel@tonic-gate 		return;
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 	if ((ap->last - ap->head) > ULONG_MAX) {
637*0Sstevel@tonic-gate 		(void) fprintf(stderr,
638*0Sstevel@tonic-gate 		    gettext("Argument expansion too large to sort\n"));
639*0Sstevel@tonic-gate 	} else {
640*0Sstevel@tonic-gate 		/* LINTED pointer arith just range-checked */
641*0Sstevel@tonic-gate 		qsort((char *)ap->head, (size_t)(ap->last - ap->head),
642*0Sstevel@tonic-gate 		    sizeof (*ap->head),
643*0Sstevel@tonic-gate 		    (int (*)(const void *, const void *)) fcmp);
644*0Sstevel@tonic-gate 	}
645*0Sstevel@tonic-gate }
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate /*
648*0Sstevel@tonic-gate  * Do an "ls" style listing of a directory
649*0Sstevel@tonic-gate  */
650*0Sstevel@tonic-gate static void
printlist(name,ino,basename,marked_only)651*0Sstevel@tonic-gate printlist(name, ino, basename, marked_only)
652*0Sstevel@tonic-gate 	char *name;
653*0Sstevel@tonic-gate 	ino_t ino;
654*0Sstevel@tonic-gate 	char *basename;
655*0Sstevel@tonic-gate 	int marked_only;
656*0Sstevel@tonic-gate {
657*0Sstevel@tonic-gate 	struct afile *fp;
658*0Sstevel@tonic-gate 	struct direct *dp;
659*0Sstevel@tonic-gate 	static struct arglist alist = { 0, 0, 0, 0, "ls" };
660*0Sstevel@tonic-gate 	struct afile single;
661*0Sstevel@tonic-gate 	struct entry *np;
662*0Sstevel@tonic-gate 	RST_DIR *dirp;
663*0Sstevel@tonic-gate 	int list_entry;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	if ((dirp = rst_opendir(name)) == NULL) {
666*0Sstevel@tonic-gate 		single.fnum = ino;
667*0Sstevel@tonic-gate 		if (strncmp(name, basename, strlen(basename)) == 0)
668*0Sstevel@tonic-gate 			single.fname = savename(name + strlen(basename) + 1);
669*0Sstevel@tonic-gate 		else
670*0Sstevel@tonic-gate 			single.fname = savename(name);
671*0Sstevel@tonic-gate 		alist.head = &single;
672*0Sstevel@tonic-gate 		alist.last = alist.head + 1;
673*0Sstevel@tonic-gate 		if (alist.base != NULL) {
674*0Sstevel@tonic-gate 			free(alist.base);
675*0Sstevel@tonic-gate 			alist.base = NULL;
676*0Sstevel@tonic-gate 		}
677*0Sstevel@tonic-gate 	} else {
678*0Sstevel@tonic-gate 		alist.head = (struct afile *)0;
679*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:\n", name);
680*0Sstevel@tonic-gate 		while (dp = rst_readdir(dirp)) {
681*0Sstevel@tonic-gate 			if (dp == NULL || dp->d_ino == 0) {
682*0Sstevel@tonic-gate 				rst_closedir(dirp);
683*0Sstevel@tonic-gate 				dirp = NULL;
684*0Sstevel@tonic-gate 				break;
685*0Sstevel@tonic-gate 			}
686*0Sstevel@tonic-gate 			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
687*0Sstevel@tonic-gate 				continue;
688*0Sstevel@tonic-gate 			if (vflag == 0 &&
689*0Sstevel@tonic-gate 			    (strcmp(dp->d_name, ".") == 0 ||
690*0Sstevel@tonic-gate 			    strcmp(dp->d_name, "..") == 0))
691*0Sstevel@tonic-gate 				continue;
692*0Sstevel@tonic-gate 			list_entry = 1;
693*0Sstevel@tonic-gate 			if (marked_only) {
694*0Sstevel@tonic-gate 				np = lookupino(dp->d_ino);
695*0Sstevel@tonic-gate 				if ((np == NIL) || ((np->e_flags & NEW) == 0))
696*0Sstevel@tonic-gate 					list_entry = 0;
697*0Sstevel@tonic-gate 			}
698*0Sstevel@tonic-gate 			if (list_entry) {
699*0Sstevel@tonic-gate 				if (!mkentry(dp->d_name, dp->d_ino, &alist)) {
700*0Sstevel@tonic-gate 					rst_closedir(dirp);
701*0Sstevel@tonic-gate 					return;
702*0Sstevel@tonic-gate 				}
703*0Sstevel@tonic-gate 			}
704*0Sstevel@tonic-gate 		}
705*0Sstevel@tonic-gate 	}
706*0Sstevel@tonic-gate 	if (alist.head != 0) {
707*0Sstevel@tonic-gate 		if ((alist.last - alist.head) > ULONG_MAX) {
708*0Sstevel@tonic-gate 			(void) fprintf(stderr,
709*0Sstevel@tonic-gate 			    gettext("Directory too large to sort\n"));
710*0Sstevel@tonic-gate 		} else {
711*0Sstevel@tonic-gate 			qsort((char *)alist.head,
712*0Sstevel@tonic-gate 			    /* LINTED range-checked */
713*0Sstevel@tonic-gate 			    (size_t)(alist.last - alist.head),
714*0Sstevel@tonic-gate 			    sizeof (*alist.head),
715*0Sstevel@tonic-gate 			    (int (*)(const void *, const void *)) fcmp);
716*0Sstevel@tonic-gate 		}
717*0Sstevel@tonic-gate 		formatf(&alist);
718*0Sstevel@tonic-gate 		for (fp = alist.head; fp < alist.last; fp++)
719*0Sstevel@tonic-gate 			freename(fp->fname);
720*0Sstevel@tonic-gate 		alist.head = NULL;
721*0Sstevel@tonic-gate 		/*
722*0Sstevel@tonic-gate 		 * Don't free alist.base, as we'll probably be called
723*0Sstevel@tonic-gate 		 * again, and might as well re-use what we've got.
724*0Sstevel@tonic-gate 		 */
725*0Sstevel@tonic-gate 	}
726*0Sstevel@tonic-gate 	if (dirp != NULL) {
727*0Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
728*0Sstevel@tonic-gate 		rst_closedir(dirp);
729*0Sstevel@tonic-gate 	}
730*0Sstevel@tonic-gate }
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate /*
733*0Sstevel@tonic-gate  * Print out a pretty listing of a directory
734*0Sstevel@tonic-gate  */
735*0Sstevel@tonic-gate static void
formatf(ap)736*0Sstevel@tonic-gate formatf(ap)
737*0Sstevel@tonic-gate 	struct arglist *ap;
738*0Sstevel@tonic-gate {
739*0Sstevel@tonic-gate 	struct afile *fp;
740*0Sstevel@tonic-gate 	struct entry *np;
741*0Sstevel@tonic-gate 	/* LINTED: result fits into an int */
742*0Sstevel@tonic-gate 	int nentry = (int)(ap->last - ap->head);
743*0Sstevel@tonic-gate 	int i, j;
744*0Sstevel@tonic-gate 	uint_t len, w, width = 0, columns, lines;
745*0Sstevel@tonic-gate 	char *cp;
746*0Sstevel@tonic-gate 	FILE *output = stderr;
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	if (ap->head == ap->last)
749*0Sstevel@tonic-gate 		return;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 	if (paginating) {
752*0Sstevel@tonic-gate 		int fds[2];
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 		if (pipe(fds) < 0) {
755*0Sstevel@tonic-gate 			perror(gettext("could not create pipe"));
756*0Sstevel@tonic-gate 			goto no_page;
757*0Sstevel@tonic-gate 		}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 		switch (fork()) {
760*0Sstevel@tonic-gate 		case -1:
761*0Sstevel@tonic-gate 			perror(gettext("could not fork"));
762*0Sstevel@tonic-gate 			goto no_page;
763*0Sstevel@tonic-gate 		case 0:
764*0Sstevel@tonic-gate 			/*
765*0Sstevel@tonic-gate 			 * Make sure final output still ends up in
766*0Sstevel@tonic-gate 			 * the same place.
767*0Sstevel@tonic-gate 			 */
768*0Sstevel@tonic-gate 			(void) dup2(fileno(stderr), fileno(stdout));
769*0Sstevel@tonic-gate 			(void) close(fds[0]);
770*0Sstevel@tonic-gate 			(void) dup2(fds[1], fileno(stdin));
771*0Sstevel@tonic-gate 			execvp(pager_vector[0], pager_vector);
772*0Sstevel@tonic-gate 			perror(gettext("execvp of pager failed"));
773*0Sstevel@tonic-gate 			exit(1);
774*0Sstevel@tonic-gate 			/*NOTREACHED*/
775*0Sstevel@tonic-gate 		default:
776*0Sstevel@tonic-gate 			(void) close(fds[1]);
777*0Sstevel@tonic-gate 			output = fdopen(fds[0], "w");
778*0Sstevel@tonic-gate 			if (output != (FILE *)NULL) {
779*0Sstevel@tonic-gate 				break;
780*0Sstevel@tonic-gate 			}
781*0Sstevel@tonic-gate 			perror(gettext("could not open pipe to pager"));
782*0Sstevel@tonic-gate 			output = stderr;
783*0Sstevel@tonic-gate 		no_page:
784*0Sstevel@tonic-gate 			(void) fprintf(stderr,
785*0Sstevel@tonic-gate 			    gettext("pagination disabled\n"));
786*0Sstevel@tonic-gate 			paginating = 0;
787*0Sstevel@tonic-gate 		}
788*0Sstevel@tonic-gate 	}
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	for (fp = ap->head; fp < ap->last; fp++) {
791*0Sstevel@tonic-gate 		fp->ftype = inodetype(fp->fnum);
792*0Sstevel@tonic-gate 		np = lookupino(fp->fnum);
793*0Sstevel@tonic-gate 		if (np != NIL)
794*0Sstevel@tonic-gate 			fp->fflags = np->e_flags;
795*0Sstevel@tonic-gate 		else
796*0Sstevel@tonic-gate 			fp->fflags = 0;
797*0Sstevel@tonic-gate 		len = strlen(fmtentry(fp));
798*0Sstevel@tonic-gate 		if (len > width)
799*0Sstevel@tonic-gate 			width = len;
800*0Sstevel@tonic-gate 	}
801*0Sstevel@tonic-gate 	width += 2;
802*0Sstevel@tonic-gate 	columns = 80 / width;
803*0Sstevel@tonic-gate 	if (columns == 0)
804*0Sstevel@tonic-gate 		columns = 1;
805*0Sstevel@tonic-gate 	lines = (nentry + columns - 1) / columns;
806*0Sstevel@tonic-gate 	for (i = 0; i < lines && !ferror(output); i++) {
807*0Sstevel@tonic-gate 		for (j = 0; j < columns && !ferror(output); j++) {
808*0Sstevel@tonic-gate 			fp = ap->head + j * lines + i;
809*0Sstevel@tonic-gate 			cp = fmtentry(fp);
810*0Sstevel@tonic-gate 			(void) fprintf(output, "%s", cp);
811*0Sstevel@tonic-gate 			if (fp + lines >= ap->last) {
812*0Sstevel@tonic-gate 				(void) fprintf(output, "\n");
813*0Sstevel@tonic-gate 				break;
814*0Sstevel@tonic-gate 			}
815*0Sstevel@tonic-gate 			w = strlen(cp);
816*0Sstevel@tonic-gate 			while (w < width) {
817*0Sstevel@tonic-gate 				w++;
818*0Sstevel@tonic-gate 				if (fprintf(output, " ") < 0)
819*0Sstevel@tonic-gate 					break;
820*0Sstevel@tonic-gate 			}
821*0Sstevel@tonic-gate 		}
822*0Sstevel@tonic-gate 	}
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 	if (paginating) {
825*0Sstevel@tonic-gate 		(void) fclose(output);
826*0Sstevel@tonic-gate 		(void) wait((int *)NULL);
827*0Sstevel@tonic-gate 	}
828*0Sstevel@tonic-gate }
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate /*
831*0Sstevel@tonic-gate  * Comparison routine for qsort.
832*0Sstevel@tonic-gate  */
833*0Sstevel@tonic-gate static int
fcmp(f1,f2)834*0Sstevel@tonic-gate fcmp(f1, f2)
835*0Sstevel@tonic-gate 	struct afile *f1, *f2;
836*0Sstevel@tonic-gate {
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	return (strcoll(f1->fname, f2->fname));
839*0Sstevel@tonic-gate }
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate /*
842*0Sstevel@tonic-gate  * Format a directory entry.
843*0Sstevel@tonic-gate  */
844*0Sstevel@tonic-gate static char *
fmtentry(fp)845*0Sstevel@tonic-gate fmtentry(fp)
846*0Sstevel@tonic-gate 	struct afile *fp;
847*0Sstevel@tonic-gate {
848*0Sstevel@tonic-gate 	static char fmtres[MAXCOMPLEXLEN];
849*0Sstevel@tonic-gate 	static int precision = 0;
850*0Sstevel@tonic-gate 	ino_t i;
851*0Sstevel@tonic-gate 	char *cp, *dp, *limit;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 	if (!vflag) {
854*0Sstevel@tonic-gate 		/* MAXCOMPLEXLEN assumed to be >= 1 */
855*0Sstevel@tonic-gate 		fmtres[0] = '\0';
856*0Sstevel@tonic-gate 	} else {
857*0Sstevel@tonic-gate 		if (precision == 0) {
858*0Sstevel@tonic-gate 			for (i = maxino; i != 0; i /= 10)
859*0Sstevel@tonic-gate 				precision++;
860*0Sstevel@tonic-gate 			if (sizeof (fmtres) < (unsigned)(precision + 2)) {
861*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
862*0Sstevel@tonic-gate "\nInternal check failed, minimum width %d exceeds available size %d\n"),
863*0Sstevel@tonic-gate 				    (precision + 2), sizeof (fmtres));
864*0Sstevel@tonic-gate 				done(1);
865*0Sstevel@tonic-gate 			}
866*0Sstevel@tonic-gate 		}
867*0Sstevel@tonic-gate 		(void) snprintf(fmtres, sizeof (fmtres), "%*ld ",
868*0Sstevel@tonic-gate 		    precision, fp->fnum);
869*0Sstevel@tonic-gate 	}
870*0Sstevel@tonic-gate 	dp = &fmtres[strlen(fmtres)];
871*0Sstevel@tonic-gate 	limit = fmtres + sizeof (fmtres) - 1;
872*0Sstevel@tonic-gate 	if (dflag && BIT(fp->fnum, dumpmap) == 0)
873*0Sstevel@tonic-gate 		*dp++ = '^';
874*0Sstevel@tonic-gate 	else if ((fp->fflags & NEW) != 0)
875*0Sstevel@tonic-gate 		*dp++ = '*';
876*0Sstevel@tonic-gate 	else
877*0Sstevel@tonic-gate 		*dp++ = ' ';
878*0Sstevel@tonic-gate 	for (cp = fp->fname; *cp && dp < limit; cp++)
879*0Sstevel@tonic-gate 		/* LINTED: precedence ok, can't fix system macro */
880*0Sstevel@tonic-gate 		if (!vflag && (!ISPRINT(*cp, wp)))
881*0Sstevel@tonic-gate 			*dp++ = '?';
882*0Sstevel@tonic-gate 		else
883*0Sstevel@tonic-gate 			*dp++ = *cp;
884*0Sstevel@tonic-gate 	if (fp->ftype == NODE && dp < limit)
885*0Sstevel@tonic-gate 		*dp++ = '/';
886*0Sstevel@tonic-gate 	*dp++ = 0;
887*0Sstevel@tonic-gate 	return (fmtres);
888*0Sstevel@tonic-gate }
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate /*
891*0Sstevel@tonic-gate  * respond to interrupts
892*0Sstevel@tonic-gate  */
893*0Sstevel@tonic-gate /* ARGSUSED */
894*0Sstevel@tonic-gate void
onintr(sig)895*0Sstevel@tonic-gate onintr(sig)
896*0Sstevel@tonic-gate 	int	sig;
897*0Sstevel@tonic-gate {
898*0Sstevel@tonic-gate 	char	buf[300];
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	if (command == 'i' && reset_OK)
901*0Sstevel@tonic-gate 		longjmp(reset, 1);
902*0Sstevel@tonic-gate 
903*0Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf),
904*0Sstevel@tonic-gate 	    gettext("%s interrupted, continue"), progname);
905*0Sstevel@tonic-gate 	if (reply(buf) == FAIL)
906*0Sstevel@tonic-gate 		done(1);
907*0Sstevel@tonic-gate }
908*0Sstevel@tonic-gate /*
909*0Sstevel@tonic-gate  * Set up pager_catenated and pager_vector.
910*0Sstevel@tonic-gate  */
911*0Sstevel@tonic-gate void
912*0Sstevel@tonic-gate #ifdef __STDC__
initpagercmd(void)913*0Sstevel@tonic-gate initpagercmd(void)
914*0Sstevel@tonic-gate #else
915*0Sstevel@tonic-gate initpagercmd()
916*0Sstevel@tonic-gate #endif
917*0Sstevel@tonic-gate {
918*0Sstevel@tonic-gate 	char *cp;
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	cp = getenv("PAGER");
921*0Sstevel@tonic-gate 	if (cp != NULL)
922*0Sstevel@tonic-gate 		pager_catenated = strdup(cp);
923*0Sstevel@tonic-gate 	if ((pager_catenated == NULL) || (*pager_catenated == '\0')) {
924*0Sstevel@tonic-gate 		if (pager_catenated != NULL)
925*0Sstevel@tonic-gate 			free(pager_catenated);
926*0Sstevel@tonic-gate 		pager_catenated = strdup(DEF_PAGER);
927*0Sstevel@tonic-gate 	}
928*0Sstevel@tonic-gate 	if (pager_catenated == NULL) {
929*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
930*0Sstevel@tonic-gate 		done(1);
931*0Sstevel@tonic-gate 	}
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 	pager_vector = (char **)malloc(sizeof (char *));
934*0Sstevel@tonic-gate 	if (pager_vector == NULL) {
935*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
936*0Sstevel@tonic-gate 		done(1);
937*0Sstevel@tonic-gate 	}
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	pager_len = 1;
940*0Sstevel@tonic-gate 	cp = pager_catenated;
941*0Sstevel@tonic-gate 	(void) setpagerargs(&cp);
942*0Sstevel@tonic-gate }
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate /*
946*0Sstevel@tonic-gate  * Resets pager_catenated and pager_vector from user input.
947*0Sstevel@tonic-gate  */
948*0Sstevel@tonic-gate void
949*0Sstevel@tonic-gate #ifdef __STDC__
setpagercmd(void)950*0Sstevel@tonic-gate setpagercmd(void)
951*0Sstevel@tonic-gate #else
952*0Sstevel@tonic-gate setpagercmd()
953*0Sstevel@tonic-gate #endif
954*0Sstevel@tonic-gate {
955*0Sstevel@tonic-gate 	uint_t catenate_length;
956*0Sstevel@tonic-gate 	int index;
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	/*
959*0Sstevel@tonic-gate 	 * We'll get called immediately after setting a pager, due to
960*0Sstevel@tonic-gate 	 * our interaction with getcmd()'s internal state.  Don't do
961*0Sstevel@tonic-gate 	 * anything when that happens.
962*0Sstevel@tonic-gate 	 */
963*0Sstevel@tonic-gate 	if (*input == '\0')
964*0Sstevel@tonic-gate 		return;
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 	if (pager_len > 0) {
967*0Sstevel@tonic-gate 		for (index = 0; pager_vector[index] != (char *)NULL; index += 1)
968*0Sstevel@tonic-gate 			free(pager_vector[index]);
969*0Sstevel@tonic-gate 		free(pager_vector);
970*0Sstevel@tonic-gate 		free(pager_catenated);
971*0Sstevel@tonic-gate 	}
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 	pager_vector = (char **)malloc(2 * sizeof (char *));
974*0Sstevel@tonic-gate 	if (pager_vector == NULL) {
975*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
976*0Sstevel@tonic-gate 		done(1);
977*0Sstevel@tonic-gate 	}
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate 	pager_len = 2;
980*0Sstevel@tonic-gate 	pager_vector[0] = strdup(input);
981*0Sstevel@tonic-gate 	if (pager_vector[0] == NULL) {
982*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
983*0Sstevel@tonic-gate 		done(1);
984*0Sstevel@tonic-gate 	}
985*0Sstevel@tonic-gate 	if (dflag)
986*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("got command `%s'\n"), input);
987*0Sstevel@tonic-gate 	catenate_length = setpagerargs(&nextarg) + strlen(pager_vector[0]) + 1;
988*0Sstevel@tonic-gate 	pager_catenated = (char *)malloc(catenate_length *
989*0Sstevel@tonic-gate 		(size_t)sizeof (char));
990*0Sstevel@tonic-gate 	if (pager_catenated == (char *)NULL) {
991*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
992*0Sstevel@tonic-gate 		done(1);
993*0Sstevel@tonic-gate 	}
994*0Sstevel@tonic-gate 	for (index = 0; pager_vector[index] != (char *)NULL; index += 1) {
995*0Sstevel@tonic-gate 		if (index > 0)
996*0Sstevel@tonic-gate 			(void) strcat(pager_catenated, " ");
997*0Sstevel@tonic-gate 		(void) strcat(pager_catenated, pager_vector[index]);
998*0Sstevel@tonic-gate 	}
999*0Sstevel@tonic-gate }
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate /*
1003*0Sstevel@tonic-gate  * Extract arguments for the pager command from getcmd()'s input buffer.
1004*0Sstevel@tonic-gate  */
1005*0Sstevel@tonic-gate static uint_t
setpagerargs(source)1006*0Sstevel@tonic-gate setpagerargs(source)
1007*0Sstevel@tonic-gate 	char	**source;
1008*0Sstevel@tonic-gate {
1009*0Sstevel@tonic-gate 	char	word[MAXCOMPLEXLEN];
1010*0Sstevel@tonic-gate 	char	*cp = *source;
1011*0Sstevel@tonic-gate 	uint_t	length = 0;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	while ((cp != (char *)NULL) && (*cp != '\0')) {
1014*0Sstevel@tonic-gate 		cp = copynext(cp, word, sizeof (word));
1015*0Sstevel@tonic-gate 		if (dflag)
1016*0Sstevel@tonic-gate 			fprintf(stderr, gettext("got word `%s'\n"), word);
1017*0Sstevel@tonic-gate 		pager_vector = (char **)realloc(pager_vector,
1018*0Sstevel@tonic-gate 			(size_t)sizeof (char *) * (pager_len + 1));
1019*0Sstevel@tonic-gate 		if (pager_vector == (char **)NULL) {
1020*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1021*0Sstevel@tonic-gate 			done(1);
1022*0Sstevel@tonic-gate 		}
1023*0Sstevel@tonic-gate 		pager_vector[pager_len - 1] = strdup(word);
1024*0Sstevel@tonic-gate 		if (pager_vector[pager_len - 1] == (char *)NULL) {
1025*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1026*0Sstevel@tonic-gate 			done(1);
1027*0Sstevel@tonic-gate 		}
1028*0Sstevel@tonic-gate 		length += strlen(word) + 1;
1029*0Sstevel@tonic-gate 		pager_len += 1;
1030*0Sstevel@tonic-gate 	}
1031*0Sstevel@tonic-gate 	pager_vector[pager_len - 1] = (char *)NULL;
1032*0Sstevel@tonic-gate 	*source = cp;
1033*0Sstevel@tonic-gate 	return (length);
1034*0Sstevel@tonic-gate }
1035