filedat.c 20 KB
Newer Older
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
/* filedat.c */

/* Synchronet file database-related exported functions */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
/* Gets filedata from dircode.DAT file										*/
/* Need fields .name ,.dir and .offset to get other info    				*/
/* Does not fill .dateuled or .datedled fields.                             */
/****************************************************************************/
45
BOOL DLLCALL getfiledat(scfg_t* cfg, file_t* f)
46
47
48
49
50
{
	char buf[F_LEN+1],str[256],tmp[128];
	int file;
	long length;

51
	sprintf(str,"%s%s.dat",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
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
	if((file=sopen(str,O_RDONLY|O_BINARY,SH_DENYWR))==-1) {
	//	errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
		return(FALSE); 
	}
	length=filelength(file);
	if(f->datoffset>length) {
		close(file);
	//	errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE); 
	}
	if(length%F_LEN) {
		close(file);
	//	errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE); 
	}
	lseek(file,f->datoffset,SEEK_SET);
	if(read(file,buf,F_LEN)!=F_LEN) {
		close(file);
	//	errormsg(WHERE,ERR_READ,str,F_LEN);
		return(FALSE); 
	}
	close(file);
	getrec(buf,F_ALTPATH,2,str);
	f->altpath=hptoi(str);
	getrec(buf,F_CDT,LEN_FCDT,str);
	f->cdt=atol(str);

	if(!f->size) {					/* only read disk if this is null */
	//	  if(dir[f->dir]->misc&DIR_FCHK) {
			sprintf(str,"%s%s"
				,f->altpath>0 && f->altpath<=cfg->altpaths ? cfg->altpath[f->altpath-1]
				: cfg->dir[f->dir]->path,unpadfname(f->name,tmp));
			f->size=flength(str);
			f->date=fdate(str);
	/*
			}
		else {
			f->size=f->cdt;
			f->date=0; }
	*/
			}
#if 0
	if((f->size>0L) && cur_cps)
		f->timetodl=(ushort)(f->size/(ulong)cur_cps);
	else
#endif
		f->timetodl=0;

	getrec(buf,F_DESC,LEN_FDESC,f->desc);
	getrec(buf,F_ULER,LEN_ALIAS,f->uler);
	getrec(buf,F_TIMESDLED,5,str);
	f->timesdled=atoi(str);
	getrec(buf,F_OPENCOUNT,3,str);
	f->opencount=atoi(str);
	if(buf[F_MISC]!=ETX)
		f->misc=buf[F_MISC]-SP;
	else
		f->misc=0;
	return(TRUE);
}

/****************************************************************************/
/* Puts filedata into DIR_code.DAT file                                     */
/* Called from removefiles                                                  */
/****************************************************************************/
117
BOOL DLLCALL putfiledat(scfg_t* cfg, file_t* f)
118
119
120
121
122
123
124
125
126
127
{
    char buf[F_LEN+1],str[256],tmp[128];
    int file;
    long length;

	putrec(buf,F_CDT,LEN_FCDT,ultoa(f->cdt,tmp,10));
	putrec(buf,F_DESC,LEN_FDESC,f->desc);
	putrec(buf,F_DESC+LEN_FDESC,2,crlf);
	putrec(buf,F_ULER,LEN_ALIAS+5,f->uler);
	putrec(buf,F_ULER+LEN_ALIAS+5,2,crlf);
128
	putrec(buf,F_TIMESDLED,5,ultoa(f->timesdled,tmp,10));
129
	putrec(buf,F_TIMESDLED+5,2,crlf);
130
	putrec(buf,F_OPENCOUNT,3,ultoa(f->opencount,tmp,10));
131
132
133
134
	putrec(buf,F_OPENCOUNT+3,2,crlf);
	buf[F_MISC]=f->misc+SP;
	putrec(buf,F_ALTPATH,2,hexplus(f->altpath,tmp));
	putrec(buf,F_ALTPATH+2,2,crlf);
135
	sprintf(str,"%s%s.dat",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
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
	if((file=sopen(str,O_WRONLY|O_BINARY,SH_DENYRW))==-1) {
//		errormsg(WHERE,ERR_OPEN,str,O_WRONLY);
		return(FALSE); 
	}
	length=filelength(file);
	if(length%F_LEN) {
		close(file);
//		errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE); 
	}
	if(f->datoffset>length) {
		close(file);
//		errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE); 
	}
	lseek(file,f->datoffset,SEEK_SET);
	if(write(file,buf,F_LEN)!=F_LEN) {
		close(file);
//		errormsg(WHERE,ERR_WRITE,str,F_LEN);
		return(FALSE); 
	}
	length=filelength(file);
	close(file);
	if(length%F_LEN) {
//		errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE);
	}
	return(TRUE);
}

/****************************************************************************/
/* Adds the data for struct filedat to the directory's data base.           */
/* changes the .datoffset field only                                        */
/* returns 1 if added successfully, 0 if not.								*/
/****************************************************************************/
171
BOOL DLLCALL addfiledat(scfg_t* cfg, file_t* f)
172
173
174
175
176
177
178
179
180
181
182
183
{
	char	str[256],fname[13],c,fdat[F_LEN+1];
	char	tmp[128];
	uchar	HUGE16 *ixbbuf,idx[3];
    int		i,file;
	long	l,length;
	time_t	now;
	time_t	uldate;

	/************************/
	/* Add data to DAT File */
	/************************/
184
	sprintf(str,"%s%s.dat",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
185
	if((file=sopen(str,O_RDWR|O_BINARY|O_CREAT,SH_DENYRW))==-1) {
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
//		errormsg(WHERE,ERR_OPEN,str,O_RDWR|O_CREAT);
		return(FALSE); 
	}
	length=filelength(file);
	if(length==0L)
		l=0L;
	else {
		if(length%F_LEN) {
			close(file);
//			errormsg(WHERE,ERR_LEN,str,length);
			return(FALSE); 
		}
		for(l=0;l<length;l+=F_LEN) {    /* Find empty slot */
			lseek(file,l,SEEK_SET);
			read(file,&c,1);
			if(c==ETX) break; 
		}
		if(l/F_LEN>=MAX_FILES || l/F_LEN>=cfg->dir[f->dir]->maxfiles) {
			close(file);
#if 0
			bputs(text[DirFull]);
			sprintf(str,"Directory Full: %s %s"
				,cfg->lib[cfg->dir[f->dir]->lib]->sname,cfg->dir[f->dir]->sname);
			logline("U!",str);
#endif
			return(FALSE); 
		} 
	}
	putrec(fdat,F_CDT,LEN_FCDT,ultoa(f->cdt,tmp,10));
	putrec(fdat,F_DESC,LEN_FDESC,f->desc);
	putrec(fdat,F_DESC+LEN_FDESC,2,crlf);
	putrec(fdat,F_ULER,LEN_ALIAS+5,f->uler);
	putrec(fdat,F_ULER+LEN_ALIAS+5,2,crlf);
	putrec(fdat,F_TIMESDLED,5,ultoa(f->timesdled,tmp,10));
	putrec(fdat,F_TIMESDLED+5,2,crlf);
221
	putrec(fdat,F_OPENCOUNT,3,ultoa(f->opencount,tmp,10));
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	putrec(fdat,F_OPENCOUNT+3,2,crlf);
	fdat[F_MISC]=f->misc+SP;
	putrec(fdat,F_ALTPATH,2,hexplus(f->altpath,tmp));
	putrec(fdat,F_ALTPATH+2,2,crlf);
	f->datoffset=l;
	idx[0]=(uchar)(l&0xff);          /* Get offset within DAT file for IXB file */
	idx[1]=(uchar)((l>>8)&0xff);
	idx[2]=(uchar)((l>>16)&0xff);
	lseek(file,l,SEEK_SET);
	if(write(file,fdat,F_LEN)!=F_LEN) {
		close(file);
//		errormsg(WHERE,ERR_WRITE,str,F_LEN);
		return(FALSE); 
	}
	length=filelength(file);
	close(file);
	if(length%F_LEN) {
//		errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE);
	}

	/*******************************************/
	/* Update last upload date/time stamp file */
	/*******************************************/
246
	sprintf(str,"%s%s.dab",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
247
	if((file=sopen(str,O_WRONLY|O_CREAT|O_BINARY,SH_DENYRW))!=-1) {
248
249
250
251
252
253
254
255
256
257
258
		now=time(NULL);
		write(file,&now,4);
		close(file); 
	}

	/************************/
	/* Add data to IXB File */
	/************************/
	strcpy(fname,f->name);
	for(i=8;i<12;i++)   /* Turn FILENAME.EXT into FILENAMEEXT */
		fname[i]=fname[i+1];
259
	sprintf(str,"%s%s.ixb",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
260
	if((file=sopen(str,O_RDWR|O_CREAT|O_BINARY,SH_DENYRW))==-1) {
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
//		errormsg(WHERE,ERR_OPEN,str,O_RDWR|O_CREAT);
		return(FALSE); 
	}
	length=filelength(file);
	if(length) {    /* IXB file isn't empty */
		if(length%F_IXBSIZE) {
			close(file);
//			errormsg(WHERE,ERR_LEN,str,length);
			return(FALSE); 
		}
		if((ixbbuf=(uchar *)MALLOC(length))==NULL) {
			close(file);
//			errormsg(WHERE,ERR_ALLOC,str,length);
			return(FALSE); 
		}
		if(lread(file,ixbbuf,length)!=length) {
			close(file);
//			errormsg(WHERE,ERR_READ,str,length);
			FREE((char *)ixbbuf);
			return(FALSE); 
		}
	/************************************************/
	/* Sort by Name or Date, Assending or Decending */
	/************************************************/
		if(cfg->dir[f->dir]->sort==SORT_NAME_A || cfg->dir[f->dir]->sort==SORT_NAME_D) {
			for(l=0;l<length;l+=F_IXBSIZE) {
				for(i=0;i<12 && fname[i]==ixbbuf[l+i];i++);
				if(i==12) {     /* file already in directory index */
					close(file);
//					errormsg(WHERE,ERR_CHK,str,0);
					FREE((char *)ixbbuf);
					return(FALSE); 
				}
				if(cfg->dir[f->dir]->sort==SORT_NAME_A && fname[i]<ixbbuf[l+i])
					break;
				if(cfg->dir[f->dir]->sort==SORT_NAME_D && fname[i]>ixbbuf[l+i])
					break; 
			} 
		}
		else {  /* sort by date */
			for(l=0;l<length;l+=F_IXBSIZE) {
				uldate=(ixbbuf[l+14]|((long)ixbbuf[l+15]<<8)
					|((long)ixbbuf[l+16]<<16)|((long)ixbbuf[l+17]<<24));
				if(cfg->dir[f->dir]->sort==SORT_DATE_A && f->dateuled<uldate)
					break;
				if(cfg->dir[f->dir]->sort==SORT_DATE_D && f->dateuled>uldate)
					break; 
			} 
		}
		lseek(file,l,SEEK_SET);
		if(write(file,fname,11)!=11) {  /* Write filename to IXB file */
			close(file);
//			errormsg(WHERE,ERR_WRITE,str,11);
			FREE((char *)ixbbuf);
			return(FALSE); 
		}
		if(write(file,idx,3)!=3) {  /* Write DAT offset into IXB file */
			close(file);
//			errormsg(WHERE,ERR_WRITE,str,3);
			FREE((char *)ixbbuf);
			return(FALSE); 
		}
		write(file,&f->dateuled,sizeof(time_t));
		write(file,&f->datedled,4);              /* Write 0 for datedled */
		if(lwrite(file,&ixbbuf[l],length-l)!=length-l) { /* Write rest of IXB */
			close(file);
//			errormsg(WHERE,ERR_WRITE,str,length-l);
			FREE((char *)ixbbuf);
			return(FALSE); 
		}
		FREE((char *)ixbbuf); }
	else {              /* IXB file is empty... No files */
		if(write(file,fname,11)!=11) {  /* Write filename it IXB file */
			close(file);
//			errormsg(WHERE,ERR_WRITE,str,11);
			return(FALSE); 
		}
		if(write(file,idx,3)!=3) {  /* Write DAT offset into IXB file */
			close(file);
//			errormsg(WHERE,ERR_WRITE,str,3);
			return(FALSE); 
		}
		write(file,&f->dateuled,sizeof(time_t));
		write(file,&f->datedled,4); 
	}
	length=filelength(file);
	close(file);
///	if(length%F_IXBSIZE)
//		errormsg(WHERE,ERR_LEN,str,length);
	return(TRUE);
}

/****************************************************************************/
354
/* Gets file data from dircode.ixb file										*/
355
356
357
/* Need fields .name and .dir filled.                                       */
/* only fills .offset, .dateuled, and .datedled                             */
/****************************************************************************/
358
BOOL DLLCALL getfileixb(scfg_t* cfg, file_t* f)
359
360
361
362
363
364
{
	char			str[256],fname[13];
	uchar HUGE16 *	ixbbuf;
	int				file;
	long			l,length;

365
	sprintf(str,"%s%s.ixb",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
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
	if((file=sopen(str,O_RDONLY|O_BINARY,SH_DENYWR))==-1) {
	//	errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
		return(FALSE); 
	}
	length=filelength(file);
	if(length%F_IXBSIZE) {
		close(file);
	//	errormsg(WHERE,ERR_LEN,str,length);
		return(FALSE); 
	}
	if((ixbbuf=(uchar *)MALLOC(length))==NULL) {
		close(file);
	//	errormsg(WHERE,ERR_ALLOC,str,length);
		return(FALSE); 
	}
	if(lread(file,ixbbuf,length)!=length) {
		close(file);
		FREE((char *)ixbbuf);
	//	errormsg(WHERE,ERR_READ,str,length);
		return(FALSE); 
	}
	close(file);
	strcpy(fname,f->name);
	for(l=8;l<12;l++)	/* Turn FILENAME.EXT into FILENAMEEXT */
		fname[l]=fname[l+1];
	for(l=0;l<length;l+=F_IXBSIZE) {
		sprintf(str,"%11.11s",ixbbuf+l);
393
		if(!stricmp(str,fname))
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
			break; 
	}
	if(l>=length) {
	//	errormsg(WHERE,ERR_CHK,str,0);
		FREE((char *)ixbbuf);
		return(FALSE); 
	}
	l+=11;
	f->datoffset=ixbbuf[l]|((long)ixbbuf[l+1]<<8)|((long)ixbbuf[l+2]<<16);
	f->dateuled=ixbbuf[l+3]|((long)ixbbuf[l+4]<<8)
		|((long)ixbbuf[l+5]<<16)|((long)ixbbuf[l+6]<<24);
	f->datedled=ixbbuf[l+7]|((long)ixbbuf[l+8]<<8)
		|((long)ixbbuf[l+9]<<16)|((long)ixbbuf[l+10]<<24);
	FREE((char *)ixbbuf);
	return(TRUE);
}

/****************************************************************************/
/* Removes DAT and IXB entries for the file in the struct 'f'               */
/****************************************************************************/
414
BOOL DLLCALL removefiledat(scfg_t* cfg, file_t* f)
415
416
{
	char	c,str[256],ixbname[12],HUGE16 *ixbbuf,fname[13];
417
    int		i,file;
418
419
420
	long	l,length;

	strcpy(fname,f->name);
421
422
	for(i=8;i<12;i++)   /* Turn FILENAME.EXT into FILENAMEEXT */
		fname[i]=fname[i+1];
423
	sprintf(str,"%s%s.ixb",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
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
	if((file=sopen(str,O_RDONLY|O_BINARY,SH_DENYWR))==-1) {
//		errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
		return(FALSE); 
	}
	length=filelength(file);
	if(!length) {
		close(file);
		return(FALSE); 
	}
	if((ixbbuf=(char *)MALLOC(length))==0) {
		close(file);
//		errormsg(WHERE,ERR_ALLOC,str,length);
		return(FALSE); 
	}
	if(lread(file,ixbbuf,length)!=length) {
		close(file);
//		errormsg(WHERE,ERR_READ,str,length);
		FREE((char *)ixbbuf);
		return(FALSE); 
	}
	close(file);
	if((file=sopen(str,O_WRONLY|O_TRUNC|O_BINARY,SH_DENYRW))==-1) {
//		errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_TRUNC);
		return(FALSE); 
	}
	for(l=0;l<length;l+=F_IXBSIZE) {
450
451
452
		for(i=0;i<11;i++)
			ixbname[i]=ixbbuf[l+i];
		ixbname[i]=0;
453
		if(stricmp(ixbname,fname))
454
455
456
457
458
459
460
461
462
			if(lwrite(file,&ixbbuf[l],F_IXBSIZE)!=F_IXBSIZE) {
				close(file);
//				errormsg(WHERE,ERR_WRITE,str,F_IXBSIZE);
				FREE((char *)ixbbuf);
				return(FALSE); 
		} 
	}
	FREE((char *)ixbbuf);
	close(file);
463
	sprintf(str,"%s%s.dat",cfg->dir[f->dir]->data_dir,cfg->dir[f->dir]->code);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
	if((file=sopen(str,O_WRONLY|O_BINARY,SH_DENYRW))==-1) {
//		errormsg(WHERE,ERR_OPEN,str,O_WRONLY);
		return(FALSE); 
	}
	lseek(file,f->datoffset,SEEK_SET);
	c=ETX;          /* If first char of record is ETX, record is unused */
	if(write(file,&c,1)!=1) { /* So write a D_T on the first byte of the record */
		close(file);
//		errormsg(WHERE,ERR_WRITE,str,1);
		return(FALSE); 
	}
	close(file);
	if(f->dir==cfg->user_dir)  /* remove file from index */
		rmuserxfers(cfg,0,0,f->name);
	return(TRUE);
}

/****************************************************************************/
/* Checks  directory data file for 'filename' (must be padded). If found,   */
/* it returns the 1, else returns 0.                                        */
/* Called from upload and bulkupload                                        */
/****************************************************************************/
486
BOOL DLLCALL findfile(scfg_t* cfg, uint dirnum, char *filename)
487
{
488
	char str[256],fname[13],HUGE16 *ixbbuf;
489
    int i,file;
490
491
492
493
    long length,l;

	sprintf(fname,"%.12s",filename);
	strupr(fname);
494
495
	for(i=8;i<12;i++)   /* Turn FILENAME.EXT into FILENAMEEXT */
		fname[i]=fname[i+1];
496
	sprintf(str,"%s%s.ixb",cfg->dir[dirnum]->data_dir,cfg->dir[dirnum]->code);
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
	if((file=sopen(str,O_RDONLY|O_BINARY,SH_DENYWR))==-1) return(FALSE);
	length=filelength(file);
	if(!length) {
		close(file);
		return(FALSE); }
	if((ixbbuf=(char *)MALLOC(length))==NULL) {
		close(file);
	//    errormsg(WHERE,ERR_ALLOC,str,length);
		return(FALSE); }
	if(lread(file,ixbbuf,length)!=length) {
		close(file);
	//    errormsg(WHERE,ERR_READ,str,length);
		FREE((char *)ixbbuf);
		return(FALSE); }
	close(file);
	for(l=0;l<length;l+=F_IXBSIZE) {
513
514
515
		for(i=0;i<11;i++)
			if(fname[i]!=toupper(ixbbuf[l+i])) break;
		if(i==11) break; }
516
517
518
519
520
521
522
523
524
	FREE((char *)ixbbuf);
	if(l!=length)
		return(TRUE);
	return(FALSE);
}

/****************************************************************************/
/* Turns FILE.EXT into FILE    .EXT                                         */
/****************************************************************************/
525
char* DLLCALL padfname(char *filename, char *str)
526
{
527
    int c,d;
528
529
530
531
532
533
534

	for(c=0;c<8;c++)
		if(filename[c]=='.' || !filename[c]) break;
		else str[c]=filename[c];
	d=c;
	if(filename[c]=='.') c++;
	while(d<8)
535
536
537
538
539
		str[d++]=' ';
	if(filename[c]>' ')	/* Change "FILE" to "FILE        " */
		str[d++]='.';	/* (don't add a dot if there's no extension) */
	else
		str[d++]=' ';
540
541
542
543
	while(d<12)
		if(!filename[c]) break;
		else str[d++]=filename[c++];
	while(d<12)
544
		str[d++]=' ';
545
546
547
548
549
550
551
	str[d]=0;
	return(str);
}

/****************************************************************************/
/* Turns FILE    .EXT into FILE.EXT                                         */
/****************************************************************************/
552
char* DLLCALL unpadfname(char *filename, char *str)
553
{
554
    int c,d;
555
556
557
558
559
560
561
562
563
564
565

	for(c=0,d=0;filename[c];c++)
		if(filename[c]!=SP) str[d++]=filename[c];
	str[d]=0;
	return(str);
}

/****************************************************************************/
/* Removes any files in the user transfer index (XFER.IXT) that match the   */
/* specifications of dest, or source user, or filename or any combination.  */
/****************************************************************************/
566
BOOL DLLCALL rmuserxfers(scfg_t* cfg, int fromuser, int destuser, char *fname)
567
568
569
570
571
{
    char str[256],*ixtbuf;
    int file;
    long l,length;

572
	sprintf(str,"%sxfer.ixt", cfg->data_dir);
573
574
575
576
577
578
	if(!fexist(str))
		return(FALSE);
	if(!flength(str)) {
		remove(str);
		return(FALSE); 
	}
579
	if((file=sopen(str,O_RDONLY|O_BINARY,SH_DENYWR))==-1) {
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
//		errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
		return(FALSE); 
	}
	length=filelength(file);
	if((ixtbuf=(char *)MALLOC(length))==NULL) {
		close(file);
//		errormsg(WHERE,ERR_ALLOC,str,length);
		return(FALSE); 
	}
	if(read(file,ixtbuf,length)!=length) {
		close(file);
		FREE(ixtbuf);
//		errormsg(WHERE,ERR_READ,str,length);
		return(FALSE); 
	}
	close(file);
	if((file=sopen(str,O_WRONLY|O_TRUNC|O_BINARY,SH_DENYRW))==-1) {
		FREE(ixtbuf);
//		errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_TRUNC);
		return(FALSE); 
	}
	for(l=0;l<length;l+=24) {
		if(fname!=NULL && fname[0]) {               /* fname specified */
			if(!strncmp(ixtbuf+l+5,fname,12)) {     /* this is the file */
				if(destuser && fromuser) {          /* both dest and from user */
					if(atoi(ixtbuf+l)==destuser && atoi(ixtbuf+l+18)==fromuser)
						continue; }                 /* both match */
				else if(fromuser) {                 /* from user */
					if(atoi(ixtbuf+l+18)==fromuser) /* matches */
						continue; }
				else if(destuser) {                 /* dest user */
					if(atoi(ixtbuf+l)==destuser)    /* matches */
						continue; }
				else continue; } }                  /* no users, so match */
		else if(destuser && fromuser) {
			if(atoi(ixtbuf+l+18)==fromuser && atoi(ixtbuf+l)==destuser)
				continue; }
		else if(destuser && atoi(ixtbuf+l)==destuser)
			continue;
		else if(fromuser && atoi(ixtbuf+l+18)==fromuser)
			continue;
		write(file,ixtbuf+l,24); }
	close(file);
	FREE(ixtbuf);

	return(TRUE);
}

628
629
630
631
632
void DLLCALL getextdesc(scfg_t* cfg, uint dirnum, ulong datoffset, char *ext)
{
	char str[256];
	int file;

633
	memset(ext,0,F_EXBSIZE+1);
634
635
636
	sprintf(str,"%s%s.exb",cfg->dir[dirnum]->data_dir,cfg->dir[dirnum]->code);
	if((file=nopen(str,O_RDONLY))==-1)
		return;
637
638
	lseek(file,(datoffset/F_LEN)*F_EXBSIZE,SEEK_SET);
	read(file,ext,F_EXBSIZE);
639
640
641
642
643
	close(file);
}

void DLLCALL putextdesc(scfg_t* cfg, uint dirnum, ulong datoffset, char *ext)
{
644
	char str[256],nulbuf[F_EXBSIZE];
645
646
647
648
649
650
651
	int file;

	memset(nulbuf,0,sizeof(nulbuf));
	sprintf(str,"%s%s.exb",cfg->dir[dirnum]->data_dir,cfg->dir[dirnum]->code);
	if((file=nopen(str,O_WRONLY|O_CREAT))==-1)
		return;
	lseek(file,0L,SEEK_END);
652
653
654
655
	while(filelength(file)<(long)(datoffset/F_LEN)*F_EXBSIZE)
		write(file,nulbuf,sizeof(nulbuf));
	lseek(file,(datoffset/F_LEN)*F_EXBSIZE,SEEK_SET);
	write(file,ext,F_EXBSIZE);
656
657
	close(file);
}