xref: /freebsd/usr.bin/mail/fio.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #ifndef lint
330c3a8314SMike Heffner #if 0
34856f23edSMike Heffner static char sccsid[] = "@(#)fio.c	8.2 (Berkeley) 4/20/95";
350c3a8314SMike Heffner #endif
369b50d902SRodney W. Grimes #endif /* not lint */
37e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
38e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #include "rcv.h"
419b50d902SRodney W. Grimes #include <sys/file.h>
429b50d902SRodney W. Grimes #include <sys/wait.h>
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <unistd.h>
459b50d902SRodney W. Grimes #include <paths.h>
469b50d902SRodney W. Grimes #include <errno.h>
479b50d902SRodney W. Grimes #include "extern.h"
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes /*
509b50d902SRodney W. Grimes  * Mail -- a mail program
519b50d902SRodney W. Grimes  *
529b50d902SRodney W. Grimes  * File I/O.
539b50d902SRodney W. Grimes  */
549b50d902SRodney W. Grimes 
559ce73e90SMike Heffner extern int wait_status;
569ce73e90SMike Heffner 
579b50d902SRodney W. Grimes /*
589b50d902SRodney W. Grimes  * Set up the input pointers while copying the mail file into /tmp.
599b50d902SRodney W. Grimes  */
609b50d902SRodney W. Grimes void
616d8484b0SPhilippe Charnier setptr(FILE *ibuf, off_t offset)
629b50d902SRodney W. Grimes {
639ce73e90SMike Heffner 	int c, count;
649ce73e90SMike Heffner 	char *cp, *cp2;
659b50d902SRodney W. Grimes 	struct message this;
669b50d902SRodney W. Grimes 	FILE *mestmp;
679b50d902SRodney W. Grimes 	int maybe, inhead;
680c3a8314SMike Heffner 	char linebuf[LINESIZE], pathbuf[PATHSIZE];
69856f23edSMike Heffner 	int omsgCount;
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes 	/* Get temporary file. */
720c3a8314SMike Heffner 	(void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
730c3a8314SMike Heffner 	if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
740c3a8314SMike Heffner 		err(1, "can't open %s", pathbuf);
750c3a8314SMike Heffner 	(void)rm(pathbuf);
769b50d902SRodney W. Grimes 
77856f23edSMike Heffner 	if (offset == 0) {
789b50d902SRodney W. Grimes 		 msgCount = 0;
79856f23edSMike Heffner 	} else {
80856f23edSMike Heffner 		/* Seek into the file to get to the new messages */
816d48fa43SAndrey A. Chernov 		(void)fseeko(ibuf, offset, SEEK_SET);
82856f23edSMike Heffner 		/*
83856f23edSMike Heffner 		 * We need to make "offset" a pointer to the end of
84856f23edSMike Heffner 		 * the temp file that has the copy of the mail file.
85856f23edSMike Heffner 		 * If any messages have been edited, this will be
86856f23edSMike Heffner 		 * different from the offset into the mail file.
87856f23edSMike Heffner 		 */
886d48fa43SAndrey A. Chernov 		(void)fseeko(otf, (off_t)0, SEEK_END);
896d48fa43SAndrey A. Chernov 		offset = ftello(otf);
90856f23edSMike Heffner 	}
91856f23edSMike Heffner 	omsgCount = msgCount;
929b50d902SRodney W. Grimes 	maybe = 1;
939b50d902SRodney W. Grimes 	inhead = 0;
949b50d902SRodney W. Grimes 	this.m_flag = MUSED|MNEW;
959b50d902SRodney W. Grimes 	this.m_size = 0;
969b50d902SRodney W. Grimes 	this.m_lines = 0;
979b50d902SRodney W. Grimes 	this.m_block = 0;
989b50d902SRodney W. Grimes 	this.m_offset = 0;
999b50d902SRodney W. Grimes 	for (;;) {
1000c3a8314SMike Heffner 		if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
1010c3a8314SMike Heffner 			if (append(&this, mestmp))
1020c3a8314SMike Heffner 				errx(1, "temporary file");
103856f23edSMike Heffner 			makemessage(mestmp, omsgCount);
1049b50d902SRodney W. Grimes 			return;
1059b50d902SRodney W. Grimes 		}
1069b50d902SRodney W. Grimes 		count = strlen(linebuf);
107afa3a100SMike Heffner 		/*
108afa3a100SMike Heffner 		 * Transforms lines ending in <CR><LF> to just <LF>.
109afa3a100SMike Heffner 		 * This allows mail to be able to read Eudora mailboxes.
110afa3a100SMike Heffner 		 */
111afa3a100SMike Heffner 		if (count >= 2 && linebuf[count - 1] == '\n' &&
112afa3a100SMike Heffner 		    linebuf[count - 2] == '\r') {
113afa3a100SMike Heffner 			count--;
114afa3a100SMike Heffner 			linebuf[count - 1] = '\n';
115afa3a100SMike Heffner 		}
116afa3a100SMike Heffner 
1179ce73e90SMike Heffner 		(void)fwrite(linebuf, sizeof(*linebuf), count, otf);
1180c3a8314SMike Heffner 		if (ferror(otf))
1190c3a8314SMike Heffner 			errx(1, "/tmp");
1200c3a8314SMike Heffner 		if (count)
1210c3a8314SMike Heffner 			linebuf[count - 1] = '\0';
1229b50d902SRodney W. Grimes 		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
1239b50d902SRodney W. Grimes 			msgCount++;
1240c3a8314SMike Heffner 			if (append(&this, mestmp))
1250c3a8314SMike Heffner 				errx(1, "temporary file");
1269b50d902SRodney W. Grimes 			this.m_flag = MUSED|MNEW;
1279b50d902SRodney W. Grimes 			this.m_size = 0;
1289b50d902SRodney W. Grimes 			this.m_lines = 0;
1299b50d902SRodney W. Grimes 			this.m_block = blockof(offset);
130c23d986eSPoul-Henning Kamp 			this.m_offset = boffsetof(offset);
1319b50d902SRodney W. Grimes 			inhead = 1;
1329b50d902SRodney W. Grimes 		} else if (linebuf[0] == 0) {
1339b50d902SRodney W. Grimes 			inhead = 0;
1349b50d902SRodney W. Grimes 		} else if (inhead) {
1359b50d902SRodney W. Grimes 			for (cp = linebuf, cp2 = "status";; cp++) {
1369ce73e90SMike Heffner 				if ((c = *cp2++) == '\0') {
1376d48fa43SAndrey A. Chernov 					while (isspace((unsigned char)*cp++))
1389b50d902SRodney W. Grimes 						;
1399b50d902SRodney W. Grimes 					if (cp[-1] != ':')
1409b50d902SRodney W. Grimes 						break;
1419ce73e90SMike Heffner 					while ((c = *cp++) != '\0')
1429b50d902SRodney W. Grimes 						if (c == 'R')
1439b50d902SRodney W. Grimes 							this.m_flag |= MREAD;
1449b50d902SRodney W. Grimes 						else if (c == 'O')
1459b50d902SRodney W. Grimes 							this.m_flag &= ~MNEW;
1469b50d902SRodney W. Grimes 					inhead = 0;
1479b50d902SRodney W. Grimes 					break;
1489b50d902SRodney W. Grimes 				}
1496d48fa43SAndrey A. Chernov 				if (*cp != c && *cp != toupper((unsigned char)c))
1509b50d902SRodney W. Grimes 					break;
1519b50d902SRodney W. Grimes 			}
1529b50d902SRodney W. Grimes 		}
1539b50d902SRodney W. Grimes 		offset += count;
1549b50d902SRodney W. Grimes 		this.m_size += count;
1559b50d902SRodney W. Grimes 		this.m_lines++;
1569b50d902SRodney W. Grimes 		maybe = linebuf[0] == 0;
1579b50d902SRodney W. Grimes 	}
1589b50d902SRodney W. Grimes }
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes /*
1619b50d902SRodney W. Grimes  * Drop the passed line onto the passed output buffer.
1629b50d902SRodney W. Grimes  * If a write error occurs, return -1, else the count of
163856f23edSMike Heffner  * characters written, including the newline if requested.
1649b50d902SRodney W. Grimes  */
1659b50d902SRodney W. Grimes int
1666d8484b0SPhilippe Charnier putline(FILE *obuf, char *linebuf, int outlf)
1679b50d902SRodney W. Grimes {
1689ce73e90SMike Heffner 	int c;
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes 	c = strlen(linebuf);
1719ce73e90SMike Heffner 	(void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
172856f23edSMike Heffner 	if (outlf) {
1739ce73e90SMike Heffner 		fprintf(obuf, "\n");
174856f23edSMike Heffner 		c++;
175856f23edSMike Heffner 	}
1769b50d902SRodney W. Grimes 	if (ferror(obuf))
1779b50d902SRodney W. Grimes 		return (-1);
178856f23edSMike Heffner 	return (c);
1799b50d902SRodney W. Grimes }
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes /*
1829b50d902SRodney W. Grimes  * Read up a line from the specified input into the line
1839b50d902SRodney W. Grimes  * buffer.  Return the number of characters read.  Do not
184afa3a100SMike Heffner  * include the newline (or carriage return) at the end.
1859b50d902SRodney W. Grimes  */
1869b50d902SRodney W. Grimes int
1876d8484b0SPhilippe Charnier readline(FILE *ibuf, char *linebuf, int linesize)
1889b50d902SRodney W. Grimes {
1899ce73e90SMike Heffner 	int n;
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes 	clearerr(ibuf);
1929b50d902SRodney W. Grimes 	if (fgets(linebuf, linesize, ibuf) == NULL)
1939ce73e90SMike Heffner 		return (-1);
1949b50d902SRodney W. Grimes 	n = strlen(linebuf);
1959b50d902SRodney W. Grimes 	if (n > 0 && linebuf[n - 1] == '\n')
1969b50d902SRodney W. Grimes 		linebuf[--n] = '\0';
197afa3a100SMike Heffner 	if (n > 0 && linebuf[n - 1] == '\r')
198afa3a100SMike Heffner 		linebuf[--n] = '\0';
1999ce73e90SMike Heffner 	return (n);
2009b50d902SRodney W. Grimes }
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes /*
2039b50d902SRodney W. Grimes  * Return a file buffer all ready to read up the
2049b50d902SRodney W. Grimes  * passed message pointer.
2059b50d902SRodney W. Grimes  */
2069b50d902SRodney W. Grimes FILE *
2076d8484b0SPhilippe Charnier setinput(struct message *mp)
2089b50d902SRodney W. Grimes {
2099b50d902SRodney W. Grimes 
2109ce73e90SMike Heffner 	(void)fflush(otf);
211af8c3262SAndrey A. Chernov 	if (fseeko(itf,
212af8c3262SAndrey A. Chernov 		   positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
2136d48fa43SAndrey A. Chernov 		err(1, "fseeko");
2149b50d902SRodney W. Grimes 	return (itf);
2159b50d902SRodney W. Grimes }
2169b50d902SRodney W. Grimes 
2179b50d902SRodney W. Grimes /*
2189b50d902SRodney W. Grimes  * Take the data out of the passed ghost file and toss it into
2199b50d902SRodney W. Grimes  * a dynamically allocated message structure.
2209b50d902SRodney W. Grimes  */
2219b50d902SRodney W. Grimes void
2226d8484b0SPhilippe Charnier makemessage(FILE *f, int omsgCount)
2239b50d902SRodney W. Grimes {
224856f23edSMike Heffner 	size_t size;
225856f23edSMike Heffner 	struct message *nmessage;
2269b50d902SRodney W. Grimes 
227856f23edSMike Heffner 	size = (msgCount + 1) * sizeof(struct message);
228856f23edSMike Heffner 	nmessage = (struct message *)realloc(message, size);
229856f23edSMike Heffner 	if (nmessage == NULL)
230856f23edSMike Heffner 		errx(1, "Insufficient memory for %d messages\n",
231856f23edSMike Heffner 		    msgCount);
232856f23edSMike Heffner 	if (omsgCount == 0 || message == NULL)
233856f23edSMike Heffner 		dot = nmessage;
234856f23edSMike Heffner 	else
235856f23edSMike Heffner 		dot = nmessage + (dot - message);
236856f23edSMike Heffner 	message = nmessage;
237856f23edSMike Heffner 	size -= (omsgCount + 1) * sizeof(struct message);
2389ce73e90SMike Heffner 	(void)fflush(f);
2399ce73e90SMike Heffner 	(void)lseek(fileno(f), (off_t)sizeof(*message), 0);
240b22a8699SPedro F. Giffuni 	if (read(fileno(f), (void *)&message[omsgCount], size) != size)
2410c3a8314SMike Heffner 		errx(1, "Message temporary file corrupted");
2429b50d902SRodney W. Grimes 	message[msgCount].m_size = 0;
2439b50d902SRodney W. Grimes 	message[msgCount].m_lines = 0;
2449ce73e90SMike Heffner 	(void)Fclose(f);
2459b50d902SRodney W. Grimes }
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes /*
2489b50d902SRodney W. Grimes  * Append the passed message descriptor onto the temp file.
2499b50d902SRodney W. Grimes  * If the write fails, return 1, else 0
2509b50d902SRodney W. Grimes  */
2519b50d902SRodney W. Grimes int
2526d8484b0SPhilippe Charnier append(struct message *mp, FILE *f)
2539b50d902SRodney W. Grimes {
2549ce73e90SMike Heffner 	return (fwrite((char *)mp, sizeof(*mp), 1, f) != 1);
2559b50d902SRodney W. Grimes }
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes /*
2589b50d902SRodney W. Grimes  * Delete a file, but only if the file is a plain file.
2599b50d902SRodney W. Grimes  */
2609b50d902SRodney W. Grimes int
2616d8484b0SPhilippe Charnier rm(char *name)
2629b50d902SRodney W. Grimes {
2639b50d902SRodney W. Grimes 	struct stat sb;
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes 	if (stat(name, &sb) < 0)
2669b50d902SRodney W. Grimes 		return (-1);
2679b50d902SRodney W. Grimes 	if (!S_ISREG(sb.st_mode)) {
2689b50d902SRodney W. Grimes 		errno = EISDIR;
2699b50d902SRodney W. Grimes 		return (-1);
2709b50d902SRodney W. Grimes 	}
2719b50d902SRodney W. Grimes 	return (unlink(name));
2729b50d902SRodney W. Grimes }
2739b50d902SRodney W. Grimes 
2749b50d902SRodney W. Grimes static int sigdepth;		/* depth of holdsigs() */
275856f23edSMike Heffner static sigset_t nset, oset;
2769b50d902SRodney W. Grimes /*
2779b50d902SRodney W. Grimes  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
2789b50d902SRodney W. Grimes  */
2799b50d902SRodney W. Grimes void
2806d8484b0SPhilippe Charnier holdsigs(void)
2819b50d902SRodney W. Grimes {
2829b50d902SRodney W. Grimes 
283856f23edSMike Heffner 	if (sigdepth++ == 0) {
284856f23edSMike Heffner 		(void)sigemptyset(&nset);
285856f23edSMike Heffner 		(void)sigaddset(&nset, SIGHUP);
286856f23edSMike Heffner 		(void)sigaddset(&nset, SIGINT);
287856f23edSMike Heffner 		(void)sigaddset(&nset, SIGQUIT);
288856f23edSMike Heffner 		(void)sigprocmask(SIG_BLOCK, &nset, &oset);
289856f23edSMike Heffner 	}
2909b50d902SRodney W. Grimes }
2919b50d902SRodney W. Grimes 
2929b50d902SRodney W. Grimes /*
2939b50d902SRodney W. Grimes  * Release signals SIGHUP, SIGINT, and SIGQUIT.
2949b50d902SRodney W. Grimes  */
2959b50d902SRodney W. Grimes void
2966d8484b0SPhilippe Charnier relsesigs(void)
2979b50d902SRodney W. Grimes {
2989b50d902SRodney W. Grimes 
2999b50d902SRodney W. Grimes 	if (--sigdepth == 0)
300856f23edSMike Heffner 		(void)sigprocmask(SIG_SETMASK, &oset, NULL);
3019b50d902SRodney W. Grimes }
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes /*
3049b50d902SRodney W. Grimes  * Determine the size of the file possessed by
3059b50d902SRodney W. Grimes  * the passed buffer.
3069b50d902SRodney W. Grimes  */
3079b50d902SRodney W. Grimes off_t
3086d8484b0SPhilippe Charnier fsize(FILE *iob)
3099b50d902SRodney W. Grimes {
3109b50d902SRodney W. Grimes 	struct stat sbuf;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	if (fstat(fileno(iob), &sbuf) < 0)
3139ce73e90SMike Heffner 		return (0);
3149ce73e90SMike Heffner 	return (sbuf.st_size);
3159b50d902SRodney W. Grimes }
3169b50d902SRodney W. Grimes 
3179b50d902SRodney W. Grimes /*
3189b50d902SRodney W. Grimes  * Evaluate the string given as a new mailbox name.
3199b50d902SRodney W. Grimes  * Supported meta characters:
3209b50d902SRodney W. Grimes  *	%	for my system mail box
3219b50d902SRodney W. Grimes  *	%user	for user's system mail box
3229b50d902SRodney W. Grimes  *	#	for previous file
3239b50d902SRodney W. Grimes  *	&	invoker's mbox file
3249b50d902SRodney W. Grimes  *	+file	file in folder directory
3259b50d902SRodney W. Grimes  *	any shell meta character
3269b50d902SRodney W. Grimes  * Return the file name as a dynamic string.
3279b50d902SRodney W. Grimes  */
3289b50d902SRodney W. Grimes char *
3296d8484b0SPhilippe Charnier expand(char *name)
3309b50d902SRodney W. Grimes {
3319b50d902SRodney W. Grimes 	char xname[PATHSIZE];
3329b50d902SRodney W. Grimes 	char cmdbuf[PATHSIZE];		/* also used for file names */
3339ce73e90SMike Heffner 	int pid, l;
3349ce73e90SMike Heffner 	char *cp, *sh;
3359b50d902SRodney W. Grimes 	int pivec[2];
3369b50d902SRodney W. Grimes 	struct stat sbuf;
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes 	/*
3399b50d902SRodney W. Grimes 	 * The order of evaluation is "%" and "#" expand into constants.
3409b50d902SRodney W. Grimes 	 * "&" can expand into "+".  "+" can expand into shell meta characters.
3419b50d902SRodney W. Grimes 	 * Shell meta characters expand into constants.
3429b50d902SRodney W. Grimes 	 * This way, we make no recursive expansion.
3439b50d902SRodney W. Grimes 	 */
3449b50d902SRodney W. Grimes 	switch (*name) {
3459b50d902SRodney W. Grimes 	case '%':
3460c3a8314SMike Heffner 		findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
3479ce73e90SMike Heffner 		return (savestr(xname));
3489b50d902SRodney W. Grimes 	case '#':
3499b50d902SRodney W. Grimes 		if (name[1] != 0)
3509b50d902SRodney W. Grimes 			break;
3519b50d902SRodney W. Grimes 		if (prevfile[0] == 0) {
3529b50d902SRodney W. Grimes 			printf("No previous file\n");
3539ce73e90SMike Heffner 			return (NULL);
3549b50d902SRodney W. Grimes 		}
3559ce73e90SMike Heffner 		return (savestr(prevfile));
3569b50d902SRodney W. Grimes 	case '&':
3579ce73e90SMike Heffner 		if (name[1] == 0 && (name = value("MBOX")) == NULL)
3589b50d902SRodney W. Grimes 			name = "~/mbox";
3599b50d902SRodney W. Grimes 		/* fall through */
3609b50d902SRodney W. Grimes 	}
3610c3a8314SMike Heffner 	if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
3629ce73e90SMike Heffner 		(void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
3639b50d902SRodney W. Grimes 		name = savestr(xname);
3649b50d902SRodney W. Grimes 	}
3659b50d902SRodney W. Grimes 	/* catch the most common shell meta character */
3660c3a8314SMike Heffner 	if (name[0] == '~' && homedir != NULL &&
3670c3a8314SMike Heffner 	    (name[1] == '/' || name[1] == '\0')) {
3689ce73e90SMike Heffner 		(void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
3699b50d902SRodney W. Grimes 		name = savestr(xname);
3709b50d902SRodney W. Grimes 	}
3710c3a8314SMike Heffner 	if (!strpbrk(name, "~{[*?$`'\"\\"))
372090fc1c8SDon Lewis 		return (savestr(name));
3739b50d902SRodney W. Grimes 	if (pipe(pivec) < 0) {
3740c3a8314SMike Heffner 		warn("pipe");
375090fc1c8SDon Lewis 		return (NULL);
3769b50d902SRodney W. Grimes 	}
3779ce73e90SMike Heffner 	(void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
3789ce73e90SMike Heffner 	if ((sh = value("SHELL")) == NULL)
3799ce73e90SMike Heffner 		sh = _PATH_CSHELL;
3809ce73e90SMike Heffner 	pid = start_command(sh, 0, -1, pivec[1], "-c", cmdbuf, NULL);
3819b50d902SRodney W. Grimes 	if (pid < 0) {
3829ce73e90SMike Heffner 		(void)close(pivec[0]);
3839ce73e90SMike Heffner 		(void)close(pivec[1]);
3849ce73e90SMike Heffner 		return (NULL);
3859b50d902SRodney W. Grimes 	}
3869ce73e90SMike Heffner 	(void)close(pivec[1]);
3879b50d902SRodney W. Grimes 	l = read(pivec[0], xname, BUFSIZ);
3889ce73e90SMike Heffner 	(void)close(pivec[0]);
3899ce73e90SMike Heffner 	if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) &&
3909ce73e90SMike Heffner 	    WTERMSIG(wait_status) != SIGPIPE) {
3919b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Expansion failed.\n", name);
3929ce73e90SMike Heffner 		return (NULL);
3939b50d902SRodney W. Grimes 	}
3949b50d902SRodney W. Grimes 	if (l < 0) {
3950c3a8314SMike Heffner 		warn("read");
3969ce73e90SMike Heffner 		return (NULL);
3979b50d902SRodney W. Grimes 	}
3989b50d902SRodney W. Grimes 	if (l == 0) {
3999b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": No match.\n", name);
4009ce73e90SMike Heffner 		return (NULL);
4019b50d902SRodney W. Grimes 	}
4029b50d902SRodney W. Grimes 	if (l == BUFSIZ) {
4039b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
4049ce73e90SMike Heffner 		return (NULL);
4059b50d902SRodney W. Grimes 	}
4060c3a8314SMike Heffner 	xname[l] = '\0';
4079b50d902SRodney W. Grimes 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
4089b50d902SRodney W. Grimes 		;
4099b50d902SRodney W. Grimes 	cp[1] = '\0';
4100c3a8314SMike Heffner 	if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
4119b50d902SRodney W. Grimes 		fprintf(stderr, "\"%s\": Ambiguous.\n", name);
4129ce73e90SMike Heffner 		return (NULL);
4139b50d902SRodney W. Grimes 	}
4149ce73e90SMike Heffner 	return (savestr(xname));
4159b50d902SRodney W. Grimes }
4169b50d902SRodney W. Grimes 
4179b50d902SRodney W. Grimes /*
4189b50d902SRodney W. Grimes  * Determine the current folder directory name.
4199b50d902SRodney W. Grimes  */
4209b50d902SRodney W. Grimes int
4216d8484b0SPhilippe Charnier getfold(char *name, int namelen)
4229b50d902SRodney W. Grimes {
4239b50d902SRodney W. Grimes 	char *folder;
4240c3a8314SMike Heffner 	int copylen;
4259b50d902SRodney W. Grimes 
4269ce73e90SMike Heffner 	if ((folder = value("folder")) == NULL)
4279b50d902SRodney W. Grimes 		return (-1);
4289b50d902SRodney W. Grimes 	if (*folder == '/')
4290c3a8314SMike Heffner 		copylen = strlcpy(name, folder, namelen);
4309b50d902SRodney W. Grimes 	else
4319ce73e90SMike Heffner 		copylen = snprintf(name, namelen, "%s/%s",
4329ce73e90SMike Heffner 		    homedir ? homedir : ".", folder);
433cbe1d3b6SBrian Somers 	return (copylen < 0 || copylen >= namelen ? (-1) : (0));
4349b50d902SRodney W. Grimes }
4359b50d902SRodney W. Grimes 
4369b50d902SRodney W. Grimes /*
4379b50d902SRodney W. Grimes  * Return the name of the dead.letter file.
4389b50d902SRodney W. Grimes  */
4399b50d902SRodney W. Grimes char *
4406d8484b0SPhilippe Charnier getdeadletter(void)
4419b50d902SRodney W. Grimes {
4429ce73e90SMike Heffner 	char *cp;
4439b50d902SRodney W. Grimes 
4449ce73e90SMike Heffner 	if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
4459b50d902SRodney W. Grimes 		cp = expand("~/dead.letter");
4469b50d902SRodney W. Grimes 	else if (*cp != '/') {
4479b50d902SRodney W. Grimes 		char buf[PATHSIZE];
4489b50d902SRodney W. Grimes 
4490c3a8314SMike Heffner 		(void)snprintf(buf, sizeof(buf), "~/%s", cp);
4509b50d902SRodney W. Grimes 		cp = expand(buf);
4519b50d902SRodney W. Grimes 	}
4529ce73e90SMike Heffner 	return (cp);
4539b50d902SRodney W. Grimes }
454