home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / string2.c < prev    next >
C/C++ Source or Header  |  1990-04-28  |  5KB  |  181 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: string2.c,v 4.1 90/04/28 22:44:14 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    string2.c,v $
  17.  * Revision 4.1  90/04/28  22:44:14  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This file contains string functions that are shared throughout the
  24.     various ELM utilities...
  25.  
  26. **/
  27.  
  28. #include "headers.h"
  29. #include <ctype.h>
  30.  
  31. #ifdef BSD
  32. #undef tolower
  33. #undef toupper
  34. #endif
  35.  
  36. char *shift_lower(string)
  37. char *string;
  38. {
  39.     /** return 'string' shifted to lower case.  Do NOT touch the
  40.         actual string handed to us! **/
  41.  
  42.     static char buffer[VERY_LONG_STRING];
  43.     register char *bufptr = buffer;
  44.  
  45.     for (; *string; string++, bufptr++)
  46.       if (isupper(*string))
  47.         *bufptr = tolower(*string);
  48.       else
  49.         *bufptr = *string;
  50.     
  51.     *bufptr = 0;
  52.     
  53.     return( (char *) buffer);
  54. }
  55.  
  56.  
  57. int
  58. in_list(list, target)
  59. char *list, *target;
  60.  
  61. {
  62.     /* Returns TRUE iff target is an item in the list - case ignored.
  63.      * If target is simple (an atom of an address) match must be exact.
  64.      * If target is complex (contains a special character that separates
  65.      * address atoms), the target need only match a whole number of atoms
  66.      * at the right end of an item in the list. E.g.
  67.      * target:    item:            match:
  68.      * joe        joe            yes
  69.      * joe        jojoe            no (wrong logname)
  70.      * joe        machine!joe        no (similar logname on a perhaps
  71.      *                       different machine - to
  72.      *                       test this sort of item the 
  73.      *                       passed target must include
  74.      *                       proper machine name, is
  75.      *                       in next two examples)
  76.      * machine!joe    diffmachine!joe        no  "
  77.      * machine!joe    diffmachine!machine!joe    yes
  78.      * joe@machine    jojoe@machine        no  (wrong logname)
  79.      * joe@machine    diffmachine!joe@machine    yes
  80.      */
  81.  
  82.     register char    *rest_of_list,
  83.             *next_item,
  84.             ch;
  85.     int        offset;
  86.     char        *shift_lower(),
  87.                 lower_list[VERY_LONG_STRING],
  88.                 lower_target[SLEN];
  89.  
  90.     rest_of_list = strcpy(lower_list, shift_lower(list));
  91.     strcpy(lower_target, shift_lower(target));
  92.     while((next_item = strtok(rest_of_list, ", \t\n")) != NULL) {
  93.         /* see if target matches the whole item */
  94.         if(strcmp(next_item, lower_target) == 0)
  95.         return(TRUE);
  96.  
  97.         if(strpbrk(lower_target,"!@%:") != NULL) {
  98.  
  99.           /* Target is complex */
  100.  
  101.           if((offset = strlen(next_item) - strlen(lower_target)) > 0) {
  102.  
  103.         /* compare target against right end of next item */
  104.         if(strcmp(&next_item[offset], lower_target) == 0) {
  105.  
  106.           /* make sure we are comparing whole atoms */
  107.           ch=next_item[offset-1];
  108.           if(ch == '!' || ch == '@' || ch == '%' || ch == ':')
  109.             return(TRUE);
  110.         }
  111.           }
  112.         }
  113.         rest_of_list = NULL;
  114.     }
  115.     return(FALSE);
  116. }
  117.     
  118.  
  119. int 
  120. in_string(buffer, pattern)
  121. char *buffer, *pattern;
  122. {
  123.     /** Returns TRUE iff pattern occurs IN IT'S ENTIRETY in buffer. **/ 
  124.  
  125.     register int i = 0, j = 0;
  126.     
  127.     while (buffer[i] != '\0') {
  128.       while (buffer[i++] == pattern[j++]) 
  129.         if (pattern[j] == '\0') 
  130.           return(TRUE);
  131.       i = i - j + 1;
  132.       j = 0;
  133.     }
  134.     return(FALSE);
  135. }
  136.  
  137. int
  138. chloc(string, ch)
  139. char *string, ch;
  140. {
  141.     /** returns the index of ch in string, or -1 if not in string **/
  142.     register int i, len;
  143.  
  144.     for (i=0, len = strlen(string); i<len; i++)
  145.       if (string[i] == ch) return(i);
  146.     return(-1);
  147. }
  148.  
  149. int
  150. occurances_of(ch, string)
  151. char ch, *string;
  152. {
  153.     /** returns the number of occurances of 'ch' in string 'string' **/
  154.  
  155.     register int count = 0;
  156.  
  157.     for (; *string; string++)
  158.       if (*string == ch) count++;
  159.  
  160.     return(count);
  161. }
  162.  
  163. remove_possible_trailing_spaces(string)
  164. char *string;
  165. {
  166.     /** an incredibly simple routine that will read backwards through
  167.         a string and remove all trailing whitespace.
  168.     **/
  169.  
  170.     register int i;
  171.  
  172.     for ( i = strlen(string); --i >= 0 && whitespace(string[i]); )
  173.         /** spin backwards, semicolon intented **/ ;
  174.  
  175.     string[i+1] = '\0';    /* note that even in the worst case when there
  176.                    are no trailing spaces at all, we'll simply
  177.                    end up replacing the existing '\0' with
  178.                    another one!  No worries, as M.G. would say
  179.                 */
  180. }
  181.