From fc83bdab6643a27ed8919dabe328d8ffcffe86a6 Mon Sep 17 00:00:00 2001 From: ache <ache@FreeBSD.org> Date: Wed, 10 Oct 2001 02:17:35 +0000 Subject: Implement strcasestr() which many others (f.e. Linux) already have. --- lib/libc/string/Makefile.inc | 3 +- lib/libc/string/strcasestr.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ lib/libc/string/strstr.3 | 10 ++++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 lib/libc/string/strcasestr.c diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index dcd2dfd..54903e3 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -10,7 +10,7 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c index.c memccpy.c memchr.c memcmp.c \ memcpy.c memmove.c memset.c rindex.c strcasecmp.c strcat.c strchr.c \ strcmp.c strcoll.c strcpy.c strcspn.c strdup.c strerror.c \ strlcat.c strlcpy.c strlen.c strmode.c strncat.c strncmp.c strncpy.c \ - strnstr.c \ + strcasestr.c strnstr.c \ strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \ strxfrm.c swab.c wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c \ wcslcat.c wcslcpy.c wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c \ @@ -37,6 +37,7 @@ MLINKS+=strcpy.3 strncpy.3 MLINKS+=strerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 MLINKS+=strlcpy.3 strlcat.3 MLINKS+=strtok.3 strtok_r.3 +MLINKS+=strstr.3 strcasestr.3 MLINKS+=strstr.3 strnstr.3 MLINKS+=wmemchr.3 wmemcmp.3 wmemchr.3 wmemcpy.3 \ wmemchr.3 wmemmove.3 wmemchr.3 wmemset.3 \ diff --git a/lib/libc/string/strcasestr.c b/lib/libc/string/strcasestr.c new file mode 100644 index 0000000..b56c63b --- /dev/null +++ b/lib/libc/string/strcasestr.c @@ -0,0 +1,68 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static const char rcsid[] = + "$FreeBSD$"; +#endif + +#include <sys/cdefs.h> +#include <ctype.h> +#include <string.h> + +/* + * Find the first occurrence of find in s, ignore case. + */ +char * +strcasestr(s, find) + register const char *s, *find; +{ + register char c, sc; + register size_t len; + + if ((c = *find++) != 0) { + c = tolower((unsigned char)c); + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while ((char)tolower((unsigned char)sc) != c); + } while (strncasecmp(s, find, len) != 0); + s--; + } + return ((char *)s); +} diff --git a/lib/libc/string/strstr.3 b/lib/libc/string/strstr.3 index c8aca32..d71537d 100644 --- a/lib/libc/string/strstr.3 +++ b/lib/libc/string/strstr.3 @@ -41,7 +41,7 @@ .Dt STRSTR 3 .Os .Sh NAME -.Nm strstr , strnstr +.Nm strstr , strcasestr , strnstr .Nd locate a substring in a string .Sh LIBRARY .Lb libc @@ -50,6 +50,8 @@ .Ft char * .Fn strstr "const char *big" "const char *little" .Ft char * +.Fn strcasestr "const char *big" "const char *little" +.Ft char * .Fn strnstr "const char *big" "const char *little" "size_t len" .Sh DESCRIPTION The @@ -61,6 +63,12 @@ in the null-terminated string .Fa big . .Pp The +.Fn strcasestr +function is similar to +.Fn strstr +but ignores the case of both strings. +.Pp +The .Fn strnstr function locates the first occurrence of the null-terminated string -- cgit v1.1