]> mj.ucw.cz Git - umpf.git/blobdiff - umpf.c
umpf.1 done
[umpf.git] / umpf.c
diff --git a/umpf.c b/umpf.c
index e97da9eadc89554a0346a57b707c13c294387664..663294f752b1d21aa4c193bef4737238068c925f 100644 (file)
--- a/umpf.c
+++ b/umpf.c
@@ -1,9 +1,15 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <pwd.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "umpf.h"
 
+#define DEFAULT_CONF ".umpf"
+
 void
 init(void)
 {
@@ -15,31 +21,124 @@ init(void)
        empty = "";
 }
 
+char* buf;
+int buflen;
+char tmpbuf[BUFSIZE];
+int tmpbufpos;
+int tmpread;
+
+char*
+read_line(int fd)
+{
+       if (! buflen)
+               buf = xmalloc(BUFSIZE);
+       char* p = buf;
+       do {
+               if (tmpbufpos >= tmpread) {
+                       tmpread = read(fd, tmpbuf, BUFSIZE);
+                       tmpbufpos = 0;
+               }
+               if (!tmpread)
+                       return NULL;    
+               if (p >= (buf + buflen - 1))
+                       buf = xrealloc(buf, buflen *= 2);
+               *p++ = tmpbuf[tmpbufpos++];
+       } while (*p != '\n');
+       *++p = 0; 
+
+       return buf;     
+}
+
+char*
+get_home_from_passwd(void)
+{
+       struct passwd* pas;
+       pas = getpwuid(getuid());
+       if (! pas)
+               die("Cannot get uid, please specify config file path");
+
+       char* login = pas->pw_name;
+       int len = strlen(pas->pw_name);
+       char* buf;
+       char* p, *q;
+       int r, i;
+
+       int fd;
+       fd = open("/etc/passwd", O_RDONLY);
+       if (fd < 0)
+               die("Cannot open /etc/passwd, please specify config file path");
+
+       do {
+               buf = read_line(fd);
+               r = strncmp(buf, login, len);
+               if (!r)
+                       break;
+       } while (buf);
+       if (!r)
+               die("Cannot find your login %s in /etc/passwd, please specify config file path", login);
+       p = buf;
+       for (i = 0; i < 5; i++) {
+               p = strchr(p, ':');
+               if (! p)
+                       die("Cannot parse /etc/passwd, please specify config file path");
+       }       
+       q = p + 1;
+       while (*q && *q != '\n' && *q != ':')
+               q++;
+       *q = 0;
+       
+       return xstrdup(p + 1);
+}
+
 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());
+               if (! p)
+                       die("Cannot get uid, please specify default mailbox");
+               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)
+                       home = get_home_from_passwd();
+               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)
@@ -49,9 +148,10 @@ main(int argc, char** argv)
        compile(input_tree, NULL);
 
 skip_conf:
-       var_tab = xmalloc((max_varcode + 1) * sizeof(char*));
+       var_tab = xmalloc((max_varcode + 1) * sizeof(struct vartab));
        for (i = 0; i <= max_varcode; i++) {
-               var_tab[i] = empty;
+               var_tab[i].value = empty;
+               var_tab[i].modif = 0;
        }       
 
        current_headers = make_hlist(0);