145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * strcasecmp() implementation for systems that don't have it or stricmp()
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * or strcmpi().
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (c) 1987, 1993
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *      The Regents of the University of California.  All rights reserved.
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 3. Neither the name of the University nor the names of its contributors
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    may be used to endorse or promote products derived from this software
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    without specific prior written permission.
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUCH DAMAGE.
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "util.h"
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef USE_OUR_OWN_STRCASECMP
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm__strcasecmp
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm__strncasecmp
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#if defined(LIBC_SCCS) && !defined(lint)
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic char sccsid[] = "@(#)strcasecmp.c        8.1 (Berkeley) 6/4/93";
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif /* LIBC_SCCS and not lint */
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <ctype.h>
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm__strcasecmp(const char *s1, const char *s2)
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_STRCASECMP
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return strcasecmp(s1, s2);
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRICMP
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return stricmp(s1, s2);
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE__STRICMP
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return _stricmp(s1, s2);
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRCMPI
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return strcmpi(s1, s2);
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        const unsigned char
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        *us1 = (const unsigned char *)s1,
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        *us2 = (const unsigned char *)s2;
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        while (tolower(*us1) == tolower(*us2++))
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                if (*us1++ == '\0')
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        return (0);
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return (tolower(*us1) - tolower(*--us2));
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm__strncasecmp(const char *s1, const char *s2, size_t n)
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef HAVE_STRCASECMP
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return strncasecmp(s1, s2, n);
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRICMP
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return strnicmp(s1, s2, n);
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE__STRNICMP
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return _strnicmp(s1, s2, n);
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#elif HAVE_STRCMPI
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return strncmpi(s1, s2, n);
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        const unsigned char
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        *us1 = (const unsigned char *)s1,
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        *us2 = (const unsigned char *)s2;
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (n != 0) {
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                do {
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        if (tolower(*us1) != tolower(*us2++))
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                                return (tolower(*us1) - tolower(*--us2));
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        if (*us1++ == '\0')
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                                break;
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                } while (--n != 0);
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return (0);
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
95