File: tree.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (874 lines) | stat: -rw-r--r-- 18,058 bytes parent folder | download | duplicates (3)
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
/*
 *	tree.c
 *
 *	tree mode module
 */

#include "fd.h"
#include "func.h"
#include "kanji.h"

#define	DIRFIELD		3
#define	TREEFIELD		(((dircountlimit > 0) \
				? (n_column * 3) / 5 : n_column) - 2)
#define	FILEFIELD		((dircountlimit > 0) \
				? n_column - ((n_column * 3) / 5) - 3 : 0)
#define	bufpos(y)		(((y) - 1) * (TREEFIELD + 1) * KANAWID)
#define	bufptr(y)		(&(tr_scr[bufpos(y)]))

#ifndef	_NOTREE

extern char fullpath[];
extern int autoupdate;
extern int win_x;
extern int win_y;

static int NEAR evaldir __P_((CONST char *, int));
static treelist *NEAR maketree __P_((CONST char *, treelist *, treelist *,
		int, int *));
static int NEAR _showtree __P_((treelist *, int, int, int));
static VOID NEAR showtree __P_((VOID_A));
static VOID NEAR treebar __P_((VOID_A));
static treelist *NEAR _searchtree __P_((treelist *, int, int));
static VOID NEAR searchtree __P_((VOID_A));
static int NEAR expandtree __P_((treelist *));
static int NEAR expandall __P_((treelist *));
static int NEAR treeup __P_((VOID_A));
static int NEAR treedown __P_((VOID_A));
static int NEAR freetree __P_((treelist *, int));
static int NEAR bottomtree __P_((VOID_A));
static VOID NEAR _tree_search __P_((VOID_A));
static int NEAR _tree_input __P_((VOID_A));
static char *NEAR _tree __P_((VOID_A));

int sorttree = 0;
int dircountlimit = 0;

static int redraw = 0;
static int tr_no = 0;
static int tr_line = 0;
static int tr_top = 0;
static int tr_bottom = 0;
static char *tr_scr = NULL;
static treelist *tr_root = NULL;
static treelist *tr_cur = NULL;


static int NEAR evaldir(dir, disp)
CONST char *dir;
int disp;
{
	DIR *dirp;
	struct dirent *dp;
	struct stat st;
	char *cp, path[MAXPATHLEN];
	int i, x, y, w, min, limit;

	if ((limit = dircountlimit) <= 0) return(1);

	min = filetop(win);
	if (disp) for (i = 1; i < FILEPERROW; i++) {
		Xlocate(TREEFIELD + 2, min + i);
		VOID_C XXputch('|');
		Xputterm(L_CLEAR);
	}

	if (!(dirp = Xopendir(dir))) return(0);

	Xstrcpy(path, dir);
	cp = strcatdelim(path);
	i = x = 0;
	y = 1;
	w = FILEFIELD;
	if (limit > FILEPERROW - 1) w /= 2;

	while ((dp = Xreaddir(dirp))) {
		if (isdotdir(dp -> d_name)) continue;
		if (strcatpath(path, cp, dp -> d_name) < 0) continue;

		if (limit-- <= 0 || (stat2(path, &st) >= 0 && s_isdir(&st))) {
			if (!disp) {
				i++;
				break;
			}
		}
		else if (disp) {
			Xlocate(x + TREEFIELD + 4, min + y);
			VOID_C XXcprintf("%^.*k", w, dp -> d_name);
			i++;
			if (++y >= FILEPERROW) {
				y = 1;
				x += w + 1;
				if (x >= w * 2 + 2) break;
			}
		}
	}
	VOID_C Xclosedir(dirp);

	if (disp && !i) {
		Xlocate(x + TREEFIELD + 4, min + 1);
		cputstr(w, "[No Files]");
	}

	return(i);
}

