#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)fio.c 8.2 (Berkeley) 4/20/95";
#else
__RCSID("$NetBSD: fio.c,v 1.46 2026/06/25 22:27:52 kre Exp $");
#endif
#endif
#include "rcv.h"
#include "extern.h"
#include "thread.h"
#include "sig.h"
#include <wordexp.h>
#ifndef THREAD_SUPPORT
static struct message *message;
static int msgCount;
PUBLIC struct message *
next_message(struct message *mp)
{
if (mp + 1 < message || mp + 1 >= message + msgCount)
return NULL;
return mp + 1;
}
PUBLIC struct message *
prev_message(struct message *mp)
{
if (mp - 1 < message || mp - 1 >= message + msgCount)
return NULL;
return mp - 1;
}
PUBLIC struct message *
get_message(int msgnum)
{
if (msgnum < 1 || msgnum > msgCount)
return NULL;
return message + msgnum - 1;
}
PUBLIC int
get_msgnum(struct message *mp)
{
if (mp < message || mp >= message + msgCount)
return 0;
return mp - message + 1;
}
PUBLIC int
get_msgCount(void)
{
return msgCount;
}
#endif
static void
message_init(struct message *mp, off_t offset, short flags)
{
(void)memset(mp, 0, sizeof(*mp));
mp->m_flag = flags;
mp->m_block = blockof(offset);
mp->m_offset = blkoffsetof(offset);
}
static void
makemessage(FILE *f, int omsgCount, int nmsgCount)
{
ssize_t size;
struct message *omessage;
struct message *nmessage;
ptrdiff_t off;
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
omessage = get_abs_message(1);
size = (nmsgCount + 1) * sizeof(*nmessage);
if (omsgCount == 0 || omessage == NULL)
off = 0;
else
off = dot - omessage;
thread_save_old_links(omessage, omsgCount);
nmessage = realloc(omessage, size);
if (nmessage == NULL)
err(EXIT_FAILURE,
"Insufficient memory for %d messages", nmsgCount);
dot = nmessage + off;
thread_fix_old_links(nmessage, min(omsgCount, nmsgCount));
#ifndef THREAD_SUPPORT
message = nmessage;
#endif
size -= (omsgCount + 1) * sizeof(*nmessage);
if (size >= 0) {
(void)fflush(f);
(void)lseek(fileno(f), (off_t)sizeof(*nmessage), SEEK_SET);
if (read(fileno(f), &nmessage[omsgCount], (size_t)size) != size)
errx(EXIT_FAILURE, "Message temporary file corrupted");
}
message_init(&nmessage[nmsgCount], (off_t)0, 0);
thread_fix_new_links(nmessage, omsgCount, nmsgCount);
(void)Fclose(f);
}
static int
append(struct message *mp, FILE *f)
{
return fwrite(mp, sizeof(*mp), 1, f) != 1;
}
PUBLIC void
setptr(FILE *ibuf, off_t offset)
{
int c;
size_t len;
char *cp;
const char *cp2;
struct message this;
FILE *mestmp;
int maybe, inhead;
char linebuf[LINESIZE];
int omsgCount;
#ifdef THREAD_SUPPORT
int nmsgCount;
#else
# define nmsgCount msgCount
#endif
(void)snprintf(linebuf, LINESIZE, "%s/mail.XXXXXX", tmpdir);
if ((c = mkstemp(linebuf)) == -1 ||
(mestmp = Fdopen(c, "ref+")) == NULL) {
(void)fprintf(stderr, "mail: can't open %s\n", linebuf);
exit(1);
}
(void)unlink(linebuf);
nmsgCount = get_abs_msgCount();
if (offset == 0) {
nmsgCount = 0;
} else {
(void)fseeko(ibuf, offset, 0);
(void)fseek(otf, 0L, SEEK_END);
offset = ftell(otf);
}
omsgCount = nmsgCount;
maybe = 1;
inhead = 0;
message_init(&this, (off_t)0, MUSED|MNEW);
for (;;) {
if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
if (append(&this, mestmp))
err(EXIT_FAILURE, "temporary file");
makemessage(mestmp, omsgCount, nmsgCount);
return;
}
len = strlen(linebuf);
if (len >= 2 && linebuf[len - 1] == '\n' &&
linebuf[len - 2] == '\r') {
linebuf[len - 2] = '\n';
len--;
}
(void)fwrite(linebuf, sizeof(*linebuf), len, otf);
if (ferror(otf))
err(EXIT_FAILURE, "/tmp");
if (len)
linebuf[len - 1] = 0;
if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
nmsgCount++;
if (append(&this, mestmp))
err(EXIT_FAILURE, "temporary file");
message_init(&this, offset, MUSED|MNEW);
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++))
continue;
if (cp[-1] != ':')
break;
while ((c = *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 += len;
this.m_size += len;
this.m_lines++;
if (!inhead) {
int lines_plus_wraps = 1;
int linelen = (int)strlen(linebuf);
if (screenwidth && (int)linelen > screenwidth) {
lines_plus_wraps = linelen / screenwidth;
if (linelen % screenwidth != 0)
++lines_plus_wraps;
}
this.m_blines += lines_plus_wraps;
}
maybe = linebuf[0] == 0;
}
}
PUBLIC int
putline(FILE *obuf, const char *linebuf, int outlf)
{
size_t c;
c = strlen(linebuf);
(void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
if (outlf) {
(void)putc('\n', obuf);
c++;
}
if (ferror(obuf))
return -1;
return (int)c;
}
PUBLIC int
readline(FILE *ibuf, char *linebuf, int linesize, int no_restart)
{
struct sigaction osa_sigtstp;
struct sigaction osa_sigttin;
struct sigaction osa_sigttou;
int n;
clearerr(ibuf);
sig_check();
if (no_restart) {
(void)sig_setflags(SIGTSTP, 0, &osa_sigtstp);
(void)sig_setflags(SIGTTIN, 0, &osa_sigttin);
(void)sig_setflags(SIGTTOU, 0, &osa_sigttou);
}
if (fgets(linebuf, linesize, ibuf) == NULL)
n = -1;
else {
n = (int)strlen(linebuf);
if (n > 0 && linebuf[n - 1] == '\n')
linebuf[--n] = '\0';
}
if (no_restart) {
(void)sigaction(SIGTSTP, &osa_sigtstp, NULL);
(void)sigaction(SIGTTIN, &osa_sigttin, NULL);
(void)sigaction(SIGTTOU, &osa_sigttou, NULL);
}
sig_check();
return n;
}
PUBLIC FILE *
setinput(const struct message *mp)
{
(void)fflush(otf);
if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
err(EXIT_FAILURE, "fseek");
return itf;
}
PUBLIC int
rm(char *name)
{
struct stat sb;
if (stat(name, &sb) < 0)
return -1;
if (!S_ISREG(sb.st_mode)) {
errno = EISDIR;
return -1;
}
return unlink(name);
}
PUBLIC off_t
fsize(FILE *iob)
{
struct stat sbuf;
if (fstat(fileno(iob), &sbuf) < 0)
return 0;
return sbuf.st_size;
}
PUBLIC int
getfold(char *name, size_t namesize)
{
char unres[PATHSIZE], res[PATHSIZE];
char *folder;
if ((folder = value(ENAME_FOLDER)) == NULL)
return -1;
if (*folder != '/') {
(void)snprintf(unres, sizeof(unres), "%s/%s", homedir, folder);
folder = unres;
}
if (realpath(folder, res) == NULL)
warn("Can't canonicalize folder `%s'", folder);
else
folder = res;
(void)strlcpy(name, folder, namesize);
return 0;
}
PUBLIC const char *
expand(const char *name)
{
char xname[PATHSIZE];
char cmdbuf[PATHSIZE];
int e;
wordexp_t we;
sigset_t nset, oset;
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) {
warnx("No previous file");
return NULL;
}
return savestr(prevfile);
case '&':
if (name[1] == 0 && (name = value(ENAME_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] == '~' && (name[1] == '/' || name[1] == '\0')) {
(void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
name = savestr(xname);
}
if (strpbrk(name, "~{[*?$`'\"\\") == NULL)
return name;
*xname = '\0';
sigemptyset(&nset);
sigaddset(&nset, SIGCHLD);
sigprocmask(SIG_BLOCK, &nset, &oset);
e = wordexp(name, &we, WRDE_NOCMD);
sigprocmask(SIG_SETMASK, &oset, NULL);
switch (e) {
case 0:
break;
case WRDE_NOSPACE:
warnx("Out of memory expanding `%s'", name);
return NULL;
case WRDE_BADVAL:
case WRDE_BADCHAR:
case WRDE_SYNTAX:
warnx("Syntax error expanding `%s'", name);
return NULL;
case WRDE_CMDSUB:
warnx("Command substitution not allowed expanding `%s'",
name);
return NULL;
default:
warnx("Unknown expansion error %d expanding `%s'", e, name);
return NULL;
}
switch (we.we_wordc) {
case 0:
warnx("No match for `%s'", name);
break;
case 1:
if (strlen(we.we_wordv[0]) >= PATHSIZE)
warnx("Expansion too long for `%s'", name);
strlcpy(xname, we.we_wordv[0], PATHSIZE);
break;
default:
warnx("Ambiguous expansion for `%s'", name);
break;
}
wordfree(&we);
if (!*xname)
return NULL;
else
return savestr(xname);
}
PUBLIC const char *
getdeadletter(void)
{
const char *cp;
if ((cp = value(ENAME_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;
}