download cache.cpp
Language: C++
Copyright: Copyright 1987-96, Bit Bucket Software Co.
LOC: 921
Project Info
BinkleyTerm XE(btxe)
Server: SourceForge
Type: cvs
...e\b\btxe\btxe\btnt\sources\
   Addr.cpp
   Addr.h
   aglcrc.cpp
   aglcrc.h
   asyn_lnx.cpp
   asyn_os2.cpp
   asyn_w32.cpp
   async.h
   asyos2v1.h
   b_banner.cpp
   b_bbs.cpp
   b_faxrcv.cpp
   b_flofil.cpp
   b_frproc.cpp
   b_help.cpp
   b_initva.cpp
   b_logs.cpp
   b_passwo.cpp
   b_proto.cpp
   b_rspfil.cpp
   b_sbinit.cpp
   b_script.cpp
   b_search.cpp
   b_sessio.cpp
   b_spawn.cpp
   b_term.cpp
   b_termov.cpp
   b_today.cpp
   b_wazoo.cpp
   b_whack.cpp
   b_wzsend.cpp
   banner.cpp
   banner.h
   BasicString.h
   bbs_ctra.cpp
   bbs_data.cpp
   bbs_incl.h
   bbs_io.cpp
   bink.h
   bink_asm.cpp
   binkdlg.cpp
   binkpipe.h
   box.h
   brec.cpp
   bsend.cpp
   bt.cpp
   btconfig.cpp
   btutil.cpp
   buffer.h
   cache.cpp
   cache.h
   callback.cpp
   callerid.cpp
   callwin.cpp
   callwin.h
   capi.cpp
   cfosline.cpp
   chat.cpp
   com.h
   com_dos.h
   com_lnx.h
   com_os2.h
   com_w32.h
   common.cpp
   cpuload.cpp
   css.cpp
   css.h
   data.cpp
   debug.cpp
   debug.h
   defines.h
   dosfuncs.cpp
   emsi.cpp
   emxmxcom.cpp
   evtparse.cpp
   evtsetup.cpp
   exceptio.cpp
   exceptio.h
   Exceptions.cpp
   Exceptions.h
   externs.h
   faxproto.h
   fcfgread.cpp
   fidomail.h
   file_all.cpp
   file_dos.cpp
   file_io.h
   file_lnx.cpp
   file_os2.cpp
   file_w32.cpp
   foffsets.h
   fossil.cpp
   freepoll.cpp
   ftsc.cpp
   gethcomm.h
   heap.cpp
   history.cpp
   history.h
   hydra.cpp
   hydra.h
   includes.cpp
   includes.h
   janus.cpp
   janus.h
   keybd.h
   keyfncs.h
   keymap.cpp
   langload.cpp
   language.h
   lngmagic.h
   m7rec.cpp
   m7send.cpp
   mailer.cpp
   mailovly.cpp
   mailroot.cpp
   maxcomm.h
   maxmcp.cpp
   maxmcp.h
   maxprm.cpp
   mdm_proc.cpp
   misc.cpp
   msgs.h
   Node.cpp
   Node.h
   NodeList.cpp
   NodeList.h
   nodeproc.cpp
   os2_pm.cpp
   outbound.cpp
   pipe.cpp
   pktmsgs.cpp
   pktmsgs.h
   protcomm.cpp
   prototyp.h
   recvbark.cpp
   recvsync.cpp
   sbuf.cpp
   sbuf.h
   sched.cpp
   sched.h
   script2.cpp
   SelfTest.cpp
   SelfTest.h
   sendbark.cpp
   sendsync.cpp
   squish.cpp
   squish.h
   srif.cpp
   statetbl.cpp
   stats.cpp
   stdafx.cpp
   stdafx.h
   timer.cpp
   timer.h
   times.cpp
   types.h
   v7debug.cpp
   version.cpp
   version7.cpp
   version7.h
   vfos_dos.cpp
   vfos_lnx.cpp
   vfos_os2.cpp
   vfos_qnx.cpp
   vfos_w32.cpp
   vfossil.h
   video.h
   vt100.cpp
   vt100.h
   xfer.h
   xmrec.cpp
   xmsend.cpp
   xstring.h
   yoohoo.cpp
   zmodem.cpp
   zmodem.h

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
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
/*---------------------------------------------------------------------------*
 *
 *                               BinkleyTerm
 *
 *              (C) Copyright 1987-96, Bit Bucket Software Co.
 *     For license and contact information see /doc/orig_260/license.260.
 *
 *           This version was modified by the BinkleyTerm XE Team.
 *        For contact information see /doc/team.lst and /doc/join.us.
 *  For a complete list of changes see /doc/xe_user.doc and /doc/xe_hist.doc.
 *
 * Filename    : $Source: /cvsroot/btxe/btnt/sources/cache.cpp,v $
 * Revision    : $Revision: 1.1 $
 * Tagname     : $Name:  $
 * Last updated: $Date: 2005/01/01 15:18:12 $
 * State       : $State: Exp $
 * Orig. Author: MMP
 *
 * Description : Outbound Cache
 *
 *---------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
 * CACHE DATA STRUCTURE
 *
 * The ent[] array in DIRECTORY is a hash table with single chained lists
 * of directory entries with the same hash value. If KEEPORDER is defined,
 * an additional chain is maintained using DIRECTORY.list and DIRENTRY.dirnext.
 * This chain is used to chain the directory entries in the order they were
 * originally read. HPFS users will expect the outbound to be sorted like
 * they are used to.
 *
 *   dirlist
 *      |
 *      V                +-----------+  +-----------+  +-----------+
 *  +-----------------++>|DIRENT next|->|DIRENT next|->|DIRENT next|->NULL
 *  |DIRECTORY  ent[0]|' +-----------+  +-----------+  +-----------+
 *  |           ent[1]|->...
 *  |              :  |  +-----------+  +-----------+  +-----------+
 *  |  ent[HASHSIZE-1]|->|DIRENT next|->|DIRENT next|->|DIRENT next|->NULL
 *  | next        flow|, +-----------+  +-----------+  +-----------+
 *  +-----------------+| +-------------+  +-------------+
 *      |              +>|FLOWFILE next|->|FLOWFILE next|->NULL
 *      |                |    files    |  |    files    |
 *      |                +-------------+  +-------------+
 *      |                       |                | +------------+
 *      |                       |                }>|FLOWENT next|->...
 *      |                       |                  |     dir    |
 *      |                       |                  +------------+
 *      |                       |                         :
 *      |                       |
 *      |                       | +------------+  +------------+
 *      |                       +>|FLOWENT next|->|FLOWENT next|->NULL
 *      |                         |     dir    |  |     dir    |
 *      |                         +------------+  +------------+
 *      V                                |               |
 *  +-----------------+<-----------------|---------------+
 *  |DIRECTORY  ent[0]|->...             |
 *  |           ent[1]|->...             |
 *  |              :  |                  |
 *  |  ent[HASHSIZE-1]|->...             |
 *  | next        flow|->...             |
 *  +-----------------+                  |
 *      |                                |
 *      V                                |
 *  +-----------------+<-----------------+
 *  |DIRECTORY  ent[0]|->...
 *  |           ent[1]|->...
 *  |              :  |
 *  |  ent[HASHSIZE-1]|->...
 *  | next        flow|->...
 *  +-----------------+
 *      |
 *      V
 *     NULL
 *
 * The DIRECTORY structures are considered static; once a directory makes it
 * into the list, it is never removed again. The other structures are dynamic
 * in the sense, that they are deallocated and reused. They are not actually
 * freed, but inserted into free lists. In this way, we avoid heap manager
 * overhead, and reduce fragmentation. Structures are allocated in blocks of
 * BLOCKSIZE (50), and when the system is up and running, the memory usage is
 * likely to have stabilized, and the heap manager is no longer used. However,
 * if memory is exhausted, allocations can fail. Therefore, all allocations
 * are made by getmem() which turns the cache off, if an allocation error
 * occurs.
 *
 * The structures are dimensioned to DOS' 8.3 char filenames, and the cache
 * simply can't contain those names. Long names are handled specially in
 * cachestat() so the statistics will be correct, but dirread() will not
 * return them. This is no big problem, as Binkley is not looking for long
 * names, when scanning directories.
 *-----------------------------------------------------------------------------
 */