/*ARGSUSED*/
static treelist *NEAR maketree(path, list, parent, level, maxp)
CONST char *path;
treelist *list, *parent;
int level, *maxp;
{
#ifdef	DEP_DOSEMU
	char tmp[MAXPATHLEN];
#endif
#ifndef	NODIRLOOP
	treelist *lp;
#endif
	DIR *dirp;
	struct dirent *dp;
	struct stat st;
	CONST char *cp, *subdir;
	char *dir, cwd[MAXPATHLEN];
	int i, len;

	if ((level + 1) * DIRFIELD + 2 > TREEFIELD) {
		*maxp = -2;
		return(NULL);
	}
	if (intrkey(K_ESC)) {
		*maxp = -1;
		return(NULL);
	}
	if (!Xgetwd(cwd)) {
		*maxp = 0;
		return(NULL);
	}

	len = getpathtop(path, NULL, NULL);
	cp = &(path[len]);

	if (*cp == _SC_) cp++;
	else cp = strdelim(path, 0);
	len = (cp) ? cp - path : strlen(path);

	dir = Xstrndup(path, len);
	subdir = &(path[len]);
	if (*subdir == _SC_) subdir++;
	else if (!*subdir) subdir = NULL;

	if (!subdir) len = 0;
	else if ((cp = strdelim(subdir, 0))) len = cp - subdir;
	else len = strlen(subdir);

	*maxp = 0;
	i = _chdir2(dir);
	Xfree(dir);
	if (i < 0 || !(dirp = Xopendir(curpath))) return(NULL);

	i = 0;
	while ((dp = Xreaddir(dirp))) {
		if (isdotdir(dp -> d_name)) continue;
		else if (Xstat(nodospath(tmp, dp -> d_name), &st) < 0
		|| !s_isdir(&st))
			continue;

		list = b_realloc(list, *maxp, treelist);
		if (!subdir) {
			list[*maxp].name = Xstrdup(dp -> d_name);
			list[*maxp].sub = NULL;
			list[*maxp].max = 0;
#ifndef	NODIRLOOP
			list[*maxp].dev = st.st_dev;
			list[*maxp].ino = st.st_ino;
			list[*maxp].parent = parent;
#endif
			(*maxp)++;
		}
		else if (!strnpathcmp(dp -> d_name, subdir, len)
		&& !(dp -> d_name[len])) {
			list[0].name = Xstrdup(dp -> d_name);
			list[0].sub = &(list[0]);
			list[0].max = 0;
#ifndef	NODIRLOOP
			list[0].dev = st.st_dev;
			list[0].ino = st.st_ino;
			list[0].parent = parent;
#endif
			if (++(*maxp) >= 2) break;
		}
		else {
			if (!*maxp) {
				list[0].name = NULL;
				list[0].sub = NULL;
				list[0].max = -1;
#ifndef	NODIRLOOP
				list[0].dev = 0;
				list[0].ino = 0;
				list[0].parent = NULL;
#endif
			}
			if (!i) {
				i++;
				list[1].name = NULL;
				list[1].sub = NULL;
				list[1].max = -1;
#ifndef	NODIRLOOP
				list[1].dev = 0;
				list[1].ino = 0;
				list[1].parent = NULL;
#endif
				if (++(*maxp) >= 2) break;
			}
		}
	}
	VOID_C Xclosedir(dirp);

	if (list) for (i = 0; i < *maxp; i++) {
#ifndef	NODIRLOOP
		if (!(list[i].name)) lp = NULL;
# ifdef	DEP_URLPATH
		else if (list[i].dev == (dev_t)-1 || list[i].ino == (ino_t)-1)
			lp = NULL;
# endif
		else for (lp = parent; lp; lp = lp -> parent)
			if (lp -> dev == list[i].dev
			&& lp -> ino == list[i].ino)
				break;
		if (lp) {
			list[i].sub = NULL;
			list[i].max = 0;
		}
		else
#endif	/* !NODIRLOOP */
		if (list[i].sub)
			list[i].sub = maketree(subdir, NULL, &(list[i]),
				level + 1, &(list[i].max));
		else {
			if (list[i].max >= 0 && list[i].name
			&& evaldir(nodospath(tmp, list[i].name), 0))
				list[i].max = -1;
		}
	}
	if (sorttree && sorton) qsort(list, *maxp, sizeof(treelist), cmptree);

#if	MSDOS
	if (_dospath(path)) path += 2;
#endif
#ifdef	DEP_DOSEMU
	if (*path == _SC_ || _dospath(path))
#else
	if (*path == _SC_)
#endif
	{
		if (_chdir2(fullpath) < 0) error(fullpath);
	}
	else if (isdotdir(path) != 2 && _chdir2(cwd) < 0) error(parentpath);

	return(list);
}

