From: Anicka Bernathova Date: Tue, 21 Jul 2009 11:56:28 +0000 (+0200) Subject: fix reading email X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=dfa8f2e9555ffa45f316179abf4a5bce00d991ef;p=umpf.git fix reading email --- diff --git a/ham.c b/ham.c index 707e6f2..363a671 100644 --- a/ham.c +++ b/ham.c @@ -37,8 +37,56 @@ new_header(char* buf, struct list* h) list_add_last(h, &new->car); } +int gmc_pos; +char gmc_buf[BUFSIZE]; +int gmc_init; +int gmc_read; +int gmc_unget; + +static int +give_me_char(int fd) +{ + if (! gmc_init) { + gmc_pos = 0; + gmc_read = read(fd, gmc_buf, BUFSIZE); + gmc_init = 1; + gmc_unget = EOF; + } + + if (gmc_unget != EOF) { + int a = gmc_unget; + gmc_unget = EOF; + return a; + } + + if (! gmc_read) { + gmc_init = 0; + return EOF; + } + + if (gmc_pos < gmc_read) { + return gmc_buf[gmc_pos++]; + } else { + gmc_pos = 0; + gmc_read = read(fd, gmc_buf, BUFSIZE); + if (! gmc_read) { + gmc_init = 0; + return EOF; + } + else { + return gmc_buf[gmc_pos++]; + } + } +} + +static void +unget_me(int c) +{ + gmc_unget = c; +} + struct list* -make_hlist(void) +make_hlist(int fd) { struct list* l = xmalloc(sizeof(struct list)); char* buf; @@ -49,7 +97,7 @@ make_hlist(void) buf = xmalloc(BUFSIZE); curbufsize = BUFSIZE; - while ((c = getchar()) != EOF){ + while ((c = give_me_char(fd)) != EOF){ if (c == '\r') continue; @@ -60,9 +108,9 @@ make_hlist(void) if (c == '\n'){ if (last == '\n') break; - if ((c = getchar()) != ' ' && c != '\t'){ + if ((c = give_me_char(fd)) != ' ' && c != '\t'){ if (c != EOF) - ungetc(c, stdin); + unget_me(c); buf[i] = 0; new_header(buf, l); i = 0; @@ -76,7 +124,7 @@ make_hlist(void) } struct email* -get_body(void) +get_body(int rfd) { char* buf; struct email* b; @@ -94,7 +142,7 @@ get_body(void) b->fd = -1; /* read mail body, if it is too big, try to make tmp file */ - while ((c = getchar()) != EOF){ + while ((c = give_me_char(rfd)) != EOF){ buf[i++] = c; if (i >= curbufsize - 1) { tmpfile = xstrdup("/tmp/umpf.XXXXXX"); @@ -120,7 +168,7 @@ get_body(void) /* save rest of the body to the tmpfile */ if (will_save) { int j = 0; - while ((c = getchar()) != EOF){ + while ((c = give_me_char(rfd)) != EOF){ buf[j++] = c; b->body_len++; if (j >= MAIL_LEN) { diff --git a/umpf.c b/umpf.c index 19910a4..c3ef9a7 100644 --- a/umpf.c +++ b/umpf.c @@ -48,9 +48,9 @@ main(int argc, char** argv) var_tab[i] = empty; } - current_headers = make_hlist(); + current_headers = make_hlist(0); // print_headers(current_headers); - current_body = get_body(); + current_body = get_body(0); save_current_headers(var_hash); interp(&input_code, var_hash);