home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 400-499 / ff473.lzh / CNewsSrc / cnews_src.lzh / libcnews / gethdr.c < prev    next >
C/C++ Source or Header  |  1990-12-25  |  2KB  |  98 lines

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