#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sysexits.h>
#include <unistd.h>
{
char* buf;
struct email* b;
- int c, fd, cannot_save = 0;
+ int c, fd;
int will_save = 0;
int i = 0;
- int j = 0;
int curbufsize = MAIL_LEN;
char* tmpfile;
int res;
while ((c = getchar()) != EOF){
buf[i++] = c;
if (i >= curbufsize - 1) {
- if (cannot_save) {
- buf = xrealloc(buf, curbufsize *= 2);
- continue;
- }
tmpfile = xstrdup("/tmp/umpf.XXXXXX");
fd = mkstemp(tmpfile);
/* cannot create tmpfile, try to continue reading */
- if (fd < 0) {
- buf = xrealloc(buf, curbufsize *= 2);
- cannot_save = 1;
- free_string(tmpfile);
- continue;
- } else {
+ if (fd < 0)
+ bye(EX_TEMPFAIL, "%m");
+ else {
will_save = 1;
b->body = NULL;
b->tmpfile = tmpfile;
b->fd = fd;
+ res = write(fd, buf, MAIL_LEN);
+ if (res < MAIL_LEN) {
+ unlink(b->tmpfile);
+ bye(EX_TEMPFAIL, "%m");
+ }
break;
}
}
b->body_len = i;
/* save rest of the body to the tmpfile */
if (will_save) {
+ int j = 0;
while ((c = getchar()) != EOF){
buf[j++] = c;
b->body_len++;
j = 0;
res = write(fd, buf, MAIL_LEN);
if (res < MAIL_LEN) {
- int missing = MAIL_LEN - res;
- curbufsize = 2*b->body_len;
- buf = xrealloc(buf, curbufsize);
- // no point to check length here
- read(fd, buf, b->body_len - missing);
unlink(b->tmpfile);
- b->tmpfile = NULL;
- b->fd = -1;
- b->body = buf;
- break;
+ bye(EX_TEMPFAIL, "%m");
}
}
}
- /* could not write all the body to the tmpfile, try to read
- the rest */
- if (b->body) {
- while ((c = getchar()) != EOF){
- buf[j++] = c;
- b->body_len++;
- if (i >= curbufsize - 1) {
- buf = xrealloc(buf, curbufsize *= 2);
- }
- }
+ res = write(fd, buf, j);
+ if (res < j) {
+ unlink(b->tmpfile);
+ bye(EX_TEMPFAIL, "%m");
}
}
-
return b;
}
struct hlist* ph;
LIST_FOREACH(ph, email->headers){
written = write(fd, ph->name, strlen(ph->name));
- if (written < strlen(ph->name))
+ if (written < (ssize_t) strlen(ph->name))
return 1;
written = write(fd, ":", 1);
if (written < 1)
return 1;
written = write(fd, ph->value, strlen(ph->value));
- if (written < strlen(ph->value))
+ if (written < (ssize_t) strlen(ph->value))
return 1;
written = write(fd, "\n", 1);
if (written < 1)