fio.c revision 1.2 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)fio.c 5.24 (Berkeley) 2/3/91";*/
36 static char rcsid[] = "$Id: fio.c,v 1.2 1993/08/01 18:13:07 mycroft Exp $";
37 #endif /* not lint */
38
39 #include "rcv.h"
40 #include <sys/stat.h>
41 #include <sys/file.h>
42 #include <sys/wait.h>
43 #include <paths.h>
44 #include <errno.h>
45
46 /*
47 * Mail -- a mail program
48 *
49 * File I/O.
50 */
51
52 /*
53 * Set up the input pointers while copying the mail file into /tmp.
54 */
55 setptr(ibuf)
56 register FILE *ibuf;
57 {
58 register int c, count;
59 register char *cp, *cp2;
60 struct message this;
61 FILE *mestmp;
62 off_t offset;
63 int maybe, inhead;
64 char linebuf[LINESIZE];
65
66 /* Get temporary file. */
67 (void)sprintf(linebuf, "%s/mail.XXXXXX", _PATH_TMP);
68 if ((c = mkstemp(linebuf)) == -1 ||
69 (mestmp = Fdopen(c, "r+")) == NULL) {
70 (void)fprintf(stderr, "mail: can't open %s\n", linebuf);
71 exit(1);
72 }
73 (void)unlink(linebuf);
74
75 msgCount = 0;
76 maybe = 1;
77 inhead = 0;
78 offset = 0;
79 this.m_flag = MUSED|MNEW;
80 this.m_size = 0;
81 this.m_lines = 0;
82 this.m_block = 0;
83 this.m_offset = 0;
84 for (;;) {
85 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
86 if (append(&this, mestmp)) {
87 perror("temporary file");
88 exit(1);
89 }
90 makemessage(mestmp);
91 return;
92 }
93 count = strlen(linebuf);
94 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
95 if (ferror(otf)) {
96 perror("/tmp");
97 exit(1);
98 }
99 linebuf[count - 1] = 0;
100 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
101 msgCount++;
102 if (append(&this, mestmp)) {
103 perror("temporary file");
104 exit(1);
105 }
106 this.m_flag = MUSED|MNEW;
107 this.m_size = 0;
108 this.m_lines = 0;
109 this.m_block = blockof(offset);
110 this.m_offset = offsetof(offset);
111 inhead = 1;
112 } else if (linebuf[0] == 0) {
113 inhead = 0;
114 } else if (inhead) {
115 for (cp = linebuf, cp2 = "status";; cp++) {
116 if ((c = *cp2++) == 0) {
117 while (isspace(*cp++))
118 ;
119 if (cp[-1] != ':')
120 break;
121 while (c = *cp++)
122 if (c == 'R')
123 this.m_flag |= MREAD;
124 else if (c == 'O')
125 this.m_flag &= ~MNEW;
126 inhead = 0;
127 break;
128 }
129 if (*cp != c && *cp != toupper(c))
130 break;
131 }
132 }
133 offset += count;
134 this.m_size += count;
135 this.m_lines++;
136 maybe = linebuf[0] == 0;
137 }
138 }
139
140 /*
141 * Drop the passed line onto the passed output buffer.
142 * If a write error occurs, return -1, else the count of
143 * characters written, including the newline.
144 */
145 putline(obuf, linebuf)
146 FILE *obuf;
147 char *linebuf;
148 {
149 register int c;
150
151 c = strlen(linebuf);
152 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
153 (void) putc('\n', obuf);
154 if (ferror(obuf))
155 return (-1);
156 return (c + 1);
157 }
158
159 /*
160 * Read up a line from the specified input into the line
161 * buffer. Return the number of characters read. Do not
162 * include the newline at the end.
163 */
164 readline(ibuf, linebuf, linesize)
165 FILE *ibuf;
166 char *linebuf;
167 {
168 register int n;
169
170 clearerr(ibuf);
171 if (fgets(linebuf, linesize, ibuf) == NULL)
172 return -1;
173 n = strlen(linebuf);
174 if (n > 0 && linebuf[n - 1] == '\n')
175 linebuf[--n] = '\0';
176 return n;
177 }
178
179 /*
180 * Return a file buffer all ready to read up the
181 * passed message pointer.
182 */
183 FILE *
184 setinput(mp)
185 register struct message *mp;
186 {
187
188 fflush(otf);
189 if (fseek(itf, positionof(mp->m_block, mp->m_offset), 0) < 0) {
190 perror("fseek");
191 panic("temporary file seek");
192 }
193 return (itf);
194 }
195
196 /*
197 * Take the data out of the passed ghost file and toss it into
198 * a dynamically allocated message structure.
199 */
200 makemessage(f)
201 FILE *f;
202 {
203 register size = (msgCount + 1) * sizeof (struct message);
204 off_t lseek();
205
206 if (message != 0)
207 free((char *) message);
208 if ((message = (struct message *) malloc((unsigned) size)) == 0)
209 panic("Insufficient memory for %d messages", msgCount);
210 dot = message;
211 size -= sizeof (struct message);
212 fflush(f);
213 (void) lseek(fileno(f), (long) sizeof *message, 0);
214 if (read(fileno(f), (char *) message, size) != size)
215 panic("Message temporary file corrupted");
216 message[msgCount].m_size = 0;
217 message[msgCount].m_lines = 0;
218 Fclose(f);
219 }
220
221 /*
222 * Append the passed message descriptor onto the temp file.
223 * If the write fails, return 1, else 0
224 */
225 append(mp, f)
226 struct message *mp;
227 FILE *f;
228 {
229 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
230 }
231
232 /*
233 * Delete a file, but only if the file is a plain file.
234 */
235 rm(name)
236 char *name;
237 {
238 struct stat sb;
239
240 if (stat(name, &sb) < 0)
241 return(-1);
242 if (!S_ISREG(sb.st_mode)) {
243 errno = EISDIR;
244 return(-1);
245 }
246 return(unlink(name));
247 }
248
249 static int sigdepth; /* depth of holdsigs() */
250 static int omask;
251 /*
252 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
253 */
254 holdsigs()
255 {
256
257 if (sigdepth++ == 0)
258 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
259 }
260
261 /*
262 * Release signals SIGHUP, SIGINT, and SIGQUIT.
263 */
264 relsesigs()
265 {
266
267 if (--sigdepth == 0)
268 sigsetmask(omask);
269 }
270
271 /*
272 * Determine the size of the file possessed by
273 * the passed buffer.
274 */
275 off_t
276 fsize(iob)
277 FILE *iob;
278 {
279 struct stat sbuf;
280
281 if (fstat(fileno(iob), &sbuf) < 0)
282 return 0;
283 return sbuf.st_size;
284 }
285
286 /*
287 * Evaluate the string given as a new mailbox name.
288 * Supported meta characters:
289 * % for my system mail box
290 * %user for user's system mail box
291 * # for previous file
292 * & invoker's mbox file
293 * +file file in folder directory
294 * any shell meta character
295 * Return the file name as a dynamic string.
296 */
297 char *
298 expand(name)
299 register char *name;
300 {
301 char xname[PATHSIZE];
302 char cmdbuf[PATHSIZE]; /* also used for file names */
303 register int pid, l;
304 register char *cp, *shell;
305 int pivec[2];
306 struct stat sbuf;
307 extern union wait wait_status;
308
309 /*
310 * The order of evaluation is "%" and "#" expand into constants.
311 * "&" can expand into "+". "+" can expand into shell meta characters.
312 * Shell meta characters expand into constants.
313 * This way, we make no recursive expansion.
314 */
315 switch (*name) {
316 case '%':
317 findmail(name[1] ? name + 1 : myname, xname);
318 return savestr(xname);
319 case '#':
320 if (name[1] != 0)
321 break;
322 if (prevfile[0] == 0) {
323 printf("No previous file\n");
324 return NOSTR;
325 }
326 return savestr(prevfile);
327 case '&':
328 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
329 name = "~/mbox";
330 /* fall through */
331 }
332 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
333 sprintf(xname, "%s/%s", cmdbuf, name + 1);
334 name = savestr(xname);
335 }
336 /* catch the most common shell meta character */
337 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
338 sprintf(xname, "%s%s", homedir, name + 1);
339 name = savestr(xname);
340 }
341 if (!anyof(name, "~{[*?$`'\"\\"))
342 return name;
343 if (pipe(pivec) < 0) {
344 perror("pipe");
345 return name;
346 }
347 sprintf(cmdbuf, "echo %s", name);
348 if ((shell = value("SHELL")) == NOSTR)
349 shell = _PATH_CSHELL;
350 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
351 if (pid < 0) {
352 close(pivec[0]);
353 close(pivec[1]);
354 return NOSTR;
355 }
356 close(pivec[1]);
357 l = read(pivec[0], xname, BUFSIZ);
358 close(pivec[0]);
359 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
360 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
361 return NOSTR;
362 }
363 if (l < 0) {
364 perror("read");
365 return NOSTR;
366 }
367 if (l == 0) {
368 fprintf(stderr, "\"%s\": No match.\n", name);
369 return NOSTR;
370 }
371 if (l == BUFSIZ) {
372 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
373 return NOSTR;
374 }
375 xname[l] = 0;
376 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
377 ;
378 cp[1] = '\0';
379 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
380 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
381 return NOSTR;
382 }
383 return savestr(xname);
384 }
385
386 /*
387 * Determine the current folder directory name.
388 */
389 getfold(name)
390 char *name;
391 {
392 char *folder;
393
394 if ((folder = value("folder")) == NOSTR)
395 return (-1);
396 if (*folder == '/')
397 strcpy(name, folder);
398 else
399 sprintf(name, "%s/%s", homedir, folder);
400 return (0);
401 }
402
403 /*
404 * Return the name of the dead.letter file.
405 */
406 char *
407 getdeadletter()
408 {
409 register char *cp;
410
411 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
412 cp = expand("~/dead.letter");
413 else if (*cp != '/') {
414 char buf[PATHSIZE];
415
416 (void) sprintf(buf, "~/%s", cp);
417 cp = expand(buf);
418 }
419 return cp;
420 }
421