1*6d38604fSBaptiste Daroussin /* $Id: compat_strcasestr.c,v 1.5 2020/06/15 01:37:15 schwarze Exp $ */
261d06d6bSBaptiste Daroussin /* $NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $ */
361d06d6bSBaptiste Daroussin
461d06d6bSBaptiste Daroussin /*-
561d06d6bSBaptiste Daroussin * Copyright (c) 1990, 1993
661d06d6bSBaptiste Daroussin * The Regents of the University of California. All rights reserved.
761d06d6bSBaptiste Daroussin *
861d06d6bSBaptiste Daroussin * This code is derived from software contributed to Berkeley by
961d06d6bSBaptiste Daroussin * Chris Torek.
1061d06d6bSBaptiste Daroussin *
1161d06d6bSBaptiste Daroussin * Redistribution and use in source and binary forms, with or without
1261d06d6bSBaptiste Daroussin * modification, are permitted provided that the following conditions
1361d06d6bSBaptiste Daroussin * are met:
1461d06d6bSBaptiste Daroussin * 1. Redistributions of source code must retain the above copyright
1561d06d6bSBaptiste Daroussin * notice, this list of conditions and the following disclaimer.
1661d06d6bSBaptiste Daroussin * 2. Redistributions in binary form must reproduce the above copyright
1761d06d6bSBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the
1861d06d6bSBaptiste Daroussin * documentation and/or other materials provided with the distribution.
1961d06d6bSBaptiste Daroussin * 3. Neither the name of the University nor the names of its contributors
2061d06d6bSBaptiste Daroussin * may be used to endorse or promote products derived from this software
2161d06d6bSBaptiste Daroussin * without specific prior written permission.
2261d06d6bSBaptiste Daroussin *
2361d06d6bSBaptiste Daroussin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461d06d6bSBaptiste Daroussin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561d06d6bSBaptiste Daroussin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661d06d6bSBaptiste Daroussin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761d06d6bSBaptiste Daroussin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861d06d6bSBaptiste Daroussin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961d06d6bSBaptiste Daroussin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061d06d6bSBaptiste Daroussin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161d06d6bSBaptiste Daroussin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261d06d6bSBaptiste Daroussin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361d06d6bSBaptiste Daroussin * SUCH DAMAGE.
3461d06d6bSBaptiste Daroussin */
35*6d38604fSBaptiste Daroussin #include "config.h"
3661d06d6bSBaptiste Daroussin
3761d06d6bSBaptiste Daroussin #include <sys/types.h>
3861d06d6bSBaptiste Daroussin #include <ctype.h>
3961d06d6bSBaptiste Daroussin #include <string.h>
4061d06d6bSBaptiste Daroussin
4161d06d6bSBaptiste Daroussin #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
4261d06d6bSBaptiste Daroussin
4361d06d6bSBaptiste Daroussin /*
4461d06d6bSBaptiste Daroussin * Find the first occurrence of find in s, ignore case.
4561d06d6bSBaptiste Daroussin */
4661d06d6bSBaptiste Daroussin char *
strcasestr(const char * s,const char * find)4761d06d6bSBaptiste Daroussin strcasestr(const char *s, const char *find)
4861d06d6bSBaptiste Daroussin {
4961d06d6bSBaptiste Daroussin char c, sc;
5061d06d6bSBaptiste Daroussin size_t len;
5161d06d6bSBaptiste Daroussin
5261d06d6bSBaptiste Daroussin if ((c = *find++) != 0) {
5361d06d6bSBaptiste Daroussin c = tolower((unsigned char)c);
5461d06d6bSBaptiste Daroussin len = strlen(find);
5561d06d6bSBaptiste Daroussin do {
5661d06d6bSBaptiste Daroussin do {
5761d06d6bSBaptiste Daroussin if ((sc = *s++) == 0)
5861d06d6bSBaptiste Daroussin return (NULL);
5961d06d6bSBaptiste Daroussin } while ((char)tolower((unsigned char)sc) != c);
6061d06d6bSBaptiste Daroussin } while (strncasecmp(s, find, len) != 0);
6161d06d6bSBaptiste Daroussin s--;
6261d06d6bSBaptiste Daroussin }
6361d06d6bSBaptiste Daroussin return __UNCONST(s);
6461d06d6bSBaptiste Daroussin }
65