File: bidexms.c

package info (click to toggle)
fbb 7.07-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,244 kB
  • ctags: 8,123
  • sloc: ansic: 87,810; sh: 2,324; makefile: 297
file content (440 lines) | stat: -rw-r--r-- 8,917 bytes parent folder | download | duplicates (2)
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
   /****************************************************************
    Copyright (C) 1986-2000 by

    F6FBB - Jean-Paul ROUBELAT
    6, rue George Sand
    31120 - Roquettes - France
	jpr@f6fbb.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Parts of code have been taken from many other softwares.
    Thanks for the help.
    ****************************************************************/


/*
 * Gestion des BID memoire EMS/XMS
 *
 */

#include <serv.h>

static unsigned nb_record;

static void tst_vbid (bullist *, int, int *);
static void tst_bid (bullist *, int, int *);
static int where_bid (char *bid);

/*
 * Routines standard
 */

void w_bid (void)
{
	int i;
	long lnb, maxbid;
	char *ptr;
	FILE *fptr;
	bidfwd fwbid;

	maxbid = maxbbid;
	if ((fptr = fopen (d_disque ("WFBID.SYS"), "r+b")) == NULL)
	{
#ifdef ENGLISH
		if (!svoie[CONSOLE]->sta.connect)
			cprintf ("\r\nw_bid() Warning : Creating file %s      \r\n\n", d_disque("WFBID.SYS"));
#else
		if (!svoie[CONSOLE]->sta.connect)
			cprintf ("\r\nw_bid() Warning : Cr´┐Żation du fichier %s\r\n\n", d_disque("WFBID.SYS"));
#endif
		if ((fptr = fopen (d_disque ("WFBID.SYS"), "wb")) == NULL)
		{
#ifdef ENGLISH
			cprintf ("\r\nw_bid() Error : Impossible to create file %s   \r\n\n", d_disque("WFBID.SYS"));
#else
			cprintf ("\r\nw_bid() Erreur : Impossible de creer le fichier %s\r\n\n", d_disque("WFBID.SYS"));
#endif
			fbb_error (ERR_CREATE, d_disque ("WFBID.SYS"), 0);
			exit(1);
		}
		else {
			ptr = (char *) &fwbid;
			for (i = 0; i < sizeof (bidfwd); i++)
			*ptr++ = '\0';
			fwrite (&fwbid, sizeof (bidfwd), 1, fptr);
			rewind (fptr);
		}
	}
	fread (&fwbid, sizeof (fwbid), 1, fptr);
	++fwbid.numero;
	if ((fwbid.numero > maxbid) || (fwbid.numero < 1L))
		fwbid.numero = 1L;
	lnb = fwbid.numero;
	rewind (fptr);
	fwrite (&fwbid, sizeof (fwbid), 1, fptr);
	fseek (fptr, fwbid.numero * sizeof (bidfwd), 0);
	fwbid.mode = ptmes->type;
	strncpy (fwbid.fbid, ptmes->bid, 12);
	fwbid.fbid[12] = '\0';
	fwbid.numero = ptmes->numero;
	fwrite (&fwbid, sizeof (fwbid), 1, fptr);
	ferme (fptr, 59);

	--lnb;
	if (bid_ptr)
	{
		/* Mise a jour du tableau */
		ptr = bid_ptr + (int) (BIDCOMP * lnb);
		memcpy (ptr, comp_bid (ptmes->bid), BIDCOMP);
	}
	else if (EMS_BID_OK ())
	{
		write_bid ((int) lnb, comp_bid (ptmes->bid));
	}
}

int search_bid (char *bid)
{
	int i;
	char *ptr;
	char t_bid[BIDCOMP];

	if (bid_ptr)
	{
		memcpy (t_bid, comp_bid (bid), BIDCOMP);
		ptr = bid_ptr;
		for (i = 0; i < maxbbid; i++)
		{
			if (memcmp (ptr, t_bid, BIDCOMP) == 0)
			{
				return (i + 1);
			}
			ptr += BIDCOMP;
		}
		return (0);
	}
	else if (EMS_BID_OK ())
		return (where_exms_bid (bid));
	else
		return (where_bid (bid));
}

