1284194Sdelphij/* $NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $ */ 2284194Sdelphij 3284194Sdelphij/*- 4284194Sdelphij * Copyright (c) 1990, 1993 5284194Sdelphij * The Regents of the University of California. All rights reserved. 6284194Sdelphij * 7284194Sdelphij * This code is derived from software contributed to Berkeley by 8284194Sdelphij * Chris Torek. 9284194Sdelphij * 10284194Sdelphij * Redistribution and use in source and binary forms, with or without 11284194Sdelphij * modification, are permitted provided that the following conditions 12284194Sdelphij * are met: 13284194Sdelphij * 1. Redistributions of source code must retain the above copyright 14284194Sdelphij * notice, this list of conditions and the following disclaimer. 15284194Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 16284194Sdelphij * notice, this list of conditions and the following disclaimer in the 17284194Sdelphij * documentation and/or other materials provided with the distribution. 18284194Sdelphij * 3. Neither the name of the University nor the names of its contributors 19284194Sdelphij * may be used to endorse or promote products derived from this software 20284194Sdelphij * without specific prior written permission. 21284194Sdelphij * 22284194Sdelphij * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23284194Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24284194Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25284194Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26284194Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27284194Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28284194Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29284194Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30284194Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31284194Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32284194Sdelphij * SUCH DAMAGE. 33284194Sdelphij */ 34284194Sdelphij 35284194Sdelphij#if defined(LIBC_SCCS) && !defined(lint) 36284194Sdelphij__RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $"); 37284194Sdelphij__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); 38284194Sdelphij#endif /* LIBC_SCCS and not lint */ 39284194Sdelphij 40284194Sdelphij#include "file.h" 41284194Sdelphij 42284194Sdelphij#include <assert.h> 43284194Sdelphij#include <ctype.h> 44284194Sdelphij#include <string.h> 45284194Sdelphij 46284194Sdelphijstatic int 47284194Sdelphij_strncasecmp(const char *s1, const char *s2, size_t n) 48284194Sdelphij{ 49284194Sdelphij if (n != 0) { 50284194Sdelphij const unsigned char *us1 = (const unsigned char *)s1, 51284194Sdelphij *us2 = (const unsigned char *)s2; 52284194Sdelphij 53284194Sdelphij do { 54284194Sdelphij if (tolower(*us1) != tolower(*us2++)) 55284194Sdelphij return tolower(*us1) - tolower(*--us2); 56284194Sdelphij if (*us1++ == '\0') 57284194Sdelphij break; 58284194Sdelphij } while (--n != 0); 59284194Sdelphij } 60284194Sdelphij return 0; 61284194Sdelphij} 62284194Sdelphij 63284194Sdelphij/* 64284194Sdelphij * Find the first occurrence of find in s, ignore case. 65284194Sdelphij */ 66284194Sdelphijchar * 67284194Sdelphijstrcasestr(const char *s, const char *find) 68284194Sdelphij{ 69284194Sdelphij char c, sc; 70284194Sdelphij size_t len; 71284194Sdelphij 72284194Sdelphij if ((c = *find++) != 0) { 73284194Sdelphij c = tolower((unsigned char)c); 74284194Sdelphij len = strlen(find); 75284194Sdelphij do { 76284194Sdelphij do { 77284194Sdelphij if ((sc = *s++) == 0) 78284194Sdelphij return (NULL); 79284194Sdelphij } while ((char)tolower((unsigned char)sc) != c); 80284194Sdelphij } while (_strncasecmp(s, find, len) != 0); 81284194Sdelphij s--; 82284194Sdelphij } 83284194Sdelphij return (char *)(intptr_t)(s); 84284194Sdelphij} 85