File: HTDirBrw.c

package info (click to toggle)
cern-httpd 3.0A-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 5,392 kB
  • ctags: 6,554
  • sloc: ansic: 37,902; makefile: 1,746; perl: 535; csh: 167; sh: 143
file content (1369 lines) | stat: -rw-r--r-- 38,336 bytes parent folder | download | duplicates (6)
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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
/*			Directory Browsing	       		HTDirBrw.c
**			==================
**
**	This is unix-specific code in general
**	The module is intended for use in HTFile.c and HTFTP.c where
**	it replaces the old directory browsing routine.
**	The module is only compiled if GOT_READ_DIR is defined
**
** Authors:
**		HF	Henrik Frystyk, CERN, <frystyk@dxcern.cern.ch>
**		AL	Ari Luotonen, CERN, <luotonen@www.cern.ch>
** History:
**	   Mar 94  HF	Written by Henrik Frystyk, frystyk@dxcern.cern.ch,
**			but with some of the Directory stuff brought from
**			HTFile().
**	   Mar 94  AL	Configurable icons.
**	   Apr 94  HF	Icons moved to own module
**	   Jul 94  FM	Compile substitute strftime() for VMS && !DECC (and
**			use sys$share:vaxcrtl/share in .opt file for linking).
**			Insulate free() from _free structure element.
** BUGS:
**
*/


#ifdef VMS
typedef unsigned long mode_t;
#define lstat stat
#else /* not VMS */
#include <pwd.h>
#include <grp.h>
#endif /* not VMS */

/* Library include files */
#include "HTMLPDTD.h"
#include "HTUtils.h"
#include "HTFile.h"
#include "HTAnchor.h"
#include "HTParse.h"
#include "HTFWriter.h"
#include "HTInit.h"
#include "HTBTree.h"
#include "HTFormat.h"
#include "HTChunk.h"
#include "HTIcons.h"
#include "HTDescript.h"
#include "HTError.h"
#include "HTDirBrw.h"					 /* Implemented here */

#ifdef VMS
#include "HTVMSUtils.h"
#endif /* VMS */

