Home | History | Annotate | Line # | Download | only in mail
fio.c revision 1.9
      1 /*	$NetBSD: fio.c,v 1.9 1997/10/18 15:48:48 matt 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)fio.c	8.2 (Berkeley) 4/20/95";
     39 #else
     40 static char rcsid[] = "$NetBSD: fio.c,v 1.9 1997/10/18 15:48:48 matt Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include "rcv.h"
     45 #include <sys/file.h>
     46 #include <sys/wait.h>
     47 
     48 #include <unistd.h>
     49 #include <paths.h>
     50 #include <errno.h>
     51 #include "extern.h"
     52 
     53 /*
     54  * Mail -- a mail program
     55  *
     56  * File I/O.
     57  */
     58 
     59 /*
     60  * Set up the input pointers while copying the mail file into /tmp.
     61  */
     62 void
     63 setptr(ibuf, offset)
     64 	register FILE *ibuf;
     65 	off_t offset;
     66 {
     67 	extern char *tmpdir;
     68 	register int c, count;
     69 	register char *cp, *cp2;
     70 	struct message this;
     71 	FILE *mestmp;
     72 	int maybe, inhead;
     73 	char linebuf[LINESIZE];
     74 	int omsgCount;
     75 
     76 	/* Get temporary file. */
     77 	(void)snprintf(linebuf, LINESIZE, "%s/mail.XXXXXX", tmpdir);
     78 	if ((c = mkstemp(linebuf)) == -1 ||
     79 	    (mestmp = Fdopen(c, "r+")) == NULL) {
     80 		(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
     81 		exit(1);
     82 	}
     83 	(void)unlink(linebuf);
     84 
     85 	if (offset == 0) {
     86 		 msgCount = 0;
     87 	} else {
     88 		/* Seek into the file to get to the new messages */
     89 		(void) fseek(ibuf, offset, 0);
     90 		/*
     91 		 * We need to make "offset" a pointer to the end of
     92 		 * the temp file that has the copy of the mail file.
     93 		 * If any messages have been edited, this will be
     94 		 * different from the offset into the mail file.
     95 		 */
     96 		(void) fseek(otf, 0L, 2);
     97 		offset = ftell(otf);
     98 	}
     99 	omsgCount = msgCount;
    100 	maybe = 1;
    101 	inhead = 0;
    102 	this.m_flag = MUSED|MNEW;
    103 	this.m_size = 0;
    104 	this.m_lines = 0;
    105 	this.m_block = 0;
    106 	this.m_offset = 0;
    107 	for (;;) {
    108 		if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
    109 			if (append(&this, mestmp)) {
    110 				perror("temporary file");
    111 				exit(1);
    112 			}
    113 			makemessage(mestmp, omsgCount);
    114 			return;
    115 		}
    116 		count = strlen(linebuf);
    117 		/*
    118 		 * Transforms lines ending in <CR><LF> to just <LF>.
    119 		 * This allows mail to be able to read Eudora mailboxes
    120 		 * that reside on a DOS partition.
    121 		 */
    122 		if (count >= 2 && linebuf[count-1] == '\n' && linebuf[count-2] == '\r') {
    123 			linebuf[count-2] = '\n';
    124 			count--;
    125 		}
    126 		(void) fwrite(linebuf, sizeof *linebuf, count, otf);
    127 		if (ferror(otf)) {
    128 			perror("/tmp");
    129 			exit(1);
    130 		}
    131 		linebuf[count - 1] = 0;
    132 		if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
    133 			msgCount++;
    134 			if (append(&this, mestmp)) {
    135 				perror("temporary file");
    136 				exit(1);
    137 			}
    138 			this.m_flag = MUSED|MNEW;
    139 			this.m_size = 0;
    140 			this.m_lines = 0;
    141 			this.m_block = blockof(offset);
    142 			this.m_offset = offsetof(offset);
    143 			inhead = 1;
    144 		} else if (linebuf[0] == 0) {
    145 			inhead = 0;
    146 		} else if (inhead) {
    147 			for (cp = linebuf, cp2 = "status";; cp++) {
    148 				if ((c = *cp2++) == 0) {
    149 					while (isspace(*cp++))
    150 						;
    151 					if (cp[-1] != ':')
    152 						break;
    153 					while ((c = *cp++) != '\0')
    154 						if (c == 'R')
    155 							this.m_flag |= MREAD;
    156 						else if (c == 'O')
    157 							this.m_flag &= ~MNEW;
    158 					inhead = 0;
    159 					break;
    160 				}
    161 				if (*cp != c && *cp != toupper(c))
    162 					break;
    163 			}
    164 		}
    165 		offset += count;
    166 		this.m_size += count;
    167 		this.m_lines++;
    168 		maybe = linebuf[0] == 0;
    169 	}
    170 }
    171 
    172 /*
    173  * Drop the passed line onto the passed output buffer.
    174  * If a write error occurs, return -1, else the count of
    175  * characters written, including the newline if requested.
    176  */
    177 int
    178 putline(obuf, linebuf, outlf)
    179 	FILE *obuf;
    180 	char *linebuf;
    181 	int   outlf;
    182 {
    183 	register int c;
    184 
    185 	c = strlen(linebuf);
    186 	(void) fwrite(linebuf, sizeof *linebuf, c, obuf);
    187 	if (outlf) {
    188 		(void) putc('\n', obuf);
    189 		c++;
    190 	}
    191 	if (ferror(obuf))
    192 		return (-1);
    193 	return (c);
    194 }
    195 
    196 /*
    197  * Read up a line from the specified input into the line
    198  * buffer.  Return the number of characters read.  Do not
    199  * include the newline at the end.
    200  */
    201 int
    202 readline(ibuf, linebuf, linesize)
    203 	FILE *ibuf;
    204 	char *linebuf;
    205 	int linesize;
    206 {
    207 	register int n;
    208 
    209 	clearerr(ibuf);
    210 	if (fgets(linebuf, linesize, ibuf) == NULL)
    211 		return -1;
    212 	n = strlen(linebuf);
    213 	if (n > 0 && linebuf[n - 1] == '\n')
    214 		linebuf[--n] = '\0';
    215 	return n;
    216 }
    217 
    218 /*
    219  * Return a file buffer all ready to read up the
    220  * passed message pointer.
    221  */
    222 FILE *
    223 setinput(mp)
    224 	register struct message *mp;
    225 {
    226 
    227 	fflush(otf);
    228 	if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
    229 		perror("fseek");
    230 		panic("temporary file seek");
    231 	}
    232 	return (itf);
    233 }
    234 
    235 /*
    236  * Take the data out of the passed ghost file and toss it into
    237  * a dynamically allocated message structure.
    238  */
    239 void
    240 makemessage(f, omsgCount)
    241 	FILE *f;
    242 	int omsgCount;
    243 {
    244 	register size = (msgCount + 1) * sizeof (struct message);
    245 
    246 	if (omsgCount) {
    247 		message = (struct message *)realloc(message, (unsigned) size);
    248 		if (message == 0)
    249 			panic("Insufficient memory for %d messages\n",
    250 			      msgCount);
    251 	} else {
    252 		if (message != 0)
    253 			free((char *) message);
    254 		if ((message = (struct message *) malloc((unsigned) size)) == 0)
    255 			panic("Insufficient memory for %d messages", msgCount);
    256 		dot = message;
    257 	}
    258 	size -= (omsgCount + 1) * sizeof (struct message);
    259 	fflush(f);
    260 	(void) lseek(fileno(f), (off_t)sizeof *message, 0);
    261 	if (read(fileno(f), (char *) &message[omsgCount], size) != size)
    262 		panic("Message temporary file corrupted");
    263 	message[msgCount].m_size = 0;
    264 	message[msgCount].m_lines = 0;
    265 	Fclose(f);
    266 }
    267 
    268 /*
    269  * Append the passed message descriptor onto the temp file.
    270  * If the write fails, return 1, else 0
    271  */
    272 int
    273 append(mp, f)
    274 	struct message *mp;
    275 	FILE *f;
    276 {
    277 	return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
    278 }
    279 
    280 /*
    281  * Delete a file, but only if the file is a plain file.
    282  */
    283 int
    284 rm(name)
    285 	char *name;
    286 {
    287 	struct stat sb;
    288 
    289 	if (stat(name, &sb) < 0)
    290 		return(-1);
    291 	if (!S_ISREG(sb.st_mode)) {
    292 		errno = EISDIR;
    293 		return(-1);
    294 	}
    295 	return(unlink(name));
    296 }
    297 
    298 static int sigdepth;		/* depth of holdsigs() */
    299 static sigset_t nset, oset;
    300 /*
    301  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
    302  */
    303 void
    304 holdsigs()
    305 {
    306 
    307 	if (sigdepth++ == 0) {
    308 		sigemptyset(&nset);
    309 		sigaddset(&nset, SIGHUP);
    310 		sigaddset(&nset, SIGINT);
    311 		sigaddset(&nset, SIGQUIT);
    312 		sigprocmask(SIG_BLOCK, &nset, &oset);
    313 	}
    314 }
    315 
    316 /*
    317  * Release signals SIGHUP, SIGINT, and SIGQUIT.
    318  */
    319 void
    320 relsesigs()
    321 {
    322 
    323 	if (--sigdepth == 0)
    324 		sigprocmask(SIG_SETMASK, &oset, NULL);
    325 }
    326 
    327 /*
    328  * Determine the size of the file possessed by
    329  * the passed buffer.
    330  */
    331 off_t
    332 fsize(iob)
    333 	FILE *iob;
    334 {
    335 	struct stat sbuf;
    336 
    337 	if (fstat(fileno(iob), &sbuf) < 0)
    338 		return 0;
    339 	return sbuf.st_size;
    340 }
    341 
    342 /*
    343  * Evaluate the string given as a new mailbox name.
    344  * Supported meta characters:
    345  *	%	for my system mail box
    346  *	%user	for user's system mail box
    347  *	#	for previous file
    348  *	&	invoker's mbox file
    349  *	+file	file in folder directory
    350  *	any shell meta character
    351  * Return the file name as a dynamic string.
    352  */
    353 char *
    354 expand(name)
    355 	register char *name;
    356 {
    357 	char xname[PATHSIZE];
    358 	char cmdbuf[PATHSIZE];		/* also used for file names */
    359 	register int pid, l;
    360 	register char *cp, *shell;
    361 	int pivec[2];
    362 	struct stat sbuf;
    363 	extern union wait wait_status;
    364 
    365 	/*
    366 	 * The order of evaluation is "%" and "#" expand into constants.
    367 	 * "&" can expand into "+".  "+" can expand into shell meta characters.
    368 	 * Shell meta characters expand into constants.
    369 	 * This way, we make no recursive expansion.
    370 	 */
    371 	switch (*name) {
    372 	case '%':
    373 		findmail(name[1] ? name + 1 : myname, xname);
    374 		return savestr(xname);
    375 	case '#':
    376 		if (name[1] != 0)
    377 			break;
    378 		if (prevfile[0] == 0) {
    379 			printf("No previous file\n");
    380 			return NOSTR;
    381 		}
    382 		return savestr(prevfile);
    383 	case '&':
    384 		if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
    385 			name = "~/mbox";
    386 		/* fall through */
    387 	}
    388 	if (name[0] == '+' && getfold(cmdbuf) >= 0) {
    389 		snprintf(xname, PATHSIZE, "%s/%s", cmdbuf, name + 1);
    390 		name = savestr(xname);
    391 	}
    392 	/* catch the most common shell meta character */
    393 	if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
    394 		snprintf(xname, PATHSIZE, "%s%s", homedir, name + 1);
    395 		name = savestr(xname);
    396 	}
    397 	if (!anyof(name, "~{[*?$`'\"\\"))
    398 		return name;
    399 	if (pipe(pivec) < 0) {
    400 		perror("pipe");
    401 		return name;
    402 	}
    403 	snprintf(cmdbuf, PATHSIZE, "echo %s", name);
    404 	if ((shell = value("SHELL")) == NOSTR)
    405 		shell = _PATH_CSHELL;
    406 	pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
    407 	if (pid < 0) {
    408 		close(pivec[0]);
    409 		close(pivec[1]);
    410 		return NOSTR;
    411 	}
    412 	close(pivec[1]);
    413 	l = read(pivec[0], xname, PATHSIZE);
    414 	close(pivec[0]);
    415 	if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
    416 		fprintf(stderr, "\"%s\": Expansion failed.\n", name);
    417 		return NOSTR;
    418 	}
    419 	if (l < 0) {
    420 		perror("read");
    421 		return NOSTR;
    422 	}
    423 	if (l == 0) {
    424 		fprintf(stderr, "\"%s\": No match.\n", name);
    425 		return NOSTR;
    426 	}
    427 	if (l == PATHSIZE) {
    428 		fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
    429 		return NOSTR;
    430 	}
    431 	xname[l] = '\0';
    432 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
    433 		;
    434 	cp[1] = '\0';
    435 	if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
    436 		fprintf(stderr, "\"%s\": Ambiguous.\n", name);
    437 		return NOSTR;
    438 	}
    439 	return savestr(xname);
    440 }
    441 
    442 /*
    443  * Determine the current folder directory name.
    444  */
    445 int
    446 getfold(name)
    447 	char *name;
    448 {
    449 	char *folder;
    450 
    451 	if ((folder = value("folder")) == NOSTR)
    452 		return (-1);
    453 	if (*folder == '/') {
    454 		strncpy(name, folder, PATHSIZE - 1);
    455 		name[PATHSIZE - 1] = '\0' ;
    456 	}
    457 	else
    458 		snprintf(name, PATHSIZE, "%s/%s", homedir, folder);
    459 	return (0);
    460 }
    461 
    462 /*
    463  * Return the name of the dead.letter file.
    464  */
    465 char *
    466 getdeadletter()
    467 {
    468 	register char *cp;
    469 
    470 	if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
    471 		cp = expand("~/dead.letter");
    472 	else if (*cp != '/') {
    473 		char buf[PATHSIZE];
    474 
    475 		(void) snprintf(buf, PATHSIZE, "~/%s", cp);
    476 		cp = expand(buf);
    477 	}
    478 	return cp;
    479 }
    480