struct email {
struct list* headers;
char* body;
+ int body_len;
};
struct action {
/* 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);
return l;
}
-char*
+struct email*
get_body(void)
{
char* buf;
+ struct email* b;
int c;
int i = 0;
int curbufsize = BUFSIZE;
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
}
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)
off_t mb_end;
mb_end = lseek(fd, 0, SEEK_END);
- if (mb_end < 1)
+ if (mb_end < 0)
return -1;
/* init globals */
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;
}
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;