17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate * The Regents of the University of California
347c478bd9Sstevel@tonic-gate * All Rights Reserved
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate * contributors.
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "rcv.h"
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
477c478bd9Sstevel@tonic-gate * mail program
487c478bd9Sstevel@tonic-gate *
497c478bd9Sstevel@tonic-gate * Perform message editing functions.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static void edit1(int *msgvec, char *ed);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * Edit a message list.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate
58*2a8bcb4eSToomas Soome int
editor(int * msgvec)597c478bd9Sstevel@tonic-gate editor(int *msgvec)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate char *edname;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if ((edname = value("EDITOR")) == NOSTR || *edname == '\0')
647c478bd9Sstevel@tonic-gate edname = EDITOR;
657c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
667c478bd9Sstevel@tonic-gate return(0);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * Invoke the visual editor on a message list.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate
73*2a8bcb4eSToomas Soome int
visual(int * msgvec)747c478bd9Sstevel@tonic-gate visual(int *msgvec)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate char *edname;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate if ((edname = value("VISUAL")) == NOSTR || *edname == '\0')
797c478bd9Sstevel@tonic-gate edname = VISUAL;
807c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
817c478bd9Sstevel@tonic-gate return(0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Edit a message by writing the message into a funnily-named file
867c478bd9Sstevel@tonic-gate * (which should not exist) and forking an editor on it.
877c478bd9Sstevel@tonic-gate * We get the editor from the stuff above.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate
90*2a8bcb4eSToomas Soome static void
edit1(int * msgvec,char * ed)917c478bd9Sstevel@tonic-gate edit1(int *msgvec, char *ed)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate register int c, lastc = '\n';
947c478bd9Sstevel@tonic-gate pid_t pid;
957c478bd9Sstevel@tonic-gate int *ip, mesg, blank = 1;
967c478bd9Sstevel@tonic-gate long ms, lines;
977c478bd9Sstevel@tonic-gate void (*sigint)(int), (*sigquit)(int);
987c478bd9Sstevel@tonic-gate FILE *ibuf, *obuf;
997c478bd9Sstevel@tonic-gate struct message *mp;
1007c478bd9Sstevel@tonic-gate off_t size;
1017c478bd9Sstevel@tonic-gate struct stat statb;
1027c478bd9Sstevel@tonic-gate long modtime;
1037c478bd9Sstevel@tonic-gate int fd = -1;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * Set signals; locate editor.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate sigint = sigset(SIGINT, SIG_IGN);
110*2a8bcb4eSToomas Soome sigquit = sigset(SIGQUIT, SIG_IGN);
1117c478bd9Sstevel@tonic-gate ed = safeexpand(ed);
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * Deal with each message to be edited . . .
1157c478bd9Sstevel@tonic-gate */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
1187c478bd9Sstevel@tonic-gate mesg = *ip;
1197c478bd9Sstevel@tonic-gate touch(mesg);
1207c478bd9Sstevel@tonic-gate mp = &message[mesg-1];
1217c478bd9Sstevel@tonic-gate dot = mp;
1227c478bd9Sstevel@tonic-gate if (mp->m_text) {
1237c478bd9Sstevel@tonic-gate if (!access(tempZedit, 2)) {
1247c478bd9Sstevel@tonic-gate printf(gettext("%s: file exists\n"), tempZedit);
1257c478bd9Sstevel@tonic-gate goto out;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * Copy the message into the edit file.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate if ((fd = open(tempZedit, O_RDWR|O_CREAT|
1337c478bd9Sstevel@tonic-gate O_EXCL, 0600)) < 0 ||
1347c478bd9Sstevel@tonic-gate (obuf = fdopen(fd, "w")) == NULL) {
1357c478bd9Sstevel@tonic-gate perror(tempZedit);
1367c478bd9Sstevel@tonic-gate goto out;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate if (msend(mp, obuf, 0, fputs) < 0) {
1397c478bd9Sstevel@tonic-gate perror(tempZedit);
1407c478bd9Sstevel@tonic-gate fclose(obuf);
1417c478bd9Sstevel@tonic-gate removefile(tempZedit);
1427c478bd9Sstevel@tonic-gate goto out;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate fflush(obuf);
1457c478bd9Sstevel@tonic-gate if (fferror(obuf)) {
1467c478bd9Sstevel@tonic-gate perror(tempZedit);
1477c478bd9Sstevel@tonic-gate fclose(obuf);
1487c478bd9Sstevel@tonic-gate removefile(tempZedit);
1497c478bd9Sstevel@tonic-gate goto out;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate fclose(obuf);
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate * If we are in read only mode, make the
1557c478bd9Sstevel@tonic-gate * temporary message file readonly as well.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if (readonly)
1597c478bd9Sstevel@tonic-gate chmod(tempZedit, 0400);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * Fork/execl the editor on the edit file.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0)
1667c478bd9Sstevel@tonic-gate modtime = 0;
1677c478bd9Sstevel@tonic-gate else
1687c478bd9Sstevel@tonic-gate modtime = statb.st_mtime;
1697c478bd9Sstevel@tonic-gate pid = vfork();
1707c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) {
1717c478bd9Sstevel@tonic-gate perror("fork");
1727c478bd9Sstevel@tonic-gate removefile(tempZedit);
1737c478bd9Sstevel@tonic-gate goto out;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate if (pid == 0) {
1767c478bd9Sstevel@tonic-gate sigchild();
1777c478bd9Sstevel@tonic-gate if (sigint != SIG_IGN)
1787c478bd9Sstevel@tonic-gate sigset(SIGINT, SIG_DFL);
1797c478bd9Sstevel@tonic-gate if (sigquit != SIG_IGN)
1807c478bd9Sstevel@tonic-gate sigset(SIGQUIT, SIG_DFL);
1817c478bd9Sstevel@tonic-gate execlp(ed, ed, tempZedit, (char *)0);
1827c478bd9Sstevel@tonic-gate perror(ed);
1837c478bd9Sstevel@tonic-gate _exit(1);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate while (wait(&mesg) != pid)
1867c478bd9Sstevel@tonic-gate ;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * If in read only mode, just remove the editor
1907c478bd9Sstevel@tonic-gate * temporary and return.
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate if (readonly) {
1947c478bd9Sstevel@tonic-gate removefile(tempZedit);
1957c478bd9Sstevel@tonic-gate continue;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * Now copy the message to the end of the
2007c478bd9Sstevel@tonic-gate * temp file.
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0) {
2047c478bd9Sstevel@tonic-gate perror(tempZedit);
2057c478bd9Sstevel@tonic-gate continue;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate if (modtime == statb.st_mtime) {
2087c478bd9Sstevel@tonic-gate removefile(tempZedit);
2097c478bd9Sstevel@tonic-gate continue;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate if ((ibuf = fopen(tempZedit, "r")) == NULL) {
2127c478bd9Sstevel@tonic-gate perror(tempZedit);
2137c478bd9Sstevel@tonic-gate removefile(tempZedit);
2147c478bd9Sstevel@tonic-gate continue;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate removefile(tempZedit);
2177c478bd9Sstevel@tonic-gate fseek(otf, (long) 0, 2);
2187c478bd9Sstevel@tonic-gate size = fsize(otf);
2197c478bd9Sstevel@tonic-gate mp->m_flag |= MODIFY;
2207c478bd9Sstevel@tonic-gate mp->m_offset = size;
2217c478bd9Sstevel@tonic-gate ms = 0L;
2227c478bd9Sstevel@tonic-gate lines = 0;
2237c478bd9Sstevel@tonic-gate while ((c = getc(ibuf)) != EOF) {
2247c478bd9Sstevel@tonic-gate if (c == '\n') {
2257c478bd9Sstevel@tonic-gate lines++;
2267c478bd9Sstevel@tonic-gate blank = lastc == '\n';
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate lastc = c;
2297c478bd9Sstevel@tonic-gate putc(c, otf);
2307c478bd9Sstevel@tonic-gate if (ferror(otf))
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate ms++;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate if (!blank) {
2357c478bd9Sstevel@tonic-gate putc('\n', otf);
2367c478bd9Sstevel@tonic-gate ms++;
2377c478bd9Sstevel@tonic-gate lines++;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate mp->m_size = ms;
2407c478bd9Sstevel@tonic-gate mp->m_lines = lines;
2417c478bd9Sstevel@tonic-gate fflush(otf);
2427c478bd9Sstevel@tonic-gate if (fferror(otf))
2437c478bd9Sstevel@tonic-gate perror("/tmp");
2447c478bd9Sstevel@tonic-gate fclose(ibuf);
2457c478bd9Sstevel@tonic-gate setclen(mp);
2467c478bd9Sstevel@tonic-gate } else {
2477c478bd9Sstevel@tonic-gate printf("\n%s\n", gettext(
2487c478bd9Sstevel@tonic-gate "*** Message content is not printable: pipe to command or save to a file ***"));
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Restore signals and return.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate out:
2577c478bd9Sstevel@tonic-gate sigset(SIGINT, sigint);
2587c478bd9Sstevel@tonic-gate sigset(SIGQUIT, sigquit);
2597c478bd9Sstevel@tonic-gate }
260