]> mj.ucw.cz Git - umpf.git/blobdiff - umpf.c
cleanup in error messages
[umpf.git] / umpf.c
diff --git a/umpf.c b/umpf.c
index e97da9eadc89554a0346a57b707c13c294387664..e01aca26a1d1a042d342e466c9513ba5984f0be0 100644 (file)
--- a/umpf.c
+++ b/umpf.c
@@ -1,9 +1,13 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <pwd.h>
+#include <getopt.h>
 
 #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)