#include "includes.h"

#ifdef CACHE

static DIRECTORY *dirlist = NULL;  /* List of known directories        */
static DIRENTRY *defl = NULL;   /* Directory Entry Free List        */
static FLOWFILE *fffl = NULL;   /* Flow File Free List              */
static FLOWENT *fefl = NULL;    /* Flow Entry Free List             */

#define _C_DEBUG

/*-----------------------------------------------------------------------------
 * void *getmem(size_t size)
 *
 * size         Number of bytes to allocate
 *
 * Returns      Pointer to new memory on success; NULL on error
 *
 * This is a simple wrapper around to malloc() used for two purposes:
 *   1. Turn the cache off, if a memory allocation occurs
 *   2. Keep track of the memory usage for statistics
 *-----------------------------------------------------------------------------
 */

static void *
getmem (size_t size)
{
    void *p;

    if ((p = malloc (size)) == NULL)
    {
        cachehold = 0;
        status_line (MSG_TXT (M_NOMEM_FOR_OUTB_CACHE));
    }
    else
    {
        cachemem += size;
        memset (p, 0, size);        /* For dumping freelists in cachedump() */
    }

    return (p);
}


/*-----------------------------------------------------------------------------
 * void cacheflush(void)
 *
 * Flushes the cache for all directories. This will force dirload() to actually
 * reload the directory. cacheflush() does NOT remove the cached flow files,
 * because they can be reused after the directory has been loaded.
 *-----------------------------------------------------------------------------
 */

static void
dirflush (DIRECTORY * dir)
{
    DIRENTRY *ent;
    int i;

    if (dir->loaded)
    {
        for (i = 0; i < HASHSIZE; i++)
            while ((ent = dir->ent[i]) != NULL)
                dir->ent[i] = ent->next, ent->next = defl, defl = ent;
#ifdef KEEPORDER
        dir->list = NULL;
#endif
        dir->loaded = 0;
    }
}

void
cacheflush (void)
{
    DIRECTORY *dir;

#ifdef C_DEBUG
    cachedump ("cache.0");
#endif
    for (dir = dirlist; dir; dir = dir->next)
        dirflush (dir);
#ifdef C_DEBUG
    cachedump ("cache.1");
    status_line ("Mcacheflush() done");
#endif
}


/*-----------------------------------------------------------------------------
 * char *basename(char *path)
 *
 * path         Pathname
 *
 * Returns      Basename within path
 *
 * Returns a pointer to the basename within a pathname; that is a pointer to
 * the first character after the last drive or directory separator. If the
 * path does not contain any drive or directory seprators, path itself is
 * returned.
 *-----------------------------------------------------------------------------
 */

static char *
basename (char *path)
{
    char *a, *b;

    for (a = "\\:/"; *a; a++)
        if ((b = strrchr (path, *a)) != NULL)
            path = b + 1;

    return (path);
}


/*-----------------------------------------------------------------------------
 * DIRECTORY *getdir(char *name)
 *
 * name         Name of file
 *
 * Returns      Diretory structure on success; NULL on error
 *
 * This function is used for looking up known directories and add new ones. If
 * getdir() is able to find <name> in the known directory list, it simply
 * returns a pointer to the directory. If getdir() cannot find the directory,
 * it is added to the list, and a new (empty) directory structure is returned.
 * This addition may fail because of memory problems (getmem), in which case
 * NULL is returned.
 *-----------------------------------------------------------------------------
 */

