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