From d354d049c4067efb4611a3d466e447bb2aceb85b Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Tue, 21 Jul 2009 16:59:51 +0200 Subject: [PATCH] cleanup in error messages --- ham.c | 10 ++++------ int.c | 4 ++-- lex.c | 6 ++++-- umpf.c | 48 ++++++++++++++++++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/ham.c b/ham.c index 1441451..3e6dade 100644 --- a/ham.c +++ b/ham.c @@ -154,7 +154,7 @@ get_body(int rfd) fd = mkstemp(tmpfile); /* cannot create tmpfile, try to continue reading */ if (fd < 0) - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot create temporary file: %m"); else { will_save = 1; b->body = NULL; @@ -163,7 +163,7 @@ get_body(int rfd) res = write(fd, buf, MAIL_LEN); if (res < MAIL_LEN) { unlink(b->tmpfile); - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot write to remporary file: %m"); } break; } @@ -181,14 +181,14 @@ get_body(int rfd) res = write(fd, buf, MAIL_LEN); if (res < MAIL_LEN) { unlink(b->tmpfile); - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot write to temporary file: %m"); } } } res = write(fd, buf, j); if (res < j) { unlink(b->tmpfile); - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot write to temporary file: %m"); } } return b; @@ -343,8 +343,6 @@ copy_email(int fd, struct email* email) } write_char_to_mailbox('\n', fd); - /* body */ - /* FIXME: do not forget change Content-Length */ if (email->body) { for (pc = email->body; pc < email->body + email->body_len; pc++){ write_char_to_mailbox(*pc, fd); diff --git a/int.c b/int.c index 56861dd..d2146d7 100644 --- a/int.c +++ b/int.c @@ -451,7 +451,7 @@ deliver(char* where, int copy, struct list* hash) if (!copy) { if (res) - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot save mail to mailbox %s: %m", where); else bye(0, NULL); } @@ -486,7 +486,7 @@ end: destroy_email(em); if (!copy) { if (res) - bye(EX_TEMPFAIL, "%m"); + bye(EX_TEMPFAIL, "Cannot forward e-mail: %m"); else bye(0, NULL); } diff --git a/lex.c b/lex.c index 4e4a103..aaf10d0 100644 --- a/lex.c +++ b/lex.c @@ -28,8 +28,10 @@ read_conf(char* filename) { conf = fopen(filename, "r"); - if (! conf) - die("read_conf: %m"); + if (! conf) { + fprintf(stderr, "Error reading config file: %m\nSaving to default mailbox %s\n", default_mailbox); + longjmp(env, 1); + } } void __attribute__ ((noreturn)) diff --git a/umpf.c b/umpf.c index e97da9e..e01aca2 100644 --- a/umpf.c +++ b/umpf.c @@ -1,9 +1,13 @@ #include +#include #include #include +#include #include "umpf.h" +#define DEFAULT_CONF ".umpf" + void init(void) { @@ -18,28 +22,48 @@ init(void) int main(int argc, char** argv) { - int res; - int i; + int res, i, opt; + char* conffile = NULL; - if (argc < 2) - die("Usage: ./umpf conf_file [default_mailbox]"); + while ((opt = getopt(argc, argv, "c:m:")) != -1) { + switch (opt) { + case 'm': + default_mailbox = optarg; + break; + case 'c': + conffile = optarg; + break; + default: + die("Usage: ./umpf [-c conf_file] [-m default_mailbox]"); + } + } - struct passwd* p; - p = getpwuid(getuid()); - if (argc < 3) - default_mailbox = cat("/var/mail/", p->pw_name); - else - default_mailbox = argv[2]; + if (!default_mailbox) { + struct passwd* p; - save_gids(); + p = getpwuid(getuid()); + default_mailbox = cat("/var/mail/", p->pw_name); + } + save_gids(); init(); /* returning from longjump? save the mail and exit */ if (setjmp(env)) goto skip_conf; - read_conf(argv[1]); + if (! conffile) { + int len; + char* home; + + home = getenv("HOME"); + if (! home) + goto skip_conf; + + conffile = xmalloc(strlen(home) + strlen(DEFAULT_CONF) + 1); + sprintf(conffile, "%s/%s", home, DEFAULT_CONF); + } + read_conf(conffile); res = yyparse (); if (res) -- 2.39.2