home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / c2man-2.0pl33.lha / c2man-2.0 / string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-14  |  1.4 KB  |  83 lines

  1. /* $Id: string.c,v 2.0.1.2 1994/01/07 07:05:10 greyham Exp $
  2.  *
  3.  * Some string handling routines
  4.  */
  5. #include "c2man.h"
  6. #include <ctype.h>
  7.  
  8. /* Copy the string into an allocated memory block.
  9.  * Return a pointer to the copy.
  10.  */
  11. char *
  12. strduplicate (s)
  13. const char *s;    /* The string to copy. May be NULL */
  14. {
  15.     char *dest;
  16.  
  17.     if (!s)    return NULL;
  18.     
  19.     if ((dest = malloc((unsigned)(strlen(s)+1))) == NULL)
  20.     outmem();
  21.  
  22.     strcpy(dest, s);
  23.     return dest;
  24. }
  25.  
  26. #ifndef HAS_STRSTR
  27.  
  28. /* Return a pointer to the first occurence of the substring 
  29.  * within the string, or NULL if not found.
  30.  */
  31. char *
  32. strstr (src, key)
  33. const char *src, *key;
  34. {
  35.     char *s;
  36.     int keylen;
  37.  
  38.     keylen = strlen(key);
  39.     s = strchr(src, *key);
  40.     while (s != NULL) {
  41.     if (strncmp(s, key, keylen) == 0)
  42.         return s;
  43.     s = strchr(s+1, *key);
  44.     }
  45.     return NULL;
  46. }
  47.  
  48. #endif
  49.  
  50. /* compare two strings case insensitively, for up to n chars */
  51. int strncmpi(s1, s2, n)
  52. const char *s1, *s2;
  53. size_t n;
  54. {
  55.     while(n--)
  56.     {
  57.     char c1 = *s1, c2 = *s2;
  58.  
  59.     if (c1 == '\0' && c2 == '\0')    break;
  60.  
  61.     if (isalpha(c1) && isupper(c1))    c1 = tolower(c1);
  62.     if (isalpha(c2) && isupper(c2))    c2 = tolower(c2);
  63.  
  64.     if (c1 < c2)    return -1;
  65.     if (c1 > c2)    return 1;
  66.     s1++; s2++;
  67.     }
  68.     return 0;
  69. }
  70.  
  71. /* convert string to upper case */
  72. char *strtoupper(in)
  73. char *in;
  74. {
  75.     char *s;
  76.  
  77.     for (s = in; *s; s++)
  78.     {
  79.     if (islower(*s))    *s = toupper(*s);
  80.     }
  81.     return in;
  82. }
  83.