listfile.cpp 40.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* listfile.cpp */

/* Synchronet file database listing functions */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * 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"

#define BF_MAX	26	/* Batch Flag max: A-Z */	

int extdesclines(char *str);

/*****************************************************************************/
/* List files in directory 'dir' that match 'filespec'. Filespec must be 	 */
/* padded. ex: FILE*   .EXT, not FILE*.EXT. 'mode' determines other critiria */
/* the files must meet before they'll be listed. 'mode' bit FL_NOHDR doesn't */
/* list the directory header.                                                */
/* Returns -1 if the listing was aborted, otherwise total files listed		 */
/*****************************************************************************/
51
int sbbs_t::listfiles(uint dirnum, const char *filespec, int tofile, long mode)
52
{
53
	char	str[256],hdr[256],letter='A',*p,*datbuf,ext[513];
54
	char 	tmp[512];
55
56
	uchar*	ixbbuf;
	uchar	flagprompt=0;
57
	int		c, d;
58
59
	uint	i,j;
	int		file,found=0,lastbat=0,disp;
deuce's avatar
64-bit    
deuce committed
60
61
	long	m=0,n,anchor=0,next,datbuflen;
	int32_t	l;
62
	ulong	file_row[26];
63
64
65
66
	file_t	f,bf[26];	/* bf is batch flagged files */

	if(mode&FL_ULTIME) {
		last_ns_time=now;
67
		sprintf(str,"%s%s.dab",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
68
69
70
71
		if((file=nopen(str,O_RDONLY))!=-1) {
			read(file,&l,4);
			close(file);
			if(ns_time>(time_t)l)
72
73
74
				return(0); 
		}
	}
75
	sprintf(str,"%s%s.ixb",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
76
77
	if((file=nopen(str,O_RDONLY))==-1)
		return(0);
78
	l=(long)filelength(file);
79
80
	if(!l) {
		close(file);
81
82
		return(0); 
	}
deuce's avatar
deuce committed
83
	if((ixbbuf=(uchar *)malloc(l))==NULL) {
84
85
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,l);
86
87
		return(0); 
	}
88
89
90
	if(lread(file,ixbbuf,l)!=l) {
		close(file);
		errormsg(WHERE,ERR_READ,str,l);
deuce's avatar
deuce committed
91
		free((char *)ixbbuf);
92
93
		return(0); 
	}
94
	close(file);
95
	sprintf(str,"%s%s.dat",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
96
97
	if((file=nopen(str,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
deuce's avatar
deuce committed
98
		free((char *)ixbbuf);
99
100
		return(0); 
	}
101
	datbuflen=(long)filelength(file);
deuce's avatar
deuce committed
102
	if((datbuf=(char *)malloc(datbuflen))==NULL) {
103
104
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,datbuflen);
deuce's avatar
deuce committed
105
		free((char *)ixbbuf);
106
107
		return(0); 
	}
108
109
110
	if(lread(file,datbuf,datbuflen)!=datbuflen) {
		close(file);
		errormsg(WHERE,ERR_READ,str,datbuflen);
deuce's avatar
deuce committed
111
112
		free((char *)datbuf);
		free((char *)ixbbuf);
113
114
		return(0); 
	}
115
116
117
118
119
	close(file);
	if(!tofile) {
		action=NODE_LFIL;
		getnodedat(cfg.node_num,&thisnode,0);
		if(thisnode.action!=NODE_LFIL) {	/* was a sync */
120
121
122
123
124
125
			if(getnodedat(cfg.node_num,&thisnode,true)==0) {
				thisnode.action=NODE_LFIL;
				putnodedat(cfg.node_num,&thisnode); 
			} 
		} 
	}
126
127
128
129
130
131
132
133
134

	while(online && found<MAX_FILES) {
		if(found<0)
			found=0;
		if(m>=l || flagprompt) {		  /* End of list */
			if(useron.misc&BATCHFLAG && !tofile && found && found!=lastbat
				&& !(mode&(FL_EXFIND|FL_VIEW))) {
				flagprompt=0;
				lncntr=0;
135
				if((i=batchflagprompt(dirnum,bf,file_row,letter-'A',l/F_IXBSIZE))==2) {
136
137
					m=anchor;
					found-=letter-'A';
138
139
					letter='A'; 
				}
140
141
142
				else if(i==3) {
					if((long)anchor-((letter-'A')*F_IXBSIZE)<0) {
						m=0;
143
144
						found=0; 
					}
145
146
					else {
						m=anchor-((letter-'A')*F_IXBSIZE);
147
148
149
150
						found-=letter-'A'; 
					}
					letter='A'; 
				}
151
				else if((int)i==-1) {
deuce's avatar
deuce committed
152
153
					free((char *)ixbbuf);
					free((char *)datbuf);
154
155
					return(-1); 
				}
156
157
158
				else
					break;
				getnodedat(cfg.node_num,&thisnode,0);
159
160
				nodesync(); 
			}
161
			else
162
163
				break; 
		}
164
165
166
167
168
169
170

		if(letter>'Z')
			letter='A';
		if(letter=='A')
			anchor=m;

		if(msgabort()) {		 /* used to be !tofile && msgabort() */
deuce's avatar
deuce committed
171
172
			free((char *)ixbbuf);
			free((char *)datbuf);
173
174
			return(-1); 
		}
175
		for(j=0;j<12 && m<l;j++)
176
177
			if(j==8)
				str[j]=ixbbuf[m]>' ' ? '.' : ' ';
178
179
180
181
182
183
			else
				str[j]=ixbbuf[m++];		/* Turns FILENAMEEXT into FILENAME.EXT */
		str[j]=0;
		if(!(mode&(FL_FINDDESC|FL_EXFIND)) && filespec[0]
			&& !filematch(str,filespec)) {
			m+=11;
184
185
			continue; 
		}
186
187
188
		n=ixbbuf[m]|((long)ixbbuf[m+1]<<8)|((long)ixbbuf[m+2]<<16);
		if(n>=datbuflen) {	/* out of bounds */
			m+=11;
189
190
			continue; 
		}
191
192
193
194
195
196
		if(mode&(FL_FINDDESC|FL_EXFIND)) {
			getrec((char *)&datbuf[n],F_DESC,LEN_FDESC,tmp);
			strupr(tmp);
			p=strstr(tmp,filespec);
			if(!(mode&FL_EXFIND) && p==NULL) {
				m+=11;
197
198
				continue; 
			}
199
200
			getrec((char *)&datbuf[n],F_MISC,1,tmp);
			j=tmp[0];  /* misc bits */
201
			if(j) j-=' ';
202
			if(mode&FL_EXFIND && j&FM_EXTDESC) { /* search extended description */
203
				getextdesc(&cfg,dirnum,n,ext);
204
205
206
				strupr(ext);
				if(!strstr(ext,filespec) && !p) {	/* not in description or */
					m+=11;						 /* extended description */
207
208
209
					continue; 
				}
			}
210
211
			else if(!p) {			 /* no extended description and not in desc */
				m+=11;
212
213
				continue; } 
		}
214
215
216
217
		if(mode&FL_ULTIME) {
			if(ns_time>(ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)|((long)ixbbuf[m+5]<<16)
				|((long)ixbbuf[m+6]<<24))) {
				m+=11;
218
219
				continue; } 
		}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		if(useron.misc&BATCHFLAG && letter=='A' && found && !tofile
			&& !(mode&(FL_EXFIND|FL_VIEW))
			&& (!mode || !(useron.misc&EXPERT)))
			bputs(text[FileListBatchCommands]);
		m+=11;
		if(!found && !(mode&(FL_EXFIND|FL_VIEW))) {
			for(i=0;i<usrlibs;i++)
				if(usrlib[i]==cfg.dir[dirnum]->lib)
					break;
			for(j=0;j<usrdirs[i];j++)
				if(usrdir[i][j]==dirnum)
					break;						/* big header */
			if((!mode || !(useron.misc&EXPERT)) && !tofile && (!filespec[0]
				|| (strchr(filespec,'*') || strchr(filespec,'?')))) {
234
				sprintf(hdr,"%s%s.hdr",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
235
				if(fexistcase(hdr))
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
					printfile(hdr,0);	/* Use DATA\DIRS\<CODE>.HDR */
				else {
					if(useron.misc&BATCHFLAG)
						bputs(text[FileListBatchCommands]);
					else {
						CLS;
						d=strlen(cfg.lib[usrlib[i]]->lname)>strlen(cfg.dir[dirnum]->lname) ?
							strlen(cfg.lib[usrlib[i]]->lname)+17
							: strlen(cfg.dir[dirnum]->lname)+17;
						if(i>8 || j>8) d++;
						attr(cfg.color[clr_filelsthdrbox]);
						bputs("");            /* use to start with \r\n */
						for(c=0;c<d;c++)
							outchar('');
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrLib],i+1,cfg.lib[usrlib[i]]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
254
							outchar(' ');
255
						attr(cfg.color[clr_filelsthdrbox]);
256
257
258
259
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrDir],j+1,cfg.dir[dirnum]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
260
							outchar(' ');
261
						attr(cfg.color[clr_filelsthdrbox]);
262
263
264
265
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrFiles],l/F_IXBSIZE);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
266
							outchar(' ');
267
						attr(cfg.color[clr_filelsthdrbox]);
268
269
270
						bputs("\r\n");
						for(c=0;c<d;c++)
							outchar('');
271
						bputs("\r\n"); 
272
273
					}
				}
274
			}
275
276
277
278
			else {					/* short header */
				if(tofile) {
					sprintf(hdr,"(%u) %s ",i+1,cfg.lib[usrlib[i]]->sname);
					write(tofile,crlf,2);
279
280
					write(tofile,hdr,strlen(hdr)); 
				}
281
282
283
				else {
					sprintf(hdr,text[ShortHdrLib],i+1,cfg.lib[usrlib[i]]->sname);
					bputs("\r\1>\r\n");
284
285
					bputs(hdr); 
				}
286
287
288
				c=bstrlen(hdr);
				if(tofile) {
					sprintf(hdr,"(%u) %s",j+1,cfg.dir[dirnum]->lname);
289
290
					write(tofile,hdr,strlen(hdr)); 
				}
291
292
				else {
					sprintf(hdr,text[ShortHdrDir],j+1,cfg.dir[dirnum]->lname);
293
294
					bputs(hdr); 
				}
295
296
297
298
				c+=bstrlen(hdr);
				if(tofile) {
					write(tofile,crlf,2);
					sprintf(hdr,"%*s",c,nulstr);
rswindell's avatar
rswindell committed
299
					memset(hdr,0xC4,c);
300
					strcat(hdr,crlf);
301
302
					write(tofile,hdr,strlen(hdr)); 
				}
303
304
305
306
				else {
					CRLF;
					attr(cfg.color[clr_filelstline]);
					while(c--)
rswindell's avatar
rswindell committed
307
						outchar('\xC4');
308
309
310
311
					CRLF; 
				} 
			} 
		}
312
		long currow = row;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
		next=m;
		disp=1;
		if(mode&(FL_EXFIND|FL_VIEW)) {
			f.dir=dirnum;
			strcpy(f.name,str);
			m-=11;
			f.datoffset=n;
			f.dateuled=ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)
				|((long)ixbbuf[m+5]<<16)|((long)ixbbuf[m+6]<<24);
			f.datedled=ixbbuf[m+7]|((long)ixbbuf[m+8]<<8)
				|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24);
			m+=11;
			f.size=0;
			getfiledat(&cfg,&f);
			if(!found)
				bputs("\r\1>");
			if(mode&FL_EXFIND) {
				if(!viewfile(&f,1)) {
deuce's avatar
deuce committed
331
332
					free((char *)ixbbuf);
					free((char *)datbuf);
333
334
					return(-1); } 
			}