void libere_bid (void)
{
	if (bid_ptr)
	{
		m_libere (bid_ptr, (unsigned) (BIDCOMP * maxbbid));
		bid_ptr = NULL;
	}
}

void end_bids (void)
{
	libere_bid ();
}

void cree_bid (void)
{
	int i;
	FILE *fptr;
	bidfwd fwbuf;
	bidfwd fwbid;
	char *ptr;

#if defined(__WINDOWS__) || defined(__linux__)
	char buffer[80];

#endif
#ifdef __FBBDOS__
	fen *fen_ptr;

#endif

	/* bid_exms = 0; */
	nb_record = 0;

	if ((fptr = fopen (d_disque ("WFBID.SYS"), "rb")) == NULL)
	{
		fprintf (stderr, "WFBID.SYS file is not present. Creating %s\n", d_disque("WFBID.SYS"));
		printf ("WFBID.SYS is not present. Creating %s\n",d_disque("WFBID.SYS"));

	if ((fptr = fopen (d_disque ("WFBID.SYS"), "wb")) == NULL)
		{
#ifdef ENGLISH
			cprintf ("\r\nError : Impossible to create file %s   \r\n\n", d_disque("WFBID.SYS"));
#else
			cprintf ("\r\nErreur : Impossible de cr<82>er le fichier %s\r\n\n", d_disque("WFBID.SYS"));
#endif
			fbb_error (ERR_CREATE, d_disque ("WFBID.SYS"), 0);
			exit(1);
		}
		else
		{
			ptr = (char *) &fwbid;
			for (i = 0; i < sizeof (bidfwd); i++)
			*ptr++ = '\0';
			fwrite (&fwbid, sizeof (bidfwd), 1, fptr);
			rewind (fptr);
		}
	}
		
	deb_io ();

#ifdef __FBBDOS__
	fen_ptr = open_win (10, 5, 50, 8, INIT, "BID");
#endif

	if (EMS_BID_OK ())
	{
		init_exms_bid (fptr);
	}
	else
	{
		if ((bid_ptr == NULL) && (maxbbid <= 5000) && (tot_mem > 100000L))
		{
			bid_ptr = m_alloue ((unsigned) (BIDCOMP * maxbbid));
			memset (bid_ptr, 0, (size_t) maxbbid * BIDCOMP);
			ptr = bid_ptr;
			for (i = 0; i < maxbbid; i++)
			{
				if ((i % 500) == 0)
				{
#if defined(__WINDOWS__) || defined(__linux__)
					InitText (ltoa ((long) i, buffer, 10));
#endif
#ifdef __FBBDOS__
					cprintf ("\r%d BIDs", i);
#endif
				}
				fread (&fwbuf, sizeof (bidfwd), 1, fptr);
				memcpy (ptr, comp_bid (fwbuf.fbid), BIDCOMP);
				ptr += BIDCOMP;
			}
#if defined(__WINDOWS__) || defined(__linux__)
			InitText (ltoa ((long) i, buffer, 10));
#endif
#ifdef __FBBDOS__
			cprintf ("\r%d BIDs", i);
#endif

		}

	}
	ferme (fptr, 71);
#ifdef __FBBDOS__
	sleep_ (1);
	close_win (fen_ptr);
#endif
	fin_io ();
}


static void tst_bid (bullist * fb_mess, int nb, int *t_res)
{
	int i, j, res;
	FILE *fptr;
	bidfwd fwbid;

	if ((fptr = fopen (d_disque ("WFBID.SYS"), "rb")) != NULL)
	{
		if (fread (&fwbid, sizeof (fwbid), 1, fptr) == 0)
			return;
		for (i = 0; i < maxbbid; i++)
		{
			if (fread (&fwbid, sizeof (fwbid), 1, fptr) == 0)
				break;
			res = 1;
			for (j = 0; j < nb; j++)	/* Test si tous sont trouves */
			{
				if ((t_res[j] == 0) && (*fb_mess[j].bid))
				{
					res = 0;
					break;
				}
			}
			if (res)
				break;
			for (j = 0; j < nb; j++)
			{
				if (t_res[j])
					continue;	/* Deja trouve ? */
				if (*fb_mess[j].bid == '\0')
					continue;
				if (strcmp (fwbid.fbid, fb_mess[j].bid) == 0)
				{
					t_res[j] = 1;
				}
			}
		}
		ferme (fptr, 70);
	}
}