static int NEAR _showtree(list, max, nest, y)
treelist *list;
int max, nest, y;
{
	char *cp;
	int i, j, w, tmp;

	w = TREEFIELD - (nest * DIRFIELD) - 1;
	tmp = y;
	for (i = 0; i < max; i++) {
		if (nest > 0) {
			for (j = tmp; j < y; j++) {
				if (j <= 0 || j >= FILEPERROW) continue;
				cp = bufptr(j);
				cp[(nest - 1) * DIRFIELD] = '|';
			}
		}
		if (y > 0 && y < FILEPERROW) {
			cp = bufptr(y);
			if (nest > 0)
				memcpy(&(cp[(nest - 1) * DIRFIELD]), "+--", 3);
			if (!(list[i].name)) {
				tmp = (w > 3) ? 3 : w;
				memcpy(&(cp[nest * DIRFIELD]), "...", tmp);
			}
			else {
				tmp = Xsnprintf(&(cp[nest * DIRFIELD]),
					w * KANAWID + 1,
					"%^.*s", w, list[i].name);
#ifdef	CODEEUC
				tmp = strlen(&(cp[nest * DIRFIELD]));
#endif
				cp[nest * DIRFIELD + tmp] =
					(list[i].max < 0) ? '>' : ' ';
			}
		}
		y++;
		tmp = y;
		if (w >= DIRFIELD && list[i].sub)
			y = _showtree(list[i].sub, list[i].max, nest + 1, y);
	}

	return(y);
}

static VOID NEAR showtree(VOID_A)
{
	int i, min;

	min = filetop(win);
	for (i = 0; i < FILEPERROW; i++) {
		Xlocate(0, min + i);
		Xputterm(L_CLEAR);
	}
	for (i = 1; i < FILEPERROW; i++) {
		memset(bufptr(i), ' ', TREEFIELD);
		bufptr(i)[TREEFIELD] = '\0';
	}
	_showtree(tr_root -> sub, 1, 0, tr_top - min);
	for (i = 1; i < FILEPERROW; i++) {
		Xlocate(1, min + i);
		attrputstr(TREEFIELD, bufptr(i), min + i == tr_line);
	}
	VOID_C evaldir(treepath, 1);
	keyflush();
}

static VOID NEAR treebar(VOID_A)
{
	Xlocate(1, filetop(win));
	VOID_C XXcprintf("Tree=%^-*.*k", n_column - 6, n_column - 6, treepath);
	Xlocate(0, L_MESLINE);
	Xputterm(L_CLEAR);
}

VOID rewritetree(VOID_A)
{
	tr_scr = Xrealloc(tr_scr, bufpos(FILEPERROW));
	searchtree();
	showtree();
	treebar();
	Xtflush();
}

static treelist *NEAR _searchtree(list, max, nest)
treelist *list;
int max, nest;
{
	treelist *lp, *tmplp;
	int i, w, len;

	lp = NULL;
	w = TREEFIELD - (nest * DIRFIELD) - 1;
	for (i = 0; i < max; i++) {
		if (tr_bottom == tr_line) {
			lp = tr_root;
			tr_no = i;
			if (list[i].name) Xstrcpy(treepath, list[i].name);
			else *treepath = '\0';
		}
		tr_bottom++;

		if (w < DIRFIELD || !(list[i].sub)) continue;
		tmplp = _searchtree(list[i].sub, list[i].max, nest + 1);
		if (!tmplp) continue;

		lp = (tmplp == tr_root) ? &(list[i]) : tmplp;
		len = strlen(list[i].name);
		if (len > 0 && list[i].name[len - 1] == _SC_) len--;
		memmove(&(treepath[len + 1]), treepath, strlen(treepath) + 1);
		memcpy(treepath, list[i].name, len);
		treepath[len] = _SC_;
	}

	return(lp);
}

static VOID NEAR searchtree(VOID_A)
{
	tr_bottom = tr_top;
	tr_cur = _searchtree(tr_root -> sub, 1, 0);
}

