| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Find a word |
G'Day All,
-=> Quoting Frank Adam to All <=-
I 've made a couple of booboos here, there is a potential memory leak,
and the strcpy is an unnecessary overhead.
FA> ** char* In_Str(char *buffer,char *word)
FA> ** Public domain by Frank Adam, and so on...
FA> char *txt;char *pat;char *ptr;
FA> txt = (char*) malloc(strlen(buf)+1);
FA> pat = (char*) malloc(strlen(wrd)+1);
FA> if(!txt || !pat) return NULL;
This would be a leak if(txt && !pat).
FA> strcpy(txt,buf); strcpy(pat,wrd); /* leave the originals alone */
This was wasteful..but easy :-)
So here is the revision..
char* In_Str(char *buf,char *wrd) /* case insensitive strstr() */
{
char *txt;char *pat;char *ptr,*ptr1;
txt = (char*) malloc(strlen(buf)+1);
if(!txt) return NULL;
pat = (char*) malloc(strlen(wrd)+1);
if(!pat) {free(txt); return NULL;}
ptr = buf;ptr1 = txt; while(*ptr++) *ptr1++ = tolower(*ptr);
ptr = wrd;ptr1 = pat; while(*ptr++) *ptr1++ = tolower(*ptr);
ptr = strstr(txt,pat);
if(ptr) ptr = buf+(ptr-txt);
free(txt);free(pat);
return ptr;
}
L8r Frank (fadam{at}ozemail.com.au).
___ Blue Wave/DOS v2.21
--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413 SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1 @PATH: 635/544 50/99 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.