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