William Krick wrote:
> To make the code compatible with older C compilers, I had to move the
> declaration of c1 & c2 up to the top.
You're not serious, surely. You have C compilers that don't allow
declarations in nested blocks? This is not a recent feature.
> I also changed it from while(1)
> to for(;
because the while was throwing a warning.
Well, OK I suppose; but I'd be deeply suspicious of such a warning
myself (perhaps I used while(1) more than usual).
> However, while
> this code works great in most cases, it doesn't handle NULL strings and
> just blows up.
There are no such things as NULL strings. There are null (empty) strings,
and there are null pointers, which are not any kind of string. If you mean
that it doesn't handle null pointer arguments, well no, it doesn't; it
does case-insensitive string compare, and NULL isn't a string. The user
should not call it with null pointer arguments.
(I can't think of a sensible answer to return for any null argument. If
you really really want to guard for this case, just add to the top
if (s1 == 0 || s2 == 0) return WHATEVERYOUWANT;
or wrap it as
if (s1 && s2) THEPREVIOUSCODE
else return WHATEVERYOUWANT;
)
> int strcasecmp( const char *s1, const char *s2 )
> {
> int c1, c2;
> for(;
> {
> c1 = tolower( (unsigned char) *s1++ );
> c2 = tolower( (unsigned char) *s2++ );
> if (c1 == 0 || c1 != c2)
> return c1 - c2;
> }
> }
--
Chris "one-track" Dollin
Capability does not imply necessity.