Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

listfile.cpp 30.3 KB
Newer Older
1 2 3 4 5 6
/* Synchronet file database listing functions */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
23
#include "filedat.h"
24 25 26 27 28 29

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

int extdesclines(char *str);

/*****************************************************************************/
30 31
/* List files in directory 'dir' that match 'filespec'.						 */
/* 'mode' determines other criteria											 */
32 33 34 35
/* 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		 */
/*****************************************************************************/
36
int sbbs_t::listfiles(uint dirnum, const char *filespec, FILE* tofile, long mode)
37
{
38
	char	hdr[256],letter='A',*p;
39
	uchar	flagprompt=0;
40
	int		c, d;
41
	uint	i,j;
42 43 44 45 46
	int		found=0,lastbat=0,disp;
	size_t	m=0;
	long	anchor=0,next;
	file_t* bf[BF_MAX];	/* bf is batch flagged files */
	smb_t	smb;
47
	ulong	file_row[26];
48
	size_t	longest = 0;
49

50 51
	if(!smb_init_dir(&cfg, &smb, dirnum))
		return 0;
52
	if(mode&FL_ULTIME) {
53 54 55
		last_ns_time = now;
		if(!newfiles(&smb, ns_time))	// this is fast
			return 0;
56
	}
57 58 59 60 61 62 63 64 65 66 67 68 69 70
	if(smb_open_dir(&cfg, &smb, dirnum) != SMB_SUCCESS)
		return 0;

	size_t file_count = 0;
	file_t* file_list = loadfiles(&smb
		, (mode&(FL_FINDDESC|FL_EXFIND)) ? NULL : filespec
		, (mode&FL_ULTIME) ? ns_time : 0
		, file_detail_extdesc
		, (enum file_sort)cfg.dir[dirnum]->sort
		, &file_count);
	if(file_list == NULL || file_count < 1) {
		smb_close(&smb);
		free(file_list);
		return 0;
71
	}
72

73 74 75 76 77 78
	for(i = 0; i < file_count; i++) {
		size_t len = strlen(file_list[i].name);
		if(len > longest)
			longest = len;
	}

79 80 81 82
	if(!tofile) {
		action=NODE_LFIL;
		getnodedat(cfg.node_num,&thisnode,0);
		if(thisnode.action!=NODE_LFIL) {	/* was a sync */
83 84 85 86 87 88
			if(getnodedat(cfg.node_num,&thisnode,true)==0) {
				thisnode.action=NODE_LFIL;
				putnodedat(cfg.node_num,&thisnode); 
			} 
		} 
	}
89

90 91 92
	m = 0; // current file index
	file_t* f;
	while(online) {
93 94
		if(found<0)
			found=0;
95
		if(m>=file_count || flagprompt) {		  /* End of list */
96 97 98 99
			if(useron.misc&BATCHFLAG && !tofile && found && found!=lastbat
				&& !(mode&(FL_EXFIND|FL_VIEW))) {
				flagprompt=0;
				lncntr=0;
100
				if((i=batchflagprompt(&smb, bf, file_row, letter-'A', file_count))==2) {
101 102
					m=anchor;
					found-=letter-'A';
103 104
					letter='A'; 
				}
105
				else if(i==3) {
106
					if((long)anchor-(letter-'A')<0) {
107
						m=0;
108 109
						found=0; 
					}
110
					else {
111
						m=anchor-(letter-'A');
112 113 114 115
						found-=letter-'A'; 
					}
					letter='A'; 
				}
116
				else if((int)i==-1) {
117 118
					found = -1;
					break;
119
				}
120 121 122
				else
					break;
				getnodedat(cfg.node_num,&thisnode,0);
123 124
				nodesync(); 
			}
125
			else
126 127
				break; 
		}
128 129
		if(m < file_count)
			f = &file_list[m];
130 131 132 133 134 135 136

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

		if(msgabort()) {		 /* used to be !tofile && msgabort() */
137 138
			found = -1;
			break;
139
		}
140
#if 0 /* unnecessary? */
141 142 143
		if(!(mode&(FL_FINDDESC|FL_EXFIND)) && filespec[0]
			&& !filematch(str,filespec)) {
			m+=11;
144 145
			continue; 
		}
146
#endif
147
		if(mode&(FL_FINDDESC|FL_EXFIND)) {
148
			p = (f->desc == NULL) ? NULL : strcasestr(f->desc, filespec);
149
			if(!(mode&FL_EXFIND) && p==NULL) {
150
				m++;
151 152
				continue; 
			}
153 154 155
			if(mode&FL_EXFIND && f->extdesc != NULL) { /* search extended description */
				if(!strcasestr((char*)f->extdesc, filespec) && p == NULL) {	/* not in description or */
					m++;											 /* extended description */
156 157 158
					continue; 
				}
			}
159 160 161 162
			else if(p == NULL) {			 /* no extended description and not in desc */
				m++;
				continue; 
			} 
163
		}
164
/** necessary?
165 166 167 168
		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;
169 170
				continue; 
			}
171
		}
172
**/
173 174 175 176
		if(useron.misc&BATCHFLAG && letter=='A' && found && !tofile
			&& !(mode&(FL_EXFIND|FL_VIEW))
			&& (!mode || !(useron.misc&EXPERT)))
			bputs(text[FileListBatchCommands]);
177
		m++;
178 179 180 181 182 183 184 185 186
		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,'?')))) {
187
				sprintf(hdr,"%s%s.hdr",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
188
				if(fexistcase(hdr))
189 190 191 192 193 194 195 196 197 198 199
					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]);
