forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bids.cpp
805 lines (741 loc) · 22.5 KB
/
bids.cpp
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
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
---------------------------
Verwaltung von Bulletin-IDs
---------------------------
Copyright (C) Florian Radlherr
Taubenbergstr. 32
83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
***************************************************************/
//19980208 OE3DZW changed output format for bid -i
//19980208 OE3DZW fixed above format
//19980223 OE3DZW fixed fsetopt
//19990219 OE3DZW fixed check for unique bid
//19991111 OE3DZW 11:11h ;-) removed check for length of BID
//20000108 OE3DZW added bidoffset (multiple bcm's)
//20000625 DK2UI newbid() resets counter itself and not from crond
//20000814 DK2UI newbid() searches for uniqe BID
//20030720 hpk newbid() CB-BCMNET bbs'es have now a unique BID/MID
#include "baycom.h"
/*---------------------------------------------------------------------------*/
#define HLEN 4
#define BID_FILENAMELEN 16
#define nextpos(fptr) (filelength(fileno(fptr))/sizeof(BID))
/*---------------------------------------------------------------------------*/
// Needed to get the temp-BIDs
extern task_t *tfeld[TASKS];
/*---------------------------------------------------------------------------*/
typedef FILE *fp;
static int is_null = 0;
/*---------------------------------------------------------------------------*/
typedef struct
{ char name[BIDLEN+1];
char filename[BID_FILENAMELEN+1];
long nextsamehash;
} BID;
/*---------------------------------------------------------------------------*/
typedef struct
{ char name[BIDLEN+1];
short unsigned nextsamehash;
char filename[BID_FILENAMELEN+1];
} BID_OLD;
/*---------------------------------------------------------------------------*/
typedef struct
{ char reserved[BIDLEN + BID_FILENAMELEN];
short unsigned lastbid; // letzte vergebene BID-Nummer
long filesize; // Groesse des BID-File
} KOPF;
/*---------------------------------------------------------------------------*/
long bid_len (void)
//*************************************************************************
//
//*************************************************************************
{
return filesize(BIDNAME) / sizeof(BID);
}
/*---------------------------------------------------------------------------*/
//static void near bidfile_newformat(void)
//*************************************************************************
//
//*************************************************************************
/*
{
FILE *fi;
FILE *fo;
BID b;
BID_OLD bo;
if (filetime(BIDNAME) < filetime(BIDNAME_OLD))
{
trace(replog, "bid", "converting format");
fi = s_fopen(BIDHASHNAME_OLD, "srb");
fo = s_fopen(BHTMP, "swb");
if (fo)
s_fsetopt(fo, 1);
if (fi && fo)
{
struct
{ short unsigned l;
short unsigned h;
} b={0, 0};
while (fread(&b.l, 2, 1, fi))
fwrite(&b, 4, 1, fo);
}
if (fo) s_fclose(fo);
if (fi) s_fclose(fi);
fi = s_fopen(BIDNAME_OLD, "srb");
fo = s_fopen(BTMP, "swb");
if (fo)
s_fsetopt(fo, 1);
if (fi && fo)
{
while (fread(&bo, sizeof(BID_OLD), 1, fi))
{
strcpy(b.name, bo.name);
strcpy(b.filename, bo.filename);
b.nextsamehash = bo.nextsamehash;
fwrite(&b, sizeof(BID), 1, fo);
}
}
if (fi) s_fclose(fi);
if (fo) s_fclose(fo);
xunlink(BIDNAME);
xunlink(BIDHASHNAME);
xrename(BTMP, BIDNAME);
xrename(BHTMP, BIDHASHNAME);
}
}
*/
/*---------------------------------------------------------------------------*/
void tmpbidrein (char *bid, int number)
//*************************************************************************
//
// Locks a BID temporarily.
// This prevents, that we accept a BID, which we are currently
// receiving on another channel
//
// (DH3MB)
//
//*************************************************************************
{
strcpy(b->tmpbid[number], bid);
}
/*---------------------------------------------------------------------------*/
int tmpbidvorhanden (char *bid)
//*************************************************************************
//
// Checks, if we receive a specific BID at the moment
//
// (DH3MB)
//
//*************************************************************************
{
unsigned int i, j;
task_t *tt;
for (i = 0; i < TASKS; i++)
{
tt = tfeld[i];
if (tt)
{
tvar_t *bb = (tvar_t *) tt->b;
if (bb)
for (j = 0; j < 5; j++)
if (! strcmp(bb->tmpbid[j], bid)) return 1;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
void tmpbidraus (int number)
//*************************************************************************
//
// Deletes a temporary BID
//
// (DH3MB)
//
//*************************************************************************
{
b->tmpbid[number][0] = 0;
}
/*---------------------------------------------------------------------------*/
static void near listtmpbid (char *bid)
//*************************************************************************
//
// Lists all temporary BIDs
// (i.e. the BIDs, which we are receiving at the moment)
// (DH3MB)
//
//*************************************************************************
{
unsigned int i, j, firstfound;
task_t *tt;
putf(ms(m_incomingbids));
putf("\n");
for (i = 0; i < TASKS; i++)
{
tt = tfeld[i];
if (tt)
{
tvar_t *bb = (tvar_t *) tt->b;
if (bb)
{
firstfound = 1;
for (j = 0; j < 5; j++)
{
if (bb->tmpbid[j][0] && strstr(bb->tmpbid[j], bid))
{
if (firstfound) putf("\n%-6s: ", bb->logincall);
else putf(" ");
putf("$%-12s", bb->tmpbid[j]);
if (firstfound)
{
putf(" (%c %-6s > %-8s @ %.22s)",
bb->mailtype, bb->herkunft, bb->ziel, bb->at);
firstfound = 0;
}
putv(LF);
}
}
}
}
}
}
/*---------------------------------------------------------------------------*/
static void near tryopen (int first, fp &bidf, fp &bidhf)
//*************************************************************************
//
// Öffnet Bid-File und Hash-File
// ggf. werden die Files neu angelegt, wenn sie nicht geoeffnet
// werden koennen
//
//*************************************************************************
{
KOPF kopf = { "", 0, 0 }; // Speicher für BID-File Kopf
bidf = s_fopen(BIDNAME, "sr+b");
if (! bidf)
{
if (first)
{
bidf = s_fopen(BIDNAME, "sw+b");
if (bidf)
{
s_fsetopt(bidf, 1);
fwrite(&kopf, sizeof(KOPF), 1, bidf);
xunlink(BIDHASHNAME);
}
}
if (! bidf)
trace(tr_abbruch, "BID-file", "fopen errno=%d %s",
errno, strerror(errno));
}
bidhf = s_fopen(BIDHASHNAME, "sr+b");
if (! bidhf)
{
if (first)
{
bidhf = s_fopen(BIDHASHNAME, "sw+b");
if (bidhf) s_fsetopt(bidhf, 1);
}
if (! bidhf)
trace(tr_abbruch, "BID-hash", "fopen errno=%d %s",
errno, strerror(errno));
}
first = 0;
}
/*---------------------------------------------------------------------------*/
char *newbid (void)
//*************************************************************************
//
// Erzeugt ein neues Bulletin-ID (aus Datum, Call und laufender Nummer)
//
//*************************************************************************
{
static char bid[BIDLEN+1];
struct tm *d;
static int lastday = 0;
static KOPF k = { "", 0, 0 };
FILE *bidf; // offenes BID-File
FILE *bidhf; // offenes BID-Hashfile
char callbuf[7]; // DH3MB
short unsigned lbid;
d = ad_comtime(ad_time());
if (! lastday) lastday = d->tm_mday;
tryopen(0, bidf, bidhf);
rewind(bidf);
fread(&k, sizeof(k), 1, bidf);
s_fclose(bidf);
s_fclose(bidhf);
if (lastday != d->tm_mday)
{
#ifndef _BCMNET_FWD
k.lastbid = 36U * 36U * 8U * m.bidoffset;
#else
k.lastbid = 0; //CB-BCMNET: bidoffset not supported
#endif
lastday = d->tm_mday;
}
else
{ // on multi-bbs systems:
// if more than 10368 bid are needed, bid are no longer unique.
#ifndef _BCMNET_FWD
if (k.lastbid >= (36U * 36U * 36U))
k.lastbid = 0; //= 46656 < 65535 (short unsigned) ok
#else
if (k.lastbid >= (36U * 36U))
k.lastbid = 0;
#endif
}
// DH3MB: Fill up the boxname to 6 characters
strcpy(callbuf, m.boxname);
while (strlen(callbuf) < 6) strcat(callbuf, "_");
do {
lbid = ++k.lastbid;
#ifndef _BCMNET_FWD
// format:
// <day><month><year><call><num> <num>.. 0..9,A..Z
// DMYCCCCCC123
// 012345678901 0 1 2 9 0 1
sprintf(bid, "%c%1X%c%s%c%c%c",
deznib(d->tm_mday),
d->tm_mon + 1,
deznib(d->tm_year - 90), //this is a 2026er bug ;-)
callbuf,
deznib((lbid / (36 * 36))),
deznib((lbid / 36) % 36),
deznib((lbid) % 36));
#else
// the BID/MID for the CB-BCMNET is built as following
// <day><year><month><call>_<num>
// DYMCCCCCC_23
sprintf(bid, "%c%c%1X%s_%c%c",
deznib(d->tm_mday),
deznib(d->tm_year - 90), //this is a 2026er bug ;-)
d->tm_mon + 1,
callbuf,
deznib((lbid / 36) % 36),
deznib((lbid) % 36));
#endif
} while (bidvorhanden(bid));
tryopen(0, bidf, bidhf);
rewind(bidf);
fwrite(&k, sizeof(k), 1, bidf);
s_fclose(bidf);
s_fclose(bidhf);
return bid;
}
/*---------------------------------------------------------------------------*/
static void near updatehash (FILE *bidhf, long lindex, BID *bid)
//*************************************************************************
//
// Trägt einen neuen Eintrag in die Hashtabelle ein
// und verkettet ihn mit evtl bereits bestehenden Einträgen
// Dabei wird der neue Eintrag stets zu dem Eintrag, auf den
// der Hashzeiger zeigt. Ggf. alte Einträge werden vom neuen
// Eintrag aus angesprochen.
//
//*************************************************************************
{
long unsigned hsh = strcrc(bid->name); // CRC als Hashfunktion
fseek(bidhf, hsh * HLEN, SEEK_SET); // bestehenden Hashzeiger lesen
fread(&bid->nextsamehash, HLEN, 1, bidhf);
fseek(bidhf, hsh * HLEN, SEEK_SET); // neuen Zeiger schreiben
if (fwrite(&lindex, HLEN, 1, bidhf) != 1)
trace(serious, "BID-upd", "fwrite $%s", bid->name);
}
/*---------------------------------------------------------------------------*/
char *bidvorhanden (char *bidname)
//*************************************************************************
//
// nur fuer Kompatibilitaet
//
//*************************************************************************
{
return bidnewfilename(bidname, NULL);
}
/*---------------------------------------------------------------------------*/
char *bidnewfilename (char *bidname, char *newfilename)
//*************************************************************************
//
// schaut nach, ob bidname schon im Bid-File enthalten ist
// Zurückgegeben wird ein damit verbundener Filename, falls das
// BID gefunden worden ist, oder NULL wenn das BID nicht vorhanden ist
//
//*************************************************************************
{
long unsigned hsh;
long unsigned idx;
static BID bidtmp;
FILE *bidf = NULL; // offenes BID-File
FILE *bidhf = NULL; // offenes BID-Hashfile
if (! *bidname) return NULL;
strupr(bidname);
hsh = strcrc(bidname);
tryopen(0, bidf, bidhf);
if (*bidname)
{
fseek(bidhf, hsh * HLEN, SEEK_SET);
if (fread(&idx, HLEN, 1, bidhf) != 1)
trace(serious, "bidnewfilename", "hash %s", bidname);
if (idx)
{
int num = 0;
while (idx)
{
fseek(bidf, idx * sizeof(BID), SEEK_SET);
if (fread (&bidtmp, sizeof(BID), 1, bidf) != 1)
{
if (! sema_test("purgereorg"))
fork(P_BACK | P_MAIL, 0, mbreorg, "i");
else
reorg_noetig++;
trace(fatal, "bidnewfilename", "fread %s", bidname);
}
if ((num++) > 100)
{
trace(serious, "bidnewfilename", "loop");
s_fclose(bidf);
s_fclose(bidhf);
if (! sema_test("purgereorg"))
fork(P_BACK | P_MAIL, 0, mbreorg, "i");
else
reorg_noetig++;
return "";
}
if (! strcmp(bidname, bidtmp.name))
{
if (newfilename)
{
if (strlen(newfilename) > BID_FILENAMELEN)
{
trace(serious, "bidnewfilename", "file %s", newfilename);
newfilename[BID_FILENAMELEN] = 0;
}
strcpy(bidtmp.filename, newfilename);
fseek(bidf, idx * sizeof(BID), SEEK_SET);
fwrite(&bidtmp, sizeof(BID), 1, bidf);
}
s_fclose(bidf);
s_fclose(bidhf);
return bidtmp.filename;
}
idx = bidtmp.nextsamehash;
}
}
}
s_fclose(bidf);
s_fclose(bidhf);
return NULL;
}
/*---------------------------------------------------------------------------*/
unsigned bidrein (char *bidname, char *filename)
//*************************************************************************
//
// legt ein BID im bullid-File ab
//
// Achtung: es muss an der Stelle sichergestellt sein, dass nicht
// schon ein BID mit gleichem Namen existiert!
//
//*************************************************************************
{
BID new_bid;
FILE *bidf = NULL; // offenes BID-File
FILE *bidhf = NULL; // offenes BID-Hashfile
strupr(bidname);
if (*bidname)
{
tryopen(0, bidf, bidhf);
memset(&new_bid, 0, sizeof(BID)); // neuen Eintrag vorbesetzen
if (strlen(bidname) > BIDLEN)
{
trace(serious, "bidrein", "BID %s too long", bidname);
bidname[BIDLEN] = 0;
}
if (strlen(filename) > BID_FILENAMELEN)
{
trace(serious, "bidrein", "filename %s too long", filename);
filename[BID_FILENAMELEN] = 0;
}
strcpy(new_bid.name, bidname); // String hineinschreiben
strcpy(new_bid.filename, filename);
fseek(bidf, 0L, SEEK_END);
updatehash(bidhf, ftell(bidf) / sizeof(BID), &new_bid);
if (fwrite(&new_bid, sizeof(BID), 1, bidf) != 1)
trace(serious, "bidrein", "fwrite $%s", new_bid.name);
if ((ftell(bidf) / sizeof(BID)) > ((unsigned long) m.maxbids + 10000))
{
if (! sema_test("purgereorg"))
fork(P_BACK | P_MAIL, 0, mbreorg, "i");
trace(replog, "bidrein", "maxbids");
}
s_fclose(bidf);
s_fclose(bidhf);
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void near listhash (char *bidname)
//*************************************************************************
//
// Gibt hashcode zu <bidname> aus
//
//*************************************************************************
{
long unsigned hsh = strcrc(bidname);
long unsigned idx;
static BID bidtmp;
FILE *bidf = NULL; // offenes BID-File
FILE *bidhf = NULL; // offenes BID-Hashfile
putf("Hashcode=%04X\n", hsh);
if (tmpbidvorhanden(bidname)) putf("BID in TMP buffer.\n");
tryopen(0, bidf, bidhf);
if (*bidname)
{
fseek(bidhf, hsh * HLEN, SEEK_SET);
fread(&idx, HLEN, 1, bidhf);
while (idx)
{
fseek(bidf, idx * sizeof(BID), SEEK_SET);
fread(&bidtmp, sizeof(BID), 1, bidf);
putf("%-6lu%17s:%-12s --> %lu\n",
idx, bidtmp.filename, bidtmp.name, bidtmp.nextsamehash);
idx = bidtmp.nextsamehash;
}
}
s_fclose(bidf);
s_fclose(bidhf);
}
/*---------------------------------------------------------------------------*/
void listbids (char *sel)
//*************************************************************************
//
// Shows extracts of the BID-table
//
// Options:
// -c Only counts the found entries
// -k Only lists entries with collisions
// -h gibt vollständige Hashverkettung aus
// -s Input a BID manually
// -i Lists the BIDs, which are currently received (DH3MB)
// -f Find a bid using hash; faster but no fragments allowed
//
//*************************************************************************
{
BID bid;
KOPF k;
long count = 0;
FILE *bidf;
char fn[BID_FILENAMELEN+1];
char *p;
scanoptions(sel);
sel += blkill(sel);
strupr(sel);
if (b->optplus & o_f)
{
p = bidnewfilename(sel, NULL);
if (p)
{
safe_strcpy(fn,p);
setfullpath(fn);
putf("%s -> %s -> %s\n", sel, fn, b->mailpath);
if (parse_headerline())
{
putf("Found: %c %-6s > %-8s @ %.22s\n",
b->mailtype, b->herkunft, b->ziel, b->at);
}
}
else
putf(ms(m_noentryfound));
return;
}
if (b->optplus & o_i) // DH3MB
{
listtmpbid(sel);
return;
}
if (b->optplus & o_h) listhash(sel);
else if (b->optplus & o_s)
{
if (! bidvorhanden(sel) && b->sysop)
bidrein(sel, "DUMMY"); //df3vi: nicht fuer user!
}
else
{
bidf = s_fopen(BIDNAME, "srb");
if (bidf)
{
fread(&k, sizeof(BID), 1, bidf); // delete first entry
putf("Filesize=%lu lastbid=%u\n", nextpos(bidf), k.lastbid);
while (fread(&bid, sizeof(BID), 1, bidf))
{
if ((! *sel || strstr(bid.filename, sel) || strstr(bid.name, sel))
&& (! (b->optplus & o_k) || bid.nextsamehash))
{
if (! (b->optplus & o_c))
{
putf("%-6lu", (ftell(bidf) / sizeof(BID)) - 1);
putf("%17s:%-12s", bid.filename, bid.name);
if (bid.nextsamehash)
{
long save = ftell(bidf);
putf(" --> %ld", bid.nextsamehash);
fseek(bidf, sizeof(BID) * (long) bid.nextsamehash, SEEK_SET);
fread(&bid, sizeof(BID), 1, bidf);
putf(" (%s) %ld", bid.name, bid.nextsamehash);
fseek(bidf, save, SEEK_SET);
}
putv(LF);
}
count++;
}
if (testabbruch()) break;
waitfor(e_ticsfull);
}
s_fclose(bidf);
}
putf(ms(m_entriesfound), count);
}
}
/*---------------------------------------------------------------------------*/
void bidinit (int begin)
//*************************************************************************
//
// Zugriff BID-File vorbereiten
//
//*************************************************************************
{
FILE *bidf; // offenes BID-File
FILE *bidhf; // offenes BID-Hashfile
if (sizeof(BID) != sizeof(KOPF) || is_null)
trace(tr_abbruch, "bidinit", "size %d %d", sizeof(BID), sizeof(KOPF));
//obsolete if(begin) bidfile_newformat();
tryopen(1, bidf, bidhf);
if (! bidf)
{
trace(tr_abbruch, "bidinit", "fopen bidf errno=%d %s",
errno, strerror(errno));
return;
}
if (bidhf) hashinit(fileno(bidhf), HASH32);
else
{
trace(tr_abbruch, "bidinit", "fopen bidhf errno=%d %s",
errno, strerror(errno));
return;
}
s_fclose(bidf);
s_fclose(bidhf);
}
/*---------------------------------------------------------------------------*/
void reorgbid (void)
//*************************************************************************
//
// Reorganises BID-file and generates the hashing file new.
//
//*************************************************************************
{
FILE *balt, *bneu, *bhneu;
int error;
KOPF kopf = { "", 0, 0 };
BID bid;
long currpos, maxpos;
unsigned e_nlen = 0, e_flen = 0;
unsigned int len, i;
if (! sema_lock("sendlock")) return;
trace(replog, "reorgbid", "bidlist");
lastcmd("BID-Reorg");
unlink(BTMP); // remove old temporary files
unlink(BHTMP);
balt = s_fopen(BIDNAME, "lrb"); // the current BID file
if (hd_space(filelength(fileno(balt))))
{
s_fclose(balt);
trace(serious, "reorgbid", "disk full");
return;
}
bneu=s_fopen(BTMP, "lw+b"); // open temporary write file
if (bneu) s_fsetopt(bneu,1);
bhneu = s_fopen(BHTMP, "lw+b");
if (bhneu) s_fsetopt(bhneu, 1);
if (balt && bneu && bhneu)
{
setvbuf(balt, NULL, _IOFBF, CBUFLEN); // buffer for increasing performance
setvbuf(bneu, NULL, _IOFBF, CBUFLEN);
hashinit(fileno(bhneu), HASH32); // fill new hash file with zero value
maxpos = nextpos(balt);
if (maxpos > m.maxbids) // if more than maxbids
currpos = maxpos-m.maxbids; // the beginning of the file
else currpos = 1;
fread(&kopf, sizeof(KOPF), 1, balt); // read old header
fwrite(&kopf, sizeof(KOPF), 1, bneu); // write first header
fseek(balt, currpos * sizeof(BID), SEEK_SET);
while (currpos != maxpos)
{
waitfor(e_ticsfull); // do not stand still
error = 0;
if (! fread(&bid, sizeof(BID), 1, balt)) break;
len = strlen(bid.name);
if (bid.name[BIDLEN] || len > BIDLEN)
{
error = 1;
e_nlen++;
}
else
{
for (i = 0; i < len; i++)
{
if (bid.name[i] < '!' || bid.name[i] > 128) error = 1;
}
}
len = strlen(bid.filename);
if (bid.filename[BID_FILENAMELEN] || len > BID_FILENAMELEN || len < 3)
{
error = 1;
e_flen++;
}
else
{
for (i = 0; i < len; i++)
{
if (bid.filename[i] < '!' || bid.filename[i] > 127) error = 1;
}
}
if (! error)
{
fseek(bneu, 0L, SEEK_END);
updatehash(bhneu, ftell(bneu) / sizeof(BID), &bid);
fwrite(&bid, sizeof(BID), 1, bneu);
}
else
{
if ((e_nlen + e_flen) < 20)
trace(report, "reorgbid", "er:%1.20s:%1.20s",
bid.name, bid.filename);
}
if (! (currpos % 10000))
{
char s[40];
sprintf(s, "BID %lu/%lu:%s", currpos, maxpos-1, bid.name);
trace(report, "reorgbid", s); // damits dem Zuschauer nicht fad wird
lastcmd(s); // ... und auch im PS auftaucht
}
currpos++;
}
currpos = nextpos(bneu);
//if (currpos) currpos--; dh8ymb
trace(replog, "reorgbid", "%lu bids, %u/%u errors",
currpos, e_nlen, e_flen);
s_fclose(balt);
s_fclose(bneu);
s_fclose(bhneu);
xunlink(BBAK);
xunlink(BHBAK);
xrename(BIDNAME, BBAK);
xrename(BIDHASHNAME, BHBAK);
xrename(BTMP, BIDNAME);
xrename(BHTMP, BIDHASHNAME);
sema_unlock("sendlock");
}
}
/*---------------------------------------------------------------------------*/