home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / strstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  2.5 KB  |  136 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: strstr.c,v 1.1 1996/12/11 11:18:29 aros Exp $
  4.  
  5.     Desc: ANSI C function strstr()
  6.     Lang: english
  7. */
  8.  
  9. /*****************************************************************************
  10.  
  11.     NAME */
  12. #include <string.h>
  13.  
  14.     char * strstr (
  15.  
  16. /*  SYNOPSIS */
  17.     const char * str,
  18.     const char * search)
  19.  
  20. /*  FUNCTION
  21.     Searches for a string in a string.
  22.  
  23.     INPUTS
  24.     str - Search this string
  25.     search - Look for this string
  26.  
  27.     RESULT
  28.     A pointer to the first occurence of search in str or NULL if search
  29.     is not found in str.
  30.  
  31.     NOTES
  32.  
  33.     EXAMPLE
  34.     char buffer[64];
  35.  
  36.     strcpy (buffer, "Hello ");
  37.  
  38.     // This returns a pointer to the first l in buffer.
  39.     strstr (buffer, "llo ");
  40.  
  41.     // This returns NULL
  42.     strstr (buffer, "llox");
  43.  
  44.     BUGS
  45.  
  46.     SEE ALSO
  47.     strchr(), strrchr(), strpbrk()
  48.  
  49.     INTERNALS
  50.  
  51.     HISTORY
  52.     11.12.1996 digulla  Copied from libnix. Original code by
  53.                 Kasper B. Graversen (c) 1996.
  54.  
  55. ******************************************************************************/
  56. {
  57.     size_t     done;
  58.     size_t     len_s = strlen (search);
  59.     const char * t;
  60.  
  61.     do
  62.     {
  63.     /* If the first character matches */
  64.     if (*search == *str)
  65.     {
  66.         /* How many characters to compare */
  67.         done = len_s;
  68.  
  69.         /* Skip the first char (we have checked this one already) */
  70.         t = search + 1;
  71.         str ++;
  72.  
  73.         /*
  74.         Until all characters have been compared and the two
  75.         characters at the current position are equal ...
  76.         */
  77.         while ((--done) && (*t == *str))
  78.         {
  79.         /* Next character */
  80.         t ++;
  81.         str ++;
  82.         }
  83.  
  84.         /* All character compared ? */
  85.         if (!done)
  86.         {
  87.         /*
  88.             Then we have found what we were looking for. str points
  89.             now to the last character of the string we look for.
  90.             Therefore we must move it backward to the beginning of
  91.             the string.
  92.         */
  93.         str -= len_s;
  94.         return ((char *)str);
  95.         }
  96.         else
  97.         {
  98.         /*
  99.             The strings are the same upto this character. I must
  100.             go back since the pattern might be something like
  101.             "ccbba" and the string "cccbba".
  102.         */
  103.         str -= len_s - done;
  104.         }
  105.     }
  106.     } while (*str++);
  107.  
  108.     /* nothing found */
  109.     return(0);
  110. } /* strstr */
  111.  
  112. #ifdef TEST
  113. #include <stdio.h>
  114. #include <string.h>
  115.  
  116. int main (int argc, char ** argv)
  117. {
  118.     char * ptr;
  119.  
  120.     if (argc != 3)
  121.     {
  122.     printf ("Usage: %s string search\n", argv[0]);
  123.     return 10;
  124.     }
  125.  
  126.     ptr = strstr (argv[1], argv[2]);
  127.  
  128.     if (!ptr)
  129.     printf ("%s not found in %s\n", argv[2], argv[1]);
  130.     else
  131.     printf ("%s found in %s as %s\n", argv[2], argv[1], ptr);
  132.  
  133.     return 0;
  134. }
  135. #endif /* TEST */
  136.