static int NEAR expandtree(list)
treelist *list;
{
	treelist *lp, *lptmp;
	char *cp;
	int i;

	lp = NULL;
	if (list -> sub) {
		lp = (treelist *)Xmalloc(sizeof(treelist));
		memcpy((char *)lp, (char *)&(list -> sub[0]),
			sizeof(treelist));
		for (i = 1; i < list -> max; i++) {
			Xfree(list -> sub[i].name);
			list -> sub[i].name = NULL;
		}
	}

	if (_chdir2(treepath) < 0) {
		if (list -> sub) error(treepath);
		list -> max = 0;
		return(1);
	}
	for (cp = treepath, i = 0; (cp = strdelim(cp, 0)); cp++, i++)
		/*EMPTY*/;
	lptmp = maketree(curpath, list -> sub, list, i, &(list -> max));
	if (_chdir2(fullpath) < 0) lostcwd(fullpath);
	if (list -> max < 0) {
		i = (list -> max < -1) ? 1 : 0;
		if (!(list -> sub)) list -> max = -1;
		else {
			list -> max = 2;
			list -> sub[1].name = NULL;
			Xfree(lp);
		}
		return(i);
	}
	if (list -> sub) {
		if (!(lp -> name)) i = list -> max;
		else for (i = 0; i < list -> max; i++)
			if (lptmp[i].name
			&& !strpathcmp(lp -> name, lptmp[i].name))
				break;
		if (i < list -> max) {
			Xfree(lptmp[i].name);
			for (; i > 0; i--) memcpy((char *)&(lptmp[i]),
					(char *)&(lptmp[i - 1]),
					sizeof(treelist));
			memcpy((char *)&(lptmp[0]), (char *)lp,
				sizeof(treelist));
		}
		Xfree(lp);
	}
	list -> sub = lptmp;

	return(1);
}

static int NEAR expandall(list)
treelist *list;
{
	char *cp;
	int i;

	if (!list || list -> max >= 0) return(1);
	if (!expandtree(list)) return(0);
	if (!(list -> sub)) return(1);
	cp = &(treepath[strlen(treepath)]);
	if (cp > &(treepath[1])) {
		strcatdelim(treepath);
		cp++;
	}
	for (i = 0; i < list -> max; i++) {
		Xstrcpy(cp, list -> sub[i].name);
		if (!expandall(&(list -> sub[i]))) return(0);
	}

	return(1);
}

static int NEAR treeup(VOID_A)
{
	int min;

	min = filetop(win);
	if (tr_line > min + 1) tr_line--;
	else if (tr_top <= min) tr_top++;
	else return(-1);
	searchtree();

	return(0);
}

static int NEAR treedown(VOID_A)
{
	char *cp, *dir;
	int min, oy, otop;

	min = filetop(win);
	oy = tr_line;
	otop = tr_top;
	if (tr_line < tr_bottom - 1 && tr_line < min + FILEPERROW - 2)
		tr_line++;
	else if (tr_bottom >= min + FILEPERROW) tr_top--;
	else return(-1);
	searchtree();

	if (!tr_cur || !(tr_cur -> sub)) return(0);
	if (tr_cur -> sub[tr_no].max >= 0 || tr_cur -> sub[tr_no].name)
		return(0);

	waitmes();
	dir = treepath;
#ifdef	DEP_DOSPATH
	if (_dospath(treepath)) dir += 2;
#endif

	cp = strrdelim(dir, 0);
	if (cp == dir) cp++;
#ifdef	DOUBLESLASH
	else if (cp - treepath < isdslash(treepath)) cp++;
#endif
#ifdef	DEP_URLPATH
	else if (cp - treepath < _urlpath(treepath, NULL, NULL)) cp++;
#endif
	*cp = '\0';
	if (!expandtree(tr_cur)) {
		tr_line = oy;
		tr_top = otop;
		searchtree();
		return(-1);
	}
	searchtree();
	redraw = 1;

	return(0);
}

static int NEAR freetree(list, max)
treelist *list;
int max;
{
	int i, n;

	for (i = n = 0; i < max; i++) {
		n++;
		Xfree(list[i].name);
		if (list[i].sub) n += freetree(list[i].sub, list[i].max);
	}
	Xfree(list);

	return(n);
}

static int NEAR bottomtree(VOID_A)
{
	int min, oy, otop;

	min = filetop(win);
	oy = tr_line;
	otop = tr_top;
	while (tr_line < min + FILEPERROW - 2) if (treedown() < 0) break;
	if (tr_line >= tr_bottom - 1 && tr_top >= min + 1) {
		tr_line = oy;
		tr_top = otop;
		searchtree();
		return(-1);
	}

	return(0);
}

static VOID NEAR _tree_search(VOID_A)
{
	int oy, otop;

	oy = tr_line;
	otop = tr_top;

	while (strpathcmp(fullpath, treepath)) if (treeup() < 0) {
		tr_top = otop;
		tr_line = oy;
		searchtree();
		do {
			if (treedown() < 0) break;
		} while (strpathcmp(fullpath, treepath));
		break;
	}
}

