Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!panda!teddy!jpn From: jpn@teddy.UUCP (John P. Nelson) Newsgroups: comp.sys.ibm.pc Subject: Re: 2 bugs in Turbo C Message-ID: <4131@teddy.UUCP> Date: Mon, 22-Jun-87 18:35:23 EDT Article-I.D.: teddy.4131 Posted: Mon Jun 22 18:35:23 1987 Date-Received: Tue, 23-Jun-87 05:42:38 EDT References: <2879@zen.berkeley.edu> <3320022@hpsrlc.HP.COM> Reply-To: jpn@teddy.UUCP (John P. Nelson) Organization: GenRad, Inc., Concord, Mass. Lines: 49 >> I have encountered two bugs in Turbo C that are worth mentioning: >> >> (1) strstr() has problems. And here is a replacement, source code. char *strstr (keystr, targetstr) char *keystr, *targetstr; /* Features of Operation: - If keystr or targetstr is NULL, a NULL pointer (failure) is returned (i.e., NULL pointer input is handled gracefully). - If keystr or targetstr contains only a NULL character, a NULL pointer (failure) is returned (i.e., NULL's are not considered characters for the purpose of this function -- they are considered string terminators). - The search terminates successfully when '\0' of keystr is encountered before (or at the same time) as '\0' of targetstr. The search terminates unsuccessfully when '\0' of targetstr is reached before '\0' of keystr. */ { register unsigned short i; if (keystr == (char *) 0 || targetstr == (char *) 0) return ((char *) 0); if (*keystr == '\0' || *targetstr == '\0') return ((char *) 0); do { for (i = 0; *(keystr+i) && (*(keystr+i) == *(targetstr+i)); i++); if ( !(*(keystr+i)) ) return (targetstr); } while (*(targetstr++)); return ((char *) 0); } As you can see, this replacement handles NULL pointer input gracefully, is completely iterative and uses no calls to other library routines (for speed). It's also 4 bytes _shorter_ than the original Borland version. And, as far as I've been able to determine, it works! Brad Paulsen: Compuserve 76703,1005