Browse Source

strcasestr(3), a case-insensitive version of strstr(3). already in netbsd

and freebsd, apparently written by torek, the man we never see anymore;
submitted by jcs, but he is not around right now for the libc major crank
so i sneak it in for him
OPENBSD_3_8
deraadt 19 years ago
parent
commit
3f1febb4bc
4 changed files with 90 additions and 18 deletions
  1. +2
    -1
      src/include/string.h
  2. +5
    -4
      src/lib/libc/string/Makefile.inc
  3. +64
    -0
      src/lib/libc/string/strcasestr.c
  4. +19
    -13
      src/lib/libc/string/strstr.3

+ 2
- 1
src/include/string.h View File

@ -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)


+ 5
- 4
src/lib/libc/string/Makefile.inc View File

@ -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

+ 64
- 0
src/lib/libc/string/strcasestr.c View File

@ -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 <ctype.h>
#include <string.h>
/*
* 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);
}

+ 19
- 13
src/lib/libc/string/strstr.3 View File

@ -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 <string.h>
.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 ,


Loading…
Cancel
Save