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