335
336
			else {
				if(!viewfile(&f,0)) {
deuce's avatar
deuce committed
337
338
					free((char *)ixbbuf);
					free((char *)datbuf);
339
340
341
342
					return(-1); 
				} 
			} 
		}
343
344
345
346
347
348
349
350
351

		else if(tofile)
			listfiletofile(str,&datbuf[n],dirnum,tofile);
		else if(mode&FL_FINDDESC)
			disp=listfile(str,&datbuf[n],dirnum,filespec,letter,n);
		else
			disp=listfile(str,&datbuf[n],dirnum,nulstr,letter,n);
		if(!disp && letter>'A') {
			next=m-F_IXBSIZE;
352
353
			letter--; 
		}
354
355
		else {
			disp=1;
356
357
			found++; 
		}
358
		if(sys_status&SS_ABORT) {
deuce's avatar
deuce committed
359
360
			free((char *)ixbbuf);
			free((char *)datbuf);
361
362
			return(-1); 
		}
363
364
365
366
367
368
369
370
371
372
		if(mode&(FL_EXFIND|FL_VIEW))
			continue;
		if(useron.misc&BATCHFLAG && !tofile) {
			if(disp) {
				strcpy(bf[letter-'A'].name,str);
				m-=11;
				bf[letter-'A'].datoffset=n;
				bf[letter-'A'].dateuled=ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)
					|((long)ixbbuf[m+5]<<16)|((long)ixbbuf[m+6]<<24);
				bf[letter-'A'].datedled=ixbbuf[m+7]|((long)ixbbuf[m+8]<<8)
373
374
					|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24);
				file_row[letter-'A'] = currow;
