2 ** m_getfld2 -- replacement for m_getfld()
3 ** read an RFC 822 message
7 #define _POSIX_C_SOURCE 200809L
24 static enum threestate is_falted(FILE *);
25 static size_t copyname(char *, char *);
29 ** For the states FLD2, BODY2 memory is allocated for f->value.
30 ** For the states LENERR2, FMTERR2, ERR2 and FILEEOF2 no memory is allocated.
33 m_getfld2(enum state s, struct field *f, FILE *msg)
38 enum threestate falted;
40 nchars = getline(&tmpline, &len, msg);
49 if (nchars >= NAMESZ) {
56 if (*(tmpline + nchars - 1) != '\n') {
61 if (nchars > 0 && (*tmpline == '\n' || *tmpline == '-')) {
62 /* header/body separator found */
64 return m_getfld2(BODY2, f, msg);
67 f->namelen = copyname(f->name, tmpline);
73 /* copy the field's value */
74 if (f->alloclen <= nchars - f->namelen) {
75 f->value = mh_xrealloc(f->value, f->alloclen + len);
78 strcpy(f->value, tmpline + f->namelen + 1);
79 f->valuelen = nchars - f->namelen - 1;
81 while ((falted = is_falted(msg)) == B_TRUE) {
82 nchars = getline(&tmpline, &len, msg);
88 if (nchars >= NAMESZ) {
93 if (*(tmpline + nchars - 1) != '\n') {
98 if (f->alloclen - f->valuelen <= nchars) {
99 f->value = mh_xrealloc(f->value,
103 strcpy(f->value + f->valuelen, tmpline);
104 f->valuelen += nchars;
108 if (falted == FAIL) {
123 f->valuelen = nchars;
128 /* give error states back as received */
133 static enum threestate
139 if ((c = getc(msg)) < 0) {
152 if (ungetc(c, msg) != c) {
159 copyname(char *dst, char *src)
162 char *sep = strchr(src, ':');