home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / cproto / part02 / string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-25  |  795 b   |  44 lines

  1. /* $Id: string.c 2.1 91/02/28 11:16:33 cthuang Exp $
  2.  *
  3.  * Some string handling routines
  4.  */
  5. #include <stdio.h>
  6. #include "config.h"
  7.  
  8. /* Copy the string into an allocated memory block.
  9.  * Return a pointer to the copy.
  10.  */
  11. char *
  12. strdup (s)
  13. char *s;
  14. {
  15.     char *dest;
  16.  
  17.     if ((dest = malloc((unsigned)(strlen(s)+1))) == NULL) {
  18.     fprintf(stderr, "No memory to duplicate string.\n");
  19.     exit(1);
  20.     }
  21.     strcpy(dest, s);
  22.     return dest;
  23. }
  24.  
  25. /* Return a pointer to the first occurence of the substring 
  26.  * within the string, or NULL if not found.
  27.  */
  28. char *
  29. strstr (src, key)
  30. char *src, *key;
  31. {
  32.     char *s;
  33.     int keylen;
  34.  
  35.     keylen = strlen(key);
  36.     s = index(src, *key);
  37.     while (s != NULL) {
  38.     if (strncmp(s, key, keylen) == 0)
  39.         return s;
  40.     s = index(s+1, *key);
  41.     }
  42.     return NULL;
  43. }
  44.