200
						bputs("\xc9\xcd");            /* use to start with \r\n */
201
						for(c=0;c<d;c++)
202 203
							outchar('\xcd');
						bputs("\xbb\r\n\xba ");
204 205 206
						sprintf(hdr,text[BoxHdrLib],i+1,cfg.lib[usrlib[i]]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
207
							outchar(' ');
208
						attr(cfg.color[clr_filelsthdrbox]);
209
						bputs("\xba\r\n\xba ");
210 211 212
						sprintf(hdr,text[BoxHdrDir],j+1,cfg.dir[dirnum]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
213
							outchar(' ');
214
						attr(cfg.color[clr_filelsthdrbox]);
215 216
						bputs("\xba\r\n\xba ");
						sprintf(hdr,text[BoxHdrFiles], file_count);
217 218
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
219
							outchar(' ');
220
						attr(cfg.color[clr_filelsthdrbox]);
221
						bputs("\xba\r\n\xc8\xcd");
222
						for(c=0;c<d;c++)
223 224
							outchar('\xcd');
						bputs("\xbc\r\n"); 
225 226
					}
				}
227
			}
228 229
			else {					/* short header */
				if(tofile) {
230
					c = fprintf(tofile,"\r\n(%u) %s ",i+1,cfg.lib[usrlib[i]]->sname) - 2;
231
				}
232 233 234
				else {
					sprintf(hdr,text[ShortHdrLib],i+1,cfg.lib[usrlib[i]]->sname);
					bputs("\r\1>\r\n");
235
					bputs(hdr); 
236
					c=bstrlen(hdr);
237
				}
238
				if(tofile) {
239
					c += fprintf(tofile,"(%u) %s",j+1,cfg.dir[dirnum]->lname);
240
				}
241 242
				else {
					sprintf(hdr,text[ShortHdrDir],j+1,cfg.dir[dirnum]->lname);
243
					bputs(hdr); 
244
					c+=bstrlen(hdr);
245
				}
246
				if(tofile) {
247
					fprintf(tofile,"\r\n%.*s\r\n", c, "----------------------------------------------------------------");
248
				}
249 250 251 252
				else {
					CRLF;
					attr(cfg.color[clr_filelstline]);
					while(c--)
rswindell's avatar
rswindell committed
253
						outchar('\xC4');
254 255 256 257
					CRLF; 
				} 
			} 
		}
258
		long currow = row;
259 260 261 262 263
		next=m;
		disp=1;
		if(mode&(FL_EXFIND|FL_VIEW)) {
			if(!found)
				bputs("\r\1>");
264 265 266
			if(!viewfile(f, INT_TO_BOOL(mode&FL_EXFIND))) {
				found = -1;
				break;
267
			}
268
			CRLF;
269
		}
270
		else if(tofile)
271
			listfiletofile(f, tofile);
272
		else if(mode&FL_FINDDESC)
273
			disp=listfile(f, dirnum, filespec, letter, longest);
274
		else
275
			disp=listfile(f, dirnum, nulstr, letter, longest);
276
		if(!disp && letter>'A') {
277
			next=m-1;
278 279
			letter--; 
		}
