home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / rkive / part01 / str.c < prev    next >
C/C++ Source or Header  |  1989-06-29  |  3KB  |  128 lines

  1. /*
  2. **
  3. ** This software is Copyright (c) 1989 by Kent Landfield.
  4. **
  5. ** Permission is hereby granted to copy, distribute or otherwise 
  6. ** use any part of this package as long as you do not try to make 
  7. ** money from it or pretend that you wrote it.  This copyright 
  8. ** notice must be maintained in any copy made.
  9. **
  10. **
  11. **  History:
  12. **    Creation: Tue Feb 21 08:52:35 CST 1989 due to necessity.
  13. **                                                               
  14. */
  15. #ifndef lint
  16. static char SID[] = "@(#)str.c    1.1 6/1/89";
  17. #endif
  18.  
  19. /*
  20. ** Strchr() and Strrchr() are included for portability sake only.
  21. */
  22.  
  23. #ifndef NULL
  24. #define NULL 0
  25. #endif
  26.  
  27. #ifndef lint
  28. /*
  29. ** strchr:
  30. **
  31. ** strchr(str, c) returns a pointer to the first place in
  32. ** str where c occurs, or NULL if c does not occur in str.
  33. **
  34. ** char *strchr(str, c) char *str, c; { return (str); }
  35. **
  36. */
  37.  
  38. char *strchr(str, c)
  39. register char *str, c;
  40. {
  41.     for (;;) {
  42.         if (*str == c)
  43.             return (str);
  44.         if (!*str++)
  45.             return (NULL);
  46.     }
  47. }
  48.  
  49. /*
  50. ** strrchr:
  51. **
  52. ** strrchr(str, c) returns a pointer to the last place in 
  53. ** str where c occurs, or NULL if c does not occur in str.
  54. **
  55. ** char *strrchr(str, c) char *str, c; { return (str); }
  56. */
  57.  
  58. char *strrchr(str, c)
  59. register char *str, c;
  60. {
  61.     register char *t;
  62.  
  63.     t = NULL;
  64.     do {
  65.         if (*str == c)
  66.             t = str;
  67.     } while (*str++);
  68.     return(t);
  69. }
  70. #endif /* lint */
  71.  
  72. /*
  73. ** strstrip:
  74. **
  75. ** strstrip(str) returns a pointer to the first non-blank character 
  76. ** in str. It strips all blanks, and tabs from the front and back
  77. ** of a string as well as strip off newlines from the rear.
  78. **
  79. ** char *strstrip(str) char *str; { return (str); }
  80. */
  81.  
  82. char *strstrip(str)
  83. register char *str;
  84. {
  85.     register char *cp, *dp;
  86.     
  87.     cp = str;
  88.     dp = ((str+strlen(str))-1);
  89.  
  90.     while(*cp && (*cp == ' ' || *cp == '\t')) 
  91.       cp++;
  92.  
  93.     while(dp != cp && (*dp == ' ' || *dp == '\t' || *dp == '\n'))
  94.       --dp;
  95.     *(dp+1) = '\0';
  96.  
  97.     return(cp);
  98. }
  99.  
  100. /*
  101. ** substr:
  102. **
  103. ** substr(str, substr) returns a pointer to the first place in
  104. ** str where the substring substr occurs, or NULL if substr does
  105. ** not occur in str.
  106. **
  107. ** char *substr(str, substr) char *str, *substr; { return (str); }
  108. */
  109. char *substr(from, find)
  110.     char *from, *find;
  111. {
  112.     register char *sp, *np;
  113.     register int len;
  114.     char *strchr();
  115.  
  116.     np = from;
  117.     len = strlen(find);
  118.  
  119.     while ((sp = strchr(np,*find)) != NULL) {
  120.         if (strlen(sp) < len)
  121.             break;
  122.         if (strncmp(sp,find,len) == 0) 
  123.             return(sp);
  124.         np = sp + 1;
  125.     }
  126.     return(NULL);
  127. }
  128.