summaryrefslogtreecommitdiff
path: root/usr/src/cmd/backup/restore/interactive.c
blob: 7c4be452554b5b65849014d2c6946982851624a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 * Copyright 1998,2001-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <setjmp.h>
#include <euc.h>
#include <widec.h>
#include "restore.h"
#include <ctype.h>
#include <limits.h>
#include <sys/wait.h>

extern eucwidth_t wp;

#define	round(a, b) ((((a) + (b) - 1) / (b)) * (b))

/*
 * Things to handle interruptions.
 */
static jmp_buf reset;
static int reset_OK;
static char *nextarg = NULL;

static int dontexpand;	/* co-routine state set in getnext, used in expandarg */

static void getcmd(char *, char *, size_t, char *, size_t, struct arglist *);
static void expandarg(char *, struct arglist *);
static void printlist(char *, ino_t, char *, int);
static void formatf(struct arglist *);
static char *copynext(char *, char *, size_t);
static int fcmp(struct afile *, struct afile *);
static char *fmtentry(struct afile *);
static void setpagercmd(void);
static uint_t setpagerargs(char **);

/*
 * Read and execute commands from the terminal.
 */
void
runcmdshell(void)
{
	struct entry *np;
	ino_t ino;
	static struct arglist alist = { 0, 0, 0, 0, 0 };
	char curdir[MAXCOMPLEXLEN];
	char name[MAXCOMPLEXLEN];
	char cmd[BUFSIZ];

#ifdef	lint
	curdir[0] = '\0';
#endif	/* lint */

	canon("/", curdir, sizeof (curdir));
loop:
	if (setjmp(reset) != 0) {
		for (; alist.head < alist.last; alist.head++)
			freename(alist.head->fname);
		nextarg = NULL;
		volno = 0;
		goto loop;	/* make sure jmpbuf is up-to-date */
	}
	reset_OK = 1;
	getcmd(curdir, cmd, sizeof (cmd), name, sizeof (name), &alist);

	/*
	 * Using strncmp() to catch unique prefixes.
	 */
	switch (cmd[0]) {
	/*
	 * Add elements to the extraction list.
	 */
	case 'a':
		if (strncmp(cmd, "add", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		if (mflag)
			pathcheck(name);
		treescan(name, ino, addfile);
		break;
	/*
	 * Change working directory.
	 */
	case 'c':
		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		if (inodetype(ino) == LEAF) {
			(void) fprintf(stderr,
			    gettext("%s: not a directory\n"), name);
			break;
		}

		/* No need to canon(name), getcmd() did it for us */
		(void) strncpy(curdir, name, sizeof (curdir));
		curdir[sizeof (curdir) - 1] = '\0';
		break;
	/*
	 * Delete elements from the extraction list.
	 */
	case 'd':
		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		np = lookupname(name);
		if (np == NIL || (np->e_flags & NEW) == 0) {
			(void) fprintf(stderr,
			    gettext("%s: not on extraction list\n"), name);
			break;
		}
		treescan(name, np->e_ino, deletefile);
		break;
	/*
	 * Extract the requested list.
	 */
	case 'e':
		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
			goto bad;
		attrscan(0, addfile);
		createfiles();
		createlinks();
		setdirmodes();
		if (dflag)
			checkrestore();
		volno = 0;
		break;
	/*
	 * List available commands.
	 */
	case 'h':
		if (strncmp(cmd, "help", strlen(cmd)) != 0)
			goto bad;
		/*FALLTHROUGH*/
	case '?':
		/* ANSI string catenation, to shut cstyle up */
		(void) fprintf(stderr, "%s",
		    gettext("Available commands are:\n"
"\tls [arg] - list directory\n"
"\tmarked [arg] - list items marked for extraction from directory\n"
"\tcd arg - change directory\n"
"\tpwd - print current directory\n"
"\tadd [arg] - add `arg' to list of files to be extracted\n"
"\tdelete [arg] - delete `arg' from list of files to be extracted\n"
"\textract - extract requested files\n"
"\tsetmodes - set modes of requested directories\n"
"\tquit - immediately exit program\n"
"\twhat - list dump header information\n"
"\tverbose - toggle verbose flag (useful with ``ls'')\n"
"\tpaginate - toggle pagination flag (affects ``ls'' and ``marked'')\n"
"\tsetpager - set pagination command and arguments\n"
"\thelp or `?' - print this list\n"
"If no `arg' is supplied, the current directory is used\n"));
		break;
	/*
	 * List a directory.
	 */
	case 'l':
	case 'm':
		if ((strncmp(cmd, "ls", strlen(cmd)) != 0) &&
		    (strncmp(cmd, "marked", strlen(cmd)) != 0))
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		printlist(name, ino, curdir, *cmd == 'm');
		break;
	/*
	 * Print current directory or enable pagination.
	 */
	case 'p':
		if (strlen(cmd) < 2)
			goto ambiguous;
		if (strncmp(cmd, "pwd", strlen(cmd)) == 0) {
			if (curdir[1] == '\0') {
				(void) fprintf(stderr, "/\n");
			} else {
				(void) fprintf(stderr, "%s\n", &curdir[1]);
			}
		} else if (strncmp(cmd, "paginate", strlen(cmd)) == 0) {
			if (paginating) {
				(void) fprintf(stderr,
				    gettext("paging disabled\n"));
				paginating = 0;
				break;
			}
			if (vflag) {
				(void) fprintf(stderr,
				    gettext("paging enabled (%s)\n"),
				    pager_catenated);
			} else {
				(void) fprintf(stderr,
				    gettext("paging enabled\n"));
			}
			if (dflag) {
				int index = 0;

				while (index < pager_len) {
					(void) fprintf(stderr,
					    ">>>pager_vector[%d] = `%s'\n",
					    index,
					    pager_vector[index] ?
					    pager_vector[index] : "(null)");
					index += 1;
				}
			}
			paginating = 1;
		} else {
			goto bad;
		}
		break;
	/*
	 * Quit.
	 */
	case 'q':
		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
			goto bad;
		reset_OK = 0;
		return;
	case 'x':
		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
			goto bad;
		reset_OK = 0;
		return;
	/*
	 * Toggle verbose mode.
	 */
	case 'v':
		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
			goto bad;
		if (vflag) {
			(void) fprintf(stderr, gettext("verbose mode off\n"));
			vflag = 0;
			break;
		}
		(void) fprintf(stderr, gettext("verbose mode on\n"));
		vflag = 1;
		break;
	/*
	 * Just restore requested directory modes, or set pagination command.
	 */
	case 's':
		if (strlen(cmd) < 4)
			goto ambiguous;
		if (strncmp(cmd, "setmodes", strlen(cmd)) == 0) {
			setdirmodes();
		} else if (strncmp(cmd, "setpager", strlen(cmd)) == 0) {
			setpagercmd();
		} else {
			goto bad;
		}
		break;
	/*
	 * Print out dump header information.
	 */
	case 'w':
		if (strncmp(cmd, "what", strlen(cmd)) != 0)
			goto bad;
		printdumpinfo();
		break;
	/*
	 * Turn on debugging.
	 */
	case 'D':
		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
			goto bad;
		if (dflag) {
			(void) fprintf(stderr, gettext("debugging mode off\n"));
			dflag = 0;
			break;
		}
		(void) fprintf(stderr, gettext("debugging mode on\n"));
		dflag++;
		break;
	/*
	 * Unknown command.
	 */
	default:
	bad:
		(void) fprintf(stderr,
		    gettext("%s: unknown command; type ? for help\n"), cmd);
		break;
	ambiguous:
		(void) fprintf(stderr,
		    gettext("%s: ambiguous command; type ? for help\n"), cmd);
		break;
	}
	goto loop;
}

static char input[MAXCOMPLEXLEN]; /* shared by getcmd() and setpagercmd() */
#define	rawname input	/* save space by reusing input buffer */

/*
 * Read and parse an interactive command.
 * The first word on the line is assigned to "cmd". If
 * there are no arguments on the command line, then "curdir"
 * is returned as the argument. If there are arguments
 * on the line they are returned one at a time on each
 * successive call to getcmd. Each argument is first assigned
 * to "name". If it does not start with "/" the pathname in
 * "curdir" is prepended to it. Finally "canon" is called to
 * eliminate any embedded ".." components.
 */
/* ARGSUSED */
static void
getcmd(char *curdir, char *cmd, size_t cmdsiz, char *name, size_t namesiz,
    struct arglist *ap)
{
	char *cp;
	char output[MAXCOMPLEXLEN];

	/*
	 * Check to see if still processing arguments.
	 */
	if (ap->head != ap->last) {
		(void) strncpy(name, ap->head->fname, namesiz);
		name[namesiz - 1] = '\0';
		/* double null terminate string */
		if ((strlen(name) + 2) > namesiz) {
			fprintf(stderr, gettext("name is too long, ignoring"));
			memset(name, 0, namesiz);
		} else {
			name[strlen(name) + 1] = '\0';
		}
		freename(ap->head->fname);
		ap->head++;
		return;
	}
	if (nextarg != NULL)
		goto getnext;
	/*
	 * Read a command line and trim off trailing white space.
	 */
readagain:
	do {
		(void) fprintf(stderr, "%s > ", progname);
		(void) fflush(stderr);
		(void) fgets(input, sizeof (input), terminal);
	} while (!feof(terminal) && input[0] == '\n');
	if (feof(terminal)) {
		(void) strncpy(cmd, "quit", cmdsiz);
		return;
	}
	/* trim off trailing white space and newline */
	for (cp = &input[strlen(input) - 2];
	    cp >= &input[0] && isspace((uchar_t)*cp);
	    cp--) {
		continue;
		/*LINTED [empty loop body]*/
	}
	*++cp = '\0';
	if ((strlen(input) + 2) > MAXCOMPLEXLEN) {
		fprintf(stderr, gettext("command is too long\n"));
		goto readagain;
	} else {
		/* double null terminate string */
		*(cp + 1) = '\0';
	}

	if (cp == &input[0])
		goto readagain;

	/*
	 * Copy the command into "cmd".
	 */
	cp = copynext(input, cmd, cmdsiz);
	ap->cmd = cmd;
	/*
	 * If no argument, use curdir as the default.
	 */
	if (*cp == '\0') {
		(void) strncpy(name, curdir, namesiz);
		name[namesiz - 1] = '\0';
		/* double null terminate string */
		if ((strlen(name) + 2) > namesiz) {
			fprintf(stderr, gettext("name is too long, ignoring"));
			memset(name, 0, namesiz);
		} else {
			name[strlen(name) + 1] = '\0';
		}
		return;
	}
	nextarg = cp;
	/*
	 * Find the next argument.
	 */
getnext:
	cp = copynext(nextarg, rawname, sizeof (rawname));
	if (*cp == '\0')
		nextarg = NULL;
	else
		nextarg = cp;
	/*
	 * If it an absolute pathname, canonicalize it and return it.
	 */
	if (rawname[0] == '/') {
		canon(rawname, name, namesiz);
	} else {
		/*
		 * For relative pathnames, prepend the current directory to
		 * it then canonicalize and return it.
		 */
		(void) snprintf(output, sizeof (output), "%s/%s",
		    curdir, rawname);
		canon(output, name, namesiz);
	}
	expandarg(name, ap);
	/*
	 * ap->head->fname guaranteed to be double null-terminated and
	 * no more than MAXCOMPLEXLEN characters long.
	 */
	assert(namesiz >= (MAXCOMPLEXLEN));
	(void) strcpy(name, ap->head->fname);
	/* double null terminate string */
	name[strlen(name) + 1] = '\0';
	freename(ap->head->fname);
	ap->head++;
#undef	rawname
}

/*
 * Strip off the next token of the input.
 */
static char *
copynext(char *input, char *output, size_t outsize)
{
	char *cp, *bp, *limit;
	char quote;

	dontexpand = 0;
	/* skip to argument */
	for (cp = input; *cp != '\0' && isspace((uchar_t)*cp); cp++) {
		continue;
		/*LINTED [empty loop body]*/
	}
	bp = output;
	limit = output + outsize - 1; /* -1 for the trailing \0 */
	while (!isspace((uchar_t)*cp) && *cp != '\0' && bp < limit) {
		/*
		 * Handle back slashes.
		 */
		if (*cp == '\\') {
			if (*++cp == '\0') {
				(void) fprintf(stderr, gettext(
				    "command lines cannot be continued\n"));
				continue;
			}
			*bp++ = *cp++;
			continue;
		}
		/*
		 * The usual unquoted case.
		 */
		if (*cp != '\'' && *cp != '"') {
			*bp++ = *cp++;
			continue;
		}
		/*
		 * Handle single and double quotes.
		 */
		quote = *cp++;
		dontexpand = 1;
		while (*cp != quote && *cp != '\0' && bp < limit)
			*bp++ = *cp++;
		if (*cp++ == '\0') {
			(void) fprintf(stderr,
			    gettext("missing %c\n"), (uchar_t)quote);
			cp--;
			continue;
		}
	}
	*bp = '\0';
	if ((strlen(output) + 2) > outsize) {
		fprintf(stderr, gettext(
		    "name is too long, ignoring"));
		memset(output, 0, outsize);
	} else {
		/* double null terminate string */
		*(bp + 1) = '\0';
	}
	return (cp);
}

/*
 * Canonicalize file names to always start with ``./'' and
 * remove any imbedded "." and ".." components.
 *
 * The pathname "canonname" is returned double null terminated.
 */
void
canon(char *rawname, char *canonname, size_t limit)
{
	char *cp, *np, *prefix;
	uint_t len;

	assert(limit > 3);
	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
		prefix = "";
	else if (rawname[0] == '/')
		prefix = ".";
	else
		prefix = "./";
	(void) snprintf(canonname, limit, "%s%s", prefix, rawname);
	/*
	 * Eliminate multiple and trailing '/'s
	 */
	for (cp = np = canonname; *np != '\0'; cp++) {
		*cp = *np++;
		while (*cp == '/' && *np == '/')
			np++;
	}
	*cp = '\0';
	if ((strlen(canonname) + 2) > limit) {
		fprintf(stderr,
		    gettext("canonical name is too long, ignoring name\n"));
		memset(canonname, 0, limit);
	} else {
		/* double null terminate string */
		*(cp + 1) = '\0';
	}

	if (*--cp == '/')
		*cp = '\0';
	/*
	 * Eliminate extraneous "." and ".." from pathnames.  Uses
	 * memmove(), as strcpy() might do the wrong thing for these
	 * small overlaps.
	 */
	np = canonname;
	while (*np != '\0') {
		np++;
		cp = np;
		while (*np != '/' && *np != '\0')
			np++;
		if (np - cp == 1 && *cp == '.') {
			cp--;
			len = strlen(np);
			(void) memmove(cp, np, len);
			*(cp + len) = '\0';
			/* double null terminate string */
			*(cp + len + 1) = '\0';
			np = cp;
		}
		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
			cp--;
			/* find beginning of name */
			while (cp > &canonname[1] && *--cp != '/') {
				continue;
				/*LINTED [empty loop body]*/
			}
			len = strlen(np);
			(void) memmove(cp, np, len);
			*(cp + len) = '\0';
			/* double null terminate string */
			*(cp + len + 1) = '\0';
			np = cp;
		}
	}
}

/*
 * globals (file name generation)
 *
 * "*" in params matches r.e ".*"
 * "?" in params matches r.e. "."
 * "[...]" in params matches character class
 * "[...a-z...]" in params matches a through z.
 */
static void
expandarg(char *arg, struct arglist *ap)
{
	static struct afile single;
	int size;

	ap->head = ap->last = (struct afile *)0;
	if (dontexpand)
		size = 0;
	else
		size = expand(arg, 0, ap);
	if (size == 0) {
		struct entry *ep;

		ep = lookupname(arg);
		single.fnum = ep ? ep->e_ino : 0;
		single.fname = savename(arg);
		ap->head = &single;
		ap->last = ap->head + 1;
		return;
	}
	if ((ap->last - ap->head) > ULONG_MAX) {
		(void) fprintf(stderr,
		    gettext("Argument expansion too large to sort\n"));
	} else {
		/* LINTED pointer arith just range-checked */
		qsort((char *)ap->head, (size_t)(ap->last - ap->head),
		    sizeof (*ap->head),
		    (int (*)(const void *, const void *)) fcmp);
	}
}

/*
 * Do an "ls" style listing of a directory
 */
static void
printlist(char *name, ino_t ino, char *basename, int marked_only)
{
	struct afile *fp;
	struct direct *dp;
	static struct arglist alist = { 0, 0, 0, 0, "ls" };
	struct afile single;
	struct entry *np;
	RST_DIR *dirp;
	int list_entry;

	if ((dirp = rst_opendir(name)) == NULL) {
		single.fnum = ino;
		if (strncmp(name, basename, strlen(basename)) == 0)
			single.fname = savename(name + strlen(basename) + 1);
		else
			single.fname = savename(name);
		alist.head = &single;
		alist.last = alist.head + 1;
		if (alist.base != NULL) {
			free(alist.base);
			alist.base = NULL;
		}
	} else {
		alist.head = (struct afile *)0;
		(void) fprintf(stderr, "%s:\n", name);
		while (dp = rst_readdir(dirp)) {
			if (dp == NULL || dp->d_ino == 0) {
				rst_closedir(dirp);
				dirp = NULL;
				break;
			}
			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
				continue;
			if (vflag == 0 &&
			    (strcmp(dp->d_name, ".") == 0 ||
			    strcmp(dp->d_name, "..") == 0))
				continue;
			list_entry = 1;
			if (marked_only) {
				np = lookupino(dp->d_ino);
				if ((np == NIL) || ((np->e_flags & NEW) == 0))
					list_entry = 0;
			}
			if (list_entry) {
				if (!mkentry(dp->d_name, dp->d_ino, &alist)) {
					rst_closedir(dirp);
					return;
				}
			}
		}
	}
	if (alist.head != 0) {
		if ((alist.last - alist.head) > ULONG_MAX) {
			(void) fprintf(stderr,
			    gettext("Directory too large to sort\n"));
		} else {
			qsort((char *)alist.head,
			    /* LINTED range-checked */
			    (size_t)(alist.last - alist.head),
			    sizeof (*alist.head),
			    (int (*)(const void *, const void *)) fcmp);
		}
		formatf(&alist);
		for (fp = alist.head; fp < alist.last; fp++)
			freename(fp->fname);
		alist.head = NULL;
		/*
		 * Don't free alist.base, as we'll probably be called
		 * again, and might as well re-use what we've got.
		 */
	}
	if (dirp != NULL) {
		(void) fprintf(stderr, "\n");
		rst_closedir(dirp);
	}
}

/*
 * Print out a pretty listing of a directory
 */
static void
formatf(struct arglist *ap)
{
	struct afile *fp;
	struct entry *np;
	/* LINTED: result fits into an int */
	int nentry = (int)(ap->last - ap->head);
	int i, j;
	uint_t len, w, width = 0, columns, lines;
	char *cp;
	FILE *output = stderr;

	if (ap->head == ap->last)
		return;

	if (paginating) {
		int fds[2];

		if (pipe(fds) < 0) {
			perror(gettext("could not create pipe"));
			goto no_page;
		}

		switch (fork()) {
		case -1:
			perror(gettext("could not fork"));
			goto no_page;
		case 0:
			/*
			 * Make sure final output still ends up in
			 * the same place.
			 */
			(void) dup2(fileno(stderr), fileno(stdout));
			(void) close(fds[0]);
			(void) dup2(fds[1], fileno(stdin));
			execvp(pager_vector[0], pager_vector);
			perror(gettext("execvp of pager failed"));
			exit(1);
			/*NOTREACHED*/
		default:
			(void) close(fds[1]);
			output = fdopen(fds[0], "w");
			if (output != (FILE *)NULL) {
				break;
			}
			perror(gettext("could not open pipe to pager"));
			output = stderr;
		no_page:
			(void) fprintf(stderr,
			    gettext("pagination disabled\n"));
			paginating = 0;
		}
	}

	for (fp = ap->head; fp < ap->last; fp++) {
		fp->ftype = inodetype(fp->fnum);
		np = lookupino(fp->fnum);
		if (np != NIL)
			fp->fflags = np->e_flags;
		else
			fp->fflags = 0;
		len = strlen(fmtentry(fp));
		if (len > width)
			width = len;
	}
	width += 2;
	columns = 80 / width;
	if (columns == 0)
		columns = 1;
	lines = (nentry + columns - 1) / columns;
	for (i = 0; i < lines && !ferror(output); i++) {
		for (j = 0; j < columns && !ferror(output); j++) {
			fp = ap->head + j * lines + i;
			cp = fmtentry(fp);
			(void) fprintf(output, "%s", cp);
			if (fp + lines >= ap->last) {
				(void) fprintf(output, "\n");
				break;
			}
			w = strlen(cp);
			while (w < width) {
				w++;
				if (fprintf(output, " ") < 0)
					break;
			}
		}
	}

	if (paginating) {
		(void) fclose(output);
		(void) wait((int *)NULL);
	}
}

/*
 * Comparison routine for qsort.
 */
static int
fcmp(struct afile *f1, struct afile *f2)
{

	return (strcoll(f1->fname, f2->fname));
}

/*
 * Format a directory entry.
 */
static char *
fmtentry(struct afile *fp)
{
	static char fmtres[MAXCOMPLEXLEN];
	static int precision = 0;
	ino_t i;
	char *cp, *dp, *limit;

	if (!vflag) {
		/* MAXCOMPLEXLEN assumed to be >= 1 */
		fmtres[0] = '\0';
	} else {
		if (precision == 0) {
			for (i = maxino; i != 0; i /= 10)
				precision++;
			if (sizeof (fmtres) < (unsigned)(precision + 2)) {
				(void) fprintf(stderr, gettext(
"\nInternal check failed, minimum width %d exceeds available size %d\n"),
				    (precision + 2), sizeof (fmtres));
				done(1);
			}
		}
		(void) snprintf(fmtres, sizeof (fmtres), "%*ld ",
		    precision, fp->fnum);
	}
	dp = &fmtres[strlen(fmtres)];
	limit = fmtres + sizeof (fmtres) - 1;
	if (dflag && BIT(fp->fnum, dumpmap) == 0)
		*dp++ = '^';
	else if ((fp->fflags & NEW) != 0)
		*dp++ = '*';
	else
		*dp++ = ' ';
	for (cp = fp->fname; *cp && dp < limit; cp++)
		/* LINTED: precedence ok, can't fix system macro */
		if (!vflag && (!ISPRINT(*cp, wp)))
			*dp++ = '?';
		else
			*dp++ = *cp;
	if (fp->ftype == NODE && dp < limit)
		*dp++ = '/';
	*dp++ = 0;
	return (fmtres);
}

/*
 * respond to interrupts
 */
/* ARGSUSED */
void
onintr(int sig)
{
	char	buf[300];

	if (command == 'i' && reset_OK)
		longjmp(reset, 1);

	(void) snprintf(buf, sizeof (buf),
	    gettext("%s interrupted, continue"), progname);
	if (reply(buf) == FAIL)
		done(1);
}
/*
 * Set up pager_catenated and pager_vector.
 */
void
initpagercmd(void)
{
	char *cp;

	cp = getenv("PAGER");
	if (cp != NULL)
		pager_catenated = strdup(cp);
	if ((pager_catenated == NULL) || (*pager_catenated == '\0')) {
		if (pager_catenated != NULL)
			free(pager_catenated);
		pager_catenated = strdup(DEF_PAGER);
	}
	if (pager_catenated == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_vector = (char **)malloc(sizeof (char *));
	if (pager_vector == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_len = 1;
	cp = pager_catenated;
	(void) setpagerargs(&cp);
}


/*
 * Resets pager_catenated and pager_vector from user input.
 */
void
setpagercmd(void)
{
	uint_t catenate_length;
	int index;

	/*
	 * We'll get called immediately after setting a pager, due to
	 * our interaction with getcmd()'s internal state.  Don't do
	 * anything when that happens.
	 */
	if (*input == '\0')
		return;

	if (pager_len > 0) {
		for (index = 0; pager_vector[index] != (char *)NULL; index += 1)
			free(pager_vector[index]);
		free(pager_vector);
		free(pager_catenated);
	}

	pager_vector = (char **)malloc(2 * sizeof (char *));
	if (pager_vector == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_len = 2;
	pager_vector[0] = strdup(input);
	if (pager_vector[0] == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}
	if (dflag)
		(void) fprintf(stderr, gettext("got command `%s'\n"), input);
	catenate_length = setpagerargs(&nextarg) + strlen(pager_vector[0]) + 1;
	pager_catenated = (char *)malloc(catenate_length *
	    (size_t)sizeof (char));
	if (pager_catenated == (char *)NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}
	for (index = 0; pager_vector[index] != (char *)NULL; index += 1) {
		if (index > 0)
			(void) strcat(pager_catenated, " ");
		(void) strcat(pager_catenated, pager_vector[index]);
	}
}


/*
 * Extract arguments for the pager command from getcmd()'s input buffer.
 */
static uint_t
setpagerargs(char **source)
{
	char	word[MAXCOMPLEXLEN];
	char	*cp = *source;
	uint_t	length = 0;

	while ((cp != (char *)NULL) && (*cp != '\0')) {
		cp = copynext(cp, word, sizeof (word));
		if (dflag)
			fprintf(stderr, gettext("got word `%s'\n"), word);
		pager_vector = (char **)realloc(pager_vector,
		    (size_t)sizeof (char *) * (pager_len + 1));
		if (pager_vector == (char **)NULL) {
			(void) fprintf(stderr, gettext("out of memory\n"));
			done(1);
		}
		pager_vector[pager_len - 1] = strdup(word);
		if (pager_vector[pager_len - 1] == (char *)NULL) {
			(void) fprintf(stderr, gettext("out of memory\n"));
			done(1);
		}
		length += strlen(word) + 1;
		pager_len += 1;
	}
	pager_vector[pager_len - 1] = (char *)NULL;
	*source = cp;
	return (length);
}