/* $NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $ */12/*-3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#if defined(LIBC_SCCS) && !defined(lint)35__RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $");36__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $");37#endif /* LIBC_SCCS and not lint */3839#include "file.h"4041#include <assert.h>42#include <ctype.h>43#include <string.h>4445static int46_strncasecmp(const char *s1, const char *s2, size_t n)47{48if (n != 0) {49const unsigned char *us1 = (const unsigned char *)s1,50*us2 = (const unsigned char *)s2;5152do {53if (tolower(*us1) != tolower(*us2++))54return tolower(*us1) - tolower(*--us2);55if (*us1++ == '\0')56break;57} while (--n != 0);58}59return 0;60}6162/*63* Find the first occurrence of find in s, ignore case.64*/65char *66strcasestr(const char *s, const char *find)67{68char c, sc;69size_t len;7071if ((c = *find++) != 0) {72c = tolower((unsigned char)c);73len = strlen(find);74do {75do {76if ((sc = *s++) == 0)77return (NULL);78} while ((char)tolower((unsigned char)sc) != c);79} while (_strncasecmp(s, find, len) != 0);80s--;81}82return (char *)(intptr_t)(s);83}848586