static int NEAR _tree_input(VOID_A)
{
	treelist *old;
	char *cwd;
	int ch, min, tmp, half;

	keyflush();
	Xgetkey(-1, 0, 0);
	ch = Xgetkey(1, 0, autoupdate);
	Xgetkey(-1, 0, 0);

	old = tr_cur;
	min = filetop(win);
	switch (ch) {
		case K_UP:
			treeup();
			break;
		case K_DOWN:
			treedown();
			break;
		case K_PPAGE:
			if (bottomtree() < 0) break;
			half = (FILEPERROW - 1) / 2;
			tmp = min + half + 1;
			if (tr_top + half > min + 1) half = min - tr_top + 1;
			if (half > 0) tr_top += half;
			tr_line = tmp;
			searchtree();
			break;
		case K_NPAGE:
			if (bottomtree() < 0) break;
			half = (FILEPERROW - 1) / 2;
			tmp = min + half + 1;
			while (half-- > 0) if (treedown() < 0) break;
			tr_line = tmp;
			searchtree();
			break;
		case K_BEG:
		case K_HOME:
		case '<':
			tr_line = tr_top = min + 1;
			searchtree();
			break;
		case K_EOL:
		case K_END:
		case '>':
			while (treedown() >= 0);
			break;
		case '?':
			_tree_search();
			break;
		case '(':
			tmp = tr_no - 1;
			do {
				if (treeup() < 0) break;
			} while (tmp >= 0 && (tr_cur != old || tr_no != tmp));
			break;
		case ')':
			tmp = tr_no + 1;
			do {
				if (treedown() < 0) break;
			} while (tmp < old -> max
			&& (tr_cur != old || tr_no != tmp));
			break;
		case '\t':
			if (!tr_cur || !(tr_cur -> sub)
			|| tr_cur -> sub[tr_no].max >= 0)
				break;
			waitmes();
			expandall(&(tr_cur -> sub[tr_no]));
			searchtree();
			redraw = 1;
			break;
		case K_RIGHT:
			if (!tr_cur || !(tr_cur -> sub)) break;
			if (tr_cur -> sub[tr_no].max >= 0) {
				if (!(tr_cur -> sub[tr_no].sub)) break;
				if (treedown() < 0) break;
			}
			waitmes();
			expandtree(&(tr_cur -> sub[tr_no]));
			searchtree();
			redraw = 1;
			break;
		case K_LEFT:
			if (tr_cur && tr_cur -> sub
			&& tr_cur -> sub[tr_no].sub) {
				tmp = freetree(tr_cur -> sub[tr_no].sub,
					tr_cur -> sub[tr_no].max);
				tr_bottom -= tmp;
				tr_cur -> sub[tr_no].max = -1;
				tr_cur -> sub[tr_no].sub = NULL;
				redraw = 1;
				break;
			}
/*FALLTHRU*/
		case K_BS:
			do {
				if (treeup() < 0) break;
				if (!tr_cur || !(tr_cur -> sub)) break;
			} while (&(tr_cur -> sub[tr_no]) != old);
			break;
		case 'l':
			if (!(cwd = inputstr(LOGD_K, 0, -1, NULL, HST_PATH)))
				break;
			if (!*(cwd = evalpath(cwd, 0))) break;
			if (chdir2(cwd) >= 0) {
				Xfree(cwd);
				break;
			}
			warning(-1, cwd);
			ch = '\0';
			Xfree(cwd);
			break;
		case K_CTRL('L'):
#if	FD >= 3
		case K_TIMEOUT:
#endif
			rewritefile(1);
			break;
		case K_ESC:
			break;
		default:
			if (ch >= K_MIN) break;
			if (!Xisupper(ch)) break;
			if (tr_line == tr_bottom - 1) {
				tr_line = tr_top = min + 1;
				searchtree();
			}
			else do {
				if (treedown() < 0) break;
				if (!tr_cur || !(tr_cur -> sub)) break;
				tmp = Xtoupper(*(tr_cur -> sub[tr_no].name));
			} while (ch != tmp);
			break;
	}

	return(ch);
}

