]> mj.ucw.cz Git - umpf.git/commitdiff
fix reading email
authorAnicka Bernathova <anicka@anicka.net>
Tue, 21 Jul 2009 11:56:28 +0000 (13:56 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Tue, 21 Jul 2009 11:56:28 +0000 (13:56 +0200)
ham.c
umpf.c

diff --git a/ham.c b/ham.c
index 707e6f2664346336475030cb3b5fabd492b86d32..363a671e47747d0cb4310e0752a46b372f62c269 100644 (file)
--- 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 19910a4a28376c4e894ab5c62ec491b8d5fbaf76..c3ef9a7044a53d7291ee6c524a325eb114aef312 100644 (file)
--- 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);