static DIRECTORY *
getdir (char *name)
{
    char drive;                   /* Drive letter */
    long len;                     /* Lenght of path without drive */
    DIRECTORY *dir;               /* The one to return */
    DIRECTORY **dpp;              /* Used for rearanging list */
    int hit;                      /* Counter for debugging and statistics */

    /* Extract drive letter. The drive letter might no be present, in which
     * we use the current drive. The logic about the current drive, ensures
     * the the current drive is only obtained once; the operating system is
     * only bothered with a single call. */

    if (isalpha (name[0]) && name[1] == ':')
    {
        drive = (char) toupper (*name);
        name += 2;
    }
    else
    {
        static char curdrive = '\0';

        if (!curdrive)
        {
            char cwd[PATHLEN];

            getcwd (cwd, sizeof (cwd));
            curdrive = (char) toupper (*cwd);
        }
        drive = curdrive;
    }

    /* Get length of path. We use the length and memicmp() instead of
     * stricmp(). This has two advantanges: 1) it allows us to skip a
     * lot of string comparisons 2) we don't need to strip off the
     * filename in the name.
     */
    len = (long) (basename (name) - name);

    /* Check if directory is listed. If we can't find, we continue below
     * and creates a new entry for it. However, if we can find it, the
     * directory is move in front of the list. This speeds up the next
     * call to getdir() if we are searching for a file in the same
     * directory, which is most likely. Then we return the directory.
     */
    hit = 0;
    dpp = &dirlist;
    for (dir = *dpp; dir; dpp = &dir->next, dir = *dpp, hit++)
        if (drive == dir->drive && len == dir->len && !memicmp (dir->name, name,
                (size_t) len))
        {
#ifdef C_DEBUG
            status_line ("MDIRECTORY %c:%.*s hit at %d", drive, len, name, hit);
#endif
            *dpp = dir->next;
            dir->next = dirlist;
            dirlist = dir;
            return (dir);
        }

    /* Allocate new directory node */
    if ((dir = (DIRECTORY *) getmem ((size_t) (sizeof (DIRECTORY) + len))) == NULL)
        return (NULL);

    /* Initalize new node, and insert in list */
#ifdef C_DEBUG
    status_line ("MDIRECTORY %c:%.*s not hit", drive, len, name);
#endif
    dir->flow = NULL;
    dir->loaded = 0;
    memset (dir->ent, 0, sizeof (dir->ent));  /* Reset hash table */
#ifdef KEEPORDER
    dir->list = NULL;
#endif
    dir->drive = drive;
    dir->len = (int) len;
    memcpy (dir->name, name, (size_t) len);
    dir->name[(int) len] = '\0';

    /* Insert in directory list */
    dir->next = dirlist;
    dirlist = dir;

    /* Return new directory node */
    return (dir);
}


/*-----------------------------------------------------------------------------
 * int strhash(char *str)
 *
 * str          String
 *
 * Returns      Hash value
 *
 * Computes a hash value in the range 0..HASHSIZE-1 for the string str. The
 * same hash key is generated for both lower and upper case strings.
 *-----------------------------------------------------------------------------
 */
#define NBITS   (sizeof(unsigned)<<3)  /* bits in an int (2*8 or 4*8) */
#define N75     ((unsigned)(((NBITS)*3)>>2))
#define N12     ((unsigned)((NBITS)>>3))
#define HB      (~((unsigned)(~0)>>N12))

/*
   ----------------------------------- 16 bits --------------------------
   nbits =  16 ==   2*8
   n75   =  12 == ((2*8)*3)/4 == 12
   N12   =  2  ==  (2*8)/8
   HB    =  ~(1111 1111 1111 1111 >> n12)
   =  ~(0011 1111 1111 1111)
   =  0xC000
   ----------------------------------- 32 bits --------------------------
   nbits =  32 == 4*8
   n75   =  24 == ((4*8)*3)/4 == 24
   n12   =   4 == (4*8)/8
   HB    =  ~(1111 1111 1111 1111 1111 1111 1111 1111 >> N12)
   =  ~(0000 1111 1111 1111 1111 1111 1111 1111)
   =  0xF0000000
   ----------------------------------------------------------------------
 */

static int
strhash (char *s)
{
    unsigned g, h = 0;

    while (*s)
    {
        h = (h << N12) + toupper (*s);
        s++;
        if ((g = (h & HB)) != 0)
            h = (h ^ (g >> N75)) & ~HB;
    }

    return ((int) (h % HASHSIZE));
}


/*-----------------------------------------------------------------------------
 * int isdosname(char *name)
 *
 * name         Basename
 *
 * Returns      Non-zero if 8.3, zero if not
 *
 * Checks if a filename, without path, is a valid DOS 8.3 name.
 *-----------------------------------------------------------------------------
 */

static int
isdosname (char *name)
{
    char *e;

    e = strchr (name, '.');
    if (e)
        return ((e - name) < 9 && strlen (e) < 5 && (e > name) && *name);
    return (strlen (name) < 9 && *name);
}


/*-----------------------------------------------------------------------------
 * int dirload(DIRECTORY *dir)
 *
 * dir          Directory
 * lazy         Flag. Use cache if available?
 *
 * Returns      0 on success; non-zero on memory problems.
 *
 * Loads a directory from the disk into the cache. If the directory is already
 * cached, this is a no-operation. Use cacheflush() first to force a reload of
 * directories. dirload() may fail because of memory problems, in which case a
 * non-zero value if returned.
 *-----------------------------------------------------------------------------
 */