280 281
		else {
			disp=1;
282 283
			found++; 
		}
284
		if(sys_status&SS_ABORT) {
285 286
			found = -1;
			break;
287
		}
288 289 290 291
		if(mode&(FL_EXFIND|FL_VIEW))
			continue;
		if(useron.misc&BATCHFLAG && !tofile) {
			if(disp) {
292
				bf[letter-'A'] = f;
293
				file_row[letter-'A'] = currow;
294
			}
295
			m++;
296 297 298 299 300 301 302 303
			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;
304
				if((int)(i=batchflagprompt(&smb, bf, file_row, letter-'A'+1, file_count))<1) {
305
					if((int)i==-1)
306 307
						found = -1;
					break;
308
				}
309 310
				if(i==2) {
					next=anchor;
311 312
					found-=(letter-'A')+1; 
				}
313
				else if(i==3) {
314
					if((long)anchor-((letter-'A'+1))<0) {
315
						next=0;
316 317
						found=0; 
					}
318
					else {
319 320 321
						next=anchor-((letter-'A'+1));
						found-=letter-'A'+1; 
					} 
322
				}
323 324
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync();
325 326 327 328
				letter='A';	
			}
			else
				letter++; 
329
		}
330 331 332
		if(useron.misc&BATCHFLAG && !tofile
			&& lncntr>=rows-2) {
			lncntr=0;		/* defeat pause() */
333 334
			flagprompt=1; 
		}
335 336 337
		m=next;
		if(mode&FL_FINDDESC) continue;
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?') && m)
338 339
			break; 
	}
340

341 342 343
	freefiles(file_list, file_count);
	smb_close(&smb);
	return found;
344 345 346 347 348 349
}

/****************************************************************************/
/* Prints one file's information on a single line                           */
/* Return 1 if displayed, 0 otherwise										*/
/****************************************************************************/
350
bool sbbs_t::listfile(file_t* f, uint dirnum, const char *search, const char letter, size_t namelen)
351
{
352
	char	*ptr;
353 354
	bool	exist = true;
	char*	ext=NULL;
355
	char	path[MAX_PATH+1];
356
    int		i,j;
357
    off_t	cdt;
358
	int		size_attr=clr_filecdt;
359

360 361 362 363
	if(f->extdesc != NULL && *f->extdesc && (useron.misc&EXTDESC)) {
		ext = f->extdesc;
		if((useron.misc&BATCHFLAG) && lncntr+extdesclines(ext)>=rows-2 && letter!='A')
			return false;
364
	}
365

366
	cond_newline();
367
	attr(cfg.color[(f->hdr.attr & MSG_DELETE) ? clr_err : clr_filename]);
368 369 370 371 372
	char fname[SMB_FILEIDX_NAMELEN + 1];
	if(namelen < 12 || cols < 132)
		namelen = 12;
	else if(namelen > sizeof(fname) - 1)
		namelen = sizeof(fname) - 1;
Rob Swindell's avatar
Rob Swindell committed
373
	bprintf("%-*s", (int)namelen, format_filename(f->name, fname, namelen, /* pad: */TRUE));
374
	getfilepath(&cfg, f, path);
375

376 377
	if(f->extdesc != NULL && *f->extdesc && !(useron.misc&EXTDESC))
		outchar('+');
378
	else
379
		outchar(' '); 
380
	if(useron.misc&BATCHFLAG) {
381
		attr(curatr ^ HIGH);
382 383
		bprintf("%c",letter); 
	}
384 385 386 387
	cdt = f->cost;
	if(f->size == -1) {
		exist = false;
		size_attr = clr_err; 
388
	}
389 390 391 392 393 394 395 396
	else if((cfg.dir[dirnum]->misc & (DIR_FREE | DIR_FCHK)) == (DIR_FREE | DIR_FCHK))
		cdt = getfilesize(&cfg, f);
	char bytes[32];
	unsigned units = 1;
	do {
		byte_estimate_to_str(cdt, bytes, sizeof(bytes), units, /* precision: */1);
		units *= 1024;
	} while(strlen(bytes) > 6 && units < 1024 * 1024 * 1024);
397
	attr(cfg.color[size_attr]);
398
	if(useron.misc&BATCHFLAG) {
399
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {
400
			attr(curatr^(HIGH|BLINK));
401 402
			bputs("  FREE"); 
		}
403 404
		else 
			bprintf("%6s", bytes);
405
	}
406
	else {
407
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {  /* FREE file */
408
			attr(curatr^(HIGH|BLINK));
409 410
			bputs("   FREE"); 
		}
411 412
		else 
			bprintf("%7s", bytes);
413
	}
414
	if(exist)
415
		outchar(' ');
416 417 418
	else
		outchar('-');
	attr(cfg.color[clr_filedesc]);
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	if(ext == NULL) {
		char* fdesc = f->desc;
		SKIP_WHITESPACE(fdesc);
		if(fdesc == NULL || *fdesc == '\0')
			bputs(P_TRUNCATE, f->name);
		else if(search[0]) { /* high-light string in string */
			ptr = strcasestr(fdesc, search);
			if(ptr != NULL) {
				i=strlen(search);
				j=ptr - fdesc;
				bprintf("%.*s",j,fdesc);
				attr(cfg.color[clr_filedesc]^HIGH);
				bprintf("%.*s",i,fdesc+j);
				attr(cfg.color[clr_filedesc]);
				bprintf("%.*s",(int)strlen(fdesc)-(j+i),fdesc+j+i);
			}
		}
		else {
			bputs(P_TRUNCATE, fdesc);
439 440
		}
		CRLF; 
441
	} else {
442 443 444 445 446 447 448
		truncsp(ext);
		while(strncmp(ext, "\r\n", 2) == 0
			|| strnicmp(ext, "\001N", 2) == 0
			|| strnicmp(ext, "\0010", 2) == 0
			|| strnicmp(ext, "\001W", 2) == 0)
			ext += 2;
		putmsg(ext, P_INDENT | P_NOATCODES | P_CPM_EOF | P_TRUNCATE);
449
	}
450
	return true;
451 452 453 454 455 456 457
}

