]> diplodocus.org Git - nmh/blob - sbr/context_read.c
new.c: Order two return statements to match comment.
[nmh] / sbr / context_read.c
1 /* context_read.c -- find and read profile and context files
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 *
7 * This function must be called early on in any nmh utility, and
8 * may only be called once. It does the following:
9 *
10 * o Sets the global variable "mypath" to the home directory path.
11 *
12 * o Sets the global variable "defpath" to the absolute path of
13 * the profile file.
14 *
15 * o Reads in the profile file. Bails out if it can't.
16 *
17 * o Makes sure that the mail directory exists, prompting for
18 * creation if it doesn't.
19 *
20 * o Reads the context file either as set by the MHCONTEXT
21 * environment variable or by the profile.
22 */
23
24 #include <h/mh.h> /* mh internals */
25 #include "lock_file.h"
26 #include "m_maildir.h"
27 #include "makedir.h"
28 #include <pwd.h> /* structure for getpwuid() results */
29
30 void
31 context_read (void)
32 {
33 char buf[BUFSIZ]; /* path name buffer */
34 char *cp; /* miscellaneous pointer */
35 char *nd; /* nmh directory pointer */
36 struct stat st; /* stat() results */
37 struct passwd *pw; /* getpwuid() results */
38 FILE *ib; /* profile and context file pointer */
39 int failed_to_lock = 0;
40
41 /*
42 * If this routine _is_ called again (despite the warnings in the
43 * comments above), return immediately.
44 */
45 if ( m_defs != 0 )
46 return;
47
48 /*
49 * Find user's home directory. Try the HOME environment variable first,
50 * the home directory field in the password file if that's not found.
51 */
52
53 if ((mypath = getenv("HOME")) == NULL) {
54 if ((pw = getpwuid(getuid())) == NULL || *pw->pw_dir == '\0')
55 adios(NULL, "cannot determine your home directory");
56 mypath = pw->pw_dir;
57 }
58
59 /*
60 * Find and read user's profile. Check for the existence of an MH environment
61 * variable first with non-empty contents. Convert any relative path name
62 * found there to an absolute one. Look for the profile in the user's home
63 * directory if the MH environment variable isn't set.
64 */
65
66 if ((cp = getenv("MH")) && *cp != '\0') {
67 defpath = path(cp, TFILE);
68
69 /* defpath is an absolute path; make sure that always MH is, too. */
70 setenv("MH", defpath, 1);
71 if (stat(defpath, &st) != -1 && (st.st_mode & S_IFREG) == 0)
72 adios(NULL, "`%s' specified by your MH environment variable is not a normal file", cp);
73
74 if ((ib = fopen(defpath, "r")) == NULL)
75 adios(NULL, "unable to read the `%s' profile specified by your MH environment variable", defpath);
76 }
77 else {
78 defpath = concat(mypath, "/", mh_profile, NULL);
79
80 if ((ib = fopen(defpath, "r")) == NULL)
81 adios(NULL, "Doesn't look like nmh is installed. Run install-mh to do so.");
82
83 cp = mh_profile;
84 }
85
86 readconfig (&m_defs, ib, cp, 0);
87 fclose (ib);
88
89 /*
90 * Find the user's nmh directory, which is specified by the "path" profile component.
91 * Convert a relative path name to an absolute one rooted in the home directory.
92 */
93
94 if ((cp = context_find ("path")) == NULL)
95 adios(NULL, "Your %s file does not contain a path entry.", defpath);
96
97 if (*cp == '\0')
98 adios(NULL, "Your `%s' profile file does not contain a valid path entry.", defpath);
99
100 if (*cp != '/')
101 (void)snprintf (nd = buf, sizeof(buf), "%s/%s", mypath, cp);
102 else
103 nd = cp;
104
105 if (stat(nd, &st) == -1) {
106 if (errno != ENOENT)
107 adios (nd, "error opening");
108
109 cp = concat ("Your MH-directory \"", nd, "\" doesn't exist; Create it? ", NULL);
110
111 if (!read_yes_or_no_if_tty(cp))
112 adios (NULL, "unable to access MH-directory \"%s\"", nd);
113
114 free (cp);
115
116 if (!makedir (nd))
117 adios (NULL, "unable to create %s", nd);
118 }
119
120 else if ((st.st_mode & S_IFDIR) == 0)
121 adios (NULL, "`%s' is not a directory", nd);
122
123 /*
124 * Open and read user's context file. The name of the context file comes from the
125 * profile unless overridden by the MHCONTEXT environment variable.
126 */
127
128 if ((cp = getenv ("MHCONTEXT")) == NULL || *cp == '\0')
129 cp = context;
130
131 /* context is NULL if context_foil() was called to disable use of context
132 * We also support users setting explicitly setting MHCONTEXT to /dev/null.
133 * (if this wasn't special-cased then the locking would be liable to fail)
134 */
135 if (!cp || (strcmp(cp,"/dev/null") == 0)) {
136 ctxpath = NULL;
137 return;
138 }
139
140 ctxpath = getcpy (m_maildir (cp));
141
142 if ((ib = lkfopendata (ctxpath, "r", &failed_to_lock))) {
143 readconfig(NULL, ib, cp, 1);
144 lkfclosedata (ib, ctxpath);
145 }
146 }