diff --git a/src/include/string.h b/src/include/string.h index c3359807..745be2d6 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -1,4 +1,4 @@ -/* $OpenBSD: string.h,v 1.14 2004/06/20 17:47:07 avsm Exp $ */ +/* $OpenBSD: string.h,v 1.15 2005/03/30 03:04:16 deraadt Exp $ */ /* $NetBSD: string.h,v 1.6 1994/10/26 00:56:30 cgd Exp $ */ /*- @@ -101,6 +101,7 @@ void *memccpy(void *, const void *, int, size_t) char *rindex(const char *, int); int strcasecmp(const char *, const char *); char *strdup(const char *); +char *strcasestr(const char *, const char *); size_t strlcat(char *, const char *, size_t) __attribute__ ((__bounded__(__string__,1,3))); size_t strlcpy(char *, const char *, size_t) diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 902cf83c..81be9cea 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc @@ -1,11 +1,11 @@ -# $OpenBSD: Makefile.inc,v 1.13 2004/05/03 19:56:08 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.14 2005/03/30 03:04:19 deraadt Exp $ # string sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string -SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ - strerror_r.c strlcat.c strmode.c strsignal.c strtok.c strxfrm.c \ - __strsignal.c +SRCS+= bm.c memccpy.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ + strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ + strxfrm.c __strsignal.c # machine-dependent net sources # m-d Makefile.inc must include sources for: @@ -148,5 +148,6 @@ MLINKS+=strcat.3 strncat.3 MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcpy.3 strncpy.3 MLINKS+=strlcpy.3 strlcat.3 +MLINKS+=strstr.3 strcasestr.3 MLINKS+=strtok.3 strtok_r.3 MLINKS+=strerror.3 strerror_r.3 diff --git a/src/lib/libc/string/strcasestr.c b/src/lib/libc/string/strcasestr.c new file mode 100644 index 00000000..075e6f19 --- /dev/null +++ b/src/lib/libc/string/strcasestr.c @@ -0,0 +1,64 @@ +/* $OpenBSD: strcasestr.c,v 1.1 2005/03/30 03:04:19 deraadt Exp $ */ +/* $NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $ */ + +/*- + * 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. 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strcasestr.c,v 1.1 2005/03/30 03:04:19 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * Find the first occurrence of find in s, ignore case. + */ +char * +strcasestr(const char *s, const char *find) +{ + char c, sc; + 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/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3 index 64396e78..2c8fa188 100644 --- a/src/lib/libc/string/strstr.3 +++ b/src/lib/libc/string/strstr.3 @@ -29,18 +29,20 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strstr.3,v 1.7 2005/02/25 03:12:44 cloder Exp $ +.\" $OpenBSD: strstr.3,v 1.8 2005/03/30 03:04:19 deraadt Exp $ .\" .Dd June 29, 1991 .Dt STRSTR 3 .Os .Sh NAME -.Nm strstr +.Nm strstr , strcasestr .Nd locate a substring in a string .Sh SYNOPSIS .Fd #include .Ft char * .Fn strstr "const char *big" "const char *little" +.Ft char * +.Fn strcasestr "const char *big" "const char *little" .Sh DESCRIPTION The .Fn strstr @@ -48,23 +50,27 @@ function locates the first occurrence of the NUL-terminated string .Fa little in the NUL-terminated string .Fa big . +.Pp +The +.Fn strcasestr +function is similar to +.Fn strstr +but ignores the case of both strings. +.Pp If .Fa little -is the empty string, -.Fn strstr -returns -.Fa big ; +is an empty string, +.Fa big +is returned; if .Fa little occurs nowhere in .Fa big , -.Fn strstr -returns -.Dv NULL ; -otherwise -.Fn strstr -returns a pointer to the first character of the first occurrence of -.Fa little . +.Dv NULL +is returned; +otherwise a pointer to the first character of the first occurrence of +.Fa little +is returned. .Sh SEE ALSO .Xr memchr 3 , .Xr strchr 3 ,