fio.c revision 67496
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.
3267496Sphk *
3367496Sphk * $FreeBSD: head/usr.bin/mail/fio.c 67496 2000-10-24 13:54:31Z phk $
341590Srgrimes */
351590Srgrimes
361590Srgrimes#ifndef lint
371590Srgrimesstatic char sccsid[] = "@(#)fio.c	8.1 (Berkeley) 6/6/93";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#include "rcv.h"
411590Srgrimes#include <sys/file.h>
421590Srgrimes#include <sys/wait.h>
431590Srgrimes
441590Srgrimes#include <unistd.h>
451590Srgrimes#include <paths.h>
461590Srgrimes#include <errno.h>
471590Srgrimes#include "extern.h"
481590Srgrimes
491590Srgrimes/*
501590Srgrimes * Mail -- a mail program
511590Srgrimes *
521590Srgrimes * File I/O.
531590Srgrimes */
541590Srgrimes
551590Srgrimes/*
561590Srgrimes * Set up the input pointers while copying the mail file into /tmp.
571590Srgrimes */
581590Srgrimesvoid
591590Srgrimessetptr(ibuf)
601590Srgrimes	register FILE *ibuf;
611590Srgrimes{
621590Srgrimes	extern char *tmpdir;
631590Srgrimes	register int c, count;
641590Srgrimes	register char *cp, *cp2;
651590Srgrimes	struct message this;
661590Srgrimes	FILE *mestmp;
671590Srgrimes	off_t offset;
681590Srgrimes	int maybe, inhead;
691590Srgrimes	char linebuf[LINESIZE];
701590Srgrimes
711590Srgrimes	/* Get temporary file. */
721590Srgrimes	(void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir);
731590Srgrimes	if ((c = mkstemp(linebuf)) == -1 ||
741590Srgrimes	    (mestmp = Fdopen(c, "r+")) == NULL) {
7527643Scharnier		errx(1, "can't open %s", linebuf);
761590Srgrimes	}
771590Srgrimes	(void)unlink(linebuf);
781590Srgrimes
791590Srgrimes	msgCount = 0;
801590Srgrimes	maybe = 1;
811590Srgrimes	inhead = 0;
821590Srgrimes	offset = 0;
831590Srgrimes	this.m_flag = MUSED|MNEW;
841590Srgrimes	this.m_size = 0;
851590Srgrimes	this.m_lines = 0;
861590Srgrimes	this.m_block = 0;
871590Srgrimes	this.m_offset = 0;
881590Srgrimes	for (;;) {
891590Srgrimes		if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
901590Srgrimes			if (append(&this, mestmp)) {
911590Srgrimes				perror("temporary file");
921590Srgrimes				exit(1);
931590Srgrimes			}
941590Srgrimes			makemessage(mestmp);
951590Srgrimes			return;
961590Srgrimes		}
971590Srgrimes		count = strlen(linebuf);
981590Srgrimes		(void) fwrite(linebuf, sizeof *linebuf, count, otf);
991590Srgrimes		if (ferror(otf)) {
1001590Srgrimes			perror("/tmp");
1011590Srgrimes			exit(1);
1021590Srgrimes		}
1031590Srgrimes		linebuf[count - 1] = 0;
1041590Srgrimes		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
1051590Srgrimes			msgCount++;
1061590Srgrimes			if (append(&this, mestmp)) {
1071590Srgrimes				perror("temporary file");
1081590Srgrimes				exit(1);
1091590Srgrimes			}
1101590Srgrimes			this.m_flag = MUSED|MNEW;
1111590Srgrimes			this.m_size = 0;
1121590Srgrimes			this.m_lines = 0;
1131590Srgrimes			this.m_block = blockof(offset);
11467496Sphk			this.m_offset = boffsetof(offset);
1151590Srgrimes			inhead = 1;
1161590Srgrimes		} else if (linebuf[0] == 0) {
1171590Srgrimes			inhead = 0;
1181590Srgrimes		} else if (inhead) {
1191590Srgrimes			for (cp = linebuf, cp2 = "status";; cp++) {
1201590Srgrimes				if ((c = *cp2++) == 0) {
1211590Srgrimes					while (isspace(*cp++))
1221590Srgrimes						;
1231590Srgrimes					if (cp[-1] != ':')
1241590Srgrimes						break;
1251590Srgrimes					while (c = *cp++)
1261590Srgrimes						if (c == 'R')
1271590Srgrimes							this.m_flag |= MREAD;
1281590Srgrimes						else if (c == 'O')
1291590Srgrimes							this.m_flag &= ~MNEW;
1301590Srgrimes					inhead = 0;
1311590Srgrimes					break;
1321590Srgrimes				}
1331590Srgrimes				if (*cp != c && *cp != toupper(c))
1341590Srgrimes					break;
1351590Srgrimes			}
1361590Srgrimes		}
1371590Srgrimes		offset += count;
1381590Srgrimes		this.m_size += count;
1391590Srgrimes		this.m_lines++;
1401590Srgrimes		maybe = linebuf[0] == 0;
1411590Srgrimes	}
1421590Srgrimes}
1431590Srgrimes
1441590Srgrimes/*
1451590Srgrimes * Drop the passed line onto the passed output buffer.
1461590Srgrimes * If a write error occurs, return -1, else the count of
1471590Srgrimes * characters written, including the newline.
1481590Srgrimes */
1491590Srgrimesint
1501590Srgrimesputline(obuf, linebuf)
1511590Srgrimes	FILE *obuf;
1521590Srgrimes	char *linebuf;
1531590Srgrimes{
1541590Srgrimes	register int c;
1551590Srgrimes
1561590Srgrimes	c = strlen(linebuf);
1571590Srgrimes	(void) fwrite(linebuf, sizeof *linebuf, c, obuf);
1581590Srgrimes	(void) putc('\n', obuf);
1591590Srgrimes	if (ferror(obuf))
1601590Srgrimes		return (-1);
1611590Srgrimes	return (c + 1);
1621590Srgrimes}
1631590Srgrimes
1641590Srgrimes/*
1651590Srgrimes * Read up a line from the specified input into the line
1661590Srgrimes * buffer.  Return the number of characters read.  Do not
1671590Srgrimes * include the newline at the end.
1681590Srgrimes */
1691590Srgrimesint
1701590Srgrimesreadline(ibuf, linebuf, linesize)
1711590Srgrimes	FILE *ibuf;
1721590Srgrimes	char *linebuf;
1731590Srgrimes	int linesize;
1741590Srgrimes{
1751590Srgrimes	register int n;
1761590Srgrimes
1771590Srgrimes	clearerr(ibuf);
1781590Srgrimes	if (fgets(linebuf, linesize, ibuf) == NULL)
1791590Srgrimes		return -1;
1801590Srgrimes	n = strlen(linebuf);
1811590Srgrimes	if (n > 0 && linebuf[n - 1] == '\n')
1821590Srgrimes		linebuf[--n] = '\0';
1831590Srgrimes	return n;
1841590Srgrimes}
1851590Srgrimes
1861590Srgrimes/*
1871590Srgrimes * Return a file buffer all ready to read up the
1881590Srgrimes * passed message pointer.
1891590Srgrimes */
1901590SrgrimesFILE *
1911590Srgrimessetinput(mp)
1921590Srgrimes	register struct message *mp;
1931590Srgrimes{
1941590Srgrimes
1951590Srgrimes	fflush(otf);
1961590Srgrimes	if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
1971590Srgrimes		perror("fseek");
19827643Scharnier		panic("Temporary file seek");
1991590Srgrimes	}
2001590Srgrimes	return (itf);
2011590Srgrimes}
2021590Srgrimes
2031590Srgrimes/*
2041590Srgrimes * Take the data out of the passed ghost file and toss it into
2051590Srgrimes * a dynamically allocated message structure.
2061590Srgrimes */
2071590Srgrimesvoid
2081590Srgrimesmakemessage(f)
2091590Srgrimes	FILE *f;
2101590Srgrimes{
2111590Srgrimes	register size = (msgCount + 1) * sizeof (struct message);
2121590Srgrimes
2131590Srgrimes	if (message != 0)
2141590Srgrimes		free((char *) message);
2151590Srgrimes	if ((message = (struct message *) malloc((unsigned) size)) == 0)
2161590Srgrimes		panic("Insufficient memory for %d messages", msgCount);
2171590Srgrimes	dot = message;
2181590Srgrimes	size -= sizeof (struct message);
2191590Srgrimes	fflush(f);
2201590Srgrimes	(void) lseek(fileno(f), (off_t)sizeof *message, 0);
2211590Srgrimes	if (read(fileno(f), (char *) message, size) != size)
2221590Srgrimes		panic("Message temporary file corrupted");
2231590Srgrimes	message[msgCount].m_size = 0;
2241590Srgrimes	message[msgCount].m_lines = 0;
2251590Srgrimes	Fclose(f);
2261590Srgrimes}
2271590Srgrimes
2281590Srgrimes/*
2291590Srgrimes * Append the passed message descriptor onto the temp file.
2301590Srgrimes * If the write fails, return 1, else 0
2311590Srgrimes */
2321590Srgrimesint
2331590Srgrimesappend(mp, f)
2341590Srgrimes	struct message *mp;
2351590Srgrimes	FILE *f;
2361590Srgrimes{
2371590Srgrimes	return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
2381590Srgrimes}
2391590Srgrimes
2401590Srgrimes/*
2411590Srgrimes * Delete a file, but only if the file is a plain file.
2421590Srgrimes */
2431590Srgrimesint
2441590Srgrimesrm(name)
2451590Srgrimes	char *name;
2461590Srgrimes{
2471590Srgrimes	struct stat sb;
2481590Srgrimes
2491590Srgrimes	if (stat(name, &sb) < 0)
2501590Srgrimes		return(-1);
2511590Srgrimes	if (!S_ISREG(sb.st_mode)) {
2521590Srgrimes		errno = EISDIR;
2531590Srgrimes		return(-1);
2541590Srgrimes	}
2551590Srgrimes	return(unlink(name));
2561590Srgrimes}
2571590Srgrimes
2581590Srgrimesstatic int sigdepth;		/* depth of holdsigs() */
2591590Srgrimesstatic int omask;
2601590Srgrimes/*
2611590Srgrimes * Hold signals SIGHUP, SIGINT, and SIGQUIT.
2621590Srgrimes */
2631590Srgrimesvoid
2641590Srgrimesholdsigs()
2651590Srgrimes{
2661590Srgrimes
2671590Srgrimes	if (sigdepth++ == 0)
2681590Srgrimes		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
2691590Srgrimes}
2701590Srgrimes
2711590Srgrimes/*
2721590Srgrimes * Release signals SIGHUP, SIGINT, and SIGQUIT.
2731590Srgrimes */
2741590Srgrimesvoid
2751590Srgrimesrelsesigs()
2761590Srgrimes{
2771590Srgrimes
2781590Srgrimes	if (--sigdepth == 0)
2791590Srgrimes		sigsetmask(omask);
2801590Srgrimes}
2811590Srgrimes
2821590Srgrimes/*
2831590Srgrimes * Determine the size of the file possessed by
2841590Srgrimes * the passed buffer.
2851590Srgrimes */
2861590Srgrimesoff_t
2871590Srgrimesfsize(iob)
2881590Srgrimes	FILE *iob;
2891590Srgrimes{
2901590Srgrimes	struct stat sbuf;
2911590Srgrimes
2921590Srgrimes	if (fstat(fileno(iob), &sbuf) < 0)
2931590Srgrimes		return 0;
2941590Srgrimes	return sbuf.st_size;
2951590Srgrimes}
2961590Srgrimes
2971590Srgrimes/*
2981590Srgrimes * Evaluate the string given as a new mailbox name.
2991590Srgrimes * Supported meta characters:
3001590Srgrimes *	%	for my system mail box
3011590Srgrimes *	%user	for user's system mail box
3021590Srgrimes *	#	for previous file
3031590Srgrimes *	&	invoker's mbox file
3041590Srgrimes *	+file	file in folder directory
3051590Srgrimes *	any shell meta character
3061590Srgrimes * Return the file name as a dynamic string.
3071590Srgrimes */
3081590Srgrimeschar *
3091590Srgrimesexpand(name)
3101590Srgrimes	register char *name;
3111590Srgrimes{
3121590Srgrimes	char xname[PATHSIZE];
3131590Srgrimes	char cmdbuf[PATHSIZE];		/* also used for file names */
3141590Srgrimes	register int pid, l;
3151590Srgrimes	register char *cp, *shell;
3161590Srgrimes	int pivec[2];
3171590Srgrimes	struct stat sbuf;
3181590Srgrimes	extern union wait wait_status;
3191590Srgrimes
3201590Srgrimes	/*
3211590Srgrimes	 * The order of evaluation is "%" and "#" expand into constants.
3221590Srgrimes	 * "&" can expand into "+".  "+" can expand into shell meta characters.
3231590Srgrimes	 * Shell meta characters expand into constants.
3241590Srgrimes	 * This way, we make no recursive expansion.
3251590Srgrimes	 */
3261590Srgrimes	switch (*name) {
3271590Srgrimes	case '%':
3281590Srgrimes		findmail(name[1] ? name + 1 : myname, xname);
3291590Srgrimes		return savestr(xname);
3301590Srgrimes	case '#':
3311590Srgrimes		if (name[1] != 0)
3321590Srgrimes			break;
3331590Srgrimes		if (prevfile[0] == 0) {
3341590Srgrimes			printf("No previous file\n");
3351590Srgrimes			return NOSTR;
3361590Srgrimes		}
3371590Srgrimes		return savestr(prevfile);
3381590Srgrimes	case '&':
3391590Srgrimes		if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
3401590Srgrimes			name = "~/mbox";
3411590Srgrimes		/* fall through */
3421590Srgrimes	}
3431590Srgrimes	if (name[0] == '+' && getfold(cmdbuf) >= 0) {
3441590Srgrimes		sprintf(xname, "%s/%s", cmdbuf, name + 1);
3451590Srgrimes		name = savestr(xname);
3461590Srgrimes	}
3471590Srgrimes	/* catch the most common shell meta character */
3481590Srgrimes	if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
3491590Srgrimes		sprintf(xname, "%s%s", homedir, name + 1);
3501590Srgrimes		name = savestr(xname);
3511590Srgrimes	}
3521590Srgrimes	if (!anyof(name, "~{[*?$`'\"\\"))
3531590Srgrimes		return name;
3541590Srgrimes	if (pipe(pivec) < 0) {
3551590Srgrimes		perror("pipe");
3561590Srgrimes		return name;
3571590Srgrimes	}
3581590Srgrimes	sprintf(cmdbuf, "echo %s", name);
3591590Srgrimes	if ((shell = value("SHELL")) == NOSTR)
3601590Srgrimes		shell = _PATH_CSHELL;
3611590Srgrimes	pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
3621590Srgrimes	if (pid < 0) {
3631590Srgrimes		close(pivec[0]);
3641590Srgrimes		close(pivec[1]);
3651590Srgrimes		return NOSTR;
3661590Srgrimes	}
3671590Srgrimes	close(pivec[1]);
3681590Srgrimes	l = read(pivec[0], xname, BUFSIZ);
3691590Srgrimes	close(pivec[0]);
3701590Srgrimes	if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
3711590Srgrimes		fprintf(stderr, "\"%s\": Expansion failed.\n", name);
3721590Srgrimes		return NOSTR;
3731590Srgrimes	}
3741590Srgrimes	if (l < 0) {
3751590Srgrimes		perror("read");
3761590Srgrimes		return NOSTR;
3771590Srgrimes	}
3781590Srgrimes	if (l == 0) {
3791590Srgrimes		fprintf(stderr, "\"%s\": No match.\n", name);
3801590Srgrimes		return NOSTR;
3811590Srgrimes	}
3821590Srgrimes	if (l == BUFSIZ) {
3831590Srgrimes		fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
3841590Srgrimes		return NOSTR;
3851590Srgrimes	}
3861590Srgrimes	xname[l] = 0;
3871590Srgrimes	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
3881590Srgrimes		;
3891590Srgrimes	cp[1] = '\0';
3901590Srgrimes	if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
3911590Srgrimes		fprintf(stderr, "\"%s\": Ambiguous.\n", name);
3921590Srgrimes		return NOSTR;
3931590Srgrimes	}
3941590Srgrimes	return savestr(xname);
3951590Srgrimes}
3961590Srgrimes
3971590Srgrimes/*
3981590Srgrimes * Determine the current folder directory name.
3991590Srgrimes */
4001590Srgrimesint
4011590Srgrimesgetfold(name)
4021590Srgrimes	char *name;
4031590Srgrimes{
4041590Srgrimes	char *folder;
4051590Srgrimes
4061590Srgrimes	if ((folder = value("folder")) == NOSTR)
4071590Srgrimes		return (-1);
4081590Srgrimes	if (*folder == '/')
4091590Srgrimes		strcpy(name, folder);
4101590Srgrimes	else
4111590Srgrimes		sprintf(name, "%s/%s", homedir, folder);
4121590Srgrimes	return (0);
4131590Srgrimes}
4141590Srgrimes
4151590Srgrimes/*
4161590Srgrimes * Return the name of the dead.letter file.
4171590Srgrimes */
4181590Srgrimeschar *
4191590Srgrimesgetdeadletter()
4201590Srgrimes{
4211590Srgrimes	register char *cp;
4221590Srgrimes
4231590Srgrimes	if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
4241590Srgrimes		cp = expand("~/dead.letter");
4251590Srgrimes	else if (*cp != '/') {
4261590Srgrimes		char buf[PATHSIZE];
4271590Srgrimes
4281590Srgrimes		(void) sprintf(buf, "~/%s", cp);
4291590Srgrimes		cp = expand(buf);
4301590Srgrimes	}
4311590Srgrimes	return cp;
4321590Srgrimes}
433