#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: edit.c,v 1.29 2017/11/09 20:27:50 christos Exp $");
#endif
#endif
#include "rcv.h"
#include "extern.h"
#include "thread.h"
#include "sig.h"
PUBLIC FILE *
run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
{
FILE *nf = NULL;
int t;
time_t modtime;
const char *editcmd;
char tempname[PATHSIZE];
struct stat statb;
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.ReXXXXXXXXXX", tmpdir);
if ((t = mkstemp(tempname)) == -1) {
warn("%s", tempname);
goto out;
}
if (readonlyflag && fchmod(t, 0400) == -1) {
(void)close(t);
warn("%s", tempname);
(void)unlink(tempname);
goto out;
}
if ((nf = Fdopen(t, "wef")) == NULL) {
(void)close(t);
warn("%s", tempname);
(void)unlink(tempname);
goto out;
}
if (size >= 0)
while (--size >= 0 && (t = getc(fp)) != EOF)
(void)putc(t, nf);
else
while ((t = getc(fp)) != EOF)
(void)putc(t, nf);
(void)fflush(nf);
if (ferror(nf)) {
(void)Fclose(nf);
warn("%s", tempname);
(void)unlink(tempname);
nf = NULL;
goto out;
}
if (fstat(fileno(nf), &statb) < 0)
modtime = 0;
else
modtime = statb.st_mtime;
if (Fclose(nf) < 0) {
warn("%s", tempname);
(void)unlink(tempname);
nf = NULL;
goto out;
}
nf = NULL;
editcmd = value(editortype == 'e' ? ENAME_EDITOR : ENAME_VISUAL);
if (editcmd == NULL)
editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
if (run_command(editcmd, NULL, 0, -1, tempname, NULL) < 0) {
(void)unlink(tempname);
goto out;
}
if (readonlyflag) {
(void)unlink(tempname);
goto out;
}
if (stat(tempname, &statb) < 0) {
warn("%s", tempname);
goto out;
}
if (modtime == statb.st_mtime) {
(void)unlink(tempname);
goto out;
}
if ((nf = Fopen(tempname, "aef+")) == NULL) {
warn("%s", tempname);
(void)unlink(tempname);
goto out;
}
(void)unlink(tempname);
out:
return nf;
}
static int
edit1(int *msgvec, int editortype)
{
int c;
int i;
int msgCount;
FILE *fp;
struct message *mp;
off_t size;
msgCount = get_msgCount();
for (i = 0; i < msgCount && msgvec[i]; i++) {
sigset_t oset;
struct sigaction osa;
if (i > 0) {
char buf[100];
char *p;
(void)printf("Edit message %d [ynq]? ", msgvec[i]);
if (fgets(buf, (int)sizeof(buf), stdin) == NULL)
break;
p = skip_WSP(buf);
if (*p == 'q')
break;
if (*p == 'n')
continue;
}
dot = mp = get_message(msgvec[i]);
touch(mp);
sig_check();
(void)sig_ignore(SIGINT, &osa, &oset);
fp = run_editor(setinput(mp), mp->m_size, editortype,
readonly);
if (fp != NULL) {
(void)fseek(otf, 0L, 2);
size = ftell(otf);
mp->m_block = blockof(size);
mp->m_offset = blkoffsetof(size);
mp->m_size = fsize(fp);
mp->m_lines = 0;
mp->m_blines = 0;
mp->m_flag |= MMODIFY;
rewind(fp);
while ((c = getc(fp)) != EOF) {
if (c == '\n') {
mp->m_lines++;
mp->m_blines++;
}
if (putc(c, otf) == EOF)
break;
}
if (ferror(otf))
warn("/tmp");
(void)Fclose(fp);
}
(void)sig_restore(SIGINT, &osa, &oset);
sig_check();
}
return 0;
}
PUBLIC int
editor(void *v)
{
int *msgvec = v;
return edit1(msgvec, 'e');
}
PUBLIC int
visual(void *v)
{
int *msgvec = v;
return edit1(msgvec, 'v');
}