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