home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11612 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.8 KB  |  109 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HIgher problems at hand. HELP!
  5. Message-ID: <24902@dog.ee.lbl.gov>
  6. Date: 26 Jul 92 02:05:43 GMT
  7. References: <Brv6oD.Dq5@usenet.ucs.indiana.edu> <1992Jul25.142216.5636@druid.uucp>
  8. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  9. Organization: Lawrence Berkeley Laboratory, Berkeley
  10. Lines: 96
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <1992Jul25.142216.5636@druid.uucp> darcy@druid.uucp
  14. (D'Arcy J.M. Cain) writes:
  15. >... if the Amiga really doesn't have a strstr here it is:
  16. >
  17. >/*
  18. > * strstr - find first occurrence of wanted in s
  19. > *
  20. > * based on code by Henry Spencer
  21. > * modified for ANSI by D'Arcy J.M. Cain
  22. > */
  23.  
  24. The given strstr() fails to meet the second sentence under `Returns'
  25. in the ANSI C standard description of strstr() (line 25, p. 168):
  26.  
  27.     If |s2| points to a string with zero length, the function
  28.     returns |s1|.
  29.  
  30. Thus,
  31.  
  32.     strstr("foo", "")
  33.  
  34. must return a pointer to the `f' in "foo".
  35.  
  36. Other than that, and its peculiar redefinition of `const', it is OK.
  37. Nonetheless, here is my strstr() from the BSD net.2 C library.  Note
  38. that <sys/cdefs.h> is a hack for Classic C compilers, and is not needed
  39. (nor useful) for a Pure ANSI compiler.
  40.  
  41. /*-
  42.  * Copyright (c) 1990 The Regents of the University of California.
  43.  * All rights reserved.
  44.  *
  45.  * This code is derived from software contributed to Berkeley by
  46.  * Chris Torek.
  47.  *
  48.  * Redistribution and use in source and binary forms, with or without
  49.  * modification, are permitted provided that the following conditions
  50.  * are met:
  51.  * 1. Redistributions of source code must retain the above copyright
  52.  *    notice, this list of conditions and the following disclaimer.
  53.  * 2. Redistributions in binary form must reproduce the above copyright
  54.  *    notice, this list of conditions and the following disclaimer in the
  55.  *    documentation and/or other materials provided with the distribution.
  56.  * 3. All advertising materials mentioning features or use of this software
  57.  *    must display the following acknowledgement:
  58.  *    This product includes software developed by the University of
  59.  *    California, Berkeley and its contributors.
  60.  * 4. Neither the name of the University nor the names of its contributors
  61.  *    may be used to endorse or promote products derived from this software
  62.  *    without specific prior written permission.
  63.  *
  64.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  65.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  67.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  68.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  69.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  70.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  71.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  74.  * SUCH DAMAGE.
  75.  */
  76.  
  77. #if defined(LIBC_SCCS) && !defined(lint)
  78. static char sccsid[] = "@(#)strstr.c    5.2 (Berkeley) 1/26/91";
  79. #endif /* LIBC_SCCS and not lint */
  80.  
  81. #include <sys/cdefs.h>
  82. #include <string.h>
  83.  
  84. /*
  85.  * Find the first occurrence of find in s.
  86.  */
  87. char *
  88. strstr(s, find)
  89.     register const char *s, *find;
  90. {
  91.     register char c, sc;
  92.     register size_t len;
  93.  
  94.     if ((c = *find++) != 0) {
  95.         len = strlen(find);
  96.         do {
  97.             do {
  98.                 if ((sc = *s++) == 0)
  99.                     return (NULL);
  100.             } while (sc != c);
  101.         } while (strncmp(s, find, len) != 0);
  102.         s--;
  103.     }
  104.     return ((char *)s);
  105. }
  106. -- 
  107. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  108. Berkeley, CA        Domain:    torek@ee.lbl.gov
  109.