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