/****************************************************************************/
/* 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. 														*/
/****************************************************************************/
458
int sbbs_t::batchflagprompt(smb_t* smb, file_t** bf, ulong* row, uint total
459 460
							,long totalfiles)
{
461
	char	ch,str[256],*p,remcdt=0,remfile=0;
462
	int		c, d;
463
	char 	path[MAX_PATH + 1];
464
	uint	i,j,ml=0,md=0,udir,ulib;
465 466

	for(ulib=0;ulib<usrlibs;ulib++)
467
		if(usrlib[ulib]==cfg.dir[smb->dirnum]->lib)
468 469
			break;
	for(udir=0;udir<usrdirs[ulib];udir++)
470
		if(usrdir[ulib][udir]==smb->dirnum)
471 472 473 474 475 476
			break;

	CRLF;
	while(online) {
		bprintf(text[BatchFlagPrompt]
			,ulib+1
477
			,cfg.lib[cfg.dir[smb->dirnum]->lib]->sname
478
			,udir+1
479
			,cfg.dir[smb->dirnum]->sname
480
			,total, totalfiles);
481 482 483
		ch=getkey(K_UPPER);
		clearline();
		if(ch=='?') {
484
			menu("batflag");
485 486
			if(lncntr)
				pause();
487 488
			return(2); 
		}
489
		if(ch==text[YNQP][2] || sys_status&SS_ABORT)
490 491 492 493 494
			return(-1);
		if(ch=='S')
			return(0);
		if(ch=='P' || ch=='-')
			return(3);
495 496
		if(ch=='T') {
			useron.misc ^= EXTDESC;
497
			putuserrec(&cfg, useron.number, U_MISC, 0, ultoa(useron.misc,str,16));
498 499
			return 2;
		}
500
		if(ch=='B' || ch=='D') {    /* Flag for batch download */
501 502
			if(useron.rest&FLAG('D')) {
				bputs(text[R_Download]);
503 504
				return(2); 
			}
505
			if(total==1) {
506
				addtobatdl(bf[0]);
507
				if(ch=='D')
508
					start_batch_download();
509
				CRLF;
510 511
				return(2); 
			}
512 513 514 515
			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]);
516
			bputs(text[BatchDlFlags]);
517
			d=getstr(str, BF_MAX, K_NOCRLF);
518 519
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
520 521 522
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
523 524
			if(d > 0) { 	/* d is string length */
				strupr(str);
525 526 527
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
528
					if(batdn_total() >= cfg.max_batdn) {
529
						bprintf(text[BatchDlQueueIsFull],str+c);
530 531
						break; 
					}
532
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
533
//						f.dir=dirnum;
534
						p=strchr(str+c,' ');
535 536 537
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
538
							if(batdn_total() >= cfg.max_batdn) {
539
								bprintf(text[BatchDlQueueIsFull],str+c);
540 541
								break; 
							}
542 543
							if(filematch(bf[i]->name, str+c)) {
								addtobatdl(bf[i]); 
544 545 546
							} 
						} 
					}