375
			}
376
377
378
379
380
381
382
383
384
			m+=11;
			if(flagprompt || letter=='Z' || !disp ||
				(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?')
				&& !(mode&FL_FINDDESC))
				|| (useron.misc&BATCHFLAG && !tofile && lncntr>=rows-2)
				) {
				flagprompt=0;
				lncntr=0;
				lastbat=found;
385
				if((int)(i=batchflagprompt(dirnum,bf,file_row,letter-'A'+1,l/F_IXBSIZE))<1) {
deuce's avatar
deuce committed
386
387
					free((char *)ixbbuf);
					free((char *)datbuf);
388
					if((int)i==-1)
389
390
						return(-1);
					else
391
392
						return(found); 
				}
393
394
				if(i==2) {
					next=anchor;
395
396
					found-=(letter-'A')+1; 
				}
397
398
399
				else if(i==3) {
					if((long)anchor-((letter-'A'+1)*F_IXBSIZE)<0) {
						next=0;
400
401
						found=0; 
					}
402
403
					else {
						next=anchor-((letter-'A'+1)*F_IXBSIZE);
404
405
						found-=letter-'A'+1; } 
				}
406
407
408
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync();
				letter='A';	}
409
410
			else letter++; 
		}
411
412
413
		if(useron.misc&BATCHFLAG && !tofile
			&& lncntr>=rows-2) {
			lncntr=0;		/* defeat pause() */
414
415
			flagprompt=1; 
		}
