purge remaining gpl code from clib, and make clib build again
[tangerine.git] / compiler / clib / strstr.c
blob30fa9be190c427c9fc3717620ff070597c83ec3b
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function strstr().
6 */
8 /*****************************************************************************
10 NAME */
11 #include <string.h>
13 char * strstr (
15 /* SYNOPSIS */
16 const char * str,
17 const char * search)
19 /* FUNCTION
20 Searches for a string in a string.
22 INPUTS
23 str - Search this string
24 search - Look for this string
26 RESULT
27 A pointer to the first occurence of search in str or NULL if search
28 is not found in str.
30 NOTES
32 EXAMPLE
33 char buffer[64];
35 strcpy (buffer, "Hello ");
37 // This returns a pointer to the first l in buffer.
38 strstr (buffer, "llo ");
40 // This returns NULL
41 strstr (buffer, "llox");
43 BUGS
45 SEE ALSO
46 strchr(), strrchr(), strpbrk()
48 INTERNALS
50 ******************************************************************************/
52 size_t done;
53 size_t len_s = strlen (search);
54 const char * t;
58 /* If the first character matches */
59 if (*search == *str)
61 /* How many characters to compare */
62 done = len_s;
64 /* Skip the first char (we have checked this one already) */
65 t = search + 1;
66 str ++;
69 Until all characters have been compared and the two
70 characters at the current position are equal ...
72 while ((--done) && (*t == *str))
74 /* Next character */
75 t ++;
76 str ++;
79 /* All character compared ? */
80 if (!done)
83 Then we have found what we were looking for. str points
84 now to the last character of the string we look for.
85 Therefore we must move it backward to the beginning of
86 the string.
88 str -= len_s;
89 return ((char *)str);
91 else
94 The strings are the same upto this character. I must
95 go back since the pattern might be something like
96 "ccbba" and the string "cccbba".
98 str -= len_s - done;
101 } while (*str++);
103 /* nothing found */
104 return(0);
105 } /* strstr */
107 #ifdef TEST
108 #include <stdio.h>
109 #include <string.h>
111 int main (int argc, char ** argv)
113 char * ptr;
115 if (argc != 3)
117 printf ("Usage: %s string search\n", argv[0]);
118 return 10;
121 ptr = strstr (argv[1], argv[2]);
123 if (!ptr)
124 printf ("%s not found in %s\n", argv[2], argv[1]);
125 else
126 printf ("%s found in %s as %s\n", argv[2], argv[1], ptr);
128 return 0;
130 #endif /* TEST */