#include "rcv.h"
#include <unistd.h>
#include <paths.h>
#include <errno.h>
#include <glob.h>
#include "extern.h"
static volatile sig_atomic_t fiosignal;
static ssize_t
myread(int fd, char *buf, int len)
{
ssize_t nread;
while ((nread = read(fd, buf, len)) == -1 && errno == EINTR)
;
return(nread);
}
void
setptr(FILE *ibuf, off_t offset)
{
int c, count;
char *cp, *cp2;
struct message this;
FILE *mestmp;
int maybe, inhead, omsgCount;
char linebuf[LINESIZE], pathbuf[PATHSIZE];
(void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
err(1, "can't open %s", pathbuf);
(void)rm(pathbuf);
if (offset == 0) {
msgCount = 0;
} else {
(void)fseeko(ibuf, offset, SEEK_SET);
(void)fseeko(otf, (off_t)0, SEEK_END);
offset = ftell(otf);
}
omsgCount = msgCount;
maybe = 1;
inhead = 0;
this.m_flag = MUSED|MNEW;
this.m_size = 0;
this.m_lines = 0;
this.m_block = 0;
this.m_offset = 0;
for (;;) {
if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
if (append(&this, mestmp))
err(1, "temporary file");
makemessage(mestmp, omsgCount);
return;
}
count = strlen(linebuf);
if (count >= 2 && linebuf[count-1] == '\n' &&
linebuf[count - 2] == '\r') {
linebuf[count - 2] = '\n';
linebuf[count - 1] = '\0';
count--;
}
(void)fwrite(linebuf, sizeof(*linebuf), count, otf);
if (ferror(otf))
err(1, "%s", pathbuf);
if (count && linebuf[count - 1] == '\n')
linebuf[count - 1] = '\0';
if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
msgCount++;
if (append(&this, mestmp))
err(1, "temporary file");
this.m_flag = MUSED|MNEW;
this.m_size = 0;
this.m_lines = 0;
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 = (unsigned char)*cp2++) == 0) {
while (isspace((unsigned char)*cp++))
;
if (cp[-1] != ':')
break;
while ((c = (unsigned char)*cp++) != '\0')
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;
}
}
offset += count;
this.m_size += count;
this.m_lines++;
maybe = linebuf[0] == 0;
}
}
int
putline(FILE *obuf, char *linebuf, int outlf)
{
int c;
c = strlen(linebuf);
(void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
if (outlf) {
(void)putc('\n', obuf);
c++;
}
if (ferror(obuf))
return(-1);
return(c);
}
int
readline(FILE *ibuf, char *linebuf, int linesize, int *signo)
{
struct sigaction act;
struct sigaction savetstp;
struct sigaction savettou;
struct sigaction savettin;
struct sigaction saveint;
struct sigaction savehup;
sigset_t oset;
int n;
if (signo) {
fiosignal = 0;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = fioint;
if (sigaction(SIGINT, NULL, &saveint) == 0 &&
saveint.sa_handler != SIG_IGN) {
(void)sigaction(SIGINT, &act, &saveint);
(void)sigprocmask(SIG_UNBLOCK, &intset, &oset);
}
if (sigaction(SIGHUP, NULL, &savehup) == 0 &&
savehup.sa_handler != SIG_IGN)
(void)sigaction(SIGHUP, &act, &savehup);
(void)sigaction(SIGTSTP, &act, &savetstp);
(void)sigaction(SIGTTOU, &act, &savettou);
(void)sigaction(SIGTTIN, &act, &savettin);
}
clearerr(ibuf);
if (fgets(linebuf, linesize, ibuf) == NULL) {
if (ferror(ibuf))
clearerr(ibuf);
n = -1;
} else {
n = strlen(linebuf);
if (n > 0 && linebuf[n - 1] == '\n')
linebuf[--n] = '\0';
if (n > 0 && linebuf[n - 1] == '\r')
linebuf[--n] = '\0';
}
if (signo) {
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
(void)sigaction(SIGINT, &saveint, NULL);
(void)sigaction(SIGHUP, &savehup, NULL);
(void)sigaction(SIGTSTP, &savetstp, NULL);
(void)sigaction(SIGTTOU, &savettou, NULL);
(void)sigaction(SIGTTIN, &savettin, NULL);
*signo = fiosignal;
}
return(n);
}
FILE *
setinput(struct message *mp)
{
fflush(otf);
if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), SEEK_SET)
== -1)
err(1, "fseek");
return(itf);
}
void
makemessage(FILE *f, int omsgCount)
{
size_t size;
struct message *nmessage;
size = (msgCount + 1) * sizeof(struct message);
nmessage = realloc(message, size);
if (nmessage == 0)
err(1, "realloc");
if (omsgCount == 0 || message == NULL)
dot = nmessage;
else
dot = nmessage + (dot - message);
message = nmessage;
size -= (omsgCount + 1) * sizeof(struct message);
fflush(f);
(void)lseek(fileno(f), (off_t)sizeof(*message), SEEK_SET);
if (myread(fileno(f), (void *) &message[omsgCount], size) != size)
errx(1, "Message temporary file corrupted");
message[msgCount].m_size = 0;
message[msgCount].m_lines = 0;
(void)Fclose(f);
}
int
append(struct message *mp, FILE *f)
{
return(fwrite((char *) mp, sizeof(*mp), 1, f) != 1);
}
int
rm(char *name)
{
struct stat sb;
if (stat(name, &sb) == -1)
return(-1);
if (!S_ISREG(sb.st_mode)) {
errno = EISDIR;
return(-1);
}
if (unlink(name) == -1) {
if (errno == EPERM)
return(truncate(name, (off_t)0));
else
return(-1);
}
return(0);
}
static int sigdepth;
static sigset_t nset, oset;
void
holdsigs(void)
{
if (sigdepth++ == 0) {
sigemptyset(&nset);
sigaddset(&nset, SIGHUP);
sigaddset(&nset, SIGINT);
sigaddset(&nset, SIGQUIT);
sigprocmask(SIG_BLOCK, &nset, &oset);
}
}
void
relsesigs(void)
{
if (--sigdepth == 0)
sigprocmask(SIG_SETMASK, &oset, NULL);
}
int
ignoresig(int sig, struct sigaction *oact, sigset_t *oset)
{
struct sigaction act;
sigset_t nset;
int error;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_IGN;
error = sigaction(sig, &act, oact);
if (error == 0) {
sigemptyset(&nset);
sigaddset(&nset, sig);
(void)sigprocmask(SIG_UNBLOCK, &nset, oset);
} else if (oset != NULL)
(void)sigprocmask(SIG_BLOCK, NULL, oset);
return(error);
}
off_t
fsize(FILE *iob)
{
struct stat sbuf;
if (fstat(fileno(iob), &sbuf) == -1)
return(0);
return(sbuf.st_size);
}
char *
expand(char *name)
{
const int flags = GLOB_BRACE|GLOB_TILDE|GLOB_NOSORT;
char xname[PATHSIZE];
char cmdbuf[PATHSIZE];
char *match = NULL;
glob_t names;
switch (*name) {
case '%':
findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
return(savestr(xname));
case '#':
if (name[1] != 0)
break;
if (prevfile[0] == 0) {
puts("No previous file");
return(NULL);
}
return(savestr(prevfile));
case '&':
if (name[1] == 0 && (name = value("MBOX")) == NULL)
name = "~/mbox";
}
if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
(void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
name = savestr(xname);
}
if (name[0] == '~' && homedir && (name[1] == '/' || name[1] == '\0')) {
(void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
name = savestr(xname);
}
if (strpbrk(name, "~{[*?\\") == NULL)
return(savestr(name));
switch (glob(name, flags, NULL, &names)) {
case 0:
if (names.gl_pathc == 1)
match = savestr(names.gl_pathv[0]);
else
fprintf(stderr, "\"%s\": Ambiguous.\n", name);
break;
case GLOB_NOSPACE:
fprintf(stderr, "\"%s\": Out of memory.\n", name);
break;
case GLOB_NOMATCH:
fprintf(stderr, "\"%s\": No match.\n", name);
break;
default:
fprintf(stderr, "\"%s\": Expansion failed.\n", name);
break;
}
globfree(&names);
return(match);
}
int
getfold(char *name, int namelen)
{
char *folder;
if ((folder = value("folder")) == NULL)
return(-1);
if (*folder == '/')
strlcpy(name, folder, namelen);
else
(void)snprintf(name, namelen, "%s/%s", homedir ? homedir : ".",
folder);
return(0);
}
char *
getdeadletter(void)
{
char *cp;
if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
cp = expand("~/dead.letter");
else if (*cp != '/') {
char buf[PATHSIZE];
(void)snprintf(buf, sizeof(buf), "~/%s", cp);
cp = expand(buf);
}
return(cp);
}
void
fioint(int s)
{
fiosignal = s;
}