Home | History | Annotate | Line # | Download | only in mail
fio.c revision 1.35.2.2
      1  1.35.2.2       tls /*	$NetBSD: fio.c,v 1.35.2.2 2013/02/25 00:30:36 tls Exp $	*/
      2       1.5  christos 
      3       1.1       cgd /*
      4       1.4   deraadt  * Copyright (c) 1980, 1993
      5       1.4   deraadt  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15      1.22       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.10     lukem #include <sys/cdefs.h>
     33       1.1       cgd #ifndef lint
     34       1.5  christos #if 0
     35       1.6       tls static char sccsid[] = "@(#)fio.c	8.2 (Berkeley) 4/20/95";
     36       1.5  christos #else
     37  1.35.2.2       tls __RCSID("$NetBSD: fio.c,v 1.35.2.2 2013/02/25 00:30:36 tls Exp $");
     38       1.5  christos #endif
     39       1.1       cgd #endif /* not lint */
     40       1.1       cgd 
     41       1.1       cgd #include "rcv.h"
     42       1.4   deraadt #include "extern.h"
     43      1.28  christos #include "thread.h"
     44      1.32  christos #include "sig.h"
     45       1.1       cgd 
     46       1.1       cgd /*
     47       1.1       cgd  * Mail -- a mail program
     48       1.1       cgd  *
     49       1.1       cgd  * File I/O.
     50       1.1       cgd  */
     51       1.1       cgd 
     52      1.28  christos #ifndef THREAD_SUPPORT
     53      1.28  christos /************************************************************************/
     54      1.28  christos /*
     55      1.28  christos  * If we have threading support, these routines live in thread.c.
     56      1.28  christos  */
     57      1.28  christos static struct message *message;		/* message structure array */
     58      1.28  christos static int msgCount;			/* Count of messages read in */
     59      1.28  christos 
     60      1.28  christos PUBLIC struct message *
     61      1.28  christos next_message(struct message *mp)
     62      1.28  christos {
     63      1.28  christos 	if (mp + 1 < message || mp + 1 >= message + msgCount)
     64      1.30  christos 		return NULL;
     65      1.30  christos 
     66      1.28  christos 	return mp + 1;
     67      1.28  christos }
     68      1.28  christos 
     69      1.28  christos PUBLIC struct message *
     70      1.28  christos prev_message(struct message *mp)
     71      1.28  christos {
     72      1.28  christos 	if (mp - 1 < message || mp - 1 >= message + msgCount)
     73      1.30  christos 		return NULL;
     74      1.30  christos 
     75      1.28  christos 	return mp - 1;
     76      1.28  christos }
     77      1.28  christos 
     78      1.28  christos PUBLIC struct message *
     79      1.28  christos get_message(int msgnum)
     80      1.28  christos {
     81      1.28  christos 	if (msgnum < 1 || msgnum > msgCount)
     82      1.28  christos 		return NULL;
     83      1.28  christos 
     84      1.28  christos 	return message + msgnum - 1;
     85      1.28  christos }
     86      1.28  christos 
     87      1.28  christos PUBLIC int
     88      1.28  christos get_msgnum(struct message *mp)
     89      1.28  christos {
     90      1.28  christos 	if (mp < message || mp >= message + msgCount)
     91      1.28  christos 		return 0;
     92      1.28  christos 
     93      1.28  christos 	return mp - message + 1;
     94      1.28  christos }
     95      1.28  christos 
     96      1.28  christos PUBLIC int
     97      1.28  christos get_msgCount(void)
     98      1.28  christos {
     99      1.28  christos 	return msgCount;
    100      1.28  christos }
    101      1.28  christos #endif /* THREAD_SUPPORT */
    102      1.28  christos /************************************************************************/
    103      1.28  christos 
    104      1.28  christos /*
    105      1.28  christos  * Initialize a message structure.
    106      1.28  christos  */
    107      1.28  christos static void
    108      1.28  christos message_init(struct message *mp, off_t offset, short flags)
    109      1.28  christos {
    110      1.28  christos 	/* use memset so new fields are always zeroed */
    111      1.28  christos 	(void)memset(mp, 0, sizeof(*mp));
    112      1.28  christos 	mp->m_flag = flags;
    113      1.28  christos 	mp->m_block = blockof(offset);
    114      1.29    dogcow 	mp->m_offset = blkoffsetof(offset);
    115      1.28  christos }
    116      1.28  christos 
    117      1.28  christos /*
    118      1.28  christos  * Take the data out of the passed ghost file and toss it into
    119      1.28  christos  * a dynamically allocated message structure.
    120      1.28  christos  */
    121      1.28  christos static void
    122      1.28  christos makemessage(FILE *f, int omsgCount, int nmsgCount)
    123      1.28  christos {
    124      1.28  christos 	size_t size;
    125      1.28  christos 	struct message *omessage;	/* old message structure array */
    126      1.28  christos 	struct message *nmessage;
    127      1.28  christos 
    128      1.28  christos 	omessage = get_abs_message(1);
    129      1.28  christos 
    130      1.28  christos 	size = (nmsgCount + 1) * sizeof(*nmessage);
    131      1.28  christos 	nmessage = realloc(omessage, size);
    132      1.28  christos 	if (nmessage == NULL)
    133      1.34  christos 		err(EXIT_FAILURE,
    134      1.34  christos 		    "Insufficient memory for %d messages", nmsgCount);
    135      1.28  christos 	if (omsgCount == 0 || omessage == NULL)
    136      1.28  christos 		dot = nmessage;
    137      1.28  christos 	else
    138      1.28  christos 		dot = nmessage + (dot - omessage);
    139      1.28  christos 
    140      1.28  christos 	thread_fix_old_links(nmessage, omessage, omsgCount);
    141      1.28  christos 
    142      1.28  christos #ifndef THREAD_SUPPORT
    143      1.28  christos 	message = nmessage;
    144      1.28  christos #endif
    145      1.28  christos 	size -= (omsgCount + 1) * sizeof(*nmessage);
    146      1.28  christos 	(void)fflush(f);
    147      1.28  christos 	(void)lseek(fileno(f), (off_t)sizeof(*nmessage), SEEK_SET);
    148      1.28  christos 	if (read(fileno(f), &nmessage[omsgCount], size) != (ssize_t)size)
    149      1.28  christos 		errx(EXIT_FAILURE, "Message temporary file corrupted");
    150      1.28  christos 
    151      1.28  christos 	message_init(&nmessage[nmsgCount], (off_t)0, 0); /* append a dummy */
    152      1.28  christos 
    153      1.28  christos 	thread_fix_new_links(nmessage, omsgCount, nmsgCount);
    154      1.28  christos 
    155      1.28  christos 	(void)Fclose(f);
    156      1.28  christos }
    157      1.28  christos 
    158      1.28  christos /*
    159      1.28  christos  * Append the passed message descriptor onto the temp file.
    160      1.28  christos  * If the write fails, return 1, else 0
    161      1.28  christos  */
    162      1.28  christos static int
    163      1.28  christos append(struct message *mp, FILE *f)
    164      1.28  christos {
    165      1.31  christos 	return fwrite(mp, sizeof(*mp), 1, f) != 1;
    166      1.28  christos }
    167      1.28  christos 
    168       1.1       cgd /*
    169       1.1       cgd  * Set up the input pointers while copying the mail file into /tmp.
    170       1.1       cgd  */
    171      1.28  christos PUBLIC void
    172      1.15       wiz setptr(FILE *ibuf, off_t offset)
    173       1.1       cgd {
    174      1.25  christos 	int c;
    175      1.25  christos 	size_t len;
    176      1.24  christos 	char *cp;
    177      1.24  christos 	const char *cp2;
    178       1.1       cgd 	struct message this;
    179       1.1       cgd 	FILE *mestmp;
    180       1.1       cgd 	int maybe, inhead;
    181       1.1       cgd 	char linebuf[LINESIZE];
    182       1.6       tls 	int omsgCount;
    183      1.28  christos #ifdef THREAD_SUPPORT
    184      1.28  christos 	int nmsgCount;
    185      1.28  christos #else
    186      1.28  christos # define nmsgCount	msgCount
    187      1.28  christos #endif
    188       1.1       cgd 
    189       1.1       cgd 	/* Get temporary file. */
    190       1.7     mikel 	(void)snprintf(linebuf, LINESIZE, "%s/mail.XXXXXX", tmpdir);
    191       1.1       cgd 	if ((c = mkstemp(linebuf)) == -1 ||
    192      1.35  christos 	    (mestmp = Fdopen(c, "re+")) == NULL) {
    193       1.1       cgd 		(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
    194       1.1       cgd 		exit(1);
    195       1.1       cgd 	}
    196       1.1       cgd 	(void)unlink(linebuf);
    197       1.1       cgd 
    198      1.28  christos 	nmsgCount = get_abs_msgCount();
    199       1.6       tls 	if (offset == 0) {
    200      1.28  christos 		nmsgCount = 0;
    201       1.6       tls 	} else {
    202       1.6       tls 		/* Seek into the file to get to the new messages */
    203      1.25  christos 		(void)fseeko(ibuf, offset, 0);
    204       1.6       tls 		/*
    205       1.6       tls 		 * We need to make "offset" a pointer to the end of
    206       1.6       tls 		 * the temp file that has the copy of the mail file.
    207       1.6       tls 		 * If any messages have been edited, this will be
    208       1.6       tls 		 * different from the offset into the mail file.
    209       1.6       tls 		 */
    210      1.28  christos 		(void)fseek(otf, 0L, SEEK_END);
    211       1.6       tls 		offset = ftell(otf);
    212       1.6       tls 	}
    213      1.28  christos 	omsgCount = nmsgCount;
    214       1.1       cgd 	maybe = 1;
    215       1.1       cgd 	inhead = 0;
    216      1.28  christos 	message_init(&this, (off_t)0, MUSED|MNEW);
    217      1.28  christos 
    218       1.1       cgd 	for (;;) {
    219       1.1       cgd 		if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
    220      1.32  christos 			if (append(&this, mestmp))
    221      1.32  christos 				err(EXIT_FAILURE, "temporary file");
    222      1.28  christos 			makemessage(mestmp, omsgCount, nmsgCount);
    223       1.1       cgd 			return;
    224       1.1       cgd 		}
    225      1.16       wiz 		len = strlen(linebuf);
    226       1.9      matt 		/*
    227       1.9      matt 		 * Transforms lines ending in <CR><LF> to just <LF>.
    228       1.9      matt 		 * This allows mail to be able to read Eudora mailboxes
    229       1.9      matt 		 * that reside on a DOS partition.
    230       1.9      matt 		 */
    231      1.30  christos 		if (len >= 2 && linebuf[len - 1] == '\n' &&
    232      1.30  christos 		    linebuf[len - 2] == '\r') {
    233      1.30  christos 			linebuf[len - 2] = '\n';
    234      1.16       wiz 			len--;
    235       1.9      matt 		}
    236      1.31  christos 		(void)fwrite(linebuf, sizeof(*linebuf), len, otf);
    237      1.32  christos 		if (ferror(otf))
    238      1.32  christos 			err(EXIT_FAILURE, "/tmp");
    239      1.33  christos 		if (len)
    240      1.16       wiz 			linebuf[len - 1] = 0;
    241       1.1       cgd 		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
    242      1.28  christos 			nmsgCount++;
    243      1.32  christos 			if (append(&this, mestmp))
    244      1.32  christos 				err(EXIT_FAILURE, "temporary file");
    245      1.28  christos 			message_init(&this, offset, MUSED|MNEW);
    246       1.1       cgd 			inhead = 1;
    247       1.1       cgd 		} else if (linebuf[0] == 0) {
    248       1.1       cgd 			inhead = 0;
    249       1.1       cgd 		} else if (inhead) {
    250       1.1       cgd 			for (cp = linebuf, cp2 = "status";; cp++) {
    251       1.1       cgd 				if ((c = *cp2++) == 0) {
    252      1.12  christos 					while (isspace((unsigned char)*cp++))
    253      1.28  christos 						continue;
    254       1.1       cgd 					if (cp[-1] != ':')
    255       1.1       cgd 						break;
    256       1.5  christos 					while ((c = *cp++) != '\0')
    257       1.1       cgd 						if (c == 'R')
    258       1.1       cgd 							this.m_flag |= MREAD;
    259       1.1       cgd 						else if (c == 'O')
    260       1.1       cgd 							this.m_flag &= ~MNEW;
    261       1.1       cgd 					inhead = 0;
    262       1.1       cgd 					break;
    263       1.1       cgd 				}
    264       1.1       cgd 				if (*cp != c && *cp != toupper(c))
    265       1.1       cgd 					break;
    266       1.1       cgd 			}
    267       1.1       cgd 		}
    268      1.16       wiz 		offset += len;
    269      1.16       wiz 		this.m_size += len;
    270       1.1       cgd 		this.m_lines++;
    271      1.21      ross 		if (!inhead) {
    272      1.21      ross 			int lines_plus_wraps = 1;
    273      1.32  christos 			int linelen = (int)strlen(linebuf);
    274      1.21      ross 
    275      1.28  christos 			if (screenwidth && (int)linelen > screenwidth) {
    276      1.21      ross 				lines_plus_wraps = linelen / screenwidth;
    277      1.21      ross 				if (linelen % screenwidth != 0)
    278      1.21      ross 					++lines_plus_wraps;
    279      1.21      ross 			}
    280      1.21      ross 			this.m_blines += lines_plus_wraps;
    281      1.21      ross 		}
    282       1.1       cgd 		maybe = linebuf[0] == 0;
    283       1.1       cgd 	}
    284       1.1       cgd }
    285       1.1       cgd 
    286       1.1       cgd /*
    287       1.1       cgd  * Drop the passed line onto the passed output buffer.
    288       1.1       cgd  * If a write error occurs, return -1, else the count of
    289       1.8      phil  * characters written, including the newline if requested.
    290       1.1       cgd  */
    291      1.28  christos PUBLIC int
    292      1.24  christos putline(FILE *obuf, const char *linebuf, int outlf)
    293       1.1       cgd {
    294      1.25  christos 	size_t c;
    295       1.1       cgd 
    296       1.1       cgd 	c = strlen(linebuf);
    297      1.31  christos 	(void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
    298       1.8      phil 	if (outlf) {
    299      1.19       wiz 		(void)putc('\n', obuf);
    300       1.8      phil 		c++;
    301       1.8      phil 	}
    302       1.1       cgd 	if (ferror(obuf))
    303      1.28  christos 		return -1;
    304      1.32  christos 	return (int)c;
    305       1.1       cgd }
    306       1.1       cgd 
    307       1.1       cgd /*
    308       1.1       cgd  * Read up a line from the specified input into the line
    309       1.1       cgd  * buffer.  Return the number of characters read.  Do not
    310       1.1       cgd  * include the newline at the end.
    311       1.1       cgd  */
    312      1.28  christos PUBLIC int
    313      1.32  christos readline(FILE *ibuf, char *linebuf, int linesize, int no_restart)
    314       1.1       cgd {
    315      1.32  christos 	struct sigaction osa_sigtstp;
    316      1.32  christos 	struct sigaction osa_sigttin;
    317      1.32  christos 	struct sigaction osa_sigttou;
    318      1.10     lukem 	int n;
    319       1.1       cgd 
    320       1.1       cgd 	clearerr(ibuf);
    321      1.32  christos 
    322      1.32  christos 	sig_check();
    323      1.32  christos 	if (no_restart) {
    324      1.32  christos 		(void)sig_setflags(SIGTSTP, 0, &osa_sigtstp);
    325      1.32  christos 		(void)sig_setflags(SIGTTIN, 0, &osa_sigttin);
    326      1.32  christos 		(void)sig_setflags(SIGTTOU, 0, &osa_sigttou);
    327      1.32  christos 	}
    328       1.1       cgd 	if (fgets(linebuf, linesize, ibuf) == NULL)
    329      1.32  christos 		n = -1;
    330      1.32  christos 	else {
    331      1.32  christos 		n = (int)strlen(linebuf);
    332      1.32  christos 		if (n > 0 && linebuf[n - 1] == '\n')
    333      1.32  christos 			linebuf[--n] = '\0';
    334      1.32  christos 	}
    335      1.32  christos 	if (no_restart) {
    336      1.32  christos 		(void)sigaction(SIGTSTP, &osa_sigtstp, NULL);
    337      1.32  christos 		(void)sigaction(SIGTTIN, &osa_sigttin, NULL);
    338      1.32  christos 		(void)sigaction(SIGTTOU, &osa_sigttou, NULL);
    339      1.32  christos 	}
    340      1.32  christos 	sig_check();
    341       1.1       cgd 	return n;
    342       1.1       cgd }
    343       1.1       cgd 
    344       1.1       cgd /*
    345       1.1       cgd  * Return a file buffer all ready to read up the
    346       1.1       cgd  * passed message pointer.
    347       1.1       cgd  */
    348      1.28  christos PUBLIC FILE *
    349      1.23      ross setinput(const struct message *mp)
    350       1.1       cgd {
    351       1.1       cgd 
    352      1.25  christos 	(void)fflush(otf);
    353      1.28  christos 	if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
    354      1.34  christos 		err(EXIT_FAILURE, "fseek");
    355      1.28  christos 	return itf;
    356       1.1       cgd }
    357       1.1       cgd 
    358       1.1       cgd /*
    359       1.1       cgd  * Delete a file, but only if the file is a plain file.
    360       1.1       cgd  */
    361      1.28  christos PUBLIC int
    362      1.15       wiz rm(char *name)
    363       1.1       cgd {
    364       1.1       cgd 	struct stat sb;
    365       1.1       cgd 
    366       1.1       cgd 	if (stat(name, &sb) < 0)
    367      1.28  christos 		return -1;
    368       1.1       cgd 	if (!S_ISREG(sb.st_mode)) {
    369       1.1       cgd 		errno = EISDIR;
    370      1.28  christos 		return -1;
    371       1.1       cgd 	}
    372      1.28  christos 	return unlink(name);
    373       1.1       cgd }
    374       1.1       cgd 
    375       1.1       cgd /*
    376       1.1       cgd  * Determine the size of the file possessed by
    377       1.1       cgd  * the passed buffer.
    378       1.1       cgd  */
    379      1.28  christos PUBLIC off_t
    380      1.15       wiz fsize(FILE *iob)
    381       1.1       cgd {
    382       1.1       cgd 	struct stat sbuf;
    383       1.1       cgd 
    384       1.1       cgd 	if (fstat(fileno(iob), &sbuf) < 0)
    385       1.1       cgd 		return 0;
    386       1.1       cgd 	return sbuf.st_size;
    387       1.1       cgd }
    388       1.1       cgd 
    389       1.1       cgd /*
    390      1.28  christos  * Determine the current folder directory name.
    391      1.28  christos  */
    392      1.28  christos PUBLIC int
    393      1.28  christos getfold(char *name, size_t namesize)
    394      1.28  christos {
    395  1.35.2.2       tls 	char unres[PATHSIZE], res[PATHSIZE];
    396      1.28  christos 	char *folder;
    397      1.28  christos 
    398      1.28  christos 	if ((folder = value(ENAME_FOLDER)) == NULL)
    399      1.28  christos 		return -1;
    400  1.35.2.2       tls 	if (*folder != '/') {
    401  1.35.2.2       tls 		(void)snprintf(unres, sizeof(unres), "%s/%s", homedir, folder);
    402  1.35.2.2       tls 		folder = unres;
    403  1.35.2.2       tls 	}
    404  1.35.2.2       tls 	if (realpath(folder, res) == NULL)
    405  1.35.2.2       tls 		warn("Can't canonicalize folder `%s'", folder);
    406      1.28  christos 	else
    407  1.35.2.2       tls 		folder = res;
    408  1.35.2.2       tls 	(void)strlcpy(name, folder, namesize);
    409      1.28  christos 	return 0;
    410      1.28  christos }
    411      1.28  christos 
    412      1.28  christos /*
    413       1.1       cgd  * Evaluate the string given as a new mailbox name.
    414       1.1       cgd  * Supported meta characters:
    415       1.1       cgd  *	%	for my system mail box
    416       1.1       cgd  *	%user	for user's system mail box
    417       1.1       cgd  *	#	for previous file
    418       1.1       cgd  *	&	invoker's mbox file
    419       1.1       cgd  *	+file	file in folder directory
    420       1.1       cgd  *	any shell meta character
    421       1.1       cgd  * Return the file name as a dynamic string.
    422       1.1       cgd  */
    423      1.28  christos PUBLIC const char *
    424      1.24  christos expand(const char *name)
    425       1.1       cgd {
    426       1.1       cgd 	char xname[PATHSIZE];
    427       1.1       cgd 	char cmdbuf[PATHSIZE];		/* also used for file names */
    428      1.32  christos 	pid_t pid;
    429      1.32  christos 	ssize_t l;
    430      1.24  christos 	char *cp;
    431      1.24  christos 	const char *shellcmd;
    432       1.1       cgd 	int pivec[2];
    433       1.1       cgd 	struct stat sbuf;
    434       1.1       cgd 
    435       1.1       cgd 	/*
    436       1.1       cgd 	 * The order of evaluation is "%" and "#" expand into constants.
    437       1.1       cgd 	 * "&" can expand into "+".  "+" can expand into shell meta characters.
    438       1.1       cgd 	 * Shell meta characters expand into constants.
    439       1.1       cgd 	 * This way, we make no recursive expansion.
    440       1.1       cgd 	 */
    441       1.1       cgd 	switch (*name) {
    442       1.1       cgd 	case '%':
    443      1.28  christos 		findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
    444       1.1       cgd 		return savestr(xname);
    445       1.1       cgd 	case '#':
    446       1.1       cgd 		if (name[1] != 0)
    447       1.1       cgd 			break;
    448       1.1       cgd 		if (prevfile[0] == 0) {
    449  1.35.2.1       tls 			warnx("No previous file");
    450      1.17       wiz 			return NULL;
    451       1.1       cgd 		}
    452       1.1       cgd 		return savestr(prevfile);
    453       1.1       cgd 	case '&':
    454      1.28  christos 		if (name[1] == 0 && (name = value(ENAME_MBOX)) == NULL)
    455       1.1       cgd 			name = "~/mbox";
    456       1.1       cgd 		/* fall through */
    457       1.1       cgd 	}
    458      1.28  christos 	if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
    459      1.28  christos 		(void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
    460       1.1       cgd 		name = savestr(xname);
    461       1.1       cgd 	}
    462       1.1       cgd 	/* catch the most common shell meta character */
    463       1.1       cgd 	if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
    464      1.28  christos 		(void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
    465       1.1       cgd 		name = savestr(xname);
    466       1.1       cgd 	}
    467      1.18       wiz 	if (strpbrk(name, "~{[*?$`'\"\\") == NULL)
    468       1.1       cgd 		return name;
    469       1.1       cgd 	if (pipe(pivec) < 0) {
    470      1.20       wiz 		warn("pipe");
    471       1.1       cgd 		return name;
    472       1.1       cgd 	}
    473      1.28  christos 	(void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
    474      1.28  christos 	if ((shellcmd = value(ENAME_SHELL)) == NULL)
    475      1.16       wiz 		shellcmd = _PATH_CSHELL;
    476      1.32  christos 	pid = start_command(shellcmd, NULL, -1, pivec[1], "-c", cmdbuf, NULL);
    477       1.1       cgd 	if (pid < 0) {
    478      1.25  christos 		(void)close(pivec[0]);
    479      1.25  christos 		(void)close(pivec[1]);
    480      1.17       wiz 		return NULL;
    481       1.1       cgd 	}
    482      1.25  christos 	(void)close(pivec[1]);
    483      1.28  christos 	l = read(pivec[0], xname, sizeof(xname));
    484      1.25  christos 	(void)close(pivec[0]);
    485      1.12  christos 	if (wait_child(pid) < 0 && WTERMSIG(wait_status) != SIGPIPE) {
    486      1.34  christos 		warnx("Expansion `%s' failed [%x]", cmdbuf, wait_status);
    487      1.17       wiz 		return NULL;
    488       1.1       cgd 	}
    489       1.1       cgd 	if (l < 0) {
    490      1.20       wiz 		warn("read");
    491      1.17       wiz 		return NULL;
    492       1.1       cgd 	}
    493       1.1       cgd 	if (l == 0) {
    494      1.34  christos 		warnx("No match for `%s'", name);
    495      1.17       wiz 		return NULL;
    496       1.1       cgd 	}
    497      1.28  christos 	if (l == sizeof(xname)) {
    498      1.34  christos 		warnx("Expansion buffer overflow for `%s'", name);
    499      1.17       wiz 		return NULL;
    500       1.1       cgd 	}
    501       1.7     mikel 	xname[l] = '\0';
    502       1.1       cgd 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
    503      1.28  christos 		continue;
    504       1.1       cgd 	cp[1] = '\0';
    505      1.12  christos 	if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
    506      1.34  christos 		warnx("Ambiguous expansion for `%s'", name);
    507      1.17       wiz 		return NULL;
    508       1.1       cgd 	}
    509       1.1       cgd 	return savestr(xname);
    510       1.1       cgd }
    511       1.1       cgd 
    512       1.1       cgd /*
    513       1.1       cgd  * Return the name of the dead.letter file.
    514       1.1       cgd  */
    515      1.28  christos PUBLIC const char *
    516      1.15       wiz getdeadletter(void)
    517       1.1       cgd {
    518      1.24  christos 	const char *cp;
    519       1.1       cgd 
    520      1.28  christos 	if ((cp = value(ENAME_DEAD)) == NULL || (cp = expand(cp)) == NULL)
    521       1.1       cgd 		cp = expand("~/dead.letter");
    522       1.1       cgd 	else if (*cp != '/') {
    523       1.1       cgd 		char buf[PATHSIZE];
    524      1.28  christos 		(void)snprintf(buf, sizeof(buf), "~/%s", cp);
    525       1.1       cgd 		cp = expand(buf);
    526       1.1       cgd 	}
    527       1.1       cgd 	return cp;
    528       1.1       cgd }
    529