static int
dirload (DIRECTORY * dir)
{
    DTA dta;
    DIRENTRY *ent;
    FLOWFILE *flow;
    FLOWFILE **flowp;
    FLOWENT *fent;

#ifdef KEEPORDER
    DIRENTRY *last;

#endif
    char path[PATHLEN];
    int j, cnt;
    int hash;                     /* Hash key for filename */

    /* Check if directory is already loaded */
    if (dir->loaded)
    {
#ifdef C_DEBUG
        status_line ("M%c:%s already loaded", dir->drive, dir->name);
#endif
        return (0);
    }

    /* Setup search path */
    sprintf (path, "%c:%s*.*", dir->drive, dir->name);

    /* Traverse directory. */
    cnt = j = 0;
#ifdef KEEPORDER
    dir->list = last = NULL;
#endif
    while (!dfind (&dta, path, j))
    {
        j = 1;
        if (isdosname (dta.name) && !(dta.attr & FA_VOLID))
        {

            /* Allocate DIRENTRY structure from the free list. If the free list
             * is empty, we expand it first. */
            if (defl == NULL)
            {
                int i;

#ifdef C_DEBUG
                status_line ("MExpanding freelist with DIRENTRY");
#endif
                if ((defl = getmem (BLOCKSIZE * sizeof (DIRENTRY))) == NULL)
                    return (1);
                for (i = 0; i < BLOCKSIZE - 1; i++)
                    defl[i].next = defl + i + 1;
                defl[i].next = NULL;
            }
            ent = defl;
            defl = defl->next;

            /* Initialize directory entry. The name is lower cased, because this
             * will simplify comparisons later when traversing the directory. */
            ent->time = dta.time;
            ent->attr = dta.attr;
            ent->size = dta.size;
            strcpy (ent->name, dta.name);
            strlwr (ent->name);       /* Better now, than when comparing */

            /* Insert in hash table of directory */
            hash = strhash (ent->name);
            ent->next = dir->ent[hash];
            dir->ent[hash] = ent;

            /* Insert in single chained list */
#ifdef KEEPORDER
            ent->dirnext = NULL;
            if (last)
                last->dirnext = ent;
            else
                dir->list = ent;
            last = ent;
#endif
            cnt++;
        }
    }

#ifdef C_DEBUG
    status_line ("Mdirload(%s) found %d files", path, cnt);

    /* Dump hash table */
    for (hash = 0; hash < HASHSIZE; hash++)
    {
        for (ent = dir->ent[hash]; ent; ent = ent->next)
            status_line ("M    dir[\"%s\"].ent[%d], %s", dir->name, hash, ent->name);
    }

    /* Dump single chained list */
    for (ent = dir->list; ent; ent = ent->next)
    {
        status_line ("M    %s%s", dir->name, ent->name);
    }
#endif

    /* Clean up */
    if (j)
        dfind (&dta, NULL, 2);

    /* Set time of loading and return */
    dir->loaded = unix_time (NULL);

    /* Invalidate cached flow files */
    flowp = &dir->flow, j = 0;
    while ((flow = *flowp) != NULL)
    {
#ifdef C_DEBUG
        status_line ("Mdirload(): check %s", flow->name);
#endif

        ent = dir->ent[flow->hash];
        while (ent && strcmp (ent->name, flow->name))
            ent = ent->next;

        if (ent == NULL ||
                ent->time != flow->time ||
                ent->size != flow->size ||
                ent->attr != flow->attr)
        {
#ifdef C_DEBUG
            status_line ("M%s%s has %s", dir->name, flow->name, ent ? "changed" :
                         "disappeared");
#endif

            while ((fent = flow->files) != NULL)
            {
                flow->files = fent->next;
                fent->next = fefl;
                fefl = fent;
            }

            *flowp = flow->next;
            flow->next = fffl;
            fffl = flow;
            j++;
        }
        else
        {
            flowp = &flow->next;
        }
    }

#ifdef C_DEBUG
    if (j)
    {
        status_line ("M%d flow files invalidated. Dumping cache", j);
        cachedump ("cache.inv");
    }
#endif

    return (0);
}


/*-----------------------------------------------------------------------------
 * void dirinit(DIRBUF *buf, char *fspec)
 *
 * buf          Directory buffer
 * fspec        File specification
 *
 * Initializes reading of a directory. After the setup, the directory entries
 * can actually be read using dirread().
 *
 * dirinit() and dirread() implements the optional caching of directories. If
 * caching is turned off, they are simple wrappers around dfind(). If caching
 * is turned on, dirinit() sets up a list of directory entries by loading the
 * directory, or simple initializes the pointer to an already loaded directory
 * list.
 *-----------------------------------------------------------------------------
 */

void
dirinit (DIRBUF * buf, char *fspec)
{
    DIRECTORY *dir;
    int len;
    char *src, *srcext, *dest;

    if (!cachehold ||
            (dir = getdir (fspec)) == NULL ||
            (!dir->loaded && dirload (dir)))
    {
        /* Better be safe and clear the DTA structure. Originally, it was
         * initialized to {0} everywhere, eventhough i can se no reason for
         * that */
        buf->cache = 0;
        memset (&buf->u.slow.dta, 0, sizeof (DTA));
        buf->u.slow.more = buf->u.slow.cleanup = !dfind (&buf->u.slow.dta,
                           fspec, 0);
        return;
    }

    /* Initialize cached traversal */
    buf->cache = 1;
    buf->u.fast.dir = dir;
#ifdef KEEPORDER
    buf->u.fast.next = dir->list;
#else
buf->u.fast.hash = 0;
    buf->u.fast.next = dir->ent[0];
#endif

    /* Format mask. dirread() depends on 11 character with space padding,
     * '*' expanded to '?'s, and upper case letters.
     */
    src = basename (fspec), srcext = strchr (src, '.'), len = strlen (src);
    strcpy (buf->u.fast.mask, "           ");  /* 11 spaces */
    if (srcext == NULL)
        srcext = "???";
    else
        srcext++;
    len = 0, dest = buf->u.fast.mask;
    while (*src && *src != '.' && len < 8)
    {
        if (*src == '*')
            *dest++ = '?';
        else
            *dest++ = *src++;
        len++;
    }
    len = 0, dest = buf->u.fast.mask + 8;
    while (*srcext && len < 3)
    {
        if (*srcext == '*')
            *dest++ = '?';
        else
            *dest++ = *srcext++;
        len++;
    }
    strupr (buf->u.fast.mask);
}


/*-----------------------------------------------------------------------------
 * int fmatch(char *name, char *mask)
 *
 * name         Filename without path
 * mask         Filemask without path
 *
 * Returns      Non-zero if match; zero if mismatch
 *
 * Compares the name with the mask, and returns the result. This routine
 * assumes that the name is no longer than 12 characters.
 *-----------------------------------------------------------------------------
 */

static int
fmatch (char *name, char *mask)
{
    char nbuf[12];
    char *e;
    int len;

    len = strlen (name);
    e = strchr (name, '.');
    strcpy (nbuf, "           "); /* 11 spaces */
    if (e)
    {
        if (e > name + 8)
            return (0);
        memcpy (nbuf, name, (size_t) (e - name));
        e++, len = strlen (e);
        memcpy (nbuf + 8, e, len);
    }
    else
    {
        memcpy (nbuf, name, len);
    }
    strupr (nbuf);

    /* Match filename */
    name = nbuf;
    while (*name && *mask)
    {
        if (*mask != '?' && *mask != *name)
            return (0);
        name++, mask++;
    }

    return (*name == '\0' && *mask == '\0');
}


