home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / gethdr.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  2KB  |  103 lines

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