#if defined(Mips) || (defined(VMS) && !defined(DECC))
PRIVATE char * months[12] = {
    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
#endif /* Mips || (VMS && !DECC) */


/* Macros and other defines */
#ifdef OLD_CODE
#ifdef USE_DIRENT			       /* Set this for Sys V systems */
#define STRUCT_DIRENT struct dirent
#else
#define STRUCT_DIRENT struct direct
#endif /* USE_DIRENT */
#endif /* OLD_CODE */

#define PUTC(c) (*target->isa->put_character)(target, c)
#define PUTS(s) (*target->isa->put_string)(target, s)
#define START(e) (*target->isa->start_element)(target, e, 0, 0)
#define END(e) (*target->isa->end_element)(target, e)
#define FREE_TARGET (*target->isa->_free)(target)


struct _HTStructured {
	CONST HTStructuredClass *	isa;
	/* ... */
};

/* Globals */
PUBLIC int HTDirReadme = HT_DIR_README_TOP;
PUBLIC int HTDirInfo = HT_DIR_INFO_TOP;
PUBLIC int HTDirAccess = HT_DIR_OK;
PUBLIC unsigned int HTDirShowMask = HT_DIR_SHOW_ICON+HT_DIR_SHOW_SIZE+HT_DIR_KEY_NAME+HT_DIR_SHOW_DATE;

#if 0
PUBLIC unsigned int HTDirShowMask = HT_DIR_SHOW_ICON+HT_DIR_SHOW_MODE+HT_DIR_SHOW_NLINK+HT_DIR_SHOW_OWNER+HT_DIR_SHOW_GROUP+HT_DIR_SHOW_SIZE+HT_DIR_KEY_NAME+HT_DIR_SHOW_DATE;
#endif

PUBLIC BOOL HTDirDescriptions = YES;
PUBLIC BOOL HTDirShowBytes = NO;
PUBLIC int HTDirMinFileLength = 15;
PUBLIC int HTDirMaxFileLength = 22;
PUBLIC int HTDirMaxDescrLength = 25;

/* Type definitions and global variables etc. local to this module */
typedef struct _HTDirKey {
    void *key;				     /* Can sort strings or integers */
    char *filename;
    BOOL is_dir;                                           /* If a directory */
    char *body;			/* Contains all the stuff as date, size etc. */
    HTIconNode *icon;
    HTHrefNode *href;
    char *symlink;
} HTDirKey;

typedef enum _HTShowLength {                        /* Width of each collumn */
    HT_LENGTH_MODE  = 10,
    HT_LENGTH_NLINK = 4,
    HT_LENGTH_OWNER = 8,
    HT_LENGTH_GROUP = 8,
    HT_LENGTH_SIZE  = 6,
    HT_LENGTH_DATE  = 15,
    HT_LENGTH_SPACE = 1
} HTShowLength;

PRIVATE int HTDirFileLength;      /* HTMinDirFileName < x < HTMaxDirFileName */
PRIVATE int HTBodyLength;
PRIVATE char *HTDirSpace = NULL;

/* ------------------------------------------------------------------------- */

/* 							     	FilePerm()
**	Writes the file permissions into strptr.
**	ISN'T THERE A FASTER METHOD???
*/
PRIVATE void FilePerm ARGS2(mode_t, mode, char *, strptr)
{
    if ((mode & S_IFMT) == S_IFREG)
	*strptr++ = '-';
    else if ((mode & S_IFMT) == S_IFDIR)
	*strptr++ = 'd';
    else if ((mode & S_IFMT) == S_IFLNK)
	*strptr++ = 'l';
    else
	*strptr++ = '?';			  /* Hmmm, any better ideas? */

    *strptr++ = (mode & S_IRUSR) ? 'r' : '-';			     /* User */
    *strptr++ = (mode & S_IWUSR) ? 'w' : '-';
    if (mode & S_ISUID) {
	if (mode & S_IXUSR)
	    *strptr++ = 's';
	else
	    *strptr++ = 'S';
    } else if (mode & S_IXUSR)
	*strptr++ = 'x';
    else
	*strptr++ = '-';	
    *strptr++ = (mode & S_IRGRP) ? 'r' : '-';			    /* Group */
    *strptr++ = (mode & S_IWGRP) ? 'w' : '-';
    if (mode & S_ISGID) {
	if (mode & S_IXGRP)
	    *strptr++ = 's';
	else
	    *strptr++ = 'S';
    } else if (mode & S_IXGRP)
	*strptr++ = 'x';
    else
	*strptr++ = '-';
    *strptr++ = (mode & S_IROTH) ? 'r' : '-';			    /* Other */
    *strptr++ = (mode & S_IWOTH) ? 'w' : '-';
    if (mode & S_ISVTX) {
	if (mode & S_IXOTH)
	    *strptr++ = 't';
	else
	    *strptr++ = 'T';
    } else if (mode & S_IXOTH)
	*strptr++ = 'x';
    else
	*strptr++ = '-';
}


#if 0	/* Not needed currently */
/* 							     	CenterStr()
**	Centers str_in into str_out expecting str_out having size length
**	inclusive the terminating '\0'. The output string MUST be long enough.
*/
PRIVATE void CenterStr ARGS3(char *, str_out, char *, str_in, int, length)
{
    char *inptr = str_in;
    char *outptr = str_out + (length-strlen(str_in))/2;
    memset((void *) str_out, ' ', length);
    while ((*outptr++ = *inptr++) != '\0');
    *--outptr = ' ';
    *(str_out+length-1) = '\0';
}
#endif


/*								LeftStr()
**	Like CenterStr(), but result is left-justified.
*/
PRIVATE void LeftStr ARGS3(char *, str_out, char *, str_in, int, length)
{
    char *inptr = str_in;
    char *outptr = str_out;
    memset((void *) str_out, ' ', length);
    while ((*outptr++ = *inptr++));
    *--outptr = ' ';
    str_out[length-1] = 0;
}


/*								RightStr()
**	Like CenterStr(), but result is right-justified.
*/
PRIVATE void RightStr ARGS3(char *, str_out, char *, str_in, int, length)
{
    char *inptr = str_in;
    char *outptr = str_out + length - strlen(str_in) - 1;
    memset((void *) str_out, ' ', length);
    while ((*outptr++ = *inptr++));
}


/* 							     	ItoA()
**	Converts a positive int to a string backwards starting at start+len-1.
*/
PRIVATE void ItoA ARGS3(unsigned int, n, char *, start, char, len)
{
    char *sptr = start+len-1;
    do {
	*sptr-- = n%10 + '0';
    } while (n /= 10);
}


/* 							     	HTDirSize()
 *	Converts a long (byte count) to a string
 *
 *	This function was a PAIN!  In computer-world 1K is 1024 bytes
 *	and 1M is 1024K -- however, sprintf() still formats in base-10.
 *	Therefore I output only until 999, and then start using the
 *	next unit.  This doesn't work wrong, it's just a feature.
 */
PRIVATE void HTDirSize ARGS3(unsigned long, n, char *, start, char, len)
{
    float size = n/1024.0;
    if (n < 1000) {
	if (HTDirShowBytes)
	    ItoA((int) n, start, len);
	else
	    sprintf(start+len-6, "%5dK", n>0 ? 1 : 0);
    }
    else if (size + 0.999 < 1000)
	sprintf(start+len-6, "%5dK", (int)(size + 0.5));
    else if ((size /= 1024) < 9.9)
	sprintf(start+len-6, "%5.1fM", (size + 0.05));
    else if (size < 1000)
	sprintf(start+len-6, "%5dM", (int)(size + 0.5));
    else if ((size /= 1024) < 9.9)
	sprintf(start+len-6, "%5.1fG", (size + 0.05));
    else
	sprintf(start+len-6, "%5dG", (int)(size + 0.5));
    *(start+len) = ' ';
}


/* 							     	KeyFree()
**	Frees the contents of the key structure AND the key it self
*/
PRIVATE void  KeyFree ARGS1(HTDirKey *, key)
{
    if (key->key != key->filename)
	FREE(key->key);
    FREE(key->filename);
    FREE(key->body);
    FREE(key->symlink);
    FREE(key);
}


/* 							     DirFileInfoClear()
**	Frees the contents of the dir_file_info structure and puts ints to 0
*/
PRIVATE void  DirFileInfoClear ARGS1(dir_file_info *, file_info)
{
    FREE(file_info->f_name);
    file_info->f_mode = 0L;
    file_info->f_nlink = 0;
    FREE(file_info->f_uid);
    FREE(file_info->f_gid);
    file_info->f_size = 0L;
    file_info->f_mtime = (time_t) 0;
}


/* 							     	DirAbort()
**	Goes through the whole tree and frees whatever is left.
*/
PRIVATE void  DirAbort ARGS1(HTBTree *, tree)
{
    HTBTElement *next_node = HTBTree_next(tree, NULL);

    while (next_node) {
	KeyFree((HTDirKey *) next_node->object);
	next_node = HTBTree_next(tree, next_node);
    }
}


/* 								HTDirLintCmp()
**	Acts like  strcmp but operates on positive long integers.
**   	Though, in order to get the biggest element first, the
**    	return values are reversed, so it returns >0 if a<b, 0 if a==b
**    	and <0 if a>b 
*/
PRIVATE long HTDirLintCmp ARGS2(unsigned long **, a, unsigned long **, b)
{
    return (long) (**b)-(**a);
}


/* 								HTDirStrCmp()
**	Acts like strcmp but operates on pointers to pointers.
*/
PRIVATE long HTDirStrCmp ARGS2(char **, a, char **, b)
{
    return (int) strcmp(*a, *b);
}


/* 							    HTDirStrCaseCmp()
**	Acts like ctrcasecomp but operates on pointers to pointers.
*/
PRIVATE long HTDirStrCaseCmp ARGS2(char **, a, char **, b)
{
    return (int) strcasecomp(*a, *b);
}


/* 								InitBody()
**	Finds the lenght of the body part of the key and generates a
**	header line used in HTDirOutTop().
*/
PRIVATE char *InitBody NOARGS
{
    char *topstr = NULL;
    PRIVATE char *header[] = {
	"Size",
	"Last modified",
	"Permission",
	"Link",
	"Owner",
	"Group",
	"Description"
    };

    /* Calculate output line string length */
    HTBodyLength = HT_LENGTH_SPACE;
    StrAllocCat(topstr, HTDirSpace);
    if (HTDirShowMask & HT_DIR_SHOW_DATE) {
	char date[HT_LENGTH_DATE+1];
	LeftStr(date, *(header+1), HT_LENGTH_DATE+1);
	StrAllocCat(topstr, date);
	HTBodyLength += HT_LENGTH_DATE;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirShowMask & HT_DIR_SHOW_SIZE) {
	char size[HT_LENGTH_SIZE+1];
	RightStr(size, *header, HT_LENGTH_SIZE+1);
	StrAllocCat(topstr, size);
	HTBodyLength += HT_LENGTH_SIZE;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirShowMask & HT_DIR_SHOW_MODE) {
	char mode[HT_LENGTH_MODE+1];
	LeftStr(mode, *(header+2), HT_LENGTH_MODE+1);
	StrAllocCat(topstr, mode);
	HTBodyLength += HT_LENGTH_MODE;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirShowMask & HT_DIR_SHOW_NLINK) {
	char nlink[HT_LENGTH_NLINK+1];
	LeftStr(nlink, *(header+3), HT_LENGTH_NLINK+1);
	StrAllocCat(topstr, nlink);
	HTBodyLength += HT_LENGTH_NLINK;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirShowMask & HT_DIR_SHOW_OWNER) {
	char owner[HT_LENGTH_OWNER+1];
	LeftStr(owner, *(header+4), HT_LENGTH_OWNER+1);
	StrAllocCat(topstr, owner);
	HTBodyLength += HT_LENGTH_OWNER;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirShowMask & HT_DIR_SHOW_GROUP) {
	char group[HT_LENGTH_GROUP+1];
	LeftStr(group, *(header+5), HT_LENGTH_GROUP+1);
	StrAllocCat(topstr, group);
	HTBodyLength += HT_LENGTH_GROUP;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
    }
    if (HTDirDescriptions) {
	char * descr = (char*)malloc(HTDirMaxDescrLength+1);
	if (!descr) outofmem(__FILE__, "InitBody");

	if (HT_LENGTH_SPACE > 1) {
	    LeftStr(descr, *(header+6), HTDirMaxDescrLength+1);
	}
	else {	/* Description simply always needs at least two spaces */
	    *descr = ' ';
	    LeftStr(descr+1, *(header+6), HTDirMaxDescrLength);
	}
	StrAllocCat(topstr, descr);
	HTBodyLength += HTDirMaxDescrLength;
	StrAllocCat(topstr, HTDirSpace);
	HTBodyLength += HT_LENGTH_SPACE;
	free(descr);
    }
    return topstr;
}


/*      							HTDirOutMessage
**
**    Puts out the message to default output.
*/
PRIVATE void HTDirOutMessage ARGS2(HTStructured *, target,
				   HTChunk *, message)
{
    if (!message || !message->data)
	return;
    START(HTML_PRE);
    PUTS(message->data);
    END(HTML_PRE);
    return;
}


/*	Send README file			       		HTDirOutReadme
**
**  If a README file exists, then it is inserted into the document here.
*/
PRIVATE void HTDirOutReadme ARGS2(HTStructured *, target,
				  CONST char *, localname)
{ 
    FILE *fp;
    int ch;
    char *readme_file_name = (char *) 
	malloc(strlen(localname)+1+strlen(HT_DIR_README_FILE)+1);

    if (TRACE) fprintf(stderr, "HTReadMe.... Looking for file `%s'\n",
		       localname ? localname : "-null-");
    if (!target || !localname) {
	free(readme_file_name);
	return;
    }
    strcpy(readme_file_name, localname);
    strcat(readme_file_name, "/");
    strcat(readme_file_name, HT_DIR_README_FILE);
    
    if ((fp = fopen(readme_file_name, "r")) != NULL) {
	START(HTML_PRE);
	while ((ch = fgetc(fp)) >= 0)
	    PUTC(ch);
	END(HTML_PRE);
	fclose(fp);
    } 
    free(readme_file_name);	/* Leak fixed AL 6 Feb 1994 */
}

 
/*      							HTDirOutTop()
**
**    This gives the TITLE and H1 header, and also a link
**    to the parent directory if appropriate.
*/
PRIVATE void HTDirOutTop ARGS5(HTStructured *, target,
			       HTAnchor *, anchor,
			       char *, top_body,
			       char *, directory,
			       HTChunk *, message)
{
    char *logical = HTAnchor_address(anchor);
    char *path = HTParse(logical, "", PARSE_PATH + PARSE_PUNCTUATION);
    char *title = NULL;
    char *current = strrchr(path, '/');
    char *parent = NULL;

    /*
     * Make title
     */
    if (path) {
	StrAllocCopy(title,path);
	HTUnEscape(title);
    }
    else {
	StrAllocCopy(title,"Welcome Directory");
    }

    /*
     * Find parent directory if any
     */
    if (current) {
	if (current != path) {	/* Not root, make link to parent */
	    *current++ = 0;
	    if ((parent = strrchr(path,'/')))
		parent++;
	    else
		parent = path;
	    HTUnEscape(++parent);
	    if ((int)strlen(parent) > HTDirFileLength)
		*(parent+HTDirFileLength-1) = '\0';
	}
	else if (*(current+1)) {  /* In a subdir under root, link to root */
	    current++;
	    parent = "/";
	}
    }

    /* Output title */
    START(HTML_HTML);
    START(HTML_HEAD);
    START(HTML_TITLE);
    PUTS("Index of ");
    PUTS(title);
    END(HTML_TITLE);
    END(HTML_HEAD);

    START(HTML_BODY);
    START(HTML_H1);
    PUTS("Index of ");
    PUTS(title);
    END(HTML_H1);

    if (message && HTDirInfo == HT_DIR_INFO_TOP)
	HTDirOutMessage(target, message);
    else if (HTDirReadme == HT_DIR_README_TOP)
	HTDirOutReadme(target, directory);

    /* Output parent directory */
    START(HTML_PRE);

    /* Output the header line of the list */
    if (!icon_blank) icon_blank = icon_unknown;
    if (HTDirShowMask & HT_DIR_SHOW_ICON && icon_blank) {
	HTMLPutImg(target,
		   icon_blank->icon_url,
		   HTIcon_alt_string(icon_blank->icon_alt, NO),
		   NULL);
	PUTS(HTDirSpace);
    }
    {
	char *name;
	if ((name = (char *) malloc(HTDirFileLength+1)) == NULL)
	    outofmem(__FILE__, "HTDirOutTop");
	LeftStr(name, "Name", HTDirFileLength+1);
	PUTS(name);
	free(name);
    }
    PUTS(top_body);
    PUTC('\n');
    START(HTML_HR);
    PUTC('\n');

    if (parent) {
	if (!icon_parent)
	    icon_parent = icon_dir ? icon_dir :
		icon_blank ? icon_blank : icon_unknown;
	if (HTDirShowMask & HT_DIR_SHOW_ICON  &&  icon_parent) {
	    HTMLPutImg(target,
		       icon_parent->icon_url,
		       HTIcon_alt_string(icon_parent->icon_alt, YES),
		       NULL);
	    PUTS(HTDirSpace);
	}
	{
	    char *relative;
	    if ((relative = (char *) malloc(strlen(current) + 4)) == NULL)
		outofmem(__FILE__, "HTDirOutTop");
	    if (*current)
		sprintf(relative, "%s/..", current);
	    else
		strcpy(relative, "..");
	    HTStartAnchor(target, NULL, relative);
	    free(relative);
	    PUTS("Parent directory");
	    END(HTML_A);
	    PUTC('\n');
	}
    }
    free(logical);
    free(path);
    free(title);
}


/*      							HTDirOutList()
**
**    	This function goes through the BTRee and puts each directory entry out
**    	on line.
**
**	BUGS:
**
**	If filename is anchor and filename is longer than HTMaxFileLength,
**	the body columns are shifted to the right
*/
PRIVATE void HTDirOutList ARGS4(HTStructured *, target, HTBTree *, bt,
				char *, pathtail, char *, directory)
{
    char *escaped = NULL;	      		 	  /* Used for anchor */
    char *tail = NULL;
    int tailend;
    int filelen;
    HTBTElement *next_node = HTBTree_next(bt, NULL);
    HTDirKey *nkey;
    StrAllocCopy(tail, pathtail);
    tailend = strlen(tail);

    do {
	nkey = (HTDirKey *) next_node->object;
	escaped = HTEscape(nkey->filename, URL_XPALPHAS);
	*(tail+tailend) = '\0';
	StrAllocCat(tail, escaped);

	if (TRACE) fprintf(stderr, "OutList: %s\n", tail);
	if (HTDirShowMask & HT_DIR_ICON_ANCHOR  &&
	    nkey->icon && nkey->icon->icon_url) {	/* Icon as anchor */
	    HTStartAnchor(target, NULL, tail);
	    HTMLPutImg(target,
		       nkey->icon->icon_url,
		       HTIcon_alt_string(nkey->icon->icon_alt, YES),
		       NULL);
	    END(HTML_A);
	    PUTS(HTDirSpace);
	    if (HTBodyLength)
		PUTS(nkey->body);
	    filelen = strlen(nkey->filename);
	    if (filelen > HTDirFileLength) {
		*(nkey->filename+HTDirFileLength-2) = '>';
		*(nkey->filename+HTDirFileLength-1) = '\0';
	    }
	    if (HTDirShowMask & HT_DIR_SHOW_SLINK && nkey->symlink) {
		START(HTML_I);
		PUTS(nkey->filename);
		END(HTML_I);
	    } else {
		PUTS(nkey->filename);
	    } 
	} else { 	      			   /* Use filename as anchor */
	    if (HTDirShowMask & HT_DIR_SHOW_ICON  &&
		nkey->icon && nkey->icon->icon_url) {
 		if (nkey->href) {
		    char url[500];
 
		    strcpy(url, nkey->href->href_url);
		    strcat(url, directory);
		    strcat(url,"/");
		    strcat(url, nkey->filename); 
		    if (TRACE) fprintf(stderr,"Href: %s\n", url);
		    HTStartAnchor(target,NULL,url);
 		}
		HTMLPutImg(target,
			   nkey->icon->icon_url,
			   HTIcon_alt_string(nkey->icon->icon_alt, YES),
			   NULL);
		PUTS(HTDirSpace);
		if (nkey->href)
		    END(HTML_A);
	    }
	    if (HTDirShowMask & HT_DIR_SHOW_SLINK && nkey->symlink)
		START(HTML_I);
	    HTStartAnchor(target, NULL, tail);
	    {
		int extra = nkey->is_dir ? 1 : 0;
		if ((int)strlen(nkey->filename) + extra > HTDirFileLength) {
		    char * fn = (char*)malloc(HTDirFileLength + 1);
		    if (!fn) outofmem(__FILE__, "HTDirOutList");

		    strncpy(fn, nkey->filename, HTDirFileLength);
		    if (extra) {
			fn[HTDirFileLength-2] = '>';
			fn[HTDirFileLength-1] = '/';
		    }
		    else {
			fn[HTDirFileLength-1] = '>';
		    }
		    fn[HTDirFileLength] = 0;
		    PUTS(fn);
		    free(fn);
		}
		else {
		    PUTS(nkey->filename);
		    if (extra)
			PUTC('/');
		}
	    }
	    END(HTML_A);
	    if (HTDirShowMask & HT_DIR_SHOW_SLINK && nkey->symlink)
		END(HTML_I);
	}
	if (HTBodyLength) {
	    filelen = strlen(nkey->filename) + (nkey->is_dir ? 1 : 0);
	    while (filelen++ < HTDirFileLength)
		PUTC(' ');
	    PUTS(HTDirSpace);
	    PUTS(nkey->body);
	}
	PUTC('\n');
	KeyFree(nkey);
	FREE(escaped);
    } while ((next_node = HTBTree_next(bt, next_node)) != NULL);
    free(tail);
}



/*      							HTDirOutBottom
**
**    This function outputs the last part of the directory listings
*/
PRIVATE void HTDirOutBottom ARGS4(HTStructured *, target,
				  unsigned int, files,
				  char *, directory,
				  HTChunk *, message)
{
    char *outstr;

    if ((outstr = (char *) malloc(100)) == NULL)
	outofmem(__FILE__, "HTDirOutBottom");
    if (files == 0)
	sprintf(outstr, "Empty directory");
    else if (files == 1)
	sprintf(outstr, "1 file");
    else
	sprintf(outstr, "%u files", files);
    START(HTML_HR);
    PUTS(outstr);
    free(outstr);
    END(HTML_PRE);

    if (message && HTDirInfo == HT_DIR_INFO_BOTTOM)
	HTDirOutMessage(target, message);
    else if (HTDirReadme == HT_DIR_README_BOTTOM)
	HTDirOutReadme(target, directory);
    END(HTML_BODY);
    END(HTML_HTML);
}


#ifdef GOT_READ_DIR
/*						    	HTBrowseDirectory()
**	This function scrolls through the directory file given and
**	generates an HTML-object. It uses the global variables:
**
**	  - HTDirShowMask	see enum HTDirShow for details
**	  - HTDirAccess		HT_DIR_[FORBID | SELECTIVE | OK]
**	  - HTDirReadme
**	
**	Generated URLs for non symbolic links are made relative to directory:
**
**		.../xxx/yyy	=>	yyy/<element>
**		.../xxx/yyy/	=>	./<element>
**		/		=>	/<element>
**     
**	Symbolic links are given as are (relative or absolute), but see note.
**
**	NOTE:	The function expects that the URL given as directory IS NOT
**		escaped BUT Simplified.
**
**	Returns < 0 on error, HT_LOADED on succes
*/
PUBLIC int HTBrowseDirectory ARGS2(HTRequest *, req, char *, directory)
{
    char *pathname = NULL;
    int  pathend;
    char *tail = NULL;
    DIR *dp;
    struct stat file_info;
    HTList * descriptions = NULL;

    if (TRACE)
	fprintf(stderr,"HTBrowse.... Browsing `%s\'\n", directory);
        
    if (HTDirAccess == HT_DIR_FORBID)
	return HTErrorAdd(req, ERR_FATAL, NO, HTERR_FORBIDDEN,
			  NULL, 0, "HTBrowseDirectory");
    
    /* Initialize path name for stat() */
    StrAllocCopy(pathname, directory);
    pathend = strlen(pathname);
    if (*(pathname+pathend-1) != '/') {
	StrAllocCat(pathname, "/");
	pathend++;
    }
    
    /* Set up the offset string of the anchor reference */
    {
	char *tptr = strrchr(directory, '/');
	char *tailstr = NULL;
	if (!tptr) {					    /* '/' not found */
	    StrAllocCopy(tailstr, directory);
	    StrAllocCat(tailstr, "/");
	} else if (!*(tptr+1)) {
	    if (tptr != directory) {			     /* Trailing '/' */
		StrAllocCopy(tailstr, "./");
	    } else {						     /* Root */
		StrAllocCopy(tailstr, "/");
	    }
	} else {			     /* Relative to parent directory */
	    StrAllocCopy(tailstr, ++tptr);
	    StrAllocCat(tailstr, "/");
	}
	tail = HTEscape(tailstr, URL_PATH);
	free(tailstr);
    }

    if (HTDirAccess == HT_DIR_SELECTIVE) {
	StrAllocCat(pathname, HT_DIR_ENABLE_FILE);
	if (HTStat(pathname, &file_info)) {
	    if (TRACE) fprintf(stderr,
	        "HTBrowse.... Can't stat() file: %s (errno: %d)\n",
			       pathname, errno);
	    free(pathname);
	    FREE(tail);
	    return HTErrorAdd(req, ERR_FATAL, NO, HTERR_FORBIDDEN,
			      NULL, 0, "HTBrowseDirectory");
	}
    }

    if ((dp = opendir(directory)) == NULL)
	return HTErrorSysAdd(req,  ERR_FATAL, NO, "opendir");

    if (HTDirDescriptions)
	descriptions = HTReadDescriptions(directory);
    
    /* Now, generate the Btree and put it out to the output stream. */
    {
	unsigned int filecnt = 0;
	char dottest = 2;		  /* To avoid two strcmp() each time */
	char *topstr;
	void *keyptr = NULL;		   /* Points to the key in file_info */
	STRUCT_DIRENT *dirbuf;
	struct passwd *pw_info;
	struct group *gr_info;
	HTBTree *bt;

	/* Set up sort key and initialize BTree */
	if (HTDirShowMask & HT_DIR_KEY_SIZE) {
	    keyptr = &file_info.st_size;
	    bt = HTBTree_new((HTComparer) HTDirLintCmp);   
	} else if (HTDirShowMask & HT_DIR_KEY_DATE) {
	    keyptr = &file_info.st_mtime;
	    bt = HTBTree_new((HTComparer) HTDirLintCmp); 
	} else if (HTDirShowMask & HT_DIR_SHOW_CASE) {
	    bt = HTBTree_new((HTComparer) HTDirStrCmp);
	} else {
	    bt = HTBTree_new((HTComparer) HTDirStrCaseCmp);
	}
	if ((HTDirSpace = (char *) malloc(HT_LENGTH_SPACE+1)) == NULL)
	    outofmem(__FILE__, "HTBrowseDirectory");
	memset((void *) HTDirSpace, ' ', HT_LENGTH_SPACE);
	*(HTDirSpace+HT_LENGTH_SPACE) = '\0';
	topstr = InitBody();
	HTDirFileLength = 0;

	/* Build tree */
	while ((dirbuf = readdir(dp))) {
	    HTDirKey *nodekey;
	    HTAtom *encoding = NULL;
	    HTAtom *language = NULL;
	    HTFormat format;

	    if (!dirbuf->d_ino)		 		 /* Skip if not used */
		continue;
	    
	    /* Current and parent directories are never shown in list */
	    if (dottest && (!strcmp(dirbuf->d_name, ".") ||
			    !strcmp(dirbuf->d_name, ".."))) {
		dottest--;
		continue;
	    }
	    if (!(HTDirShowMask & HT_DIR_SHOW_HID) && *dirbuf->d_name == '.')
		continue;

	    format = HTFileFormat(dirbuf->d_name, &encoding, &language);

	    /* First make a lstat() and get a key ready. */
	    *(pathname+pathend) = '\0';
	    StrAllocCat(pathname, dirbuf->d_name);
	    if (HTLstat(pathname, &file_info)) {
#ifndef VMS
		if (TRACE) fprintf(stderr,
		"HTBrowse.... OUPS, lstat failed on %s (errno: %d)\n",
				   pathname, errno);
		DirAbort(bt);
		goto cleanup;
#else /* VMS */
		/* for VMS the failure here means the file is not readable...
		   we however continue to browse through the directory... */
                continue;
#endif /* VMS */
	    }
	    if ((nodekey = (HTDirKey *) calloc(1, sizeof(HTDirKey))) == NULL)
		outofmem(__FILE__, "HTFileBrowseDirectory");

#ifndef VMS
	    /* Check if symbolic link, if so do a stat(). If this fails, don't
	       show the item in the list */
	    if ((file_info.st_mode & S_IFMT) == S_IFLNK) {
		int symend;		
		if (HTStat(pathname, &file_info)) {
		    if (TRACE)
			fprintf(stderr, "HTBrowse.... stat failed on symbolic link %s, errno: %d\n", pathname, errno);
		    KeyFree(nodekey);
		    continue;
		}
		if ((nodekey->symlink = 
		     (char *) malloc(HT_MAX_PATH+2)) == NULL)
		    outofmem(__FILE__, "HTFileBrowseDirectory");
		symend = readlink(pathname, nodekey->symlink, HT_MAX_PATH);
		if (symend < 0) {
		    if (TRACE)
			fprintf(stderr,
			        "HTBrowse.... readlink errno: %d (%s)\n",
				errno, pathname);
		    FREE(nodekey->symlink);
		} else {
		    *(nodekey->symlink+symend) = '\0';
		}
	    }
#endif /* not VMS */

	    /* Generate key entry in nodekey */
	    if (keyptr) {	             /* Use content of keyptr as key */
		if ((nodekey->key = (void *) malloc(sizeof keyptr)) == NULL ||
		    (nodekey->filename =
		     (char *) malloc(strlen(dirbuf->d_name)+1)) == NULL)
		    outofmem(__FILE__, "HTFileBrowseDirectory");
		memcpy(nodekey->key, keyptr, sizeof keyptr);
		strcpy(nodekey->filename, dirbuf->d_name);
	    } else {				/* Use dirbuf->d_name as key */
		if ((nodekey->key =
		     (void *) malloc(strlen(dirbuf->d_name)+1)) == NULL)
		    outofmem(__FILE__, "HTFileBrowseDirectory");
		strcpy(nodekey->key, dirbuf->d_name);
		nodekey->filename = nodekey->key;
	    }


	    /* Update current max filename length */
	    {
		int filestrlen = strlen(nodekey->filename);

		if ((file_info.st_mode & S_IFMT) == S_IFDIR) {
#ifdef VMS  
		    /* strip .DIR part... */
                    char *dot;
                    dot = strstr(nodekey->filename,".DIR");
                    if (dot)
                    {
                       *dot = '\0';
                       filestrlen -= 4;
                    }
#endif /* VMS */
		    nodekey->is_dir = YES;	/* We need the trailing slash*/
		    filestrlen++;
		}
		if (filestrlen > HTDirFileLength)
		    HTDirFileLength = filestrlen;
	    }

	    /* Get Icon type */
	    if (HTDirShowMask & HT_DIR_SHOW_ICON) {
		nodekey->icon = HTGetIcon(file_info.st_mode, format, encoding);
		nodekey->href = HTGetHref(nodekey->filename);
	    }

	    /* Generate body entry in nodekey */
	    if (HTBodyLength) {
		char *bodyptr;
		if  ((nodekey->body = (char *) malloc(HTBodyLength+1)) == NULL)
		    outofmem(__FILE__, "HTBrowseDirectory");
		bodyptr = nodekey->body;
		memset((void *) bodyptr, ' ', HTBodyLength);
		if (HTDirShowMask & HT_DIR_SHOW_DATE) {
#if defined(Mips) || (defined(VMS) && !defined(DECC))
		    struct tm * t = localtime(&file_info.st_mtime);

		    sprintf(bodyptr,"%02d-%s-%02d %02d:%02d",
			    t->tm_mday,
			    months[t->tm_mon],
			    t->tm_year % 100,
			    t->tm_hour,
			    t->tm_min);
#else
		    strftime(bodyptr, HT_LENGTH_DATE+1, "%d-%b-%y %H:%M",
			     localtime(&file_info.st_mtime));
#endif /* Mips || (VMS && !DECC) */
		    bodyptr += HT_LENGTH_DATE;
		    *bodyptr = ' ';
		    bodyptr += HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_SIZE) {
		    if ((file_info.st_mode & S_IFMT) != S_IFDIR)
			HTDirSize(file_info.st_size, bodyptr, HT_LENGTH_SIZE);
		    else
			bodyptr[HT_LENGTH_SIZE-1] = '-';
		    bodyptr += HT_LENGTH_SIZE+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_MODE) {
		    FilePerm(file_info.st_mode, bodyptr);
		    bodyptr += HT_LENGTH_MODE+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_NLINK) {
		    ItoA(file_info.st_nlink, bodyptr, HT_LENGTH_NLINK);
		    bodyptr += HT_LENGTH_NLINK+HT_LENGTH_SPACE;
		}
#ifndef VMS
		if (HTDirShowMask & HT_DIR_SHOW_OWNER) {
		    char *bp = bodyptr;
		    char *pwptr;
		    if ((pw_info = getpwuid(file_info.st_uid)) == NULL) {
			if (TRACE) fprintf(stderr,
				      "HTBrowse.... getpwuid() failed on %s\n",
					   pathname);
			ItoA(file_info.st_uid, bodyptr, HT_LENGTH_OWNER);
		    } else {
			pwptr = pw_info->pw_name;
			while ((*bp++ = *pwptr++) != '\0');
			*--bp = ' ';
		    }
		    bodyptr += HT_LENGTH_OWNER+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_GROUP) {
		    char *bp = bodyptr;
		    char *grptr;
		    if ((gr_info = getgrgid(file_info.st_gid)) == NULL) {
			if (TRACE) fprintf(stderr,
			    "HTBrowse.... getgrgid() failed on %s\n",
					   pathname);
			ItoA(file_info.st_gid, bodyptr, HT_LENGTH_GROUP);
		    } else {
			grptr = gr_info->gr_name;
			while ((*bp++ = *grptr++) != '\0');
			*--bp = ' ';
		    }
		    bodyptr += HT_LENGTH_GROUP+HT_LENGTH_SPACE;
		}
#endif /* not VMS */
		*bodyptr = '\0';
		if (HTDirDescriptions) {
		    char * d = HTGetDescription(descriptions,
						directory,
						dirbuf->d_name,
						format);
		    if (d) {
			int len = strlen(d);
			if (HT_LENGTH_SPACE > 1) {
			    if (len > HTDirMaxDescrLength)
				len = HTDirMaxDescrLength;
			}
			else {
			    /* Description always needs at least two spaces */
			    if (len > HTDirMaxDescrLength-1)
				len = HTDirMaxDescrLength-1;
			    *bodyptr++ = ' ';
			}
			strncpy(bodyptr, d, len);
			bodyptr[len] = '\0';
		    }
		}
	    }
	    /* Now, update the BTree etc. */
	    filecnt++;
	    HTBTree_add(bt, (void *) nodekey);
	} /* End while readdir() */

	if (HTDirFileLength > HTDirMaxFileLength)
	    HTDirFileLength = HTDirMaxFileLength;
	if (HTDirFileLength < HTDirMinFileLength)
	    HTDirFileLength = HTDirMinFileLength;

#if 0
	/* Now, put up the stream if no error has occured */
	if (!req->error_stack)
#endif
	{
	    HTStructured *target = HTML_new(req, NULL, WWW_HTML,
					    req->output_format,
					    req->output_stream);
	    
	    /* Put out the header for the HTML object */
	    HTDirOutTop(target, (HTAnchor *) req->anchor, topstr, directory,
			NULL);
	    
	    /* Run through tree printing out in order, hopefully :-) */
	    if (filecnt) {
		HTDirOutList(target, bt, tail, directory);
	    }
	    
	    HTDirOutBottom(target, filecnt, directory, NULL);
	    FREE_TARGET;
	}

#ifndef VMS /* Not used for VMS */
cleanup:
#endif
	if (descriptions)
	    HTFreeDescriptions(descriptions);
	FREE(HTDirSpace);
	FREE(topstr);
	free(tail);
	free(pathname);
	HTBTree_free(bt);
	closedir(dp);
    } /* End of two big loops */
    return HT_LOADED;
} /* End of directory reading section */

#endif /* GOT_READ_DIR */

/*						    	HTFTPBrowseDirectory()
**	This function scrolls through the directory file given in a FTP session
**	and generates an HTML-object. It uses the global variables:
**
**	  - HTDirShowMask	see enum HTDirShow for details
**	  - HTDirAccess		HT_DIR_[FORBID | SELECTIVE | OK]
**	  - HTDirReadme
**	
**	Generated URLs for non symbolic links are made relative to directory:
**
**		.../xxx/yyy	=>	yyy/<element>
**		.../xxx/yyy/	=>	./<element>
**		/		=>	/<element>
**     
**	Symbolic links are given as are (relative or absolute), but see note.
**
**	NOTE:	The function expects that the URL given as directory IS
**		UNescaped AND Simplified.
**
**	Returns < 0 on error, HT_LOADED on succes
*/
PUBLIC int HTFTPBrowseDirectory ARGS3(HTRequest *, req, char *, directory,
				      HTDirLineInput, input)
{
    int status;
    char *tail = NULL;
    
    if (TRACE) fprintf(stderr, "HTFTPBrowse. Browsing `%s\'\n", directory);
        
    /* Set up the offset string of the anchor reference */
    {
	char *tptr = strrchr(directory, '/');
	char *tailstr = NULL;
	if (!tptr) {					    /* '/' not found */
	    StrAllocCopy(tailstr, directory);
	    StrAllocCat(tailstr, "/");
	} else if (!*(tptr+1)) {
	    if (tptr != directory) {			     /* Trailing '/' */
		StrAllocCopy(tailstr, "./");
	    } else {						     /* Root */
		StrAllocCopy(tailstr, "/");
	    }
	} else {			     /* Relative to parent directory */
	    StrAllocCopy(tailstr, ++tptr);
	    StrAllocCat(tailstr, "/");
	}
	tail = HTEscape(tailstr, URL_PATH);
	free(tailstr);
    }
    
    /* Now, generate the Btree and put it out to the output stream. */
    {
	BOOL old_descr = HTDirDescriptions;
	unsigned int filecnt = 0;
	char *topstr = NULL;
	char dottest = 2;		  /* To avoid two strcmp() each time */
	dir_file_info file_info;
	HTBTree *bt;

	/* TEMPORARY */
	HTDirDescriptions = NO;

	/* Set up sort key and initialize BTree */
	if (HTDirShowMask & HT_DIR_KEY_SIZE) {
	    bt = HTBTree_new((HTComparer) HTDirLintCmp);   
	} else if (HTDirShowMask & HT_DIR_KEY_DATE) {
	    bt = HTBTree_new((HTComparer) HTDirLintCmp); 
	} else if (HTDirShowMask & HT_DIR_SHOW_CASE) {
	    bt = HTBTree_new((HTComparer) HTDirStrCmp);
	} else {
	    bt = HTBTree_new((HTComparer) HTDirStrCaseCmp);
	}
	if ((HTDirSpace = (char *) malloc(HT_LENGTH_SPACE+1)) == NULL)
	    outofmem(__FILE__, "HTFTPBrowseDirectory");
	memset((void *) HTDirSpace, ' ', HT_LENGTH_SPACE);
	memset((void *) &file_info, '\0', sizeof(dir_file_info));
	*(HTDirSpace+HT_LENGTH_SPACE) = '\0';
	topstr = InitBody();
	HTDirFileLength = 0;

	/* Build tree */
	while ((status = input(req->net_info, &file_info)) > 0) {
	    HTDirKey *nodekey;
	    HTAtom *encoding;
	    HTAtom *language;
	    HTFormat format;
	    
	    /* Current and parent directories are never shown in list */
	    if (dottest && (!strcmp(file_info.f_name, ".") ||
			    !strcmp(file_info.f_name, ".."))) {
		dottest--;
		DirFileInfoClear(&file_info);
		continue;
	    }

	    format = HTFileFormat(file_info.f_name, &encoding, &language);

	    /* Get a key ready. */
	    if ((nodekey = (HTDirKey *) calloc(1, sizeof(HTDirKey))) == NULL)
		outofmem(__FILE__, "HTFTPBrowseDirectory");

	    /* Generate key entry in nodekey */
	    if ((nodekey->key =
		 (void *) malloc(strlen(file_info.f_name)+1)) == NULL)
		outofmem(__FILE__, "HTFTPBrowseDirectory");
	    strcpy(nodekey->key, file_info.f_name);
	    nodekey->filename = nodekey->key;

	    /* Update current max filename length */
            {
                int filestrlen = strlen(nodekey->filename);

                if ((file_info.f_mode & S_IFMT) == S_IFDIR) {
                    nodekey->is_dir = YES;      /* We need the trailing slash*/
                    filestrlen++;
                }
                if (filestrlen > HTDirFileLength)
                    HTDirFileLength = filestrlen;
            }

	    /* Get Icon type */
	    if (HTDirShowMask & HT_DIR_SHOW_ICON) {
		nodekey->icon = HTGetIcon(file_info.f_mode, format, encoding);
		nodekey->href = HTGetHref(nodekey->filename);
	    }

	    /* Generate body entry in nodekey */
	    if (HTBodyLength) {
		char *bodyptr;
		if  ((nodekey->body = (char *) malloc(HTBodyLength+1)) == NULL)
		    outofmem(__FILE__, "HTBrowseDirectory");
		bodyptr = nodekey->body;
		memset((void *) bodyptr, ' ', HTBodyLength);
		if (HTDirShowMask & HT_DIR_SHOW_DATE) {
		    if (file_info.f_mtime) {
#if defined(Mips) || (defined(VMS) && !defined(DECC))
			struct tm * t = localtime(&file_info.f_mtime);

			sprintf(bodyptr,"%02d-%s-%02d %02d:%02d",
				t->tm_mday,
				months[t->tm_mon],
				t->tm_year % 100,
				t->tm_hour,
				t->tm_min);
#else
			strftime(bodyptr, HT_LENGTH_DATE+1, "%d-%b-%y %H:%M",
				 localtime(&file_info.f_mtime));
#endif /* Mips || (VMS && !DECC) */
		    } else {
			*bodyptr = '-';
		    }
                    bodyptr += HT_LENGTH_DATE;
                    *bodyptr = ' ';
                    bodyptr += HT_LENGTH_SPACE;
		}

		/* If we are using NLST, then don't show any size */
		if (HTDirShowMask & HT_DIR_SHOW_SIZE) {
                    if ((file_info.f_mode & S_IFMT) != S_IFDIR &&
			HTFTUseList(req->net_info) == YES)
                        HTDirSize(file_info.f_size, bodyptr, HT_LENGTH_SIZE);
                    else
                        bodyptr[HT_LENGTH_SIZE-1] = '-';
                    bodyptr += HT_LENGTH_SIZE+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_MODE) {
                    FilePerm(file_info.f_mode, bodyptr);
		    bodyptr += HT_LENGTH_MODE+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_NLINK) {
		    ItoA(file_info.f_nlink, bodyptr, HT_LENGTH_NLINK);
		    bodyptr += HT_LENGTH_NLINK+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_OWNER) {
		    char *bp = bodyptr;
		    char *owner = file_info.f_uid;
		    if (owner) {
			while ((*bp++ = *owner++) != '\0');
			*--bp = ' ';
		    } else {
			*bp++ = '-';
		    }
		    bodyptr += HT_LENGTH_OWNER+HT_LENGTH_SPACE;
		}
		if (HTDirShowMask & HT_DIR_SHOW_GROUP) {
		    char *bp = bodyptr;
		    char *group = file_info.f_gid;
		    if (group) {
			while ((*bp++ = *group++) != '\0');
			*--bp = ' ';
		    } else {
			*bp = '-';
		    }
		    bodyptr += HT_LENGTH_GROUP+HT_LENGTH_SPACE;
		}
		*bodyptr = '\0';
	    }	    

	    /* Now, update the BTree etc. */
	    filecnt++;
	    DirFileInfoClear(&file_info);
	    HTBTree_add(bt, (void *) nodekey);
	} /* End while input() */

	if (status < 0) {
	    DirAbort(bt);
	    goto cleanup;
	}

	if (HTDirFileLength > HTDirMaxFileLength)
            HTDirFileLength = HTDirMaxFileLength;
        if (HTDirFileLength < HTDirMinFileLength)
            HTDirFileLength = HTDirMinFileLength;

#if 0
	/* Now, put up the stream if no error has occured */
	if (!req->error_stack)
#endif
	{
	    HTStructured *target = HTML_new(req, NULL, WWW_HTML,
					    req->output_format,
					    req->output_stream);

	    /* Put out the header for the HTML object */
	    HTDirOutTop(target, (HTAnchor *) req->anchor, topstr, directory,
			HTFTPWelcomeMsg(req->net_info));
	    
	    /* Run through tree printing out in order, hopefully :-) */
	    if (filecnt) {
		HTDirOutList(target, bt, tail, directory);
	    }
	    HTDirOutBottom(target, filecnt, directory,
			   HTFTPWelcomeMsg(req->net_info));
	    FREE_TARGET;
	}

cleanup:
	FREE(HTDirSpace);
	FREE(topstr);
	free(tail);
	HTBTree_free(bt);

	/* TEMPORARY */
	HTDirDescriptions = old_descr;
    } /* End of two big loops */
    return status ? status : HT_LOADED;
} /* End of FTP directory listing */