Initial revision
[mmh] / sbr / context_read.c
1
2 /*
3  * context_read.c -- find and read profile and context files
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <errno.h>
10 #include <pwd.h>
11
12 extern int errno;
13
14 void
15 context_read (void)
16 {
17     pid_t pid;
18     register char *cp, *pp;
19     char buf[BUFSIZ];
20     struct stat st;
21     register struct passwd *pw;
22     register FILE *ib;
23
24     if (defpath)
25         return;
26
27     /*
28      * Find user's home directory
29      */
30     if (!mypath) {
31         if ((mypath = getenv ("HOME")))
32             mypath = getcpy (mypath);
33         else
34             if ((pw = getpwuid (getuid ())) == NULL
35                     || pw->pw_dir == NULL
36                     || *pw->pw_dir == 0)
37                 adios (NULL, "no HOME envariable");
38             else
39                 mypath = getcpy (pw->pw_dir);
40         if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
41             *cp = 0;
42     }
43
44     /*
45      * open and read user's profile
46      */
47     if ((cp = getenv ("MH")) && *cp != '\0') {
48         defpath = path (cp, TFILE);
49         if ((ib = fopen (defpath, "r")) == NULL)
50             adios (defpath, "unable to read");
51         if (*cp != '/')
52             m_putenv ("MH", defpath);
53     } else {
54         defpath = concat (mypath, "/", mh_profile, NULL);
55
56         if ((ib = fopen (defpath, "r")) == NULL) {
57             switch (pid = vfork ()) {
58                 case -1:
59                     adios ("fork", "unable to");
60
61                 case 0:
62                     setgid (getgid ());
63                     setuid (getuid ());
64
65                     execlp (installproc, "install-mh", "-auto", NULL);
66                     fprintf (stderr, "unable to exec ");
67                     perror (installproc);
68                     _exit (-1);
69
70                 default:
71                     if (pidwait (pid, 0)
72                             || (ib = fopen (defpath, "r")) == NULL)
73                         adios (NULL, "[install-mh aborted]");
74             }
75         }
76     }
77     readconfig (&m_defs, ib, mh_profile, 0);
78     fclose (ib);
79
80     /*
81      * Find user's nmh directory
82      */
83     if ((pp = context_find ("path")) && *pp != '\0') {
84         if (*pp != '/')
85             snprintf (buf, sizeof(buf), "%s/%s", mypath, pp);
86         else
87             strncpy (buf, pp, sizeof(buf));
88         if (stat(buf, &st) == -1) {
89             if (errno != ENOENT)
90                 adios (buf, "error opening");
91             cp = concat ("Your MH-directory \"", buf,
92                 "\" doesn't exist; Create it? ", NULL);
93             if (!getanswer(cp))
94                 adios (NULL, "unable to access MH-directory \"%s\"", buf);
95             free (cp);
96             if (!makedir (buf))
97                 adios (NULL, "unable to create", buf);
98         }
99     }
100
101     /*
102      * open and read user's context file
103      */
104     if (!(cp = getenv ("MHCONTEXT")) || *cp == '\0')
105         cp = context;
106     ctxpath = getcpy (m_maildir (cp));
107     if ((ib = fopen (ctxpath, "r"))) {
108         readconfig ((struct node **) 0, ib, cp, 1);
109         fclose (ib);
110     }
111 }