home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff319.lzh / CNewsSrc / cnews.orig.lzh / libcnews / gethdr.c < prev    next >
C/C++ Source or Header  |  1989-06-27  |  2KB  |  77 lines

  1. /*
  2.  * gethdr - read an entire RFC 822 header "line", including continuations
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <fgetmfs.h>
  8. #include <sys/types.h>
  9. #include "news.h"
  10. #include "libc.h"
  11.  
  12. /*
  13.  * Read the first line; if it's a header, repeatedly read lines until a
  14.  * non-continuation line is found.  For each continuation line, grow
  15.  * hdr to accomodate it and append it to hdr.
  16.  * *limitp is updated by subtracting the number of bytes read.
  17.  * 
  18.  */
  19. char *                        /* malloced; caller must not free */
  20. gethdr(in, limitp, ishdrp)
  21. FILE *in;
  22. register long *limitp;
  23. int *ishdrp;
  24. {
  25.     register int c, hdrlen, contlen, limitset = *limitp >= 0;
  26.     register char *contin = NULL;
  27.     static char *hdr = NULL;
  28.  
  29.     nnfree(&hdr);
  30.     *ishdrp = NO;
  31.     hdr = fgetmfs(in, (int)*limitp, CONT_NO);
  32.     if (hdr == NULL)
  33.         return hdr;
  34.     hdrlen = strlen(hdr);
  35.     *limitp -= hdrlen;
  36.     *ishdrp = ishdr(hdr);
  37.     if (!*ishdrp)
  38.         return hdr;
  39.     while (hdr != NULL && (!limitset || *limitp > 1) && (c = getc(in)) != EOF) {
  40.         (void) ungetc(c, in);
  41.  
  42.         if (!iswhite(c))
  43.             break;
  44.         contin = fgetmfs(in, (int)*limitp, CONT_NO);
  45.         if (contin == NULL)
  46.             break;
  47.  
  48.         contlen = strlen(contin);
  49.         *limitp -= contlen;
  50.         hdr = realloc(hdr, (unsigned)(hdrlen + contlen + 1)); /* 1 for NUL */
  51.         if (hdr != NULL) {
  52.             (void) strcpy(hdr + hdrlen, contin);
  53.             hdrlen += contlen;
  54.         }
  55.         free(contin);
  56.         contin = NULL;
  57.     }
  58.     return hdr;
  59. }
  60.  
  61.  
  62. /*
  63.  * Is s an RFC 822 header line?
  64.  * If a colon is seen before whitespace, it is.
  65.  */
  66. int
  67. ishdr(s)
  68. register char *s;
  69. {
  70.     register char *cp = s;
  71.     register int c;
  72.  
  73.     while ((c = *cp) != '\0' && !(isascii(c) && isspace(c)) && c != ':')
  74.         ++cp;
  75.     return c == ':' && cp > s;
  76. }
  77.