547 548 549
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
550
						addtobatdl(bf[str[c]-'A']);
551
					} 
552
				}
553 554
				if(ch=='D')
					start_batch_download();
555
				CRLF;
556 557
				return(2); 
			}
558
			clearline();
559 560
			continue; 
		}
561 562 563

		if(ch=='E' || ch=='V') {    /* Extended Info */
			if(total==1) {
564
				if(!viewfile(bf[0], ch=='E'))
565
					return(-1);
566 567
				return(2); 
			}
568 569 570 571
			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]);
572
			bputs(text[BatchDlFlags]);
573
			d=getstr(str, BF_MAX, K_NOCRLF);
574 575
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
576 577 578
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
579 580
			if(d > 0) { 	/* d is string length */
				strupr(str);
581 582 583
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
584
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
585
//						f.dir=dirnum;
586
						p=strchr(str+c,' ');
587 588 589
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
590 591
							if(filematch(bf[i]->name, str+c)) {
								if(!viewfile(bf[i], ch=='E'))
592 593 594 595
									return(-1); 
							} 
						} 
					}
596 597 598
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
599 600 601
						if(!viewfile(bf[str[c]-'A'], ch=='E'))
							return(-1); 
					} 
602
				}
603
				cond_newline();
604 605
				return(2); 
			}
606
			clearline();
607 608
			continue; 
		}
609

610
		if((ch=='R' || ch=='M')     /* Delete or Move */
611
			&& !(useron.rest&FLAG('R'))
612
			&& (dir_op(smb->dirnum) || useron.exempt&FLAG('R'))) {
613 614
			if(total==1) {
				strcpy(str,"A");
615 616
				d=1; 
			}
617
			else {
618 619 620 621
				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]);
622
				bputs(text[BatchDlFlags]);
623
				d=getstr(str, BF_MAX, K_NOCRLF);
624 625
				clear_hotspots();
				mouse_hotspots = saved_hotspots;
626
			}
627 628 629
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
630 631 632 633
			if(d > 0) { 	/* d is string length */
				strupr(str);
				if(total > 1)
					newline();
634 635
				if(ch=='R') {
					if(noyes(text[RemoveFileQ]))
636
						return(2);
637 638 639 640
					remcdt = TRUE;
					remfile = TRUE;
					if(dir_op(smb->dirnum)) {
						remfile=!noyes(text[DeleteFileQ]);
641
						remcdt=!noyes(text[RemoveCreditsQ]);
642
					}
643
				}
644 645 646 647 648
				else if(ch=='M') {
					CRLF;
					for(i=0;i<usrlibs;i++)
						bprintf(text[MoveToLibLstFmt],i+1,cfg.lib[usrlib[i]]->lname);
					SYNC;
649
					bprintf(text[MoveToLibPrompt],cfg.dir[smb->dirnum]->lib+1);
650
					if((int)(ml=getnum(usrlibs))==-1)
651 652
						return(2);
					if(!ml)
653
						ml=cfg.dir[smb->dirnum]->lib;
654 655 656 657 658 659 660 661
					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]);
662
					if((int)(md=getnum(usrdirs[ml]))==-1)
663 664 665 666
						return(2);
					if(!md)
						md=usrdirs[ml]-1;
					else md--;
667 668
					CRLF; 
				}
669 670
				lncntr=0;
				for(c=0;c<d;c++) {
671
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
672
//						f.dir=dirnum;
673
						p=strchr(str+c,' ');
674 675 676
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
677
							if(filematch(bf[i]->name, str+c)) {
678
								if(ch=='R') {
679 680 681 682 683 684 685
									if(removefile(smb, bf[i])) {
										if(remfile) {
											if(remove(getfilepath(&cfg, bf[i], path)) != 0)
												errormsg(WHERE, ERR_REMOVE, path);
										}
										if(remcdt)
											removefcdt(bf[i]);
686 687
									}
								}
688
								else if(ch=='M')
689
									movefile(smb, bf[i], usrdir[ml][md]); 
690 691 692
							} 
						} 
					}
