|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <ctype.h> ! 3: #include "mail.h" ! 4: #include "header.h" ! 5: #include "string.h" ! 6: ! 7: /* imported */ ! 8: extern char *malloc(); ! 9: extern char *strcpy(); ! 10: extern char *strchr(); ! 11: extern int strncmp(); ! 12: extern char *fgets(); ! 13: ! 14: /* predeclared */ ! 15: extern void addheader(); ! 16: static char *newstring(); ! 17: ! 18: /* ! 19: * Input header from standard input. Actually two extra lines are also ! 20: * read, but this isn't a problem. Save some header lines in the header ! 21: * array. ! 22: */ ! 23: extern void ! 24: getheader() ! 25: { ! 26: char buf[2*FROMLINESIZE]; ! 27: char line[2*FROMLINESIZE]; ! 28: header *hp; ! 29: ! 30: if (fgets(buf, sizeof buf, stdin) == NULL) ! 31: buf[0] = '\0'; ! 32: while (buf[0] != '\n' && buf[0] != '\0') { ! 33: /* gather a multiple line header field */ ! 34: line[0] = '\0'; ! 35: do { ! 36: (void)strcat(line, buf); ! 37: if (fgets(buf, sizeof buf, stdin) == NULL) ! 38: buf[0] = '\0'; ! 39: } while (buf[0] == ' ' || buf[0] == '\t'); ! 40: ! 41: /* header lines must contain `:' */ ! 42: if (strchr(line, ':') == NULL) { ! 43: addheader(line); ! 44: break; ! 45: } ! 46: ! 47: /* look for `important' headers */ ! 48: for (hp = hdrs; *(hp->name) != '\0'; hp++) { ! 49: if (STRCMP(line, hp) == 0) { ! 50: hp->line = newstring(line+(hp->size)); ! 51: break; ! 52: }; ! 53: } ! 54: if (*(hp->name) == '\0') ! 55: addheader(line); ! 56: } ! 57: /* at this point, buf contains the first line of the body */ ! 58: addheader(buf); ! 59: } ! 60: ! 61: static char* ! 62: newstring(line) ! 63: char *line; ! 64: { ! 65: char *rv; ! 66: ! 67: rv = malloc(strlen(line)+1); ! 68: if (rv == NULL) { ! 69: perror("reading header"); ! 70: exit(1); ! 71: } ! 72: return strcpy(rv, line); ! 73: } ! 74: ! 75: /* ! 76: * Keep a list of header lines to output. ! 77: */ ! 78: typedef struct quux{ ! 79: struct quux *next; ! 80: char line[1]; ! 81: } hlist; ! 82: static hlist dummy = { &dummy, '\0' }; ! 83: static hlist *list = &dummy; ! 84: ! 85: /* ! 86: * Add to list of header lines. ! 87: */ ! 88: extern void ! 89: addheader(line) ! 90: char *line; ! 91: { ! 92: hlist *thing; ! 93: ! 94: thing = (hlist *)malloc(strlen(line) + sizeof(hlist)); ! 95: if (thing == NULL) { ! 96: perror("reading header"); ! 97: exit(1); ! 98: } ! 99: strcpy(thing->line, line); ! 100: thing->next = list->next; ! 101: list->next = thing; ! 102: list = thing; ! 103: } ! 104: ! 105: /* ! 106: * Print list of headers. ! 107: */ ! 108: extern void ! 109: printheaders(fp) ! 110: FILE *fp; ! 111: { ! 112: hlist *thing; ! 113: ! 114: for(thing = list->next->next; thing != &dummy; thing = thing->next) ! 115: fprintf(fp, "%s", thing->line); ! 116: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.