xref: /illumos-gate/usr/src/lib/libc/port/gen/strstr.c (revision 6a634c9d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stddef.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * strstr() locates the first occurrence in the string as1 of
367c478bd9Sstevel@tonic-gate  * the sequence of characters (excluding the terminating null
37*23a1cceaSRoger A. Faulkner  * character) in the string as2.  strstr() returns a pointer
387c478bd9Sstevel@tonic-gate  * to the located string, or a null pointer if the string is
39*23a1cceaSRoger A. Faulkner  * not found.  If as2 is empty, the function returns as1.
407c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate char *
strstr(const char * as1,const char * as2)437c478bd9Sstevel@tonic-gate strstr(const char *as1, const char *as2)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	const char *s1, *s2;
467c478bd9Sstevel@tonic-gate 	const char *tptr;
477c478bd9Sstevel@tonic-gate 	char c;
497c478bd9Sstevel@tonic-gate 	s1 = as1;
507c478bd9Sstevel@tonic-gate 	s2 = as2;
527c478bd9Sstevel@tonic-gate 	if (s2 == NULL || *s2 == '\0')
537c478bd9Sstevel@tonic-gate 		return ((char *)s1);
54*23a1cceaSRoger A. Faulkner 
557c478bd9Sstevel@tonic-gate 	c = *s2;
56*23a1cceaSRoger A. Faulkner 	while (*s1 != '\0') {
57*23a1cceaSRoger A. Faulkner 		if (c == *s1++) {
58*23a1cceaSRoger A. Faulkner 			tptr = s1;
59*23a1cceaSRoger A. Faulkner 			while ((c = *++s2) == *s1++ && c != '\0')
60*23a1cceaSRoger A. Faulkner 				continue;
61*23a1cceaSRoger A. Faulkner 			if (c == '\0')
62*23a1cceaSRoger A. Faulkner 				return ((char *)tptr - 1);
63*23a1cceaSRoger A. Faulkner 			s1 = tptr;
64*23a1cceaSRoger A. Faulkner 			s2 = as2;
65*23a1cceaSRoger A. Faulkner 			c = *s2;
66*23a1cceaSRoger A. Faulkner 		}
67*23a1cceaSRoger A. Faulkner 	}
69*23a1cceaSRoger A. Faulkner 	return (NULL);
70*23a1cceaSRoger A. Faulkner }
71*23a1cceaSRoger A. Faulkner 
72*23a1cceaSRoger A. Faulkner /*
73*23a1cceaSRoger A. Faulkner  * strnstr() locates the first occurrence in the string as1 of
74*23a1cceaSRoger A. Faulkner  * the sequence of characters (excluding the terminating null
75*23a1cceaSRoger A. Faulkner  * character) in the string as2, where not more than n characters
76*23a1cceaSRoger A. Faulkner  * from the string as1 are searched.  strnstr() returns a pointer
77*23a1cceaSRoger A. Faulkner  * to the located string, or a null pointer if the string is
78*23a1cceaSRoger A. Faulkner  * not found.  If as2 is empty, the function returns as1.
79*23a1cceaSRoger A. Faulkner  */
80*23a1cceaSRoger A. Faulkner 
81*23a1cceaSRoger A. Faulkner char *
strnstr(const char * as1,const char * as2,size_t n)82*23a1cceaSRoger A. Faulkner strnstr(const char *as1, const char *as2, size_t n)
83*23a1cceaSRoger A. Faulkner {
84*23a1cceaSRoger A. Faulkner 	const char *s1, *s2;
85*23a1cceaSRoger A. Faulkner 	const char *tptr;
86*23a1cceaSRoger A. Faulkner 	size_t k;
87*23a1cceaSRoger A. Faulkner 	char c;
88*23a1cceaSRoger A. Faulkner 
89*23a1cceaSRoger A. Faulkner 	s1 = as1;
90*23a1cceaSRoger A. Faulkner 	s2 = as2;
91*23a1cceaSRoger A. Faulkner 
92*23a1cceaSRoger A. Faulkner 	if (s2 == NULL || *s2 == '\0')
93*23a1cceaSRoger A. Faulkner 		return ((char *)s1);
94*23a1cceaSRoger A. Faulkner 
95*23a1cceaSRoger A. Faulkner 	c = *s2;
96*23a1cceaSRoger A. Faulkner 	while (*s1 != '\0' && n--) {
97*23a1cceaSRoger A. Faulkner 		if (c == *s1++) {
98*23a1cceaSRoger A. Faulkner 			k = n;
997c478bd9Sstevel@tonic-gate 			tptr = s1;
100*23a1cceaSRoger A. Faulkner 			while ((c = *++s2) == *s1++ && c != '\0' && k--)
101*23a1cceaSRoger A. Faulkner 				continue;
102*23a1cceaSRoger A. Faulkner 			if (c == '\0')
1037c478bd9Sstevel@tonic-gate 				return ((char *)tptr - 1);
1047c478bd9Sstevel@tonic-gate 			s1 = tptr;
1057c478bd9Sstevel@tonic-gate 			s2 = as2;
1067c478bd9Sstevel@tonic-gate 			c = *s2;
1077c478bd9Sstevel@tonic-gate 		}
108*23a1cceaSRoger A. Faulkner 	}
1107c478bd9Sstevel@tonic-gate 	return (NULL);
1117c478bd9Sstevel@tonic-gate }