home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTAAFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  5.5 KB  |  211 lines

  1.  
  2. /* MODULE                            HTAAFile.c
  3. **        FILE ROUTINES FOR AUTHENTICATION
  4. **        (PASSWD AND GROUP FILES) AND
  5. **        ACCESS CONTROL LIST (.www_acl)
  6. ** AUTHORS:
  7. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  8. **
  9. ** HISTORY:
  10. **
  11. **
  12. ** BUGS:
  13. **
  14. **
  15. */
  16.  
  17.  
  18. #ifndef HTUTILS_H
  19. #include "HTUtils.h"
  20. #endif /* HTUTILS_H */
  21.  
  22. #include "tcp.h"        /* Macro FROMASCII() */
  23. /*#include <stdio.h> included by HTUtils.h -- FM *//* FILE */
  24. #include <string.h>
  25. #include "HTAAUtil.h"        /* Common utilities used in AA */
  26. #include "HTAAFile.h"        /* Implemented here */
  27.  
  28. #include "LYLeaks.h"
  29.  
  30. #define SPACE            ' '
  31. #define TAB            '\t'
  32.  
  33.  
  34.  
  35. /* PUBLIC                        HTAAFile_nextRec()
  36. **        GO TO THE BEGINNING OF THE NEXT RECORD
  37. ** ON ENTRY:
  38. **    fp    is the file from which records are read from.
  39. **
  40. ** ON EXIT:
  41. **    returns    nothing. File read pointer is located at the beginning
  42. **        of the next record. Handles continuation lines
  43. **        (lines ending in comma indicate a following
  44. **        continuation line).
  45. **
  46. */
  47. PUBLIC void HTAAFile_nextRec ARGS1(FILE *, fp)
  48. {
  49.     int ch = getc(fp);
  50.     int last = (char)0;
  51.  
  52.     do {
  53.     while (ch != EOF  &&  ch != CR  &&  ch != LF) {
  54.         if (ch != ' '  && ch != '\t')
  55.         last = ch;        /* Last non-whitespace */
  56.         ch = getc(fp);        /* Skip until end-of-line */
  57.     }
  58.     while (ch != EOF &&
  59.            (ch == CR  ||  ch == LF))/*Skip carriage returns and linefeeds*/
  60.         ch = getc(fp);
  61.     if (ch != EOF)
  62.         ungetc(ch, fp);
  63.     } while (last == ',' && ch != EOF);    /* Skip also continuation lines */
  64. }
  65.  
  66.  
  67. /* PRIVATE                            read_item()
  68. **        READ AN ITEM FROM A PASSWORD, GROUP
  69. **        OR ACCESS CONTROL LIST FILE
  70. **        i.e. either a field, or a list item.
  71. ** ON ENTRY:
  72. **    fp        is the file to read the characters from
  73. **    contents    is the character array to put the characters
  74. **    reading_list    if TRUE, read a list item (ends either in
  75. **            acomma or acolon),
  76. **            if FALSE, read a field (ends in acolon).
  77. **    max_len        is the maximum number of characters that may
  78. **            be read (i.e. the size of dest minus one for
  79. **            terminating null).
  80. ** ON EXIT:
  81. **    returns        the terminating character
  82. **            (i.e. either separator or CR or LF or EOF).
  83. **    contents    contains a null-terminated string representing
  84. **            the read field.
  85. ** NOTE 1:
  86. **            Ignores leading and trailing blanks and tabs.
  87. ** NOTE 2:
  88. **            If the item is more than max_len characters
  89. **            long, the rest of the characters in that item
  90. **            are ignored.  However, contents is always
  91. **            null-terminated!
  92. */
  93. PRIVATE int read_item ARGS4(FILE *,    fp,
  94.                 char *,    contents,
  95.                 BOOL,    reading_list,
  96.                 int,    max_len)
  97. {
  98.     char * dest = contents;
  99.     char * end = contents;
  100.     int cnt = 0;
  101.     int ch = getc(fp);
  102.  
  103.     while (SPACE == ch || TAB == ch)    /* Skip spaces and tabs */
  104.     ch = getc(fp);
  105.  
  106.     while (ch != FIELD_SEPARATOR &&
  107.        (!reading_list || ch != LIST_SEPARATOR) &&
  108.        ch != CR  &&  ch != LF  &&  ch != EOF  &&  cnt < max_len) {
  109.     *(dest++) = ch;
  110.     cnt++;
  111.     if (ch != SPACE && ch != TAB)
  112.         end = dest;
  113.     ch = getc(fp);
  114.     } /* while not eol or eof or too many read */
  115.  
  116.     if (cnt == max_len)    {
  117.     /* If the field was too long (or exactly maximum) ignore the rest */
  118.     while (ch != FIELD_SEPARATOR &&
  119.            (!reading_list || ch != LIST_SEPARATOR) &&
  120.            ch != CR  &&  ch != LF  &&  ch != EOF)
  121.         ch = getc(fp);
  122.     }
  123.  
  124.     if (ch == CR || ch == LF)
  125.     ungetc(ch, fp);    /* Push back the record separator (NL or LF) */
  126.  
  127.     /* Terminate the string, truncating trailing whitespace off.
  128.     ** Otherwise (if whitespace would be included), here would
  129.     ** be *dest='\0'; and  cnt -= ... would be left out.
  130.     */
  131.     *end = '\0';
  132.     cnt -= dest-end;
  133.  
  134.     return ch;        /* Return the terminating character */
  135. }
  136.  
  137.  
  138.  
  139. /* PUBLIC                        HTAAFile_readField()
  140. **        READ A FIELD FROM A PASSWORD, GROUP
  141. **        OR ACCESS CONTROL LIST FILE
  142. **        i.e. an item terminated by colon,
  143. **        end-of-line, or end-of-file. 
  144. ** ON ENTRY:
  145. **    fp        is the file to read the characters from
  146. **    contents    is the character array to put the characters
  147. **    max_len        is the maximum number of characters that may
  148. **            be read (i.e. the size of dest minus one for
  149. **            terminating null).
  150. ** ON EXIT:
  151. **    returns        the terminating character
  152. **            (i.e. either separator or CR or LF or EOF).
  153. **    contents    contains a null-terminated string representing
  154. **            the read field.
  155. ** NOTE 1:
  156. **            Ignores leading and trailing blanks and tabs.
  157. ** NOTE 2:
  158. **            If the field is more than max_len characters
  159. **            long, the rest of the characters in that item
  160. **            are ignored.  However, contents is always
  161. **            null-terminated!
  162. */
  163. PUBLIC int HTAAFile_readField ARGS3(FILE *, fp,
  164.                     char *, contents,
  165.                     int,    max_len)
  166. {
  167.     return read_item(fp, contents, NO, max_len);
  168. }
  169.  
  170.  
  171.  
  172.  
  173. /* PUBLIC                        HTAAFile_readList()
  174. **
  175. **            READ A LIST OF STRINGS SEPARATED BY COMMAS
  176. **            (FROM A GROUP OR ACCESS CONTROL LIST FILE)
  177. ** ON ENTRY:
  178. **    fp        is a pointer to the input file.
  179. **    result        is the list to which append the read items.
  180. **    max_len        is the maximum number of characters in each
  181. **            list entry (extra characters are ignored).
  182. ** ON EXIT:
  183. **    returns        the number of items read.
  184. **
  185. */
  186. PUBLIC int HTAAFile_readList ARGS3(FILE *,    fp,
  187.                    HTList *,    result,
  188.                    int,        max_len)
  189. {
  190.     char *item = NULL;
  191.     char terminator;
  192.     int cnt = 0;
  193.  
  194.     do {
  195.     if (!item  &&  !(item = (char*)malloc(max_len+1)))
  196.         outofmem(__FILE__, "HTAAFile_readList");
  197.     terminator = read_item(fp, item, YES, max_len);
  198.     if (strlen(item) > 0) {
  199.         cnt++;
  200.         HTList_addObject(result, (void*)item);
  201.         item = NULL;
  202.     }
  203.     } while (terminator != FIELD_SEPARATOR  &&
  204.          terminator != CR  &&  terminator != LF  &&
  205.          terminator != EOF);
  206.  
  207.     if (item) free(item);    /* This was not needed */
  208.     return cnt;
  209. }
  210.  
  211.