416
417
418
		m=next;
		if(mode&FL_FINDDESC) continue;
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?') && m)
419
420
			break; 
	}
421

deuce's avatar
deuce committed
422
423
	free((char *)ixbbuf);
	free((char *)datbuf);
424
425
426
427
428
429
430
	return(found);
}

/****************************************************************************/
/* Prints one file's information on a single line                           */
/* Return 1 if displayed, 0 otherwise										*/
/****************************************************************************/
431
432
bool sbbs_t::listfile(const char *fname, const char *buf, uint dirnum
	, const char *search, const char letter, ulong datoffset)
433
{
434
	char	str[256],ext[513]="",*ptr,*cr,*lf,exist=1;
435
	char	path[MAX_PATH+1];
436
437
438
439
	char 	tmp[512];
    uchar	alt;
    int		i,j;
    ulong	cdt;
440
	off_t	size;
441
	int		size_attr=clr_filecdt;
442

443
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC && useron.misc&EXTDESC) {
444
		getextdesc(&cfg,dirnum,datoffset,ext);
445
		if(useron.misc&BATCHFLAG && lncntr+extdesclines(ext)>=rows-2 && letter!='A')
446
447
			return(false); 
	}
448

449
450
	attr(cfg.color[clr_filename]);
	bputs(fname);
451

452
	getrec(buf,F_ALTPATH,2,str);
453
454
455
456
	alt=(uchar)ahtoul(str);
	sprintf(path,"%s%s",alt>0 && alt<=cfg.altpaths ? cfg.altpath[alt-1]:cfg.dir[dirnum]->path
		,unpadfname(fname,tmp));

457
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC) {
458
459
460
		if(!(useron.misc&EXTDESC))
			outchar('+');
		else
461
462
			outchar(' '); 
	}
463
	else
464
		outchar(' ');
465
466
	if(useron.misc&BATCHFLAG) {
		attr(cfg.color[clr_filedesc]);
467
468
		bprintf("%c",letter); 
	}
469
470
471
472
473
	getrec(buf,F_CDT,LEN_FCDT,str);
	cdt=atol(str);
	if(cfg.dir[dirnum]->misc&DIR_FCHK) {
		if(!fexistcase(path)) {
			exist=0;
474
			size_attr = clr_err; 
475
476
477
		}
		else if((cfg.dir[dirnum]->misc&DIR_FREE) && (size=flength(path)) >= 0)
			cdt = size;
478
	}
479
	attr(cfg.color[size_attr]);
480
	if(useron.misc&BATCHFLAG) {
481
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {
482
			attr(curatr^(HIGH|BLINK));
483
484
			bputs("  FREE"); 
		}
485
486
487
488
489
490
491
492
		else if(cdt>=(1024*1024*1024))
			bprintf("%5.1fG",cdt/(1024.0*1024.0*1024.0));
		else if(cdt>=(1024*1024))
			bprintf("%5.1fM",cdt/(1024.0*1024.0));
		else if(cdt>=1024)
			bprintf("%5.1fK",cdt/1024.0); 
		else
			bprintf("%5luB", cdt);
493
	}
494
	else {
495
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {  /* FREE file */
496
			attr(curatr^(HIGH|BLINK));
497
498
			bputs("   FREE"); 
		}
499
500
501
502
503
504
		else if(cdt>=(1024*1024*1024))
			bprintf("%6.1fG",cdt/(1024.0*1024.0*1024.0));
		else if(cdt>=(1024*1024))
			bprintf("%6.1fM",cdt/(1024.0*1024.0));
		else if(cdt>=1024)
			bprintf("%6.1fK",cdt/1024.0); 
505
		else
506
			bprintf("%6luB", cdt);
507
	}