/*-----------------------------------------------------------------------------
 * int dirread(DIRBUF *buf)
 *
 * buf          Directory buffer
 *
 * Returns      0 on success; non-zero on error
 *
 * Reads the next entry from the directory buffer. If there are no more
 * available entries, a non-zero value is returned. If zero is returned,
 * the entry is returned in the DIRBUF fields time, attr, size, and name
 * fields.
 *-----------------------------------------------------------------------------
 */

int
dirread (DIRBUF * buf)
{
    int res;

    if (!buf->cache)
    {
        res = !buf->u.slow.more;
        if (!res)
        {
            buf->attr = buf->u.slow.dta.attr;
            buf->size = buf->u.slow.dta.size;
            buf->time = buf->u.slow.dta.time;
            strcpy (buf->name, buf->u.slow.dta.name);
            buf->u.slow.more = !dfind (&buf->u.slow.dta, NULL, 1);
        }
    }
    else
    {
#ifdef KEEPORDER
        DIRENTRY *ent;

        res = 1;
        while ((ent = buf->u.fast.next) != NULL)
        {
            buf->u.fast.next = ent->dirnext;
            if (fmatch (ent->name, buf->u.fast.mask))
            {
                buf->attr = ent->attr;
                buf->size = ent->size;
                buf->time = ent->time;
                strcpy (buf->name, ent->name);
                res = 0;
                break;
            }
        }
#else
DIRENTRY *ent;
        DIRECTORY *dir;
        int hash;

        hash = buf->u.fast.hash;
        dir = buf->u.fast.dir;
        res = 1;
        while (res && hash < HASHSIZE)
        {
            while ((ent = buf->u.fast.next) != NULL)
            {
                buf->u.fast.next = ent->next;
                if (fmatch (ent->name, buf->u.fast.mask))
                {
                    buf->attr = ent->attr;
                    buf->size = ent->size;
                    buf->time = ent->time;
                    strcpy (buf->name, ent->name);
                    res = 0;
                    break;
                }
            }
            if (res)
            {
                if (hash++ < HASHSIZE)
                    buf->u.fast.next = dir->ent[hash];
                else
                    buf->u.fast.next = NULL;
            }
        }
        buf->u.fast.hash = hash;
#endif
    }

    return (res);
}


/*-----------------------------------------------------------------------------
 * void dirquit(DIRBUF *buf, int flush)
 *
 * buf          Directory buffer
 * flush        Flag. Flush directory?
 *
 * Returns      -
 *
 * Deallocate any memory neccesary when stopping reading of a directory.
 *-----------------------------------------------------------------------------
 */

void
dirquit (DIRBUF * buf, int flush)
{
    if (!buf->cache)
    {
        if (buf->u.slow.cleanup)
            dfind (&buf->u.slow.dta, NULL, 2);
    }
    else
    {
        if (flush)
            dirflush (buf->u.fast.dir);
    }
}


/*-----------------------------------------------------------------------------
 * time_t dos2unix(unsigned long dostime)
 *
 * time         DOS file time stamp
 *
 * Returns      Unix style time stamp
 *
 * Converts the timestamp to a unix style julian
 *-----------------------------------------------------------------------------
 */

static time_t
dos2unix (unsigned long dostime)
{
    struct tm t;

    t.tm_sec = (int) (dostime & 0x1FUL) * 2;
    dostime >>= 5UL;
    t.tm_min = (int) (dostime & 0x3FUL);
    dostime >>= 6UL;
    t.tm_hour = (int) (dostime & 0x1FUL);
    dostime >>= 5UL;
    t.tm_mday = (int) (dostime & 0x1FUL);
    dostime >>= 5UL;
    t.tm_mon = (int) (dostime & 0x0FUL) - 1;
    dostime >>= 4UL;
    t.tm_year = (int) (dostime + 80);  /* TJW 971020 bugfix */
    t.tm_wday = t.tm_yday = t.tm_isdst = -1;
    return (unix_mktime (&t));
}

#endif /* CACHE */

/*-----------------------------------------------------------------------------
 * int cachestat(char *path, struct stat *buf, DIRECTORY *dirp, DIRENTRY **entp,
 *               int freqflag, long freqfilesize);
 *
 * path         Pathname of file
 * buf          stat buffer
 * dirp         Where to store directory, or NULL
 * dp           Where to store pointer to directory entry, or NULL
 * freqfilesize if != 0 : size of file to be frequested
 *
 * Returns      0 on success; non-zero on error
 *
 * stat() replacement that takes advantage of the information stored in the
 * cached directories. If cachestat() can find the entry in the cache, only
 * the st_size, st_mtime, and the directory attribute of the st_mode field
 * are guaranteed to be be valid.
 *
 * If (dp!=NULL), a pointer to the found cached entry is returned in *dp, if
 * found, otherwise *dp is set to NULL. cachestat() may run out of memory, in
 * which case cachehold will be reset.
 *-----------------------------------------------------------------------------
 */

