fio.c revision 88227
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3574769Smikeh#if 0
3688150Smikehstatic char sccsid[] = "@(#)fio.c	8.2 (Berkeley) 4/20/95";
3774769Smikeh#endif
3874769Smikehstatic const char rcsid[] =
3974769Smikeh  "$FreeBSD: head/usr.bin/mail/fio.c 88227 2001-12-19 21:50:22Z ache $";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes#include "rcv.h"
431590Srgrimes#include <sys/file.h>
441590Srgrimes#include <sys/wait.h>
451590Srgrimes
461590Srgrimes#include <unistd.h>
471590Srgrimes#include <paths.h>
481590Srgrimes#include <errno.h>
491590Srgrimes#include "extern.h"
501590Srgrimes
511590Srgrimes/*
521590Srgrimes * Mail -- a mail program
531590Srgrimes *
541590Srgrimes * File I/O.
551590Srgrimes */
561590Srgrimes
5777274Smikehextern int wait_status;
5877274Smikeh
591590Srgrimes/*
601590Srgrimes * Set up the input pointers while copying the mail file into /tmp.
611590Srgrimes */
621590Srgrimesvoid
6388150Smikehsetptr(ibuf, offset)
6477274Smikeh	FILE *ibuf;
6588150Smikeh	off_t offset;
661590Srgrimes{
6777274Smikeh	int c, count;
6877274Smikeh	char *cp, *cp2;
691590Srgrimes	struct message this;
701590Srgrimes	FILE *mestmp;
711590Srgrimes	int maybe, inhead;
7274769Smikeh	char linebuf[LINESIZE], pathbuf[PATHSIZE];
7388150Smikeh	int omsgCount;
741590Srgrimes
751590Srgrimes	/* Get temporary file. */
7674769Smikeh	(void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
7774769Smikeh	if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
7874769Smikeh		err(1, "can't open %s", pathbuf);
7977274Smikeh	(void)rm(pathbuf);
801590Srgrimes
8188150Smikeh	if (offset == 0) {
8288150Smikeh		 msgCount = 0;
8388150Smikeh	} else {
8488150Smikeh		/* Seek into the file to get to the new messages */
8588227Sache		(void)fseeko(ibuf, offset, SEEK_SET);
8688150Smikeh		/*
8788150Smikeh		 * We need to make "offset" a pointer to the end of
8888150Smikeh		 * the temp file that has the copy of the mail file.
8988150Smikeh		 * If any messages have been edited, this will be
9088150Smikeh		 * different from the offset into the mail file.
9188150Smikeh		 */
9288227Sache		(void)fseeko(otf, (off_t)0, SEEK_END);
9388227Sache		offset = ftello(otf);
9488150Smikeh	}
9588150Smikeh	omsgCount = msgCount;
961590Srgrimes	maybe = 1;
971590Srgrimes	inhead = 0;
981590Srgrimes	this.m_flag = MUSED|MNEW;
991590Srgrimes	this.m_size = 0;
1001590Srgrimes	this.m_lines = 0;
1011590Srgrimes	this.m_block = 0;
1021590Srgrimes	this.m_offset = 0;
1031590Srgrimes	for (;;) {
10474769Smikeh		if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
10574769Smikeh			if (append(&this, mestmp))
10674769Smikeh				errx(1, "temporary file");
10788150Smikeh			makemessage(mestmp, omsgCount);
1081590Srgrimes			return;
1091590Srgrimes		}
1101590Srgrimes		count = strlen(linebuf);
11176455Smikeh		/*
11276455Smikeh		 * Transforms lines ending in <CR><LF> to just <LF>.
11376455Smikeh		 * This allows mail to be able to read Eudora mailboxes.
11476455Smikeh		 */
11576455Smikeh		if (count >= 2 && linebuf[count - 1] == '\n' &&
11676455Smikeh		    linebuf[count - 2] == '\r') {
11776455Smikeh			count--;
11876455Smikeh			linebuf[count - 1] = '\n';
11976455Smikeh		}
12076455Smikeh
12177274Smikeh		(void)fwrite(linebuf, sizeof(*linebuf), count, otf);
12274769Smikeh		if (ferror(otf))
12374769Smikeh			errx(1, "/tmp");
12474769Smikeh		if (count)
12574769Smikeh			linebuf[count - 1] = '\0';
1261590Srgrimes		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
1271590Srgrimes			msgCount++;
12874769Smikeh			if (append(&this, mestmp))
12974769Smikeh				errx(1, "temporary file");
1301590Srgrimes			this.m_flag = MUSED|MNEW;
1311590Srgrimes			this.m_size = 0;
1321590Srgrimes			this.m_lines = 0;
1331590Srgrimes			this.m_block = blockof(offset);
13467496Sphk			this.m_offset = boffsetof(offset);
1351590Srgrimes			inhead = 1;
1361590Srgrimes		} else if (linebuf[0] == 0) {
1371590Srgrimes			inhead = 0;
1381590Srgrimes		} else if (inhead) {
1391590Srgrimes			for (cp = linebuf, cp2 = "status";; cp++) {
14077274Smikeh				if ((c = *cp2++) == '\0') {
14188227Sache					while (isspace((unsigned char)*cp++))
1421590Srgrimes						;
1431590Srgrimes					if (cp[-1] != ':')
1441590Srgrimes						break;
14577274Smikeh					while ((c = *cp++) != '\0')
1461590Srgrimes						if (c == 'R')
1471590Srgrimes							this.m_flag |= MREAD;
1481590Srgrimes						else if (c == 'O')
1491590Srgrimes							this.m_flag &= ~MNEW;
1501590Srgrimes					inhead = 0;
1511590Srgrimes					break;
1521590Srgrimes				}
15388227Sache				if (*cp != c && *cp != toupper((unsigned char)c))
1541590Srgrimes					break;
1551590Srgrimes			}
1561590Srgrimes		}
1571590Srgrimes		offset += count;
1581590Srgrimes		this.m_size += count;
1591590Srgrimes		this.m_lines++;
1601590Srgrimes		maybe = linebuf[0] == 0;
1611590Srgrimes	}
1621590Srgrimes}
1631590Srgrimes
1641590Srgrimes/*
1651590Srgrimes * Drop the passed line onto the passed output buffer.
1661590Srgrimes * If a write error occurs, return -1, else the count of
16788150Smikeh * characters written, including the newline if requested.
1681590Srgrimes */
1691590Srgrimesint
17088150Smikehputline(obuf, linebuf, outlf)
1711590Srgrimes	FILE *obuf;
1721590Srgrimes	char *linebuf;
17388150Smikeh	int outlf;
1741590Srgrimes{
17577274Smikeh	int c;
1761590Srgrimes
1771590Srgrimes	c = strlen(linebuf);
17877274Smikeh	(void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
17988150Smikeh	if (outlf) {
18088150Smikeh		fprintf(obuf, "\n");
18188150Smikeh		c++;
18288150Smikeh	}
1831590Srgrimes	if (ferror(obuf))
1841590Srgrimes		return (-1);
18588150Smikeh	return (c);
1861590Srgrimes}
1871590Srgrimes
1881590Srgrimes/*
1891590Srgrimes * Read up a line from the specified input into the line
1901590Srgrimes * buffer.  Return the number of characters read.  Do not
19176455Smikeh * include the newline (or carriage return) at the end.
1921590Srgrimes */
1931590Srgrimesint
1941590Srgrimesreadline(ibuf, linebuf, linesize)
1951590Srgrimes	FILE *ibuf;
1961590Srgrimes	char *linebuf;
1971590Srgrimes	int linesize;
1981590Srgrimes{
19977274Smikeh	int n;
2001590Srgrimes
2011590Srgrimes	clearerr(ibuf);
2021590Srgrimes	if (fgets(linebuf, linesize, ibuf) == NULL)
20377274Smikeh		return (-1);
2041590Srgrimes	n = strlen(linebuf);
2051590Srgrimes	if (n > 0 && linebuf[n - 1] == '\n')
2061590Srgrimes		linebuf[--n] = '\0';
20776455Smikeh	if (n > 0 && linebuf[n - 1] == '\r')
20876455Smikeh		linebuf[--n] = '\0';
20977274Smikeh	return (n);
2101590Srgrimes}
2111590Srgrimes
2121590Srgrimes/*
2131590Srgrimes * Return a file buffer all ready to read up the
2141590Srgrimes * passed message pointer.
2151590Srgrimes */
2161590SrgrimesFILE *
2171590Srgrimessetinput(mp)
21877274Smikeh	struct message *mp;
2191590Srgrimes{
2201590Srgrimes
22177274Smikeh	(void)fflush(otf);
22282793Sache	if (fseeko(itf,
22382793Sache		   positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
22488227Sache		err(1, "fseeko");
2251590Srgrimes	return (itf);
2261590Srgrimes}
2271590Srgrimes
2281590Srgrimes/*
2291590Srgrimes * Take the data out of the passed ghost file and toss it into
2301590Srgrimes * a dynamically allocated message structure.
2311590Srgrimes */
2321590Srgrimesvoid
23388150Smikehmakemessage(f, omsgCount)
2341590Srgrimes	FILE *f;
23588150Smikeh	int omsgCount;
2361590Srgrimes{
23788150Smikeh	size_t size;
23888150Smikeh	struct message *nmessage;
2391590Srgrimes
24088150Smikeh	size = (msgCount + 1) * sizeof(struct message);
24188150Smikeh	nmessage = (struct message *)realloc(message, size);
24288150Smikeh	if (nmessage == NULL)
24388150Smikeh		errx(1, "Insufficient memory for %d messages\n",
24488150Smikeh		    msgCount);
24588150Smikeh	if (omsgCount == 0 || message == NULL)
24688150Smikeh		dot = nmessage;
24788150Smikeh	else
24888150Smikeh		dot = nmessage + (dot - message);
24988150Smikeh	message = nmessage;
25088150Smikeh	size -= (omsgCount + 1) * sizeof(struct message);
25177274Smikeh	(void)fflush(f);
25277274Smikeh	(void)lseek(fileno(f), (off_t)sizeof(*message), 0);
25388150Smikeh	if (read(fileno(f), (char *)&message[omsgCount], size) != size)
25474769Smikeh		errx(1, "Message temporary file corrupted");
2551590Srgrimes	message[msgCount].m_size = 0;
2561590Srgrimes	message[msgCount].m_lines = 0;
25777274Smikeh	(void)Fclose(f);
2581590Srgrimes}
2591590Srgrimes
2601590Srgrimes/*
2611590Srgrimes * Append the passed message descriptor onto the temp file.
2621590Srgrimes * If the write fails, return 1, else 0
2631590Srgrimes */
2641590Srgrimesint
2651590Srgrimesappend(mp, f)
2661590Srgrimes	struct message *mp;
2671590Srgrimes	FILE *f;
2681590Srgrimes{
26977274Smikeh	return (fwrite((char *)mp, sizeof(*mp), 1, f) != 1);
2701590Srgrimes}
2711590Srgrimes
2721590Srgrimes/*
2731590Srgrimes * Delete a file, but only if the file is a plain file.
2741590Srgrimes */
2751590Srgrimesint
2761590Srgrimesrm(name)
2771590Srgrimes	char *name;
2781590Srgrimes{
2791590Srgrimes	struct stat sb;
2801590Srgrimes
2811590Srgrimes	if (stat(name, &sb) < 0)
28277274Smikeh		return (-1);
2831590Srgrimes	if (!S_ISREG(sb.st_mode)) {
2841590Srgrimes		errno = EISDIR;
28577274Smikeh		return (-1);
2861590Srgrimes	}
28777274Smikeh	return (unlink(name));
2881590Srgrimes}
2891590Srgrimes
2901590Srgrimesstatic int sigdepth;		/* depth of holdsigs() */
29188150Smikehstatic sigset_t nset, oset;
2921590Srgrimes/*
2931590Srgrimes * Hold signals SIGHUP, SIGINT, and SIGQUIT.
2941590Srgrimes */
2951590Srgrimesvoid
2961590Srgrimesholdsigs()
2971590Srgrimes{
2981590Srgrimes
29988150Smikeh	if (sigdepth++ == 0) {
30088150Smikeh		(void)sigemptyset(&nset);
30188150Smikeh		(void)sigaddset(&nset, SIGHUP);
30288150Smikeh		(void)sigaddset(&nset, SIGINT);
30388150Smikeh		(void)sigaddset(&nset, SIGQUIT);
30488150Smikeh		(void)sigprocmask(SIG_BLOCK, &nset, &oset);
30588150Smikeh	}
3061590Srgrimes}
3071590Srgrimes
3081590Srgrimes/*
3091590Srgrimes * Release signals SIGHUP, SIGINT, and SIGQUIT.
3101590Srgrimes */
3111590Srgrimesvoid
3121590Srgrimesrelsesigs()
3131590Srgrimes{
3141590Srgrimes
3151590Srgrimes	if (--sigdepth == 0)
31688150Smikeh		(void)sigprocmask(SIG_SETMASK, &oset, NULL);
3171590Srgrimes}
3181590Srgrimes
3191590Srgrimes/*
3201590Srgrimes * Determine the size of the file possessed by
3211590Srgrimes * the passed buffer.
3221590Srgrimes */
3231590Srgrimesoff_t
3241590Srgrimesfsize(iob)
3251590Srgrimes	FILE *iob;
3261590Srgrimes{
3271590Srgrimes	struct stat sbuf;
3281590Srgrimes
3291590Srgrimes	if (fstat(fileno(iob), &sbuf) < 0)
33077274Smikeh		return (0);
33177274Smikeh	return (sbuf.st_size);
3321590Srgrimes}
3331590Srgrimes
3341590Srgrimes/*
3351590Srgrimes * Evaluate the string given as a new mailbox name.
3361590Srgrimes * Supported meta characters:
3371590Srgrimes *	%	for my system mail box
3381590Srgrimes *	%user	for user's system mail box
3391590Srgrimes *	#	for previous file
3401590Srgrimes *	&	invoker's mbox file
3411590Srgrimes *	+file	file in folder directory
3421590Srgrimes *	any shell meta character
3431590Srgrimes * Return the file name as a dynamic string.
3441590Srgrimes */
3451590Srgrimeschar *
3461590Srgrimesexpand(name)
34777274Smikeh	char *name;
3481590Srgrimes{
3491590Srgrimes	char xname[PATHSIZE];
3501590Srgrimes	char cmdbuf[PATHSIZE];		/* also used for file names */
35177274Smikeh	int pid, l;
35277274Smikeh	char *cp, *sh;
3531590Srgrimes	int pivec[2];
3541590Srgrimes	struct stat sbuf;
3551590Srgrimes
3561590Srgrimes	/*
3571590Srgrimes	 * The order of evaluation is "%" and "#" expand into constants.
3581590Srgrimes	 * "&" can expand into "+".  "+" can expand into shell meta characters.
3591590Srgrimes	 * Shell meta characters expand into constants.
3601590Srgrimes	 * This way, we make no recursive expansion.
3611590Srgrimes	 */
3621590Srgrimes	switch (*name) {
3631590Srgrimes	case '%':
36474769Smikeh		findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
36577274Smikeh		return (savestr(xname));
3661590Srgrimes	case '#':
3671590Srgrimes		if (name[1] != 0)
3681590Srgrimes			break;
3691590Srgrimes		if (prevfile[0] == 0) {
3701590Srgrimes			printf("No previous file\n");
37177274Smikeh			return (NULL);
3721590Srgrimes		}
37377274Smikeh		return (savestr(prevfile));
3741590Srgrimes	case '&':
37577274Smikeh		if (name[1] == 0 && (name = value("MBOX")) == NULL)
3761590Srgrimes			name = "~/mbox";
3771590Srgrimes		/* fall through */
3781590Srgrimes	}
37974769Smikeh	if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
38077274Smikeh		(void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
3811590Srgrimes		name = savestr(xname);
3821590Srgrimes	}
3831590Srgrimes	/* catch the most common shell meta character */
38474769Smikeh	if (name[0] == '~' && homedir != NULL &&
38574769Smikeh	    (name[1] == '/' || name[1] == '\0')) {
38677274Smikeh		(void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
3871590Srgrimes		name = savestr(xname);
3881590Srgrimes	}
38974769Smikeh	if (!strpbrk(name, "~{[*?$`'\"\\"))
39077274Smikeh		return (name);
3911590Srgrimes	if (pipe(pivec) < 0) {
39274769Smikeh		warn("pipe");
39377274Smikeh		return (name);
3941590Srgrimes	}
39577274Smikeh	(void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
39677274Smikeh	if ((sh = value("SHELL")) == NULL)
39777274Smikeh		sh = _PATH_CSHELL;
39877274Smikeh	pid = start_command(sh, 0, -1, pivec[1], "-c", cmdbuf, NULL);
3991590Srgrimes	if (pid < 0) {
40077274Smikeh		(void)close(pivec[0]);
40177274Smikeh		(void)close(pivec[1]);
40277274Smikeh		return (NULL);
4031590Srgrimes	}
40477274Smikeh	(void)close(pivec[1]);
4051590Srgrimes	l = read(pivec[0], xname, BUFSIZ);
40677274Smikeh	(void)close(pivec[0]);
40777274Smikeh	if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) &&
40877274Smikeh	    WTERMSIG(wait_status) != SIGPIPE) {
4091590Srgrimes		fprintf(stderr, "\"%s\": Expansion failed.\n", name);
41077274Smikeh		return (NULL);
4111590Srgrimes	}
4121590Srgrimes	if (l < 0) {
41374769Smikeh		warn("read");
41477274Smikeh		return (NULL);
4151590Srgrimes	}
4161590Srgrimes	if (l == 0) {
4171590Srgrimes		fprintf(stderr, "\"%s\": No match.\n", name);
41877274Smikeh		return (NULL);
4191590Srgrimes	}
4201590Srgrimes	if (l == BUFSIZ) {
4211590Srgrimes		fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
42277274Smikeh		return (NULL);
4231590Srgrimes	}
42474769Smikeh	xname[l] = '\0';
4251590Srgrimes	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
4261590Srgrimes		;
4271590Srgrimes	cp[1] = '\0';
42874769Smikeh	if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
4291590Srgrimes		fprintf(stderr, "\"%s\": Ambiguous.\n", name);
43077274Smikeh		return (NULL);
4311590Srgrimes	}
43277274Smikeh	return (savestr(xname));
4331590Srgrimes}
4341590Srgrimes
4351590Srgrimes/*
4361590Srgrimes * Determine the current folder directory name.
4371590Srgrimes */
4381590Srgrimesint
43974769Smikehgetfold(name, namelen)
4401590Srgrimes	char *name;
44174769Smikeh	int namelen;
4421590Srgrimes{
4431590Srgrimes	char *folder;
44474769Smikeh	int copylen;
4451590Srgrimes
44677274Smikeh	if ((folder = value("folder")) == NULL)
4471590Srgrimes		return (-1);
4481590Srgrimes	if (*folder == '/')
44974769Smikeh		copylen = strlcpy(name, folder, namelen);
4501590Srgrimes	else
45177274Smikeh		copylen = snprintf(name, namelen, "%s/%s",
45277274Smikeh		    homedir ? homedir : ".", folder);
45381979Sbrian	return (copylen < 0 || copylen >= namelen ? (-1) : (0));
4541590Srgrimes}
4551590Srgrimes
4561590Srgrimes/*
4571590Srgrimes * Return the name of the dead.letter file.
4581590Srgrimes */
4591590Srgrimeschar *
4601590Srgrimesgetdeadletter()
4611590Srgrimes{
46277274Smikeh	char *cp;
4631590Srgrimes
46477274Smikeh	if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
4651590Srgrimes		cp = expand("~/dead.letter");
4661590Srgrimes	else if (*cp != '/') {
4671590Srgrimes		char buf[PATHSIZE];
4681590Srgrimes
46977274Smikeh		(void)snprintf(buf, sizeof(buf), "~/%s", cp);
4701590Srgrimes		cp = expand(buf);
4711590Srgrimes	}
47277274Smikeh	return (cp);
4731590Srgrimes}
474