From: Anicka Bernathova Date: Sat, 30 Aug 2008 16:20:56 +0000 (+0200) Subject: body can have 0 now X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=f3ec18ea41567f34c06fb9255ed30968ebacf049;p=umpf.git body can have 0 now --- diff --git a/brum.h b/brum.h index c3931b2..c72f7ac 100644 --- a/brum.h +++ b/brum.h @@ -94,6 +94,7 @@ struct hlist { struct email { struct list* headers; char* body; + int body_len; }; struct action { @@ -114,11 +115,11 @@ void save_current_headers(struct list* hash); /* ham.c */ struct list* current_headers; -char* current_body; +struct email* current_body; struct list* make_hlist(void); void print_headers(struct list* l); void do_action(struct action* a); -char* get_body(void); +struct email* get_body(void); /* lock.c */ void save_gids(void); diff --git a/ham.c b/ham.c index ddc8e3c..03102e7 100644 --- a/ham.c +++ b/ham.c @@ -72,10 +72,11 @@ make_hlist(void) return l; } -char* +struct email* get_body(void) { char* buf; + struct email* b; int c; int i = 0; int curbufsize = BUFSIZE; @@ -88,9 +89,11 @@ get_body(void) buf = xrealloc(buf, curbufsize *= 2); } - buf[i] = 0; + b = xmalloc(sizeof(struct email)); + b->body = buf; + b->body_len = i; - return buf; + return b; } void @@ -103,22 +106,23 @@ print_headers(struct list* l) } static void -write_char_to_mailbox(char c, int fd) +flush_mbox_buffer(int fd) { + if (mbox_write_err || !mbox_write_pos) + return; + int res; + res = write(fd, mbox_write_buf, mbox_write_pos); + if (res < 0) + mbox_write_err++; + mbox_write_pos = 0; +} - if (mbox_write_err) - return; +static void +write_char_to_mailbox(char c, int fd) +{ + int res; - if (!c){ - if (!mbox_write_pos) - return; - res = write(fd, mbox_write_buf, mbox_write_pos); - if (res < 0) - mbox_write_err++; - mbox_write_pos = 0; - return; - } if (mbox_write_pos >= BUFSIZE){ res = write(fd, mbox_write_buf, BUFSIZE); if (res < 0) @@ -137,7 +141,7 @@ copy_email(int fd, struct email* email) off_t mb_end; mb_end = lseek(fd, 0, SEEK_END); - if (mb_end < 1) + if (mb_end < 0) return -1; /* init globals */ @@ -164,12 +168,14 @@ copy_email(int fd, struct email* email) write_char_to_mailbox(*pc, fd); } - /* flush the buffer */ - write_char_to_mailbox(0, fd); + flush_mbox_buffer(fd); /* errors? */ - if (mbox_write_err) - return -1; + if (mbox_write_err){ + /* try to truncate to the original length */ + ftruncate(fd, mb_end); + return -1; + } return 0; } diff --git a/int.c b/int.c index 13a3077..80cf663 100644 --- a/int.c +++ b/int.c @@ -292,7 +292,9 @@ new_action(char* l, char* r, char* s, struct list* hash) modify_headers(current_headers, hash); a->e.headers = copy_headers(current_headers); - a->e.body = xstrdup(current_body); + a->e.body_len = current_body->body_len; + a->e.body = xmalloc(a->e.body_len); + memcpy(a->e.body, current_body->body, a->e.body_len); a->l = l; a->r = r; a->s = s;