int
cachestat (char *path, struct stat *buf,
#ifdef CACHE
           DIRECTORY ** dirp, DIRENTRY ** entp,
#endif
           int freqflag, long freqfilesize)
{
#ifdef CACHE
    DIRECTORY *dir;
    DIRENTRY *ent;
    char *base;
    int hit;

    /* Return NULL's */
    if (dirp)
        *dirp = NULL;
    if (entp)
        *entp = NULL;
#endif /* CACHE */

    if (freqflag)
    {                             /* keep this short, this is a freq */
        buf->st_size = freqfilesize;
#ifdef __WATCOMC__              /*  TS 960901 seems only Watcom does it like this  */
        buf->st_attr = 0;
#else /*  anybody else except Watcom  */
buf->st_mode = 0;
#endif
        buf->st_mtime = unix_time (NULL);
        buf->st_ctime = buf->st_mtime;
        buf->st_atime = buf->st_mtime;
        return (0);
    }

#ifdef CACHE
    /* Get directory structure */
    if (!cachehold || (dir = getdir (path)) == NULL)
        goto fallback;
    if (dirp)
        *dirp = dir;

    /* Load directory if neccesary */
    if (!dir->loaded && dirload (dir))
        goto fallback;

    /* Get basename, and check if it is not an 8.3 name (can't cache them) */
    base = basename (path);
    if (!isdosname (base))
        goto fallback;

    /* Search directory for a match */
    for (hit = 0, ent = dir->ent[strhash (base)]; ent; ent = ent->next, hit++)
    {
        if (!stricmp (base, ent->name))
        {
            if (entp)
                *entp = ent;
            buf->st_size = ent->size;
#ifdef __WATCOMC__              /*  TS 960901 seems only Watcom does it like this  */
            buf->st_attr = 0;
#else /*  anybody else except Watcom  */
buf->st_mode = 0;
#endif
            buf->st_mtime = dos2unix (ent->time);
            buf->st_ctime = buf->st_mtime;
            buf->st_atime = buf->st_mtime;
            if (ent->attr & FA_SUBDIR)
#ifdef __WATCOMC__              /*  TS 960901 seems only Watcom does it like this  */
                buf->st_attr |= S_IFDIR;
#else /*  anybody else except Watcom  */
                buf->st_mode |= S_IFDIR;
#endif
#ifdef C_DEBUG
            status_line ("M%s%s hit at %d", dir->name, ent->name, hit);
#endif
            return (0);
        }
    }

    /* It wasn't there - report that */
#ifdef C_DEBUG
    status_line ("M%s not found in cache", path);
#endif
    errno = ENOENT;
    return (-1);

fallback:
#endif /* CACHE */

    return (unix_stat (path, buf));
}

#ifdef CACHE

/*-----------------------------------------------------------------------------
 * int isarc(char *name)
 *
 * name         Filename, upper case
 *
 * Returns      1 if ArcMail bundle; 0 if not
 *
 * Checks if the filename designates an ArcMail bundle or not
 *-----------------------------------------------------------------------------
 */

static int
isarc (char *name)
{
    static char week[] = "MOTUWETHFRSASU";
    int i;

    if (strlen (name) != 12)
        return (0);

    for (i = 0; i < 8; i++)
        if (!isxdigit (name[i]))
            return (0);

    if (name[i++] != '.')
        return (0);

    if (!isdigit (name[11]))
        return (0);

    for (i = 0; i < 14; i += 2)
        if (name[9] == week[i] && name[10] == week[i + 1])
            break;

    return (i < 14);
}

#endif /* CACHE */

/*-----------------------------------------------------------------------------
 * long netsize(MAILP p,char *extension)
 *
 * p            Mail node
 *
 * Returns      Bytes waiting
 *
 * This is a replacement or reimplementation of netsize found in mailovly.c,
 * that takes advantage of the cache.
 *-----------------------------------------------------------------------------
 */

