xref: /freebsd/usr.bin/mail/fio.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)fio.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include "rcv.h"
399b50d902SRodney W. Grimes #include <sys/file.h>
409b50d902SRodney W. Grimes #include <sys/wait.h>
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <unistd.h>
439b50d902SRodney W. Grimes #include <paths.h>
449b50d902SRodney W. Grimes #include <errno.h>
459b50d902SRodney W. Grimes #include "extern.h"
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes /*
489b50d902SRodney W. Grimes  * Mail -- a mail program
499b50d902SRodney W. Grimes  *
509b50d902SRodney W. Grimes  * File I/O.
519b50d902SRodney W. Grimes  */
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes /*
549b50d902SRodney W. Grimes  * Set up the input pointers while copying the mail file into /tmp.
559b50d902SRodney W. Grimes  */
569b50d902SRodney W. Grimes void
579b50d902SRodney W. Grimes setptr(ibuf)
589b50d902SRodney W. Grimes 	register FILE *ibuf;
599b50d902SRodney W. Grimes {
609b50d902SRodney W. Grimes 	extern char *tmpdir;
619b50d902SRodney W. Grimes 	register int c, count;
629b50d902SRodney W. Grimes 	register char *cp, *cp2;
639b50d902SRodney W. Grimes 	struct message this;
649b50d902SRodney W. Grimes 	FILE *mestmp;
659b50d902SRodney W. Grimes 	off_t offset;
669b50d902SRodney W. Grimes 	int maybe, inhead;
679b50d902SRodney W. Grimes 	char linebuf[LINESIZE];
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes 	/* Get temporary file. */
709b50d902SRodney W. Grimes 	(void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir);
719b50d902SRodney W. Grimes 	if ((c = mkstemp(linebuf)) == -1 ||
729b50d902SRodney W. Grimes 	    (mestmp = Fdopen(c, "r+")) == NULL) {
739b50d902SRodney W. Grimes 		(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
749b50d902SRodney W. Grimes 		exit(1);
759b50d902SRodney W. Grimes 	}
769b50d902SRodney W. Grimes 	(void)unlink(linebuf);
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes 	msgCount = 0;
799b50d902SRodney W. Grimes 	maybe = 1;
809b50d902SRodney W. Grimes 	inhead = 0;
819b50d902SRodney W. Grimes 	offset = 0;
829b50d902SRodney W. Grimes 	this.m_flag = MUSED|MNEW;
839b50d902SRodney W. Grimes 	this.m_size = 0;
849b50d902SRodney W. Grimes 	this.m_lines = 0;
859b50d902SRodney W. Grimes 	this.m_block = 0;
869b50d902SRodney W. Grimes 	this.m_offset = 0;
879b50d902SRodney W. Grimes 	for (;;) {
889b50d902SRodney W. Grimes 		if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
899b50d902SRodney W. Grimes 			if (append(&this, mestmp)) {
909b50d902SRodney W. Grimes 				perror("temporary file");
919b50d902SRodney W. Grimes 				exit(1);
929b50d902SRodney W. Grimes 			}
939b50d902SRodney W. Grimes 			makemessage(mestmp);
949b50d902SRodney W. Grimes 			return;
959b50d902SRodney W. Grimes 		}
969b50d902SRodney W. Grimes 		count = strlen(linebuf);
979b50d902SRodney W. Grimes 		(void) fwrite(linebuf, sizeof *linebuf, count, otf);
989b50d902SRodney W. Grimes 		if (ferror(otf)) {
999b50d902SRodney W. Grimes 			perror("/tmp");
1009b50d902SRodney W. Grimes 			exit(1);
1019b50d902SRodney W. Grimes 		}
1029b50d902SRodney W. Grimes 		linebuf[count - 1] = 0;
1039b50d902SRodney W. Grimes 		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
1049b50d902SRodney W. Grimes 			msgCount++;
1059b50d902SRodney W. Grimes 			if (append(&this, mestmp)) {
1069b50d902SRodney W. Grimes 				perror("temporary file");
1079b50d902SRodney W. Grimes 				exit(1);
1089b50d902SRodney W. Grimes 			}
1099b50d902SRodney W. Grimes 			this.m_flag = MUSED|MNEW;
1109b50d902SRodney W. Grimes 			this.m_size = 0;
1119b50d902SRodney W. Grimes 			this.m_lines = 0;
1129b50d902SRodney W. Grimes 			this.m_block = blockof(offset);
1139b50d902SRodney W. Grimes 			this.m_offset = offsetof(offset);
1149b50d902SRodney W. Grimes 			inhead = 1;
1159b50d902SRodney W. Grimes 		} else if (linebuf[0] == 0) {
1169b50d902SRodney W. Grimes 			inhead = 0;
1179b50d902SRodney W. Grimes 		} else if (inhead) {
1189b50d902SRodney W. Grimes 			for (cp = linebuf, cp2 = "status";; cp++) {
1199b50d902SRodney W. Grimes 				if ((c = *cp2++) == 0) {
1209b50d902SRodney W. Grimes 					while (isspace(*cp++))
1219b50d902SRodney W. Grimes 						;
1229b50d902SRodney W. Grimes 					if (cp[-1] != ':')
1239b50d902SRodney W. Grimes 						break;
1249b50d902SRodney W. Grimes 					while (c = *cp++)
1259b50d902SRodney W. Grimes 						if (c == 'R')
1269b50d902SRodney W. Grimes 							this.m_flag |= MREAD;
1279b50d902SRodney W. Grimes 						else if (c == 'O')
1289b50d902SRodney W. Grimes 							this.m_flag &= ~MNEW;
1299b50d902SRodney W. Grimes 					inhead = 0;
1309b50d902SRodney W. Grimes 					break;
1319b50d902SRodney W. Grimes 				}
1329b50d902SRodney W. Grimes 				if (*cp != c && *cp != toupper(c))
1339b50d902SRodney W. Grimes 					break;
1349b50d902SRodney W. Grimes 			}
1359b50d902SRodney W. Grimes 		}
1369b50d902SRodney W. Grimes 		offset += count;
1379b50d902SRodney W. Grimes 		this.m_size += count;
1389b50d902SRodney W. Grimes 		this.m_lines++;
1399b50d902SRodney W. Grimes 		maybe = linebuf[0] == 0;
1409b50d902SRodney W. Grimes 	}
1419b50d902SRodney W. Grimes }
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes /*
1449b50d902SRodney W. Grimes  * Drop the passed line onto the passed output buffer.
1459b50d902SRodney W. Grimes  * If a write error occurs, return -1, else the count of
1469b50d902SRodney W. Grimes  * characters written, including the newline.
1479b50d902SRodney W. Grimes  */
1489b50d902SRodney W. Grimes int
1499b50d902SRodney W. Grimes putline(obuf, linebuf)
1509b50d902SRodney W. Grimes 	FILE *obuf;
1519b50d902SRodney W. Grimes 	char *linebuf;
1529b50d902SRodney W. Grimes {
1539b50d902SRodney W. Grimes 	register int c;
1549b50d902SRodney W. Grimes 
1559b50d902SRodney W. Grimes 	c = strlen(linebuf);
1569b50d902SRodney W. Grimes 	(void) fwrite(linebuf, sizeof *linebuf, c, obuf);
1579b50d902SRodney W. Grimes 	(void) putc('\n', obuf);
1589b50d902SRodney W. Grimes 	if (ferror(obuf))
1599b50d902SRodney W. Grimes 		return (-1);
1609b50d902SRodney W. Grimes 	return (c + 1);
1619b50d902SRodney W. Grimes }
1629b50d902SRodney W. Grimes 
1639b50d902SRodney W. Grimes /*
1649b50d902SRodney W. Grimes  * Read up a line from the specified input into the line
1659b50d902SRodney W. Grimes  * buffer.  Return the number of characters read.  Do not
1669b50d902SRodney W. Grimes  * include the newline at the end.
1679b50d902SRodney W. Grimes  */
1689b50d902SRodney W. Grimes int
1699b50d902SRodney W. Grimes readline(ibuf, linebuf, linesize)
1709b50d902SRodney W. Grimes 	FILE *ibuf;
1719b50d902SRodney W. Grimes 	char *linebuf;
1729b50d902SRodney W. Grimes 	int linesize;
1739b50d902SRodney W. Grimes {
1749b50d902SRodney W. Grimes 	register int n;
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes 	clearerr(ibuf);
1779b50d902SRodney W. Grimes 	if (fgets(linebuf, linesize, ibuf) == NULL)
1789b50d902SRodney W. Grimes 		return -1;
1799b50d902SRodney W. Grimes 	n = strlen(linebuf);
1809b50d902SRodney W. Grimes 	if (n > 0 && linebuf[n - 1] == '\n')
1819b50d902SRodney W. Grimes 		linebuf[--n] = '\0';
1829b50d902SRodney W. Grimes 	return n;
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes /*
1869b50d902SRodney W. Grimes  * Return a file buffer all ready to read up the
1879b50d902SRodney W. Grimes  * passed message pointer.
1889b50d902SRodney W. Grimes  */
1899b50d902SRodney W. Grimes FILE *
1909b50d902SRodney W. Grimes setinput(mp)
1919b50d902SRodney W. Grimes 	register struct message *mp;
1929b50d902SRodney W. Grimes {
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes 	fflush(otf);
1959b50d902SRodney W. Grimes 	if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
1969b50d902SRodney W. Grimes 		perror("fseek");
1979b50d902SRodney W. Grimes 		panic("temporary file seek");
1989b50d902SRodney W. Grimes 	}
1999b50d902SRodney W. Grimes 	return (itf);
2009b50d902SRodney W. Grimes }
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes /*
2039b50d902SRodney W. Grimes  * Take the data out of the passed ghost file and toss it into
2049b50d902SRodney W. Grimes  * a dynamically allocated message structure.
2059b50d902SRodney W. Grimes  */
2069b50d902SRodney W. Grimes void
2079b50d902SRodney W. Grimes makemessage(f)
2089b50d902SRodney W. Grimes 	FILE *f;
2099b50d902SRodney W. Grimes {
2109b50d902SRodney W. Grimes 	register size = (msgCount + 1) * sizeof (struct message);
2119b50d902SRodney W. Grimes 
2129b50d902SRodney W. Grimes 	if (message != 0)
2139b50d902SRodney W. Grimes 		free((char *) message);
2149b50d902SRodney W. Grimes 	if ((message = (struct message *) malloc((unsigned) size)) == 0)
2159b50d902SRodney W. Grimes 		panic("Insufficient memory for %d messages", msgCount);
2169b50d902SRodney W. Grimes 	dot = message;
2179b50d902SRodney W. Grimes 	size -= sizeof (struct message);
2189b50d902SRodney W. Grimes 	fflush(f);
2199b50d902SRodney W. Grimes 	(void) lseek(fileno(f), (off_t)sizeof *message, 0);
2209b50d902SRodney W. Grimes 	if (read(fileno(f), (char *) message, size) != size)
2219b50d902SRodney W. Grimes 		panic("Message temporary file corrupted");
2229b50d902SRodney W. Grimes 	message[msgCount].m_size = 0;
2239b50d902SRodney W. Grimes 	message[msgCount].m_lines = 0;
2249b50d902SRodney W. Grimes 	Fclose(f);
2259b50d902SRodney W. Grimes }
2269b50d902SRodney W. Grimes 
2279b50d902SRodney W. Grimes /*
2289b50d902SRodney W. Grimes  * Append the passed message descriptor onto the temp file.
2299b50d902SRodney W. Grimes  * If the write fails, return 1, else 0
2309b50d902SRodney W. Grimes  */
2319b50d902SRodney W. Grimes int
2329b50d902SRodney W. Grimes append(mp, f)
2339b50d902SRodney W. Grimes 	struct message *mp;
2349b50d902SRodney W. Grimes 	FILE *f;
2359b50d902SRodney W. Grimes {
2369b50d902SRodney W. Grimes 	return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
2379b50d902SRodney W. Grimes }
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes /*
2409b50d902SRodney W. Grimes  * Delete a file, but only if the file is a plain file.
2419b50d902SRodney W. Grimes  */
2429b50d902SRodney W. Grimes int
2439b50d902SRodney W. Grimes rm(name)
2449b50d902SRodney W. Grimes 	char *name;
2459b50d902SRodney W. Grimes {
2469b50d902SRodney W. Grimes 	struct stat sb;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	if (stat(name, &sb) < 0)
2499b50d902SRodney W. Grimes 		return(-1);
2509b50d902SRodney W. Grimes 	if (!S_ISREG(sb.st_mode)) {
2519b50d902SRodney W. Grimes 		errno = EISDIR;
2529b50d902SRodney W. Grimes 		return(-1);
2539b50d902SRodney W. Grimes 	}
2549b50d902SRodney W. Grimes 	return(unlink(name));
2559b50d902SRodney W. Grimes }
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes static int sigdepth;		/* depth of holdsigs() */
2589b50d902SRodney W. Grimes static int omask;
2599b50d902SRodney W. Grimes /*
2609b50d902SRodney W. Grimes  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
2619b50d902SRodney W. Grimes  */
2629b50d902SRodney W. Grimes void
2639b50d902SRodney W. Grimes holdsigs()
2649b50d902SRodney W. Grimes {
2659b50d902SRodney W. Grimes 
2669b50d902SRodney W. Grimes 	if (sigdepth++ == 0)
2679b50d902SRodney W. Grimes 		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
2689b50d902SRodney W. Grimes }
2699b50d902SRodney W. Grimes 
2709b50d902SRodney W. Grimes /*
2719b50d902SRodney W. Grimes  * Release signals SIGHUP, SIGINT, and SIGQUIT.
2729b50d902SRodney W. Grimes  */
2739b50d902SRodney W. Grimes void
2749b50d902SRodney W. Grimes relsesigs()
2759b50d902SRodney W. Grimes {
2769b50d902SRodney W. Grimes 
2779b50d902SRodney W. Grimes 	if (--sigdepth == 0)
2789b50d902SRodney W. Grimes 		sigsetmask(omask);
2799b50d902SRodney W. Grimes }
2809b50d902SRodney W. Grimes 
2819b50d902SRodney W. Grimes /*
2829b50d902SRodney W. Grimes  * Determine the size of the file possessed by
2839b50d902SRodney W. Grimes  * the passed buffer.
2849b50d902SRodney W. Grimes  */
2859b50d902SRodney W. Grimes off_t
2869b50d902SRodney W. Grimes fsize(iob)
2879b50d902SRodney W. Grimes 	FILE *iob;
2889b50d902SRodney W. Grimes {
2899b50d902SRodney W. Grimes 	struct stat sbuf;
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	if (fstat(fileno(iob), &sbuf) < 0)
2929b50d902SRodney W. Grimes 		return 0;
2939b50d902SRodney W. Grimes 	return sbuf.st_size;
2949b50d902SRodney W. Grimes }
2959b50d902SRodney W. Grimes 
2969b50d902SRodney W. Grimes /*
2979b50d902SRodney W. Grimes  * Evaluate the string given as a new mailbox name.
2989b50d902SRodney W. Grimes  * Supported meta characters:
2999b50d902SRodney W. Grimes  *	%	for my system mail box
3009b50d902SRodney W. Grimes  *	%user	for user's system mail box
3019b50d902SRodney W. Grimes  *	#	for previous file
3029b50d902SRodney W. Grimes  *	&	invoker's mbox file
3039b50d902SRodney W. Grimes  *	+file	file in folder directory
3049b50d902SRodney W. Grimes  *	any shell meta character
3059b50d902SRodney W. Grimes  * Return the file name as a dynamic string.
3069b50d902SRodney W. Grimes  */
3079b50d902SRodney W. Grimes char *
3089b50d902SRodney W. Grimes expand(name)
3099b50d902SRodney W. Grimes 	register char *name;
3109b50d902SRodney W. Grimes {
3119b50d902SRodney W. Grimes 	char xname[PATHSIZE];
3129b50d902SRodney W. Grimes 	char cmdbuf[PATHSIZE];		/* also used for file names */
3139b50d902SRodney W. Grimes 	register int pid, l;
3149b50d902SRodney W. Grimes 	register char *cp, *shell;
3159b50d902SRodney W. Grimes 	int pivec[2];
3169b50d902SRodney W. Grimes 	struct stat sbuf;
3179b50d902SRodney W. Grimes 	extern union wait wait_status;
3189b50d902SRodney W. Grimes 
3199b50d902SRodney W. Grimes 	/*
3209b50d902SRodney W. Grimes 	 * The order of evaluation is "%" and "#" expand into constants.
3219b50d902SRodney W. Grimes 	 * "&" can expand into "+".  "+" can expand into shell meta characters.
3229b50d902SRodney W. Grimes 	 * Shell meta characters expand into constants.
3239b50d902SRodney W. Grimes 	 * This way, we make no recursive expansion.
3249b50d902SRodney W. Grimes 	 */
3259b50d902SRodney W. Grimes 	switch (*name) {
3269b50d902SRodney W. Grimes 	case '%':
3279b50d902SRodney W. Grimes 		findmail(name[1] ? name + 1 : myname, xname);
3289b50d902SRodney W. Grimes 		return savestr(xname);
3299b50d902SRodney W. Grimes 	case '#':
3309b50d902SRodney W. Grimes 		if (name[1] != 0)
3319b50d902SRodney W. Grimes 			break;
3329b50d902SRodney W. Grimes 		if (prevfile[0] == 0) {
3339b50d902SRodney W. Grimes 			printf("No previous file\n");
3349b50d902SRodney W. Grimes 			return NOSTR;
3359b50d902SRodney W. Grimes 		}
3369b50d902SRodney W. Grimes 		return savestr(prevfile);
3379b50d902SRodney W. Grimes 	case '&':
3389b50d902SRodney W. Grimes 		if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
3399b50d902SRodney W. Grimes 			name = "~/mbox";
3409b50d902SRodney W. Grimes 		/* fall through */
3419b50d902SRodney W. Grimes 	}
3429b50d902SRodney W. Grimes 	if (name[0] == '+' && getfold(cmdbuf) >= 0) {
3439b50d902SRodney W. Grimes 		sprintf(xname, "%s/%s", cmdbuf, name + 1);
3449b50d902SRodney W. Grimes 		name = savestr(xname);
3459b50d902SRodney W. Grimes 	}
3469b50d902SRodney W. Grimes 	/* catch the most common shell meta character */
3479b50d902SRodney W. Grimes 	if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
3489b50d902SRodney W. Grimes 		sprintf(xname, "%s%s", homedir, name + 1);
3499b50d902SRodney W. Grimes 		name = savestr(xname);
3509b50d902SRodney W. Grimes 	}
3519b50d902SRodney W. Grimes 	if (!anyof(name, "~{[*?$`'\"\\"))
3529b50d902SRodney W. Grimes 		return name;
3539b50d902SRodney W. Grimes 	if (pipe(pivec) < 0) {
3549b50d902SRodney W. Grimes 		perror("pipe");
3559b50d902SRodney W. Grimes 		return name;
3569b50d902SRodney W. Grimes 	}
3579b50d902SRodney W. Grimes 	sprintf(cmdbuf, "echo %s", name);
3589b50d902SRodney W. Grimes 	if ((shell = value("SHELL")) == NOSTR)
3599b50d902SRodney W. Grimes 		shell = _PATH_CSHELL;
3609b50d902SRodney W. Grimes 	pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
3619b50d902SRodney W. Grimes 	if (pid < 0) {
3629b50d902SRodney W. Grimes 		close(pivec[0]);
3639b50d902SRodney W. Grimes 		close(pivec[1]);
3649b50d902SRodney W. Grimes 		return NOSTR;
3659b50d902SRodney W. Grimes 	}
3669b50d902SRodney W. Grimes 	close(pivec[1]);
3679b50d902SRodney W. Grimes 	l = read(pivec[0], xname, BUFSIZ);
3689b50d902SRodney W. Grimes 	close(pivec[0]);
3699b50d902SRodney W. Grimes 	if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
3709b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Expansion failed.\n", name);
3719b50d902SRodney W. Grimes 		return NOSTR;
3729b50d902SRodney W. Grimes 	}
3739b50d902SRodney W. Grimes 	if (l < 0) {
3749b50d902SRodney W. Grimes 		perror("read");
3759b50d902SRodney W. Grimes 		return NOSTR;
3769b50d902SRodney W. Grimes 	}
3779b50d902SRodney W. Grimes 	if (l == 0) {
3789b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": No match.\n", name);
3799b50d902SRodney W. Grimes 		return NOSTR;
3809b50d902SRodney W. Grimes 	}
3819b50d902SRodney W. Grimes 	if (l == BUFSIZ) {
3829b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
3839b50d902SRodney W. Grimes 		return NOSTR;
3849b50d902SRodney W. Grimes 	}
3859b50d902SRodney W. Grimes 	xname[l] = 0;
3869b50d902SRodney W. Grimes 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
3879b50d902SRodney W. Grimes 		;
3889b50d902SRodney W. Grimes 	cp[1] = '\0';
3899b50d902SRodney W. Grimes 	if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
3909b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Ambiguous.\n", name);
3919b50d902SRodney W. Grimes 		return NOSTR;
3929b50d902SRodney W. Grimes 	}
3939b50d902SRodney W. Grimes 	return savestr(xname);
3949b50d902SRodney W. Grimes }
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes /*
3979b50d902SRodney W. Grimes  * Determine the current folder directory name.
3989b50d902SRodney W. Grimes  */
3999b50d902SRodney W. Grimes int
4009b50d902SRodney W. Grimes getfold(name)
4019b50d902SRodney W. Grimes 	char *name;
4029b50d902SRodney W. Grimes {
4039b50d902SRodney W. Grimes 	char *folder;
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 	if ((folder = value("folder")) == NOSTR)
4069b50d902SRodney W. Grimes 		return (-1);
4079b50d902SRodney W. Grimes 	if (*folder == '/')
4089b50d902SRodney W. Grimes 		strcpy(name, folder);
4099b50d902SRodney W. Grimes 	else
4109b50d902SRodney W. Grimes 		sprintf(name, "%s/%s", homedir, folder);
4119b50d902SRodney W. Grimes 	return (0);
4129b50d902SRodney W. Grimes }
4139b50d902SRodney W. Grimes 
4149b50d902SRodney W. Grimes /*
4159b50d902SRodney W. Grimes  * Return the name of the dead.letter file.
4169b50d902SRodney W. Grimes  */
4179b50d902SRodney W. Grimes char *
4189b50d902SRodney W. Grimes getdeadletter()
4199b50d902SRodney W. Grimes {
4209b50d902SRodney W. Grimes 	register char *cp;
4219b50d902SRodney W. Grimes 
4229b50d902SRodney W. Grimes 	if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
4239b50d902SRodney W. Grimes 		cp = expand("~/dead.letter");
4249b50d902SRodney W. Grimes 	else if (*cp != '/') {
4259b50d902SRodney W. Grimes 		char buf[PATHSIZE];
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes 		(void) sprintf(buf, "~/%s", cp);
4289b50d902SRodney W. Grimes 		cp = expand(buf);
4299b50d902SRodney W. Grimes 	}
4309b50d902SRodney W. Grimes 	return cp;
4319b50d902SRodney W. Grimes }
432