/*------------------------------------------------------------------------- * Filename: strstr.c * Version: $Id: strstr.c,v 1.2 1999/05/02 20:26:27 erik Exp $ * Copyright: Copyright (C) 1996 - 1999, Erik Mouw * Author: Erik Mouw * Description: strstr() replacement function for brain damaged OSes * Created at: Tue Oct 1 13:27:25 1996 * Modified by: Erik Mouw * Modified at: Sun May 2 16:49:24 1999 *-----------------------------------------------------------------------*/ /* * * This will be compiled if your system misses the strstr() function. * * * strstr.c, Copyright (C) 1996 - 1999, Erik Mouw * * Email: * J.A.K.Mouw@its.tudelft.nl * * Snail mail: * J.A.K. Mouw * Information and Communication Theory Group * Department of Electrical Engineering * Faculty of Information Technology and Systems * Delft University of Technology * P.O. Box 5031 * 2600 GA Delft * The Netherlands * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software (the "Software"), to deal in the * Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Except as contained in this notice, the name of Erik Mouw shall not * be used in advertising or otherwise to promote the sale, use or * other dealings in this Software without prior written authorization * from Erik Mouw. * * * strstr - locate a substring * * The strstr() function finds the first occurrence of the substring * needle in the string haystack. The terminating `\0' characters are * not compared. * * The strstr() function returns a pointer to the beginning of the * substring, or NULL if the substring is not found. * */ #ident "$Id: strstr.c,v 1.2 1999/05/02 20:26:27 erik Exp $" #ifdef HAVE_CONFIG_H # include "config.h" #endif #if STDC_HEADERS # include #else # ifdef HAVE_MALLOC_H # include # else # ifndef NULL # define NULL ((void *)0) # endif # endif #endif #ifdef __cplusplus extern "C" char *strstr(const char *haystack, const char *needle) #else char *strstr(const char *haystack, const char *needle) #endif { char *h = (char *)haystack; char *n = (char *)needle; char *start = (char *)haystack; int stop = 0; if((*needle == '\0') || (*haystack == '\0')) { return(NULL); } while(stop == 0) { if(*n == '\0') { /* we've reached the end of the needle, this is it */ return(start); } else if(*h == '\0') { /* end of the haystack, stop */ stop = 1; } else if(*n == *h) { /* this looks good, try the next one */ n++; h++; } else { /* start all over again */ n = (char *) needle; h++; start = h; } } return(NULL); }