508
	if(exist)
509
		outchar(' ');
510
511
	else
		outchar('-');
512
	getrec(buf,F_DESC,LEN_FDESC,str);
513
	attr(cfg.color[clr_filedesc]);
514
515
516

#ifdef _WIN32
 
517
	if(exist && !(cfg.file_misc&FM_NO_LFN)) {
518
519
		fexistcase(path);	/* Get real (long?) filename */
		ptr=getfname(path);
520
		if(stricmp(ptr,tmp) && stricmp(ptr,str))
521
			bprintf("%.*s\r\n%21s",LEN_FDESC,ptr,"");
522
	}
523

524
#endif
525

526
527
528
529
530
531
532
533
534
535
536
	if(!ext[0]) {
		if(search[0]) { /* high-light string in string */
			strcpy(tmp,str);
			strupr(tmp);
			ptr=strstr(tmp,search);
			i=strlen(search);
			j=ptr-tmp;
			bprintf("%.*s",j,str);
			attr(cfg.color[clr_filedesc]^HIGH);
			bprintf("%.*s",i,str+j);
			attr(cfg.color[clr_filedesc]);
537
			bprintf("%.*s",(int)(strlen(str)-(j+i)),str+j+i); 
538
		}
539
540
		else
			bputs(str);
541
542
		CRLF; 
	}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	ptr=ext;
	while(*ptr && ptr<ext+512 && !msgabort()) {
		cr=strchr(ptr,CR);
		lf=strchr(ptr,LF);
		if(lf && (lf<cr || !cr)) cr=lf;
		if(cr>ptr+LEN_FDESC)
			cr=ptr+LEN_FDESC;
		else if(cr)
			*cr=0;
		sprintf(str,"%.*s\r\n",LEN_FDESC,ptr);
		putmsg(str,P_NOATCODES|P_SAVEATR);
		if(!cr) {
			if(strlen(ptr)>LEN_FDESC)
				cr=ptr+LEN_FDESC;
			else
558
559
				break; 
		}
560
561
562
563
564
		if(!(*(cr+1)) || !(*(cr+2)))
			break;
		bprintf("%21s",nulstr);
		ptr=cr;
		if(!(*ptr)) ptr++;
565
566
		while(*ptr==LF || *ptr==CR) ptr++; 
	}
567
568
569
570
571
572
573
574
	return(true);
}

/****************************************************************************/
/* Remove credits from uploader of file 'f'                                 */
/****************************************************************************/
bool sbbs_t::removefcdt(file_t* f)
{
575
576
577
578
	char	str[128];
	char 	tmp[512];
	int		u;
	long	cdt;
579

580
	if((u=matchuser(&cfg,f->uler,TRUE /*sysop_alias*/))==0) {
581
	   bputs(text[UnknownUser]);
582
583
	   return(false); 
	}
584
585
586
587
588
589
590
591
592
593
594
595
	cdt=0L;
	if(cfg.dir[f->dir]->misc&DIR_CDTMIN && cur_cps) {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=((ulong)(f->cdt*(cfg.dir[f->dir]->up_pct/100.0))/cur_cps)/60;
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->timesdled)  /* all downloads */
			cdt+=((ulong)((long)f->timesdled
				*f->cdt*(cfg.dir[f->dir]->dn_pct/100.0))/cur_cps)/60;
		adjustuserrec(&cfg,u,U_MIN,10,-cdt);
		sprintf(str,"%lu minute",cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? str : text[No]);
596
597
		putsmsg(&cfg,u,tmp); 
	}
598
599
600
601
602
603
604
605
606
607
	else {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=(ulong)(f->cdt*(cfg.dir[f->dir]->up_pct/100.0));
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->timesdled)  /* all downloads */
			cdt+=(ulong)((long)f->timesdled
				*f->cdt*(cfg.dir[f->dir]->dn_pct/100.0));
		adjustuserrec(&cfg,u,U_CDT,10,-cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? ultoac(cdt,str) : text[No]);
608
609
		putsmsg(&cfg,u,tmp); 
	}
610
611
612
613
614
615

	adjustuserrec(&cfg,u,U_ULB,10,-f->size);
	adjustuserrec(&cfg,u,U_ULS,5,-1);
	return(true);
}

