[comp.lang.c] undefined: strcasecmp?

dah@esfenn.UUCP (Darrin Hyrup) (08/24/89)

In article <2922@amelia.nas.nasa.gov> dell@amelia.nas.nasa.gov (Thomas E. Dell) writes:
>In article <475@esfenn.UUCP> dah@.UUCP (Darrin Hyrup) writes:
>>int cstrcmp(s1, s2)     /* also known as strcasecmp() */
>>register char *s1, *s2;
>>{
>>   while(tolower(*s1) == tolower(*s2++))
>>      if(*s1++ == '\0')
>>         return(0);
>>   return(tolower(*s1) - tolower(*--s2));
>>}
>
>Not quite.. a number of versions of Unix have the result of 
>tolower(c) undefined (read: wrong) if c is not uppercase. This
>is because #define tolower(c) ((c) + ' ') or something similar 
>is used. If you #define your own, remember you can only have ONE
>(c) in the definition or you will have side effects problems,
>s2 being incremented twice or whatnot.

Well, thats true, but I thought the ANSI definition of tolower() was a
macro or function tolower(c) that would return the lowercase value of the
character c if it was uppercase, or return c unchanged if not. Not to say
that I don't agree with you, since I've seen some brain dead C compilers out
there, but that tends to be the exception rather than the rule.

But, assuming thats the case, you can use a macro something like this:

#define tolower(c) ((c)|0x20)

to do the converting, assuming that (c) is a alphabetic character to begin
with, which should be able to be found with a isalpha() macro/function.
(And also assuming that the system uses standard ASCII not EBCDIC or
something like that!)

The original version I wrote for this task was written with a lookup table
for each ascii character, but I found this version to be faster and more
portable assuming a half intelligent C compiler.  I've tested and found this
routine to work on everything from Microsoft C on a PC to GEIS C under Mark III
with no trouble at all.  All depends on how close to ANSI the compiler is.

I do have the table driven one if that one is preferred, but the one given will
work for most applications. Check the definition of your tolower() function
to be sure.

                   Darrin Hyrup

 
/* Darrin A. Hyrup   -=-   MagikSoft, Inc. \XX| "Speak little and well, if *\
** -----------------------------------------\X|  you wish to be considered **
\* {pacbell,rencon,cpro}!esfenn!sorinc!magik \|  as possessing merit."     */