]> Pileus Git - ~andy/fetchmail/blob - strstr.c
5b80fe430a2531a1c558589c077dfb000d52e067
[~andy/fetchmail] / strstr.c
1 /*
2  * strstr.c -- return the offset of one string within another.
3  *
4  * Copyright (C) 1997 Free Software Foundation, Inc. 
5  *
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2, or (at your option) any later
9  * version. 
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details. 
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc., 59
18  * Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
19  */
20
21 /* Written by Philippe De Muyter <phdm@info.ucl.ac.be>.  */
22
23 /*
24  * NAME 
25  *
26  * strstr -- locate first occurence of a substring 
27  *
28  * SYNOPSIS 
29  *
30  * char *strstr (char *s1, char *s2) 
31  *
32  * DESCRIPTION 
33  *
34  * Locates the first occurence in the string pointed to by S1 of the string
35  * pointed to by S2.  Returns a pointer to the substring found, or a NULL
36  * pointer if not found.  If S2 points to a string with zero length, the
37  * function returns S1. 
38  *
39  * BUGS 
40  *
41  */
42
43 char *
44 strstr (buf, sub)
45      register char *buf;
46      register char *sub;
47 {
48   register char *bp;
49
50   if (!*sub)
51     return buf;
52   for (;;)
53     {
54       if (!*buf)
55         break;
56       bp = buf;
57       for (;;)
58         {
59           if (!*sub)
60             return buf;
61           if (*bp++ != *sub++)
62             break;
63         }
64       sub -= (unsigned long) bp;
65       sub += (unsigned long) buf;
66       buf += 1;
67     }
68   return 0;
69 }
70