From 2106e62962d4819266eea6bf54956552debfe8f2 Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Sun, 6 Jun 2021 20:07:01 -0700
Subject: [PATCH] Modernize finduser() a bit

Skip any prepended white-space in the passed name to find.
Use better variable names (and length) for the character strings.
Use matchusername() instead of strcmp(); matchusername() follows the liberal user-id/alias/name matching algorithm.
Use strcasestr() instead of strstr(), so no copy/modification (upper-casing) of passed name is now required.
When 'Q'uitting the search, set the SS_ABORT flag.
---
 src/sbbs3/data.cpp | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/sbbs3/data.cpp b/src/sbbs3/data.cpp
index ec2af9d6f5..d239036a5a 100644
--- a/src/sbbs3/data.cpp
+++ b/src/sbbs3/data.cpp
@@ -31,26 +31,26 @@
 /* Returns the number of the matched user or 0 if unsuccessful				*/
 /* Called from functions main_sec, useredit and readmailw					*/
 /****************************************************************************/
-uint sbbs_t::finduser(const char* instr, bool silent_failure)
+uint sbbs_t::finduser(const char* name, bool silent_failure)
 {
 	int file,i;
-	char str[128],str2[256],str3[256],ynq[25],c,pass=1;
+	char buf[256],ynq[25],c,pass=1;
+	char path[MAX_PATH + 1];
 	long l,length;
 	FILE *stream;
 
-	i=atoi(instr);
+	SKIP_WHITESPACE(name);
+	i=atoi(name);
 	if(i>0) {
-		username(&cfg, i,str2);
-		if(str2[0] && strcmp(str2,"DELETED USER"))
+		username(&cfg, i, buf);
+		if(buf[0] && strcmp(buf,"DELETED USER"))
 			return(i); 
 	}
-	SAFECOPY(str,instr);
-	strupr(str);
-	SAFEPRINTF(str3,"%suser/name.dat",cfg.data_dir);
-	if(flength(str3)<1L)
+	SAFEPRINTF(path,"%suser/name.dat",cfg.data_dir);
+	if(flength(path)<1L)
 		return(0);
-	if((stream=fnopen(&file,str3,O_RDONLY))==NULL) {
-		errormsg(WHERE,ERR_OPEN,str3,O_RDONLY);
+	if((stream=fnopen(&file,path,O_RDONLY))==NULL) {
+		errormsg(WHERE,ERR_OPEN,path,O_RDONLY);
 		return(0); 
 	}
 	SAFEPRINTF(ynq,"%.3s",text[YNQP]);
@@ -59,20 +59,18 @@ uint sbbs_t::finduser(const char* instr, bool silent_failure)
 		fseek(stream,0L,SEEK_SET);	/* seek to beginning for each pass */
 		for(l=0;l<length;l+=LEN_ALIAS+2) {
 			if(!online) break;
-			fread(str2,LEN_ALIAS+2,1,stream);
+			fread(buf,LEN_ALIAS+2,1,stream);
 			for(c=0;c<LEN_ALIAS;c++)
-				if(str2[c]==ETX) break;
-			str2[c]=0;
+				if(buf[c]==ETX) break;
+			buf[c]=0;
 			if(!c)		/* deleted user */
 				continue;
-			SAFECOPY(str3,str2);
-			strupr(str2);
-			if(pass==1 && !strcmp(str,str2)) {
+			if(pass==1 && matchusername(&cfg, name, buf)) {
 				fclose(stream);
 				return((l/(LEN_ALIAS+2))+1); 
 			}
-			if(pass==2 && strstr(str2,str)) {
-				bprintf(text[DoYouMeanThisUserQ],str3
+			if(pass==2 && strcasestr(buf, name)) {
+				bprintf(text[DoYouMeanThisUserQ], buf
 					,(uint)(l/(LEN_ALIAS+2))+1);
 				c=(char)getkeys(ynq,0);
 				if(sys_status&SS_ABORT) {
@@ -85,6 +83,7 @@ uint sbbs_t::finduser(const char* instr, bool silent_failure)
 				}
 				if(c==text[YNQP][2]) {
 					fclose(stream);
+					sys_status |= SS_ABORT;
 					return(0); 
 				} 
 			} 
-- 
GitLab