616
617
618
619
620
bool sbbs_t::removefile(file_t* f)
{
	char str[256];

	if(removefiledat(&cfg,f)) {
621
		SAFEPRINTF3(str,"removed %s from %s %s"
622
623
624
625
626
627
628
629
630
631
			,f->name
			,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
		logline("U-",str);
		return(true);
	}
	SAFEPRINTF2(str,"%s %s",cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
	errormsg(WHERE, ERR_REMOVE, f->name, 0, str);
	return(false);
}

632
633
634
635
636
/****************************************************************************/
/* Move file 'f' from f.dir to newdir                                       */
/****************************************************************************/
bool sbbs_t::movefile(file_t* f, int newdir)
{
637
	char str[MAX_PATH+1],path[MAX_PATH+1],fname[128],ext[1024];
638
639
640
641
	int olddir=f->dir;

	if(findfile(&cfg,newdir,f->name)) {
		bprintf(text[FileAlreadyThere],f->name);
642
643
		return(false); 
	}
644
	getextdesc(&cfg,olddir,f->datoffset,ext);
645
	if(cfg.dir[olddir]->misc&DIR_MOVENEW)
646
		f->dateuled=time32(NULL);
647
648
649
650
651
652
	unpadfname(f->name,fname);
	removefiledat(&cfg,f);
	f->dir=newdir;
	addfiledat(&cfg,f);
	bprintf(text[MovedFile],f->name
		,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
653
	sprintf(str,"moved %s to %s %s",f->name
654
655
656
657
		,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
	logline(nulstr,str);
	if(!f->altpath) {	/* move actual file */
		sprintf(str,"%s%s",cfg.dir[olddir]->path,fname);
658
		if(fexistcase(str)) {
659
660
661
662
			sprintf(path,"%s%s",cfg.dir[f->dir]->path,getfname(str));
			mv(str,path,0); 
		} 
	}
663
	if(f->misc&FM_EXTDESC)
664
		putextdesc(&cfg,f->dir,f->datoffset,ext);
665
666
667
668
669
670
671
672
	return(true);
}

/****************************************************************************/
/* Batch flagging prompt for download, extended info, and archive viewing	*/
/* Returns -1 if 'Q' or Ctrl-C, 0 if skip, 1 if [Enter], 2 otherwise        */
/* or 3, backwards. 														*/
/****************************************************************************/
673
int sbbs_t::batchflagprompt(uint dirnum, file_t* bf, ulong* row, uint total
674
675
							,long totalfiles)
{
676
677
	char	ch,str[256],fname[128],*p,remcdt=0,remfile=0;
	int		c, d;
678
	char 	tmp[512];
679
	uint	i,j,ml=0,md=0,udir,ulib;
680
	file_t	f;
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

	for(ulib=0;ulib<usrlibs;ulib++)
		if(usrlib[ulib]==cfg.dir[dirnum]->lib)
			break;
	for(udir=0;udir<usrdirs[ulib];udir++)
		if(usrdir[ulib][udir]==dirnum)
			break;

	CRLF;
	while(online) {
		bprintf(text[BatchFlagPrompt]
			,ulib+1
			,cfg.lib[cfg.dir[dirnum]->lib]->sname
			,udir+1
			,cfg.dir[dirnum]->sname
696
			,total, totalfiles);
697
698
699
		ch=getkey(K_UPPER);
		clearline();
		if(ch=='?') {
700
			menu("batflag");
701
702
			if(lncntr)
				pause();
703
704
			return(2); 
		}
705
		if(ch==text[YNQP][2] || sys_status&SS_ABORT)
706
707
708
709
710
			return(-1);
		if(ch=='S')
			return(0);
		if(ch=='P' || ch=='-')
			return(3);
711
		if(ch=='B' || ch=='D') {    /* Flag for batch download */
712
713
			if(useron.rest&FLAG('D')) {
				bputs(text[R_Download]);
714
715
				return(2); 
			}
716
717
718
719
720
721
			if(total==1) {
				f.dir=dirnum;
				strcpy(f.name,bf[0].name);
				f.datoffset=bf[0].datoffset;
				f.size=0;
				getfiledat(&cfg,&f);
722
				addtobatdl(&f);
723
				if(ch=='D')
724
					start_batch_download();
725
				CRLF;
726
727
				return(2); 
			}
728
729
730
731
			link_list_t saved_hotspots = mouse_hotspots;
			ZERO_VAR(mouse_hotspots);
			for(i=0; i < total; i++)
				add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
732
733
			bputs(text[BatchDlFlags]);
			d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF);
734
735
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
736
737
738
739
740
741
742
743
744
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
					if(batdn_total>=cfg.max_batdn) {
						bprintf(text[BatchDlQueueIsFull],str+c);
745
746
						break; 
					}
747
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
748
						f.dir=dirnum;
749
						p=strchr(str+c,' ');
750
751
752
753
754
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							if(batdn_total>=cfg.max_batdn) {
								bprintf(text[BatchDlQueueIsFull],str+c);
755
756
								break; 
							}
757
758
759
760
761
762
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								f.datoffset=bf[i].datoffset;
								f.size=0;
								getfiledat(&cfg,&f);
763
764
765
766
								addtobatdl(&f); 
							} 
						} 
					}
767
768
769
770
771
772
773
774
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.size=0;
						getfiledat(&cfg,&f);
775
776
						addtobatdl(&f);
					} 
777
				}
778
779
				if(ch=='D')
					start_batch_download();
780
				CRLF;
781
782
				return(2); 
			}
