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