home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / compat / strstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-26  |  1.6 KB  |  71 lines

  1. /* 
  2.  * strstr.c --
  3.  *
  4.  *    Source code for the "strstr" library routine.
  5.  *
  6.  * Copyright (c) 1988-1993 The Regents of the University of California.
  7.  * Copyright (c) 1994 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#) strstr.c 1.3 94/12/17 16:26:23";
  15. #endif /* not lint */
  16.  
  17. /*
  18.  *----------------------------------------------------------------------
  19.  *
  20.  * strstr --
  21.  *
  22.  *    Locate the first instance of a substring in a string.
  23.  *
  24.  * Results:
  25.  *    If string contains substring, the return value is the
  26.  *    location of the first matching instance of substring
  27.  *    in string.  If string doesn't contain substring, the
  28.  *    return value is 0.  Matching is done on an exact
  29.  *    character-for-character basis with no wildcards or special
  30.  *    characters.
  31.  *
  32.  * Side effects:
  33.  *    None.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. char *
  39. strstr(string, substring)
  40.     register char *string;    /* String to search. */
  41.     char *substring;        /* Substring to try to find in string. */
  42. {
  43.     register char *a, *b;
  44.  
  45.     /* First scan quickly through the two strings looking for a
  46.      * single-character match.  When it's found, then compare the
  47.      * rest of the substring.
  48.      */
  49.  
  50.     b = substring;
  51.     if (*b == 0) {
  52.     return string;
  53.     }
  54.     for ( ; *string != 0; string += 1) {
  55.     if (*string != *b) {
  56.         continue;
  57.     }
  58.     a = string;
  59.     while (1) {
  60.         if (*b == 0) {
  61.         return string;
  62.         }
  63.         if (*a++ != *b++) {
  64.         break;
  65.         }
  66.     }
  67.     b = substring;
  68.     }
  69.     return (char *) 0;
  70. }
  71.