]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/sbr/m_getdefs.c
sbr/mts.c: Delete mmdlm2; use same-valued mmdlm1 instead.
[nmh] / docs / historical / mh-6.8.5 / sbr / m_getdefs.c
1 /* m_getdefs.c - read the user's MH environment */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: m_getdefs.c,v 1.8 1993/09/03 17:38:51 jromine Exp $";
4 #endif /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <pwd.h>
12
13
14 extern int errno;
15
16 #ifndef __STDC__
17 #ifdef SYS5
18 struct passwd *getpwuid ();
19 #endif
20 #endif /* !__STDC__ */
21
22
23 #ifndef OVERHEAD
24 #define FOpen(f,m,e,c) fopen (f, m)
25 #define FClose(f) fclose (f)
26 #else /* OVERHEAD */
27 static FILE *FOpen ();
28 static int FClose ();
29
30 off_t lseek ();
31 #endif /* OVERHEAD */
32
33
34 void m_getdefs() {
35 int pid;
36 register char *cp,
37 *pp;
38 #if defined (notdef) || defined (COMPAT)
39 register char *ctx;
40 #endif /* notdef or COMPAT */
41 char buf[BUFSIZ];
42 struct stat st;
43 register struct passwd *pw;
44 register FILE *ib;
45
46 if (defpath)
47 return;
48 if (mypath == NULL) {
49 if (mypath = getenv ("HOME"))
50 mypath = getcpy (mypath);
51 else
52 if ((pw = getpwuid (getuid ())) == NULL
53 || pw -> pw_dir == NULL
54 || *pw -> pw_dir == 0)
55 adios (NULLCP, "no HOME envariable");
56 else
57 mypath = getcpy (pw -> pw_dir);
58 if ((cp = mypath + strlen (mypath) - 1) > mypath && *cp == '/')
59 *cp = 0;
60 }
61
62 if ((cp = getenv ("MH")) && *cp) {
63 defpath = path (cp, TFILE);
64 if ((ib = FOpen (defpath, "r", "MHFD", 0)) == NULL)
65 adios (defpath, "unable to read");
66 if (*cp != '/')
67 (void) m_putenv ("MH", defpath);
68 }
69 else {
70 defpath = concat (mypath, "/", mh_profile, NULLCP);
71
72 if ((ib = FOpen (defpath, "r", "MHFD", 0)) == NULL)
73 switch (pid = vfork ()) {
74 case NOTOK:
75 adios ("fork", "unable to");
76
77 case OK:
78 (void) setgid (getgid ());
79 (void) setuid (getuid ());
80
81 execlp (installproc, "install-mh", "-auto", NULLCP);
82 fprintf (stderr, "unable to exec ");
83 perror (installproc);
84 _exit (-1);
85
86 default:
87 if (pidwait (pid, OK)
88 || (ib = fopen (defpath, "r")) == NULL)
89 adios (NULLCP, "[install-mh aborted]");
90 }
91 }
92
93 #if defined (notdef) || defined (COMPAT)
94 ctx = context;
95 #endif /* notdef or COMPAT */
96 m_readefs (&m_defs, ib, mh_profile, 0);
97 (void) FClose (ib);
98
99 if ((pp = m_find ("path")) != NULL && *pp) {
100 if (*pp != '/')
101 (void) sprintf (buf, "%s/%s", mypath, pp);
102 else
103 (void) strcpy (buf, pp);
104 if (stat(buf, &st) == NOTOK) {
105 if (errno != ENOENT)
106 adios (buf, "error opening");
107 cp = concat ("Your MH-directory \"", buf,
108 "\" doesn't exist; Create it? ", NULLCP);
109 if (!getanswer(cp))
110 adios (NULLCP, "unable to access MH-directory \"%s\"", buf);
111 free (cp);
112 if (!makedir (buf))
113 adios (NULLCP, "unable to create", buf);
114 }
115 }
116
117 #ifdef COMPAT
118 if (strcmp (ctx, "/dev/null") == 0)
119 return; /* called by install-mh */
120
121 if (access (ctxpath = getcpy (m_maildir (ctx)), 0) == NOTOK)
122 switch (pid = fork ()) {
123 case NOTOK:
124 adios ("fork", "unable to");
125
126 case OK:
127 (void) setgid (getgid ());
128 (void) setuid (getuid ());
129 fprintf (stderr, "install-mh -compat\n");
130
131 execlp (installproc, "install-mh", "-compat", NULLCP);
132 fprintf (stderr, "unable to exec ");
133 perror (installproc);
134 _exit (-1);
135
136 default:
137 if (pidwait (pid, OK) || access (ctxpath, 0) == NOTOK)
138 adios (NULLCP, "[install-mh aborted]");
139
140 if ((ib = fopen (defpath, "r")) == NULL)
141 adios (defpath, "unable to read");
142 m_readefs (&m_defs, ib, mh_profile, 0);
143 (void) FClose (ib);
144 }
145 free (ctxpath);
146 #endif /* COMPAT */
147
148 if ((cp = getenv ("MHCONTEXT")) == NULL || *cp == 0)
149 cp = context;
150 if ((ib = FOpen (ctxpath = getcpy (m_maildir (cp)), "r", "MHCONTEXTFD", 1))
151 == NULL) {
152 #ifdef notdef /* XXX */
153 if (cp != ctx)
154 adios (ctxpath, "unable to read");
155 #endif /* notdef */
156 }
157 else {
158 m_readefs ((struct node **) 0, ib, cp, 1);
159 (void) FClose (ib);
160 }
161 }
162
163 /* \f */
164
165 #ifdef OVERHEAD
166 int fd_def = NOTOK;
167 int fd_ctx = NOTOK;
168
169
170 static FILE *FOpen (filename, mode, envariable, ctx)
171 register char *filename,
172 *mode,
173 *envariable;
174 register int ctx;
175 {
176 register int fd;
177 register char *cp;
178 char buffer[10];
179 struct stat st1;
180 register FILE * fp;
181
182 if ((cp = getenv (envariable)) == NULL || *cp == 0)
183 goto not_open;
184
185 if ((fd = atoi (cp)) <= fileno (stderr)) {
186 advise (NULLCP, "bad value for $%s: %s", envariable, cp);
187 (void) unputenv (envariable);
188 return fopen (filename, mode);
189 }
190 if (ctx)
191 fd_ctx = fd;
192 else
193 fd_def = fd;
194
195 if (fstat (fd, &st1) == NOTOK) {
196 not_open: ;
197 if ((fp = fopen (filename, mode))
198 && !strcmp (mode, "r")
199 && fileno (fp) > fileno (stderr)) {
200 (void) sprintf (buffer, "%d", fileno (fp));
201 if (!m_putenv (envariable, buffer))
202 if (ctx)
203 fd_ctx = fileno (fp);
204 else
205 fd_def = fileno (fp);
206 }
207
208 if ((cp = getenv ("MHFDEBUG")) && *cp)
209 fprintf (stderr, "FOpen of %s sets %d\n", filename, fileno (fp));
210 return fp;
211 }
212
213 (void) lseek (fd, (off_t)0, 0);
214 return fdopen (fd, mode);
215 }
216
217
218 static int FClose (f)
219 register FILE *f;
220 {
221 register int d1,
222 d2,
223 i;
224 register char *cp;
225
226 if (f == NULL)
227 return OK;
228
229 if ((d1 = fileno (f)) != fd_def && d1 != fd_ctx)
230 return fclose (f);
231
232 d2 = dup (d1);
233 i = fclose (f);
234
235 if (d2 != NOTOK) {
236 (void) dup2 (d2, d1);
237 (void) close (d2);
238 }
239 else
240 if (d1 == fd_def)
241 fd_def = NOTOK;
242 else
243 fd_ctx = NOTOK;
244
245 if ((cp = getenv ("MHFDEBUG")) && *cp)
246 fprintf (stderr, "FClose emulating close of %d (%d)\n", d1, d2);
247 return i;
248 }
249 #endif /* OVERHEAD */