1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2001-03-25 04:57:05 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
|
2001-03-25 04:57:05 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "rcv.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mail -- a mail program
|
|
|
|
*
|
|
|
|
* Perform message editing functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Edit a message list.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
editor(msgvec)
|
|
|
|
int *msgvec;
|
|
|
|
{
|
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
return (edit1(msgvec, 'e'));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invoke the visual editor on a message list.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
visual(msgvec)
|
|
|
|
int *msgvec;
|
|
|
|
{
|
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
return (edit1(msgvec, 'v'));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Edit a message by writing the message into a funnily-named file
|
|
|
|
* (which should not exist) and forking an editor on it.
|
|
|
|
* We get the editor from the stuff above.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
edit1(msgvec, type)
|
|
|
|
int *msgvec;
|
|
|
|
int type;
|
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
int c, i;
|
1994-05-27 12:33:43 +00:00
|
|
|
FILE *fp;
|
2001-05-27 20:26:22 +00:00
|
|
|
struct message *mp;
|
1994-05-27 12:33:43 +00:00
|
|
|
off_t size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with each message to be edited . . .
|
|
|
|
*/
|
|
|
|
for (i = 0; msgvec[i] && i < msgCount; i++) {
|
|
|
|
sig_t sigint;
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
char buf[100];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
printf("Edit message %d [ynq]? ", msgvec[i]);
|
2001-05-27 20:26:22 +00:00
|
|
|
if (fgets(buf, sizeof(buf), stdin) == 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
for (p = buf; *p == ' ' || *p == '\t'; p++)
|
|
|
|
;
|
|
|
|
if (*p == 'q')
|
|
|
|
break;
|
|
|
|
if (*p == 'n')
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dot = mp = &message[msgvec[i] - 1];
|
|
|
|
touch(mp);
|
|
|
|
sigint = signal(SIGINT, SIG_IGN);
|
|
|
|
fp = run_editor(setinput(mp), mp->m_size, type, readonly);
|
|
|
|
if (fp != NULL) {
|
2001-09-02 14:40:51 +00:00
|
|
|
(void)fseeko(otf, (off_t)0, SEEK_END);
|
|
|
|
size = ftello(otf);
|
1994-05-27 12:33:43 +00:00
|
|
|
mp->m_block = blockof(size);
|
2000-10-24 13:54:31 +00:00
|
|
|
mp->m_offset = boffsetof(size);
|
2001-12-19 21:50:22 +00:00
|
|
|
mp->m_size = (long)fsize(fp);
|
1994-05-27 12:33:43 +00:00
|
|
|
mp->m_lines = 0;
|
|
|
|
mp->m_flag |= MODIFY;
|
|
|
|
rewind(fp);
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
if (c == '\n')
|
|
|
|
mp->m_lines++;
|
|
|
|
if (putc(c, otf) == EOF)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ferror(otf))
|
2001-03-25 04:57:05 +00:00
|
|
|
warnx("/tmp");
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(fp);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, sigint);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run an editor on the file at "fpp" of "size" bytes,
|
|
|
|
* and return a new file pointer.
|
|
|
|
* Signals must be handled by the caller.
|
|
|
|
* "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI.
|
|
|
|
*/
|
|
|
|
FILE *
|
|
|
|
run_editor(fp, size, type, readonly)
|
2001-05-27 20:26:22 +00:00
|
|
|
FILE *fp;
|
1994-05-27 12:33:43 +00:00
|
|
|
off_t size;
|
|
|
|
int type, readonly;
|
|
|
|
{
|
2001-05-27 20:26:22 +00:00
|
|
|
FILE *nf = NULL;
|
|
|
|
int t;
|
1994-05-27 12:33:43 +00:00
|
|
|
time_t modtime;
|
2001-03-25 04:57:05 +00:00
|
|
|
char *edit, tempname[PATHSIZE];
|
1994-05-27 12:33:43 +00:00
|
|
|
struct stat statb;
|
|
|
|
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)snprintf(tempname, sizeof(tempname),
|
|
|
|
"%s/mail.ReXXXXXXXXXX", tmpdir);
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((t = mkstemp(tempname)) == -1 ||
|
|
|
|
(nf = Fdopen(t, "w")) == NULL) {
|
|
|
|
warn("%s", tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2001-03-25 04:57:05 +00:00
|
|
|
if (readonly && fchmod(t, 0400) == -1) {
|
|
|
|
warn("%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (size >= 0)
|
|
|
|
while (--size >= 0 && (t = getc(fp)) != EOF)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putc(t, nf);
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
|
|
|
while ((t = getc(fp)) != EOF)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)putc(t, nf);
|
|
|
|
(void)fflush(nf);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (fstat(fileno(nf), &statb) < 0)
|
|
|
|
modtime = 0;
|
|
|
|
else
|
|
|
|
modtime = statb.st_mtime;
|
|
|
|
if (ferror(nf)) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)Fclose(nf);
|
2001-03-25 04:57:05 +00:00
|
|
|
warnx("%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
nf = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (Fclose(nf) < 0) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
nf = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
nf = NULL;
|
2001-05-27 20:26:22 +00:00
|
|
|
if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
|
1994-05-27 12:33:43 +00:00
|
|
|
edit = type == 'e' ? _PATH_EX : _PATH_VI;
|
2001-05-27 20:26:22 +00:00
|
|
|
if (run_command(edit, 0, -1, -1, tempname, NULL, NULL) < 0) {
|
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If in read only mode or file unchanged, just remove the editor
|
|
|
|
* temporary and return.
|
|
|
|
*/
|
|
|
|
if (readonly) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2001-03-25 04:57:05 +00:00
|
|
|
if (stat(tempname, &statb) < 0) {
|
|
|
|
warn("%s", tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (modtime == statb.st_mtime) {
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now switch to new file.
|
|
|
|
*/
|
2001-03-25 04:57:05 +00:00
|
|
|
if ((nf = Fopen(tempname, "a+")) == NULL) {
|
|
|
|
warn("%s", tempname);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)rm(tempname);
|
1994-05-27 12:33:43 +00:00
|
|
|
out:
|
2001-05-27 20:26:22 +00:00
|
|
|
return (nf);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|