static int where_bid (char *bid)
{
	int i;
	FILE *fptr;
	bidfwd fwbid;

	if ((fptr = fopen (d_disque ("WFBID.SYS"), "rb")) != NULL)
	{
		if (fread (&fwbid, sizeof (fwbid), 1, fptr) == 0)
			return (0);
		for (i = 0; i < maxbbid; i++)
		{
			if (fread (&fwbid, sizeof (fwbid), 1, fptr) == 0)
				break;
			if (strcmp (fwbid.fbid, bid) == 0)
			{
				return (i + 1);
			}
		}
		ferme (fptr, 70);
	}
	return (0);
}


static void tst_vbid (bullist * fb_mess, int nb, int *t_res)
{
	int i, j, res;
	char *ptr;
	char t_bid[MAX_FB][BIDCOMP];

	for (i = 0; i < nb; i++)
		if (*fb_mess[i].bid)
			memcpy (t_bid[i], comp_bid (fb_mess[i].bid), BIDCOMP);

	ptr = bid_ptr;
	for (i = 0; i < maxbbid; i++)
	{
		res = 1;
		for (j = 0; j < nb; j++)
		{						/* Test si tous sont trouves */
			if ((t_res[j] == 0) && (*fb_mess[j].bid))
			{
				res = 0;
				break;
			}
		}
		if (res)
			break;
		for (j = 0; j < nb; j++)
		{
			if (t_res[j])
				continue;		/* Deja trouve ? */
			if (*fb_mess[j].bid == '\0')
				continue;
			if (strncmp (ptr, t_bid[j], BIDCOMP) == 0)
			{
				t_res[j] = 1;
			}
		}
		ptr += BIDCOMP;
	}
}


/*
   Tests divers lors de la reception d'un message
   Retour :
   0 : Message accepte
   1 : Message deja recu [ ou rejete (protocole 0) ]
   2 : en cours de reception sur un autre canal
   3 :
   4 : Message rejete (protocole 1).
   5 : Message retenu (protocole 1).
 */
int deja_recu (bullist * fb_mess, int nb, int *t_res)
{
	int i, j, voie;


	for (i = 0; i < nb; i++)
	{
		t_res[i] = rejet (&fb_mess[i]);
		/*
		   if (t_res[i] == 0)
		   t_res[i] = retenu(&fb_mess[i]);
		 */
	}

	for (i = 0; i < (nb - 1); i++)
	{
		for (j = i + 1; j < nb; j++)
		{
			if (t_res[j])
				continue;
			if (strcmp (fb_mess[i].bid, fb_mess[j].bid) == 0)
			{
				t_res[j] = 1;
				break;
			}
		}
	}

	if (bid_ptr)
		tst_vbid (fb_mess, nb, t_res);
	else if (EMS_BID_OK ())
		tst_exms_bid (fb_mess, nb, t_res);
	else
		tst_bid (fb_mess, nb, t_res);

	for (i = 0; i < nb; i++)
	{
		if (t_res[i])
			continue;

		if (*fb_mess[i].bid == '\0')
			continue;

		/* Teste si en cours de reception */
		for (voie = 0; voie < NBVOIES; voie++)
		{
			if ((voie == voiecur) || (!svoie[voie]->sta.connect))
				continue;

			if (strcmp (svoie[voie]->entmes.bid, fb_mess[i].bid) == 0)
			{
				t_res[i] = 2;
				break;
			}

			for (j = 0; j < MAX_FB; j++)
			{
				if (svoie[voie]->fb_mess[j].type == '\0')
					continue;
				if (strcmp (svoie[voie]->fb_mess[j].bid, fb_mess[i].bid) == 0)
				{
					t_res[i] = 2;
					break;
				}
			}
			if (t_res[i])
				break;
		}
	}

	for (i = 0; i < nb; i++)
	{
		if (t_res[i] == 0)
			t_res[i] = retenu (&fb_mess[i]);
	}


	return (t_res[0]);
}