783
			clearline();
784
785
			continue; 
		}
786
787
788
789
790
791
792
793
794
795
796
797

		if(ch=='E' || ch=='V') {    /* Extended Info */
			if(total==1) {
				f.dir=dirnum;
				strcpy(f.name,bf[0].name);
				f.datoffset=bf[0].datoffset;
				f.dateuled=bf[0].dateuled;
				f.datedled=bf[0].datedled;
				f.size=0;
				getfiledat(&cfg,&f);
				if(!viewfile(&f,ch=='E'))
					return(-1);
798
799
				return(2); 
			}
800
801
802
803
			link_list_t saved_hotspots = mouse_hotspots;
			ZERO_VAR(mouse_hotspots);
			for(i=0; i < total; i++)
				add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
804
805
			bputs(text[BatchDlFlags]);
			d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF);
806
807
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
808
809
810
811
812
813
814
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
815
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
816
						f.dir=dirnum;
817
						p=strchr(str+c,' ');
818
819
820
821
822
823
824
825
826
827
828
829
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								f.datoffset=bf[i].datoffset;
								f.dateuled=bf[i].dateuled;
								f.datedled=bf[i].datedled;
								f.size=0;
								getfiledat(&cfg,&f);
								if(!viewfile(&f,ch=='E'))
830
831
832
833
									return(-1); 
							} 
						} 
					}
834
835
836
837
838
839
840
841
842
843
844
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.dateuled=bf[str[c]-'A'].dateuled;
						f.datedled=bf[str[c]-'A'].datedled;
						f.size=0;
						getfiledat(&cfg,&f);
						if(!viewfile(&f,ch=='E'))
845
846
847
848
							return(-1); } 
				}
				return(2); 
			}
849
			clearline();
850
851
			continue; 
		}
852

853
		if((ch=='R' || ch=='M')     /* Delete or Move */
854
855
856
857
			&& !(useron.rest&FLAG('R'))
			&& (dir_op(dirnum) || useron.exempt&FLAG('R'))) {
			if(total==1) {
				strcpy(str,"A");
858
859
				d=1; 
			}
860
			else {
861
862
863
864
				link_list_t saved_hotspots = mouse_hotspots;
				ZERO_VAR(mouse_hotspots);
				for(i=0; i < total; i++)
					add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
865
				bputs(text[BatchDlFlags]);
866
867
868
				d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF);
				clear_hotspots();
				mouse_hotspots = saved_hotspots;
869
			}
870
871
872
873
874
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
875
876
				if(ch=='R') {
					if(noyes(text[RemoveFileQ]))
877
878
879
880
						return(2);
					remcdt=remfile=1;
					if(dir_op(dirnum)) {
						remcdt=!noyes(text[RemoveCreditsQ]);
881
882
						remfile=!noyes(text[DeleteFileQ]); } 
				}
883
884
885
886
887
888
				else if(ch=='M') {
					CRLF;
					for(i=0;i<usrlibs;i++)
						bprintf(text[MoveToLibLstFmt],i+1,cfg.lib[usrlib[i]]->lname);
					SYNC;
					bprintf(text[MoveToLibPrompt],cfg.dir[dirnum]->lib+1);
889
					if((int)(ml=getnum(usrlibs))==-1)
890
891
892
893
894
895
896
897
898
899
900
						return(2);
					if(!ml)
						ml=cfg.dir[dirnum]->lib;
					else
						ml--;
					CRLF;
					for(j=0;j<usrdirs[ml];j++)
						bprintf(text[MoveToDirLstFmt]
							,j+1,cfg.dir[usrdir[ml][j]]->lname);
					SYNC;
					bprintf(text[MoveToDirPrompt],usrdirs[ml]);
901
					if((int)(md=getnum(usrdirs[ml]))==-1)
902
903
904
905
						return(2);
					if(!md)
						md=usrdirs[ml]-1;
					else md--;
906
907
					CRLF; 
				}
