-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)edit.c 5.14 (Berkeley) %G%";
+#endif /* not lint */
#include "rcv.h"
-#include <stdio.h>
#include <sys/stat.h>
/*
* Perform message editing functions.
*/
-static char *SccsId = "@(#)edit.c 2.2 %G%";
-
/*
* Edit a message list.
*/
editor(msgvec)
int *msgvec;
{
- char *edname;
- if ((edname = value("EDITOR")) == NOSTR)
- edname = EDITOR;
- return(edit1(msgvec, edname));
+ return edit1(msgvec, 'e');
}
/*
visual(msgvec)
int *msgvec;
{
- char *edname;
- if ((edname = value("VISUAL")) == NOSTR)
- edname = VISUAL;
- return(edit1(msgvec, edname));
+ return edit1(msgvec, 'v');
}
/*
* (which should not exist) and forking an editor on it.
* We get the editor from the stuff above.
*/
-
-edit1(msgvec, ed)
+edit1(msgvec, type)
int *msgvec;
- char *ed;
+ char type;
{
- register char *cp, *cp2;
register int c;
- int *ip, pid, mesg, lines;
- unsigned int ms;
- int (*sigint)(), (*sigquit)();
- FILE *ibuf, *obuf;
- char edname[15], nbuf[10];
- struct message *mp;
- extern char tempEdit[];
- off_t fsize(), size;
- struct stat statb;
- long modtime;
-
- /*
- * Set signals; locate editor.
- */
-
- sigint = sigset(SIGINT, SIG_IGN);
- sigquit = sigset(SIGQUIT, SIG_IGN);
+ int i;
+ FILE *fp;
+ register struct message *mp;
+ off_t size;
/*
* Deal with each message to be edited . . .
*/
+ for (i = 0; msgvec[i] && i < msgCount; i++) {
+ sig_t sigint;
- for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
- mesg = *ip;
- mp = &message[mesg-1];
- mp->m_flag |= MODIFY;
-
- /*
- * Make up a name for the edit file of the
- * form "Message%d" and make sure it doesn't
- * already exist.
- */
+ if (i > 0) {
+ char buf[100];
+ char *p;
- cp = &nbuf[10];
- *--cp = 0;
- while (mesg) {
- *--cp = mesg % 10 + '0';
- mesg /= 10;
- }
- cp2 = copy("Message", edname);
- while (*cp2++ = *cp++)
- ;
- if (!access(edname, 2)) {
- printf("%s: file exists\n", edname);
- goto out;
- }
-
- /*
- * Copy the message into the edit file.
- */
-
- close(creat(edname, 0600));
- if ((obuf = fopen(edname, "w")) == NULL) {
- perror(edname);
- goto out;
- }
- if (send(mp, obuf) < 0) {
- perror(edname);
- fclose(obuf);
- remove(edname);
- goto out;
+ printf("Edit message %d [ynq]? ", msgvec[i]);
+ if (fgets(buf, sizeof buf, stdin) == 0)
+ break;
+ for (p = buf; *p == ' ' || *p == '\t'; p++)
+ ;
+ if (*p == 'q')
+ break;
+ if (*p == 'n')
+ continue;
}
- fflush(obuf);
- if (ferror(obuf)) {
- remove(edname);
- fclose(obuf);
- goto out;
+ 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) {
+ (void) fseek(otf, (long) 0, 2);
+ size = ftell(otf);
+ mp->m_block = blockof(size);
+ mp->m_offset = offsetof(size);
+ mp->m_size = fsize(fp);
+ 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))
+ perror("/tmp");
+ (void) fclose(fp);
}
- fclose(obuf);
-
- /*
- * If we are in read only mode, make the
- * temporary message file readonly as well.
- */
-
- if (readonly)
- chmod(edname, 0400);
+ (void) signal(SIGINT, sigint);
+ }
+ return 0;
+}
- /*
- * Fork/execl the editor on the edit file.
- */
+/*
+ * 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)
+ register FILE *fp;
+ off_t size;
+ char type;
+{
+ register FILE *nf = NULL;
+ register int t;
+ time_t modtime;
+ char *edit;
+ struct stat statb;
+ extern char tempEdit[];
- if (stat(edname, &statb) < 0)
- modtime = 0;
+ if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) {
+ perror(tempEdit);
+ goto out;
+ }
+ if ((nf = fdopen(t, "w")) == NULL) {
+ perror(tempEdit);
+ (void) unlink(tempEdit);
+ 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 (fstat(fileno(nf), &statb) < 0)
+ modtime = 0;
+ else
modtime = statb.st_mtime;
- pid = vfork();
- if (pid == -1) {
- perror("fork");
- remove(edname);
- goto out;
- }
- if (pid == 0) {
- sigchild();
- if (sigint != SIG_IGN)
- sigsys(SIGINT, SIG_DFL);
- if (sigquit != SIG_IGN)
- sigsys(SIGQUIT, SIG_DFL);
- execl(ed, ed, edname, 0);
- perror(ed);
- _exit(1);
- }
- while (wait(&mesg) != pid)
- ;
-
- /*
- * If in read only mode, just remove the editor
- * temporary and return.
- */
-
- if (readonly) {
- remove(edname);
- continue;
- }
-
- /*
- * Now copy the message to the end of the
- * temp file.
- */
-
- if (stat(edname, &statb) < 0) {
- perror(edname);
- goto out;
- }
- if (modtime == statb.st_mtime) {
- remove(edname);
- goto out;
- }
- if ((ibuf = fopen(edname, "r")) == NULL) {
- perror(edname);
- remove(edname);
- goto out;
- }
- remove(edname);
- fseek(otf, (long) 0, 2);
- size = fsize(otf);
- mp->m_block = blockof(size);
- mp->m_offset = offsetof(size);
- ms = 0;
- lines = 0;
- while ((c = getc(ibuf)) != EOF) {
- if (c == '\n')
- lines++;
- putc(c, otf);
- if (ferror(otf))
- break;
- ms++;
- }
- mp->m_size = ms;
- mp->m_lines = lines;
- if (ferror(otf))
- perror("/tmp");
- fclose(ibuf);
+ if (ferror(nf) || fclose(nf) < 0) {
+ perror(tempEdit);
+ (void) unlink(tempEdit);
+ nf = NULL;
+ goto out;
+ }
+ nf = NULL;
+ if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
+ edit = type == 'e' ? _PATH_EX : _PATH_VI;
+ if (run_command(edit, 0, -1, -1, tempEdit, NOSTR) < 0) {
+ (void) unlink(tempEdit);
+ goto out;
}
-
/*
- * Restore signals and return.
+ * If in read only mode or file unchanged, just remove the editor
+ * temporary and return.
*/
-
+ if (readonly) {
+ (void) unlink(tempEdit);
+ goto out;
+ }
+ if (stat(tempEdit, &statb) < 0) {
+ perror(tempEdit);
+ goto out;
+ }
+ if (modtime == statb.st_mtime) {
+ (void) unlink(tempEdit);
+ goto out;
+ }
+ /*
+ * Now switch to new file.
+ */
+ if ((nf = fopen(tempEdit, "a+")) == NULL) {
+ perror(tempEdit);
+ (void) unlink(tempEdit);
+ goto out;
+ }
+ (void) unlink(tempEdit);
out:
- sigset(SIGINT, sigint);
- sigset(SIGQUIT, sigquit);
+ return nf;
}