long
netsize (MAILP p, char *ext, unsigned short AttachType)
{
    struct stat stbuf;
    char net_path[PATHLEN];
    char *ptr;
    FILE *fp;
    char *q;

#ifdef CACHE
    char *base = NULL;
    int cachethis;
    DIRECTORY *flowdir;
    DIRECTORY *dir = NULL;
    DIRENTRY *ent;
    FLOWFILE *flow = NULL;
    FLOWENT *fent;

#endif /* CACHE */
    unsigned long holdtime;
    unsigned short holdcnt;
    long holdsize;
    int freqflag;
    long freqfilesize;

    if (!p)
        return (0);

    /* Append the ARCmail file name to the path line  */

    sprintf (net_path, "%s%s.%s", HoldAreaNameMunge (&(p->mail_addr)),
             Hex_Addr_Str (&(p->mail_addr)), ext);

#ifdef CACHE
    /* Set cache flag */
    cachethis = (cachehold > 1);
#endif /* CACHE */

    /* TJW 960809 Is this a REQ file ? */
    freqflag = !strnicmp (ext, "req", 3);

    /* Reset counters */
    holdtime = 4294967295UL;
    holdcnt = 0;
    holdsize = 0;

#ifdef CACHE
    /* Try to locate flow file in cache */
    if (!freqflag && cachethis)
    {                             /* !freqflag for debugging */
        base = basename (net_path);
        if ((dir = getdir (net_path)) != NULL)
            for (flow = dir->flow; flow; flow = flow->next)
                if (!stricmp (base, flow->name))
                {
#ifdef C_DEBUG
                    status_line ("Mfound %s in cache", flow->name);
#endif
                    for (fent = flow->files; fent; fent = fent->next)
                    {
                        flowdir = fent->dir;
                        if (!flowdir)
                        {
                            cachethis = 0;
                            holdsize = 0;
                            holdcnt = 0;
                            goto fallback;
                        }
                        if (!flowdir->loaded && dirload (flowdir))
                        {
                            cachethis = 0;
                            holdsize = 0;
                            holdcnt = 0;
                            goto fallback;
                        }
                        ent = flowdir->ent[fent->hash];
                        while (ent && strcmp (ent->name, fent->name))
                            ent = ent->next;
                        if (ent)
                        {
                            /* CFS 970504 */
                            if (!AttachType || (AttachType & get_filetype (ent->name)))
                            {
                                holdcnt++;
                                holdsize += ent->size;
                            }
                            if (isarc (ent->name))
                            {
                                if ((unsigned long) ent->time < holdtime)
                                    holdtime = ent->time;
                            }
                        }
                    }
                    if (holdtime != 4294967295UL)
                    {
                        holdtime = dos2unix (holdtime);
                        if (holdtime < (unsigned long) p->oldest)
                            p->oldest = holdtime;
                    }
                    p->numfiles += holdcnt;
                    return (holdsize);
                }
    }

fallback:
#endif /* CACHE */

    /* Open file */
    if ((fp = share_fopen (net_path, read_binary, DENY_NONE)) == NULL)
        return (0);

#ifdef CACHE
    /* Initialize FLOWFILE structure */
    if (!freqflag && cachethis)
    {                             /* !freqflag for debugging */
        /* Get information on the flow file */
        if (cachestat (net_path, &stbuf, NULL, &ent, 0, 0))
            return (0);
        /* Allocate FLOWFILE structure from the free list. If the free list
         * is empty, we expand it first.
         */
        if (fffl == NULL)
        {
            if ((fffl = getmem (BLOCKSIZE * sizeof (FLOWFILE))) != NULL)
            {
                int i;

                for (i = 0; i < BLOCKSIZE - 1; i++)
                    fffl[i].next = fffl + i + 1;
                fffl[i].next = NULL;
            }
        }
        if (fffl)
        {
            flow = fffl;
            fffl = flow->next;
            strntcpy (flow->name, base, 13);
            strlwr (flow->name);
            flow->hash = strhash (flow->name);
            flow->time = ent->time;
            flow->size = ent->size;
            flow->attr = ent->attr;
            flow->files = NULL;
            /* Insert into directory structure */
            flow->next = dir->flow;
            dir->flow = flow;
#ifdef C_DEBUG
            status_line ("MRegistered %s%s", dir->name, dir->flow->name);
#endif
        }
        else
        {
            cachethis = 0;
        }
    }
#endif /* CACHE */

    while (!feof (fp))
    {
        /*
         *      Make sure of a nice zero there if we're at an undetected EOF.
         *      Then try to read a line from the file. MMP - also make sure to
         *      NUL-terminate the line if it is too long.
         */
        net_path[0] = net_path[sizeof (net_path) - 1] = '\0';
        fgets (net_path, sizeof (net_path) - 1, fp);

        freqfilesize = 0;           /* init size */

        if (freqflag)
        {

            /*
             *      Get file sizes out of request file:
             *      NNNNnnnn.REQ:
             *      FILENAME.123 !PASSWORD $FILESIZEBYTES
             *      ...
             */
            q = ptr = net_path;
            if (!*ptr)
                continue;               /* nothing there */
            else if (*ptr == '+')     /* function request ? */
                ptr++;
            q = skip_to_blank (ptr);
            if (*q)                   /* if there is anything left */
                *q++ = '\0';            /* 0-terminate filename */
            while (*q && *q != '$')
                q = skip_to_word (q);
            if (*q == '$')            /* do we have size information ? */
                freqfilesize = atol (++q);  /* get size */
#ifdef C_DEBUG
            status_line ("MFreq %s size %ld", ptr, freqfilesize);
#endif
        }
        else
        {
            /*
             * Clean up anything we don't want to see (blanks, tabs, CR, etc)
             */
            for (q = ptr = net_path; *q; q++)
                if (*q <= ' ')
                    *q = '\0';
            /*
             * File disposition commands should be skipped over to get to
             * actual filenames.
             */
            if ((*ptr == TRUNC_AFTER) ||
                    (*ptr == DELETE_AFTER) ||
                    (*ptr == SHOW_DELETE_AFTER) ||
                    (*ptr == NOTHING_AFTER))
                ptr++;
            /*
             *  Now -- if what's left starts with a semicolon, it's a comment.
             *  If it starts with a tilde, the file has already been sent. If
             *  what we see is a zero, there's nothing on the line. In any one
             *  of these cases, we should skip this line.
             */
            if ((*ptr == '\0') ||
                    (*ptr == ';') ||
                    (*ptr == FILE_ALREADY_SENT))
                continue;
        }

        /*
         *  Get the file size by doing a stat call. If it's not there
         *  then we obviously need not add any size in. If the file is
         *  arcmail, figure it into the "oldest" calculation.
         */

        if (!AttachType || (AttachType & get_filetype (ptr)))
        {

#ifdef CACHE
            if (cachestat (ptr, &stbuf, &flowdir, NULL, freqflag, freqfilesize))
#else
if (cachestat (ptr, &stbuf, freqflag, freqfilesize))
#endif
                /* file exist? */
                continue;
            else
            {
                if (!freqflag)          /* REQs don't count as hold file, but */
                    holdcnt++;
                holdsize += stbuf.st_size;  /* this also includes FREQ traffic now */

#ifdef CACHE
                /* Register file */
                if (!freqflag && cachethis)
                {                       /* !freqflag for debugging */
                    if (!isdosname (base = basename (ptr)))
                    {
                        status_line ("MNot DOS 8.3 name: %s", ptr);
                        /* The file is not a DOS 8.3 name. Our structures are not
                         * dimensioned for anything else, so we cant cache this
                         * particular file. Clear the dos flag, and deallocate
                         */
                        while ((fent = flow->files) != NULL)
                        {
                            flow->files = fent->next;
                            fent->next = fefl;
                            fefl = fent;
                        }

                        dir->flow = flow->next;
                        flow->next = fffl;
                        fffl = flow;
                        cachethis = 0;
                    }
                    else
                    {
                        /* Expand free list */
                        if (fefl == NULL && (fefl = getmem (BLOCKSIZE * sizeof
                                                            (FLOWENT))) != NULL)
                        {
                            int i;

                            for (i = 0; i < BLOCKSIZE - 1; i++)
                                fefl[i].next = fefl + i + 1;
                            fefl[i].next = NULL;
                        }

                        if (fefl)
                        {
                            /* Allocate node */
                            fent = fefl;
                            fefl = fent->next;
                            /* Register file */
                            fent->dir = flowdir;
                            strcpy (fent->name, base);
                            strlwr (fent->name);
                            fent->hash = strhash (fent->name);
                            /* Insert in chain */

                            fent->next = flow->files;
                            flow->files = fent;
                        }
                    }
                }
#endif /* CACHE */

                if (!freqflag && is_arcmail (ptr, 1 - strlen (ptr)))
                {
                    if ((unsigned long) stbuf.st_mtime < (unsigned long) holdtime)
                        holdtime = stbuf.st_mtime;
                }
            }
        }
    }

    fclose (fp);

    /* Summarize */
    p->numfiles += holdcnt;
    p->oldest = min ((unsigned long) holdtime, p->oldest);

    return (holdsize);
}

#ifdef CACHE

/*-----------------------------------------------------------------------------
 * int cachedump(char *path)
 *
 * path         Pathname of output file
 *
 * Returns      0 on success; non-zero on i/o error
 *
 * Dumps the contents of the cache to the file specified
 *-----------------------------------------------------------------------------
 */

#ifdef C_DEBUG                  /* DEBUGGING */
static char *
timestr (time_t t)
{
    static char buf[20];

    strftime (buf, 20, "%y-%m-%d %H:%M:%S", unix_localtime (&t));
    return (buf);
}

static char *
ptrstr (void *p)
{
    static char buf[20];

    if (p)
        sprintf (buf, "*%p", p);
    else
        strcpy (buf, "NULL");
    return (buf);
}

