strcasestr.c revision 285830
1249259Sdim/* $NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $ */ 2249259Sdim 3249259Sdim/*- 4249259Sdim * Copyright (c) 1990, 1993 5249259Sdim * The Regents of the University of California. All rights reserved. 6249259Sdim * 7249259Sdim * This code is derived from software contributed to Berkeley by 8249259Sdim * Chris Torek. 9249259Sdim * 10249259Sdim * Redistribution and use in source and binary forms, with or without 11249259Sdim * modification, are permitted provided that the following conditions 12249259Sdim * are met: 13249259Sdim * 1. Redistributions of source code must retain the above copyright 14249259Sdim * notice, this list of conditions and the following disclaimer. 15249259Sdim * 2. Redistributions in binary form must reproduce the above copyright 16249259Sdim * notice, this list of conditions and the following disclaimer in the 17249259Sdim * documentation and/or other materials provided with the distribution. 18249259Sdim * 3. Neither the name of the University nor the names of its contributors 19249259Sdim * may be used to endorse or promote products derived from this software 20249259Sdim * without specific prior written permission. 21249259Sdim * 22249259Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25249259Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27249259Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32249259Sdim * SUCH DAMAGE. 33249259Sdim */ 34249259Sdim 35249259Sdim#if defined(LIBC_SCCS) && !defined(lint) 36249259Sdim__RCSID("$NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $"); 37249259Sdim__RCSID("$NetBSD: strncasecmp.c,v 1.2 2007/06/04 18:19:27 christos Exp $"); 38249259Sdim#endif /* LIBC_SCCS and not lint */ 39249259Sdim 40249259Sdim#include "file.h" 41249259Sdim 42249259Sdim#include <assert.h> 43249259Sdim#include <ctype.h> 44249259Sdim#include <string.h> 45249259Sdim 46249259Sdimstatic int 47249259Sdim_strncasecmp(const char *s1, const char *s2, size_t n) 48249259Sdim{ 49249259Sdim if (n != 0) { 50249259Sdim const unsigned char *us1 = (const unsigned char *)s1, 51249259Sdim *us2 = (const unsigned char *)s2; 52249259Sdim 53249259Sdim do { 54249259Sdim if (tolower(*us1) != tolower(*us2++)) 55249259Sdim return tolower(*us1) - tolower(*--us2); 56249259Sdim if (*us1++ == '\0') 57249259Sdim break; 58249259Sdim } while (--n != 0); 59249259Sdim } 60249259Sdim return 0; 61249259Sdim} 62249259Sdim 63249259Sdim/* 64249259Sdim * Find the first occurrence of find in s, ignore case. 65249259Sdim */ 66249259Sdimchar * 67249259Sdimstrcasestr(const char *s, const char *find) 68249259Sdim{ 69249259Sdim char c, sc; 70249259Sdim size_t len; 71249259Sdim 72249259Sdim if ((c = *find++) != 0) { 73249259Sdim c = tolower((unsigned char)c); 74249259Sdim len = strlen(find); 75249259Sdim do { 76249259Sdim do { 77249259Sdim if ((sc = *s++) == 0) 78249259Sdim return (NULL); 79249259Sdim } while ((char)tolower((unsigned char)sc) != c); 80249259Sdim } while (_strncasecmp(s, find, len) != 0); 81249259Sdim s--; 82249259Sdim } 83249259Sdim return (char *)(intptr_t)(s); 84249259Sdim} 85249259Sdim