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 28.4 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
	int		found=0,lastbat=0,disp;
	size_t	m=0;
	long	anchor=0,next;
	file_t* bf[BF_MAX];	/* bf is batch flagged files */
46
	ulong	file_row[BF_MAX];
47
	size_t	longest = 0;
48

49 50
	if(!smb_init_dir(&cfg, &smb, dirnum))
		return 0;
51
	if(mode&FL_ULTIME) {
52 53 54
		last_ns_time = now;
		if(!newfiles(&smb, ns_time))	// this is fast
			return 0;
55
	}
56 57 58 59 60 61 62 63 64 65 66 67 68 69
	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;
70
	}
71

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

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

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

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

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

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

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

361 362 363 364
	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;
365
	}
366

367
	cond_newline();
368
	attr(cfg.color[(f->hdr.attr & MSG_DELETE) ? clr_err : clr_filename]);
369 370 371 372 373
	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
374
	bprintf("%-*s", (int)namelen, format_filename(f->name, fname, namelen, /* pad: */TRUE));
375
	getfilepath(&cfg, f, path);
376

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

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	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);
440 441
		}
		CRLF; 
442
	} else {
443 444 445 446 447 448 449
		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);
450
	}
451
	return true;
452 453 454 455 456 457 458
}

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

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

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

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

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

720 721
		return(1); 
	}
722 723 724 725 726 727 728 729 730

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

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	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;
759
	}
760

761
	m=0;
762 763
	while(online && !done && m < file_count) {
		f = &file_list[m];
764 765 766 767 768
		if(mode==FI_REMOVE && dir_op(dirnum))
			action=NODE_SYSP;
		else action=NODE_LFIL;
		if(msgabort()) {
			found=-1;
769 770
			break; 
		}
771
		m++;
772 773
		if(f->hdr.attr & MSG_DELETE)
			continue;
774
		if(mode==FI_OLD && f->hdr.last_downloaded > ns_time)
775
			continue;
776
		if((mode==FI_OLDUL || mode==FI_OLD) && f->hdr.when_written.time > ns_time)
777
			continue;
778 779
		curdirnum = dirnum;
		if(mode==FI_OFFLINE && getfilesize(&cfg, f) >= 0)
780
			continue;
781 782 783 784 785 786 787 788 789
		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;
		}
790 791
		SAFECOPY(dirpath, cfg.dir[f->dir]->path);
		if((mode==FI_REMOVE) && (!dir_op(dirnum) && stricmp(f->from
792 793 794 795
			,useron.alias) && !(useron.exempt&FLAG('R'))))
			continue;
		found++;
		if(mode==FI_INFO) {
796 797 798 799 800 801 802 803 804 805 806
			switch(viewfile(f, true)) {
				case 0:
					done=1;
					found=-1;
					break;
				case -2:
					m--;
					if(m)
						m--;
					break;
			} 
807
		}
808 809 810
		else {
			showfileinfo(f, /* show_extdesc: */mode != FI_DOWNLOAD);
//			newline();