home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / len_next.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  2.6 KB  |  108 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: len_next.c,v 5.4 1993/04/12 01:27:30 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.4 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1992 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: len_next.c,v $
  16.  * Revision 5.4  1993/04/12  01:27:30  syd
  17.  * len_next_part() was botching quote-delimited strings.
  18.  * From: chip@chinacat.unicom.com (Chip Rosenthal)
  19.  *
  20.  * Revision 5.3  1993/01/19  05:07:05  syd
  21.  * Trim erroreous extra log entry
  22.  * From: Syd
  23.  *
  24.  * Revision 5.2  1993/01/19  04:47:12  syd
  25.  * Significant changes to provide consistent Date and From_ header
  26.  * cracking.  Overhauled date utilities and moved into library.  Moved
  27.  * real_from() into library.  Modified frm, newmail, and readmsg utilities
  28.  * to use library version of real_from().  Moved get_word() from Elm
  29.  * source into library.  Added new library routines atonum() and strfcpy().
  30.  * Fixed trailing backslash bug in len_next().
  31.  * From: chip@chinacat.unicom.com (Chip Rosenthal)
  32.  *
  33.  * Revision 5.1  1992/10/03  22:41:36  syd
  34.  * Initial checkin as of 2.4 Release at PL0
  35.  *
  36.  *
  37.  ******************************************************************************/
  38.  
  39. /** get the length of the next part of the address/data field
  40.  
  41.     This code returns the length of the next part of the
  42.   string field containing address/data.  It takes into account
  43.   quoting via " as well as \ escapes.
  44.   Quoting via ' is not taken into account, as RFC-822 does not
  45.   consider a ' character a valid 'quoting character'
  46.  
  47.   A 1 is returned for a single character unless:
  48.  
  49.   A 0 is returned at end of string.
  50.  
  51.   A 2 is returned for strings that start \
  52.  
  53.   The length of quoted sections is returned for quoted fields
  54.  
  55. **/
  56.  
  57. #include <ctype.h>
  58.  
  59.  
  60. int
  61. len_next_part(str)
  62. register char *str;
  63. {
  64.     register char *s;
  65.  
  66.     switch (*str) {
  67.  
  68.     case '\0':
  69.         return 0;
  70.  
  71.     case '\\':
  72.         return (*++str != '\0' ? 2 : 1);
  73.  
  74.     case '"':
  75.         for (s = str+1 ; *s != '\0' ; ++s) {
  76.             if (*s == '\\') {
  77.                 if (*++s == '\0')
  78.                     break;
  79.             } else if (*s == '"') {
  80.                 ++s;
  81.                 break;
  82.             }
  83.         }
  84.         return (s - str);
  85.  
  86.     default:
  87.         return 1;
  88.  
  89.     }
  90.  
  91.     /*NOTREACHED*/
  92. }
  93.  
  94. #ifdef _TEST
  95. #include <stdio.h>
  96. main()
  97. {
  98.     char buf[256], *s;
  99.     int len;
  100.  
  101.     while (gets(buf) != NULL) {
  102.         for (s = buf ; (len = len_next_part(s)) > 0 ; s += len)
  103.             printf("%4d %-.*s\n", len, len, s);
  104.     }
  105.     exit(0);
  106. }
  107. #endif
  108.