xref: /netbsd-src/external/bsd/am-utils/dist/libamu/strstr.c (revision 8bae5d409deb915cf7c8f0539fae22ff2cb8a313)
1*8bae5d40Schristos /*	$NetBSD: strstr.c,v 1.1.1.3 2015/01/17 16:34:18 christos Exp $	*/
2a53f50b9Schristos 
3a53f50b9Schristos /*
4*8bae5d40Schristos  * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos  * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos  * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos  * All rights reserved.
9a53f50b9Schristos  *
10a53f50b9Schristos  * This code is derived from the Free Software Foundation, Inc. GNU utilities.
11a53f50b9Schristos  *
12a53f50b9Schristos  * Redistribution and use in source and binary forms, with or without
13a53f50b9Schristos  * modification, are permitted provided that the following conditions
14a53f50b9Schristos  * are met:
15a53f50b9Schristos  * 1. Redistributions of source code must retain the above copyright
16a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer.
17a53f50b9Schristos  * 2. Redistributions in binary form must reproduce the above copyright
18a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer in the
19a53f50b9Schristos  *    documentation and/or other materials provided with the distribution.
20*8bae5d40Schristos  * 3. Neither the name of the University nor the names of its contributors
21a53f50b9Schristos  *    may be used to endorse or promote products derived from this software
22a53f50b9Schristos  *    without specific prior written permission.
23a53f50b9Schristos  *
24a53f50b9Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25a53f50b9Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26a53f50b9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27a53f50b9Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28a53f50b9Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29a53f50b9Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30a53f50b9Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31a53f50b9Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32a53f50b9Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33a53f50b9Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34a53f50b9Schristos  * SUCH DAMAGE.
35a53f50b9Schristos  *
36a53f50b9Schristos  *
37a53f50b9Schristos  * File: am-utils/libamu/strstr.c
38a53f50b9Schristos  *
39a53f50b9Schristos  */
40a53f50b9Schristos 
41a53f50b9Schristos #ifdef HAVE_CONFIG_H
42a53f50b9Schristos # include <config.h>
43a53f50b9Schristos #endif /* HAVE_CONFIG_H */
44a53f50b9Schristos #include <am_defs.h>
45a53f50b9Schristos #include <amu.h>
46a53f50b9Schristos 
47a53f50b9Schristos 
48a53f50b9Schristos /* strstr.c -- return the offset of one string within another
49a53f50b9Schristos    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
50a53f50b9Schristos 
51a53f50b9Schristos    This program is free software; you can redistribute it and/or modify
52a53f50b9Schristos    it under the terms of the GNU General Public License as published by
53a53f50b9Schristos    the Free Software Foundation; either version 2, or (at your option)
54a53f50b9Schristos    any later version.
55a53f50b9Schristos 
56a53f50b9Schristos    This program is distributed in the hope that it will be useful,
57a53f50b9Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
58a53f50b9Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59a53f50b9Schristos    GNU General Public License for more details.
60a53f50b9Schristos 
61a53f50b9Schristos    You should have received a copy of the GNU General Public License
62a53f50b9Schristos    along with this program; if not, write to the Free Software
63a53f50b9Schristos    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
64a53f50b9Schristos 
65a53f50b9Schristos /* Author:
66a53f50b9Schristos 	Mike Rendell			Department of Computer Science
67a53f50b9Schristos 	michael@garfield.mun.edu	Memorial University of Newfoundland
68a53f50b9Schristos 	..!uunet!garfield!michael	St. John's, Nfld., Canada
69a53f50b9Schristos 	(709) 737-4550			A1C 5S7
70a53f50b9Schristos */
71a53f50b9Schristos 
72a53f50b9Schristos 
73a53f50b9Schristos /*
74a53f50b9Schristos  * Return the starting address of string S2 in S1;
75a53f50b9Schristos  * return 0 if it is not found.
76a53f50b9Schristos  */
77a53f50b9Schristos char *
strstr(char * s1,char * s2)78a53f50b9Schristos strstr(char *s1, char *s2)
79a53f50b9Schristos {
80a53f50b9Schristos   int i;
81a53f50b9Schristos   char *p1;
82a53f50b9Schristos   char *p2;
83a53f50b9Schristos   char *s = s1;
84a53f50b9Schristos 
85a53f50b9Schristos   for (p2 = s2, i = 0; *s; p2 = s2, i++, s++) {
86a53f50b9Schristos     for (p1 = s; *p1 && *p2 && *p1 == *p2; p1++, p2++)
87a53f50b9Schristos       ;
88a53f50b9Schristos     if (!*p2)
89a53f50b9Schristos       break;
90a53f50b9Schristos   }
91a53f50b9Schristos   if (!*p2)
92a53f50b9Schristos     return s1 + i;
93a53f50b9Schristos 
94a53f50b9Schristos   return 0;
95a53f50b9Schristos }
96