NetBSD/usr.bin/mail/fio.c

463 lines
11 KiB
C
Raw Normal View History

/* $NetBSD: fio.c,v 1.23 2003/10/31 01:25:54 ross Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-06-29 09:09:04 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
1996-12-28 10:10:57 +03:00
static char sccsid[] = "@(#)fio.c 8.2 (Berkeley) 4/20/95";
#else
__RCSID("$NetBSD: fio.c,v 1.23 2003/10/31 01:25:54 ross Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "rcv.h"
1994-06-29 09:09:04 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
/*
* Mail -- a mail program
*
* File I/O.
*/
extern int wait_status;
extern char *tmpdir;
1993-03-21 12:45:37 +03:00
/*
* Set up the input pointers while copying the mail file into /tmp.
*/
1994-06-29 09:09:04 +04:00
void
2002-03-02 17:59:35 +03:00
setptr(FILE *ibuf, off_t offset)
1993-03-21 12:45:37 +03:00
{
2002-03-02 18:27:51 +03:00
int c, len;
char *cp, *cp2;
1993-03-21 12:45:37 +03:00
struct message this;
FILE *mestmp;
int maybe, inhead;
char linebuf[LINESIZE];
1996-12-28 10:10:57 +03:00
int omsgCount;
1993-03-21 12:45:37 +03:00
/* Get temporary file. */
(void)snprintf(linebuf, LINESIZE, "%s/mail.XXXXXX", tmpdir);
1993-03-21 12:45:37 +03:00
if ((c = mkstemp(linebuf)) == -1 ||
(mestmp = Fdopen(c, "r+")) == NULL) {
(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
exit(1);
}
(void)unlink(linebuf);
1996-12-28 10:10:57 +03:00
if (offset == 0) {
msgCount = 0;
} else {
/* Seek into the file to get to the new messages */
2002-03-06 00:18:14 +03:00
(void)fseek(ibuf, offset, 0);
1996-12-28 10:10:57 +03:00
/*
* We need to make "offset" a pointer to the end of
* the temp file that has the copy of the mail file.
* If any messages have been edited, this will be
* different from the offset into the mail file.
*/
2002-03-06 00:18:14 +03:00
(void)fseek(otf, 0L, 2);
1996-12-28 10:10:57 +03:00
offset = ftell(otf);
}
omsgCount = msgCount;
1993-03-21 12:45:37 +03:00
maybe = 1;
inhead = 0;
this.m_flag = MUSED|MNEW;
this.m_size = 0;
this.m_lines = 0;
this.m_blines = 0;
1993-03-21 12:45:37 +03:00
this.m_block = 0;
this.m_offset = 0;
for (;;) {
if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
if (append(&this, mestmp)) {
2002-03-06 00:29:30 +03:00
warn("temporary file");
1993-03-21 12:45:37 +03:00
exit(1);
}
1996-12-28 10:10:57 +03:00
makemessage(mestmp, omsgCount);
1993-03-21 12:45:37 +03:00
return;
}
2002-03-02 18:27:51 +03:00
len = strlen(linebuf);
/*
* Transforms lines ending in <CR><LF> to just <LF>.
* This allows mail to be able to read Eudora mailboxes
* that reside on a DOS partition.
*/
2002-03-02 18:27:51 +03:00
if (len >= 2 && linebuf[len-1] == '\n' && linebuf[len-2] == '\r') {
linebuf[len-2] = '\n';
len--;
}
2002-03-06 00:18:14 +03:00
(void)fwrite(linebuf, sizeof *linebuf, len, otf);
1993-03-21 12:45:37 +03:00
if (ferror(otf)) {
2002-03-06 00:29:30 +03:00
warn("/tmp");
1993-03-21 12:45:37 +03:00
exit(1);
}
2002-03-02 18:27:51 +03:00
if(len)
linebuf[len - 1] = 0;
1993-03-21 12:45:37 +03:00
if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
msgCount++;
if (append(&this, mestmp)) {
2002-03-06 00:29:30 +03:00
warn("temporary file");
1993-03-21 12:45:37 +03:00
exit(1);
}
this.m_flag = MUSED|MNEW;
this.m_size = 0;
this.m_lines = 0;
this.m_blines = 0;
1993-03-21 12:45:37 +03:00
this.m_block = blockof(offset);
this.m_offset = offsetof(offset);
inhead = 1;
} else if (linebuf[0] == 0) {
inhead = 0;
} else if (inhead) {
for (cp = linebuf, cp2 = "status";; cp++) {
if ((c = *cp2++) == 0) {
while (isspace((unsigned char)*cp++))
1993-03-21 12:45:37 +03:00
;
if (cp[-1] != ':')
break;
while ((c = *cp++) != '\0')
1993-03-21 12:45:37 +03:00
if (c == 'R')
this.m_flag |= MREAD;
else if (c == 'O')
this.m_flag &= ~MNEW;
inhead = 0;
break;
}
if (*cp != c && *cp != toupper(c))
break;
}
}
2002-03-02 18:27:51 +03:00
offset += len;
this.m_size += len;
1993-03-21 12:45:37 +03:00
this.m_lines++;
if (!inhead) {
int lines_plus_wraps = 1;
int linelen = strlen(linebuf);
if (linelen > screenwidth) {
lines_plus_wraps = linelen / screenwidth;
if (linelen % screenwidth != 0)
++lines_plus_wraps;
}
this.m_blines += lines_plus_wraps;
}
1993-03-21 12:45:37 +03:00
maybe = linebuf[0] == 0;
}
}
/*
* Drop the passed line onto the passed output buffer.
* If a write error occurs, return -1, else the count of
* characters written, including the newline if requested.
1993-03-21 12:45:37 +03:00
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
putline(FILE *obuf, char *linebuf, int outlf)
1993-03-21 12:45:37 +03:00
{
int c;
1993-03-21 12:45:37 +03:00
c = strlen(linebuf);
2002-03-06 00:18:14 +03:00
(void)fwrite(linebuf, sizeof *linebuf, c, obuf);
if (outlf) {
2002-03-06 00:18:14 +03:00
(void)putc('\n', obuf);
c++;
}
1993-03-21 12:45:37 +03:00
if (ferror(obuf))
return (-1);
return (c);
1993-03-21 12:45:37 +03:00
}
/*
* Read up a line from the specified input into the line
* buffer. Return the number of characters read. Do not
* include the newline at the end.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
readline(FILE *ibuf, char *linebuf, int linesize)
1993-03-21 12:45:37 +03:00
{
int n;
1993-03-21 12:45:37 +03:00
clearerr(ibuf);
if (fgets(linebuf, linesize, ibuf) == NULL)
return -1;
n = strlen(linebuf);
if (n > 0 && linebuf[n - 1] == '\n')
linebuf[--n] = '\0';
return n;
}
/*
* Return a file buffer all ready to read up the
* passed message pointer.
*/
FILE *
setinput(const struct message *mp)
1993-03-21 12:45:37 +03:00
{
fflush(otf);
if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0)
err(1, "fseek");
1993-03-21 12:45:37 +03:00
return (itf);
}
/*
* Take the data out of the passed ghost file and toss it into
* a dynamically allocated message structure.
*/
1994-06-29 09:09:04 +04:00
void
2002-03-02 17:59:35 +03:00
makemessage(FILE *f, int omsgCount)
1993-03-21 12:45:37 +03:00
{
int size = (msgCount + 1) * sizeof (struct message);
struct message *nmessage = realloc(message, size);
1993-03-21 12:45:37 +03:00
if (nmessage == NULL)
err(1, "Insufficient memory for %d messages", msgCount);
if (omsgCount == 0 || message == NULL)
dot = nmessage;
else
dot = nmessage + (dot - message);
message = nmessage;
1996-12-28 10:10:57 +03:00
size -= (omsgCount + 1) * sizeof (struct message);
1993-03-21 12:45:37 +03:00
fflush(f);
2002-03-06 00:18:14 +03:00
(void)lseek(fileno(f), (off_t)sizeof *message, 0);
1996-12-28 10:10:57 +03:00
if (read(fileno(f), (char *) &message[omsgCount], size) != size)
errx(1, "Message temporary file corrupted");
1993-03-21 12:45:37 +03:00
message[msgCount].m_size = 0;
message[msgCount].m_lines = 0;
message[msgCount].m_blines = 0;
1993-03-21 12:45:37 +03:00
Fclose(f);
}
/*
* Append the passed message descriptor onto the temp file.
* If the write fails, return 1, else 0
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
append(struct message *mp, FILE *f)
1993-03-21 12:45:37 +03:00
{
return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
}
/*
* Delete a file, but only if the file is a plain file.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
rm(char *name)
1993-03-21 12:45:37 +03:00
{
struct stat sb;
if (stat(name, &sb) < 0)
return(-1);
if (!S_ISREG(sb.st_mode)) {
errno = EISDIR;
return(-1);
}
return(unlink(name));
}
static int sigdepth; /* depth of holdsigs() */
static sigset_t nset, oset;
1993-03-21 12:45:37 +03:00
/*
* Hold signals SIGHUP, SIGINT, and SIGQUIT.
*/
1994-06-29 09:09:04 +04:00
void
2002-03-02 17:59:35 +03:00
holdsigs(void)
1993-03-21 12:45:37 +03:00
{
if (sigdepth++ == 0) {
sigemptyset(&nset);
sigaddset(&nset, SIGHUP);
sigaddset(&nset, SIGINT);
sigaddset(&nset, SIGQUIT);
sigprocmask(SIG_BLOCK, &nset, &oset);
}
1993-03-21 12:45:37 +03:00
}
/*
* Release signals SIGHUP, SIGINT, and SIGQUIT.
*/
1994-06-29 09:09:04 +04:00
void
2002-03-02 17:59:35 +03:00
relsesigs(void)
1993-03-21 12:45:37 +03:00
{
if (--sigdepth == 0)
sigprocmask(SIG_SETMASK, &oset, NULL);
1993-03-21 12:45:37 +03:00
}
/*
* Determine the size of the file possessed by
* the passed buffer.
*/
off_t
2002-03-02 17:59:35 +03:00
fsize(FILE *iob)
1993-03-21 12:45:37 +03:00
{
struct stat sbuf;
if (fstat(fileno(iob), &sbuf) < 0)
return 0;
return sbuf.st_size;
}
/*
* Evaluate the string given as a new mailbox name.
* Supported meta characters:
* % for my system mail box
* %user for user's system mail box
* # for previous file
* & invoker's mbox file
* +file file in folder directory
* any shell meta character
* Return the file name as a dynamic string.
*/
char *
2002-03-02 17:59:35 +03:00
expand(char *name)
1993-03-21 12:45:37 +03:00
{
char xname[PATHSIZE];
char cmdbuf[PATHSIZE]; /* also used for file names */
int pid, l;
2002-03-02 18:27:51 +03:00
char *cp, *shellcmd;
1993-03-21 12:45:37 +03:00
int pivec[2];
struct stat sbuf;
/*
* The order of evaluation is "%" and "#" expand into constants.
* "&" can expand into "+". "+" can expand into shell meta characters.
* Shell meta characters expand into constants.
* This way, we make no recursive expansion.
*/
switch (*name) {
case '%':
findmail(name[1] ? name + 1 : myname, xname);
return savestr(xname);
case '#':
if (name[1] != 0)
break;
if (prevfile[0] == 0) {
printf("No previous file\n");
return NULL;
1993-03-21 12:45:37 +03:00
}
return savestr(prevfile);
case '&':
if (name[1] == 0 && (name = value("MBOX")) == NULL)
1993-03-21 12:45:37 +03:00
name = "~/mbox";
/* fall through */
}
if (name[0] == '+' && getfold(cmdbuf) >= 0) {
snprintf(xname, PATHSIZE, "%s/%s", cmdbuf, name + 1);
1993-03-21 12:45:37 +03:00
name = savestr(xname);
}
/* catch the most common shell meta character */
if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
snprintf(xname, PATHSIZE, "%s%s", homedir, name + 1);
1993-03-21 12:45:37 +03:00
name = savestr(xname);
}
2002-03-05 22:25:16 +03:00
if (strpbrk(name, "~{[*?$`'\"\\") == NULL)
1993-03-21 12:45:37 +03:00
return name;
if (pipe(pivec) < 0) {
2002-03-06 00:29:30 +03:00
warn("pipe");
1993-03-21 12:45:37 +03:00
return name;
}
snprintf(cmdbuf, PATHSIZE, "echo %s", name);
if ((shellcmd = value("SHELL")) == NULL)
2002-03-02 18:27:51 +03:00
shellcmd = _PATH_CSHELL;
pid = start_command(shellcmd, 0, -1, pivec[1], "-c", cmdbuf, NULL);
1993-03-21 12:45:37 +03:00
if (pid < 0) {
close(pivec[0]);
close(pivec[1]);
return NULL;
1993-03-21 12:45:37 +03:00
}
close(pivec[1]);
l = read(pivec[0], xname, PATHSIZE);
1993-03-21 12:45:37 +03:00
close(pivec[0]);
if (wait_child(pid) < 0 && WTERMSIG(wait_status) != SIGPIPE) {
1993-03-21 12:45:37 +03:00
fprintf(stderr, "\"%s\": Expansion failed.\n", name);
return NULL;
1993-03-21 12:45:37 +03:00
}
if (l < 0) {
2002-03-06 00:29:30 +03:00
warn("read");
return NULL;
1993-03-21 12:45:37 +03:00
}
if (l == 0) {
fprintf(stderr, "\"%s\": No match.\n", name);
return NULL;
1993-03-21 12:45:37 +03:00
}
if (l == PATHSIZE) {
1993-03-21 12:45:37 +03:00
fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
return NULL;
1993-03-21 12:45:37 +03:00
}
xname[l] = '\0';
1993-03-21 12:45:37 +03:00
for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
;
cp[1] = '\0';
if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
1993-03-21 12:45:37 +03:00
fprintf(stderr, "\"%s\": Ambiguous.\n", name);
return NULL;
1993-03-21 12:45:37 +03:00
}
return savestr(xname);
}
/*
* Determine the current folder directory name.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
getfold(char *name)
1993-03-21 12:45:37 +03:00
{
char *folder;
if ((folder = value("folder")) == NULL)
1993-03-21 12:45:37 +03:00
return (-1);
if (*folder == '/') {
strncpy(name, folder, PATHSIZE - 1);
name[PATHSIZE - 1] = '\0' ;
}
1993-03-21 12:45:37 +03:00
else
snprintf(name, PATHSIZE, "%s/%s", homedir, folder);
1993-03-21 12:45:37 +03:00
return (0);
}
/*
* Return the name of the dead.letter file.
*/
char *
2002-03-02 17:59:35 +03:00
getdeadletter(void)
1993-03-21 12:45:37 +03:00
{
char *cp;
1993-03-21 12:45:37 +03:00
if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
1993-03-21 12:45:37 +03:00
cp = expand("~/dead.letter");
else if (*cp != '/') {
char buf[PATHSIZE];
2002-03-06 00:18:14 +03:00
(void)snprintf(buf, PATHSIZE, "~/%s", cp);
1993-03-21 12:45:37 +03:00
cp = expand(buf);
}
return cp;
}