908
909
				lncntr=0;
				for(c=0;c<d;c++) {
910
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
911
						f.dir=dirnum;
912
						p=strchr(str+c,' ');
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								unpadfname(f.name,fname);
								f.datoffset=bf[i].datoffset;
								f.dateuled=bf[i].dateuled;
								f.datedled=bf[i].datedled;
								f.size=0;
								getfiledat(&cfg,&f);
								if(f.opencount) {
									bprintf(text[FileIsOpen]
										,f.opencount,f.opencount>1 ? "s":nulstr);
928
929
									continue; 
								}
930
								if(ch=='R') {
931
									removefile(&f);
932
933
									if(remfile) {
										sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
934
935
										remove(tmp); 
									}
936
									if(remcdt)
937
938
										removefcdt(&f); 
								}
939
								else if(ch=='M')
940
941
942
943
									movefile(&f,usrdir[ml][md]); 
							} 
						} 
					}
944
945
946
947
948
949
950
951
952
953
954
955
956
957
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						unpadfname(f.name,fname);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.dateuled=bf[str[c]-'A'].dateuled;
						f.datedled=bf[str[c]-'A'].datedled;
						f.size=0;
						getfiledat(&cfg,&f);
						if(f.opencount) {
							bprintf(text[FileIsOpen]
								,f.opencount,f.opencount>1 ? "s":nulstr);
958
959
							continue; 
						}
960
						if(ch=='R') {
961
							removefile(&f);
962
963
							if(remfile) {
								sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
964
965
								remove(tmp); 
							}
966
							if(remcdt)
967
968
								removefcdt(&f); 
						}
969
						else if(ch=='M')
970
971
972
973
							movefile(&f,usrdir[ml][md]); } 
				}
				return(2); 
			}
974
			clearline();
975
976
			continue; 
		}
977

978
979
		return(1); 
	}
980
981
982
983
984
985
986
987
988
989
990
991

	return(-1);
}

/****************************************************************************/
/* List detailed information about the files in 'filespec'. Prompts for     */
/* action depending on 'mode.'                                              */
/* Returns number of files matching filespec that were found                */
/****************************************************************************/
int sbbs_t::listfileinfo(uint dirnum, char *filespec, long mode)
{
	char	str[258],path[258],dirpath[256],done=0,ch,fname[13],ext[513];
992
993
	char 	tmp[512];
	uchar	*ixbbuf,*usrxfrbuf=NULL,*p;
994
	int		file;
rswindell's avatar
rswindell committed
995
	int		error;
996
997
	int		found=0;
    uint	i,j;
998
	long	usrxfrlen=0;
999
1000
1001
1002
    long	m,l;
	long	usrcdt;
    time_t	start,end,t;
    file_t	f;
1003
	struct	tm tm;
1004

1005
	sprintf(str,"%sxfer.ixt",cfg.data_dir);
1006
1007
1008
	if(mode==FI_USERXFER) {
		if(flength(str)<1L)
			return(0);
1009
1010
		if((file=nopen(str,O_RDONLY))==-1) {
			errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
1011
1012
			return(0); 
		}
1013
		usrxfrlen=(long)filelength(file);
deuce's avatar
deuce committed
1014
		if((usrxfrbuf=(uchar *)malloc(usrxfrlen))==NULL) {
1015
1016
			close(file);
			errormsg(WHERE,ERR_ALLOC,str,usrxfrlen);
1017
1018
			return(0); 
		}
1019
1020
		if(read(file,usrxfrbuf,usrxfrlen)!=usrxfrlen) {
			close(file);
deuce's avatar
deuce committed
1021
			free(usrxfrbuf);
1022
			errormsg(WHERE,ERR_READ,str,usrxfrlen);
1023
1024
1025
1026
			return(0); 
		}
		close(file); 
	}
1027
	sprintf(str,"%s%s.ixb",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
1028
1029
	if((file=nopen(str,O_RDONLY))==-1)
		return(0);
1030
	l=(long)filelength(file);
1031
	if(!l) {
rswindell's avatar
rswindell committed
1032
		FREE_AND_NULL(usrxfrbuf);
1033
		close(file);
1034
1035
		return(0); 
	}
deuce's avatar
deuce committed
1036
	if((ixbbuf=(uchar *)malloc(l))==NULL) {
1037
		close(file);
rswindell's avatar
rswindell committed
1038
		FREE_AND_NULL(usrxfrbuf);
1039
		errormsg(WHERE,ERR_ALLOC,str,l);
1040
1041
		return(0); 
	}
1042
1043
1044
	if(lread(file,ixbbuf,l)!=l) {
		close(file);
		errormsg(WHERE,ERR_READ,str,l);
deuce's avatar
deuce committed
1045
		free((char *)ixbbuf);
1046
		if(usrxfrbuf)
deuce's avatar
deuce committed
1047
			free(usrxfrbuf);
1048
1049
		return(0); 
	}
1050
	close(file);
1051