693 694 695
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
696
						file_t* f = bf[str[c]-'A'];
697
						if(ch=='R') {
698 699 700 701 702 703 704
							if(removefile(smb, f)) {
								if(remfile) {
									if(remove(getfilepath(&cfg, f, path)) != 0 && fexist(path))
										errormsg(WHERE, ERR_REMOVE, path);
								}
								if(remcdt)
									removefcdt(f);
705 706
							}
						}
707
						else if(ch=='M')
708 709
							movefile(smb, f, usrdir[ml][md]); 
					} 
710 711 712
				}
				return(2); 
			}
713
			clearline();
714 715
			continue; 
		}
716

717 718
		return(1); 
	}
719 720 721 722 723 724 725 726 727

	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                */
/****************************************************************************/
728
int sbbs_t::listfileinfo(uint dirnum, const char *filespec, long mode)
729
{
730
	char	str[MAX_PATH + 1],path[MAX_PATH + 1],dirpath[MAX_PATH + 1],done=0,ch;
731
	char 	tmp[512];
rswindell's avatar
rswindell committed
732
	int		error;
733 734
	int		found=0;
    uint	i,j;
735
    size_t	m;
736
    time_t	start,end,t;
737
    file_t*	f;
738
	struct	tm tm;
739
	smb_t	smb;
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	if(!smb_init_dir(&cfg, &smb, dirnum))
		return 0;
	if(smb_open_dir(&cfg, &smb, dirnum) != SMB_SUCCESS)
		return 0;

	size_t file_count = 0;
	file_t* file_list = loadfiles(&smb
		, filespec
		, /* time_t */0
		, file_detail_extdesc
		, (enum file_sort)cfg.dir[dirnum]->sort
		, &file_count);
	if(file_list == NULL || file_count < 1) {
		smb_close(&smb);
		free(file_list);
		return 0;
757
	}
758

759
	m=0;
760 761
	while(online && !done && m < file_count) {
		f = &file_list[m];
762 763 764 765 766
		if(mode==FI_REMOVE && dir_op(dirnum))
			action=NODE_SYSP;
		else action=NODE_LFIL;
		if(msgabort()) {
			found=-1;
767 768
			break; 
		}
769 770
		m++;
		if(mode==FI_OLD && f->hdr.last_downloaded > ns_time)
771
			continue;
772
		if((mode==FI_OLDUL || mode==FI_OLD) && f->hdr.when_written.time > ns_time)
773
			continue;
774 775
		curdirnum = dirnum;
		if(mode==FI_OFFLINE && getfilesize(&cfg, f) >= 0)
776
			continue;
777 778 779 780 781 782 783 784 785
		if(mode==FI_USERXFER) {
			str_list_t dest_user_list = strListSplitCopy(NULL, f->to_list, ",");
			char usernum[16];
			SAFEPRINTF(usernum, "%u", useron.number);
			int dest_user = strListFind(dest_user_list, usernum, /* case-sensitive: */true);
			strListFree(&dest_user_list);
			if(dest_user < 0)
				continue;
		}
786 787
		SAFECOPY(dirpath, cfg.dir[f->dir]->path);
		if((mode==FI_REMOVE) && (!dir_op(dirnum) && stricmp(f->from
788 789 790 791
			,useron.alias) && !(useron.exempt&FLAG('R'))))
			continue;
		found++;
		if(mode==FI_INFO) {
792 793 794 795 796 797 798 799 800 801 802
			switch(viewfile(f, true)) {
				case 0:
					done=1;
					found=-1;
					break;
				case -2:
					m--;
					if(m)
						m--;
					break;
			} 
803
		}
804 805 806
		else {
			showfileinfo(f, /* show_extdesc: */mode != FI_DOWNLOAD);
//			newline();
807
		}
808
		if(mode==FI_REMOVE || mode==FI_OLD || mode==FI_OLDUL
809 810
			|| mode==FI_OFFLINE) {
			SYNC;
811 812 813
//			CRLF;
			SAFECOPY(str, "VEQRNP\b-\r");
			if(dir_op(dirnum)) {
814
				mnemonics(text[SysopRemoveFilePrompt]);
815
				SAFECAT(str,"FMC");
<