int
cachedump (char *path)
{
    FILE *fp;
    DIRECTORY *dir;
    DIRENTRY *ent;
    FLOWFILE *flow;
    FLOWENT *fent;
    int hash;

    if ((fp = fopen (path, write_ascii)) == NULL)
        return (errno);

    for (dir = dirlist; dir; dir = dir->next)
    {

        fprintf (fp, "DIRECTORY at %s\n", ptrstr (dir));
        fprintf (fp, "    next    %s\n", ptrstr (dir->next));
        fprintf (fp, "    flow    %s\n", ptrstr (dir->flow));
#ifdef KEEPORDER
        fprintf (fp, "    list    %s\n", ptrstr (dir->list));
#endif
        for (hash = 0; hash < HASHSIZE; hash++)
            if (dir->ent[hash])
                fprintf (fp, "    ent[%02d] %s\n", hash, ptrstr (dir->ent[hash]));
        fprintf (fp, "    loaded  0x%08X (%s)\n", dir->loaded, timestr (dir->loaded));
        fprintf (fp, "    len     %d\n", dir->len);
        fprintf (fp, "    drive   '%c'\n", dir->drive);
        fprintf (fp, "    name    \"%s\"\n", dir->name);
        fprintf (fp, "\n");

        for (flow = dir->flow; flow; flow = flow->next)
        {
            fprintf (fp, "    FLOWFILE at %s\n", ptrstr (flow));
            fprintf (fp, "        next    %s\n", ptrstr (flow->next));
            fprintf (fp, "        time    0x%08X (%s)\n", flow->time, timestr
                     (dos2unix (flow->time)));
            fprintf (fp, "        size    %ld\n", flow->size);
            fprintf (fp, "        attr    0x%02X\n", flow->attr);
            fprintf (fp, "        name    \"%s\"\n", flow->name);
            fprintf (fp, "        hash    %d\n", flow->hash);
            fprintf (fp, "        files   %s\n", ptrstr (flow->files));
            for (fent = flow->files; fent; fent = fent->next)
            {
                fprintf (fp, "        FLOWENT at %s\n", ptrstr (fent));
                fprintf (fp, "            next    %s\n", ptrstr (fent->next));
                fprintf (fp, "            dir     %s (\"%c:%s\")\n", ptrstr
                         (fent->dir), fent->dir->drive, fent->dir->name);
                fprintf (fp, "            name    \"%s\"\n", fent->name);
                fprintf (fp, "            hash    %d\n", fent->hash);
            }
            fprintf (fp, "\n");
        }

        for (hash = 0; hash < HASHSIZE; hash++)
            if (dir->ent[hash])
            {
                fprintf (fp, "    Hash chain ent[%d] starting at %s\n", hash, ptrstr
                         (dir->ent[hash]));
                for (ent = dir->ent[hash]; ent; ent = ent->next)
                {
                    fprintf (fp, "        DIRENTRY at %s\n", ptrstr (ent));
                    fprintf (fp, "            next    %s\n", ptrstr (ent->next));
#ifdef KEEPORDER
                    fprintf (fp, "            dirnext %s\n", ptrstr (ent->dirnext));
#endif
                    fprintf (fp, "            time    0x%08X (%s)\n", ent->time,
                             timestr (dos2unix (ent->time)));
                    fprintf (fp, "            size    %ld\n", ent->size);
                    fprintf (fp, "            attr    0x%02X\n", ent->attr);
                    fprintf (fp, "            name    \"%s\"\n", ent->name);
                }
            }
        fprintf (fp, "\n");
    }
    fprintf (fp, "\n");

    fprintf (fp, "DIRENTRY freelist starting at %s\n", ptrstr (defl));
    for (ent = defl; ent; ent = ent->next)
    {
        fprintf (fp, "    DIRENTRY at %s\n", ptrstr (ent));
        fprintf (fp, "        next    %s\n", ptrstr (ent->next));
#ifdef KEEPORDER
        fprintf (fp, "        dirnext %s\n", ptrstr (ent->dirnext));
#endif
        fprintf (fp, "        time    0x%08X (%s)\n", ent->time, timestr
                 (dos2unix (ent->time)));
        fprintf (fp, "        size    %ld\n", ent->size);
        fprintf (fp, "        attr    0x%02X\n", ent->attr);
        fprintf (fp, "        name    \"%s\"\n", ent->name);
    }
    fprintf (fp, "\n");

    fprintf (fp, "FLOWFILE freelist starting at %s\n", ptrstr (fffl));
    for (flow = fffl; flow; flow = flow->next)
    {
        fprintf (fp, "    FLOWFILE at %s\n", ptrstr (flow));
        fprintf (fp, "        next    %s\n", ptrstr (flow->next));
        fprintf (fp, "        time    0x%08X (%s)\n", flow->time, timestr
                 (dos2unix (flow->time)));
        fprintf (fp, "        size    %ld\n", flow->size);
        fprintf (fp, "        attr    0x%02X\n", flow->attr);
        fprintf (fp, "        name    \"%s\"\n", flow->name);
        fprintf (fp, "        hash    %d\n", flow->hash);
        fprintf (fp, "        files   %s\n", ptrstr (flow->files));
    }
    fprintf (fp, "\n");

    fprintf (fp, "FLOWENT freelist starting at %s\n", ptrstr (fefl));
    for (fent = fefl; fent; fent = fent->next)
    {
        fprintf (fp, "    FLOWENT at %s\n", ptrstr (fent));
        fprintf (fp, "        next    %s\n", ptrstr (fent->next));
        if (fent->dir)
            fprintf (fp, "        dir     %s (\"%c:%s\")\n", ptrstr (fent->dir),
                     fent->dir->drive, fent->dir->name);
        else
            fprintf (fp, "        dir     %s\n", ptrstr (fent->dir));
        fprintf (fp, "        name    \"%s\"\n", fent->name);
        fprintf (fp, "        hash    %d\n", fent->hash);
    }

    if (fclose (fp))
        return (errno);

    return (0);
}

#endif /* DEBUGGING */
#endif /* CACHE */

/* $Id: cache.cpp,v 1.1 2005/01/01 15:18:12 vildanov Exp $ */

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us