home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / compat / strstr.c < prev    next >
Text File  |  1993-03-19  |  3KB  |  85 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.  * All rights reserved.
  8.  *
  9.  * Permission is hereby granted, without written agreement and without
  10.  * license or royalty fees, to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose, provided that the
  12.  * above copyright notice and the following two paragraphs appear in
  13.  * all copies of this software.
  14.  * 
  15.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  16.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  17.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  18.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19.  *
  20.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  21.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  22.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  23.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  24.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25.  */
  26.  
  27. #ifndef lint
  28. static char rcsid[] = "$Header: /user6/ouster/tcl/compat/RCS/strstr.c,v 1.2 93/03/19 15:25:40 ouster Exp $ SPRITE (Berkeley)";
  29. #endif /* not lint */
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * strstr --
  35.  *
  36.  *    Locate the first instance of a substring in a string.
  37.  *
  38.  * Results:
  39.  *    If string contains substring, the return value is the
  40.  *    location of the first matching instance of substring
  41.  *    in string.  If string doesn't contain substring, the
  42.  *    return value is 0.  Matching is done on an exact
  43.  *    character-for-character basis with no wildcards or special
  44.  *    characters.
  45.  *
  46.  * Side effects:
  47.  *    None.
  48.  *
  49.  *----------------------------------------------------------------------
  50.  */
  51.  
  52. char *
  53. strstr(string, substring)
  54.     register char *string;    /* String to search. */
  55.     char *substring;        /* Substring to try to find in string. */
  56. {
  57.     register char *a, *b;
  58.  
  59.     /* First scan quickly through the two strings looking for a
  60.      * single-character match.  When it's found, then compare the
  61.      * rest of the substring.
  62.      */
  63.  
  64.     b = substring;
  65.     if (*b == 0) {
  66.     return string;
  67.     }
  68.     for ( ; *string != 0; string += 1) {
  69.     if (*string != *b) {
  70.         continue;
  71.     }
  72.     a = string;
  73.     while (1) {
  74.         if (*b == 0) {
  75.         return string;
  76.         }
  77.         if (*a++ != *b++) {
  78.         break;
  79.         }
  80.     }
  81.     b = substring;
  82.     }
  83.     return (char *) 0;
  84. }
  85.