static char *NEAR _tree(VOID_A)
{
#ifndef	NODIRLOOP
# ifdef	DEP_DOSDRIVE
	char tmp[MAXPATHLEN];
# endif
	struct stat st;
#endif	/* !NODIRLOOP */
	char *cp, *cwd, path[MAXPATHLEN];
	int ch, min, len, oy, otop;

	keyflush();
	waitmes();
	tr_root = (treelist *)Xmalloc(sizeof(treelist));
	tr_root -> name = NULL;
	tr_root -> max = 1;
	tr_root -> sub = tr_cur = (treelist *)Xmalloc(sizeof(treelist));
#ifndef	NODIRLOOP
	tr_root -> ino = 0;
	tr_root -> parent = NULL;
#endif
	treepath = path;

#ifdef	DEP_DOSEMU
	if (dospath(nullstr, path)) len = 3;
	else
#endif
	{
#ifdef	DOUBLESLASH
		if ((len = isdslash(fullpath))) /*EMPTY*/;
		else
#endif
#ifdef	DEP_URLPATH
		if ((len = _urlpath(fullpath, NULL, NULL))) /*EMPTY*/;
		else
#endif
#if	MSDOS
		len = 3;
#else
		len = 1;
#endif
		Xstrcpy(path, fullpath);
	}
	tr_cur[0].name = Xstrndup(path, len);

#ifndef	NODIRLOOP
	if (Xstat(nodospath(tmp, tr_cur[0].name), &st) < 0)
		tr_cur[0].dev = tr_cur[0].ino = 0;
	else {
		tr_cur[0].dev = st.st_dev;
		tr_cur[0].ino = st.st_ino;
	}
	tr_cur[0].parent = NULL;
#endif
	tr_cur[0].sub = maketree(path, NULL, &(tr_cur[0]),
		0, &(tr_cur[0].max));

	tr_line = 0;
	if (path[len - 1] == _SC_) len--;
	cwd = &(path[len]);

	if (isrootpath(cwd)) /*EMPTY*/;
	else for (cp = cwd; (cp = strdelim(cp, 0)); cp++, tr_line++)
		if ((tr_line + 1) * DIRFIELD + 2 > TREEFIELD
		|| !(tr_cur = &(tr_cur -> sub[0])))
			break;
	min = filetop(win);
	tr_line += (tr_top = min + 1);
	if (tr_line >= min + FILEPERROW - 2) {
		tr_top -= tr_line - (min + FILEPERROW - 2);
		tr_line = min + FILEPERROW - 2;
	}

	tr_scr = Xmalloc(bufpos(FILEPERROW));
	searchtree();
	showtree();
	win_x = 0;
	do {
		treebar();
		oy = tr_line;
		otop = tr_top;
		redraw = 0;
		win_y = tr_line;
		Xlocate(win_x, win_y);
		Xtflush();

		if ((ch = _tree_input()) == 'l') {
			Xfree(tr_scr);
			freetree(tr_root, 1);
			return(fullpath);
		}

		if (redraw || tr_top != otop) showtree();
		else if (oy != tr_line) {
			Xlocate(1, tr_line);
			attrputstr(TREEFIELD, bufptr(tr_line - min), 1);
			Xlocate(1, oy);
			if (stable_standout) Xputterm(END_STANDOUT);
			else cputstr(TREEFIELD, bufptr(oy - min));
			VOID_C evaldir(path, 1);
		}
	} while (ch != K_ESC && ch != K_CR);

	Xfree(tr_scr);
	freetree(tr_root, 1);
	treepath = NULL;
	if (ch == K_ESC) return(NULL);

	return(Xstrdup(path));
}

/*ARGSUSED*/
char *tree(cleanup, drvp)
int cleanup, *drvp;
{
#ifdef	DEP_PSEUDOPATH
	int drv;
#endif
	char *path, *dupfullpath;

	if (FILEPERROW < WFILEMINTREE) {
		warning(0, NOROW_K);
		return(NULL);
	}
#ifdef	DEP_PSEUDOPATH
	if ((drv = preparedrv(fullpath, NULL, NULL)) < 0) {
		warning(-1, fullpath);
		return(NULL);
	}
#endif
	dupfullpath = Xstrdup(fullpath);
	do {
		path = _tree();
	} while (path == fullpath);
#ifdef	DEP_PSEUDOPATH
	if (path && drvp && (*drvp = preparedrv(path, NULL, NULL)) < 0) {
		warning(-1, path);
		Xfree(path);
		path = NULL;
	}
#endif
	if (chdir2(dupfullpath) < 0) lostcwd(NULL);
	Xfree(dupfullpath);
#ifdef	DEP_PSEUDOPATH
	shutdrv(drv);
#endif
	if (cleanup) rewritefile(1);

	return(path);
}
#endif	/* !_NOTREE */