1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
33*7c478bd9Sstevel@tonic-gate * The Regents of the University of California
34*7c478bd9Sstevel@tonic-gate * All Rights Reserved
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
37*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
38*7c478bd9Sstevel@tonic-gate * contributors.
39*7c478bd9Sstevel@tonic-gate */
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate #include "rcv.h"
44*7c478bd9Sstevel@tonic-gate #include <locale.h>
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
49*7c478bd9Sstevel@tonic-gate * mail program
50*7c478bd9Sstevel@tonic-gate *
51*7c478bd9Sstevel@tonic-gate * Perform message editing functions.
52*7c478bd9Sstevel@tonic-gate */
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate static void edit1(int *msgvec, char *ed);
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate * Edit a message list.
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate int
editor(int * msgvec)61*7c478bd9Sstevel@tonic-gate editor(int *msgvec)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate char *edname;
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate if ((edname = value("EDITOR")) == NOSTR || *edname == '\0')
66*7c478bd9Sstevel@tonic-gate edname = EDITOR;
67*7c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
68*7c478bd9Sstevel@tonic-gate return(0);
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate * Invoke the visual editor on a message list.
73*7c478bd9Sstevel@tonic-gate */
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate int
visual(int * msgvec)76*7c478bd9Sstevel@tonic-gate visual(int *msgvec)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate char *edname;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate if ((edname = value("VISUAL")) == NOSTR || *edname == '\0')
81*7c478bd9Sstevel@tonic-gate edname = VISUAL;
82*7c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
83*7c478bd9Sstevel@tonic-gate return(0);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate * Edit a message by writing the message into a funnily-named file
88*7c478bd9Sstevel@tonic-gate * (which should not exist) and forking an editor on it.
89*7c478bd9Sstevel@tonic-gate * We get the editor from the stuff above.
90*7c478bd9Sstevel@tonic-gate */
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate static void
edit1(int * msgvec,char * ed)93*7c478bd9Sstevel@tonic-gate edit1(int *msgvec, char *ed)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate register int c, lastc = '\n';
96*7c478bd9Sstevel@tonic-gate pid_t pid;
97*7c478bd9Sstevel@tonic-gate int *ip, mesg, blank = 1;
98*7c478bd9Sstevel@tonic-gate long ms, lines;
99*7c478bd9Sstevel@tonic-gate void (*sigint)(int), (*sigquit)(int);
100*7c478bd9Sstevel@tonic-gate FILE *ibuf, *obuf;
101*7c478bd9Sstevel@tonic-gate struct message *mp;
102*7c478bd9Sstevel@tonic-gate off_t size;
103*7c478bd9Sstevel@tonic-gate struct stat statb;
104*7c478bd9Sstevel@tonic-gate long modtime;
105*7c478bd9Sstevel@tonic-gate int fd = -1;
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate * Set signals; locate editor.
109*7c478bd9Sstevel@tonic-gate */
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate sigint = sigset(SIGINT, SIG_IGN);
112*7c478bd9Sstevel@tonic-gate sigquit = sigset(SIGQUIT, SIG_IGN);
113*7c478bd9Sstevel@tonic-gate ed = safeexpand(ed);
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate /*
116*7c478bd9Sstevel@tonic-gate * Deal with each message to be edited . . .
117*7c478bd9Sstevel@tonic-gate */
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
120*7c478bd9Sstevel@tonic-gate mesg = *ip;
121*7c478bd9Sstevel@tonic-gate touch(mesg);
122*7c478bd9Sstevel@tonic-gate mp = &message[mesg-1];
123*7c478bd9Sstevel@tonic-gate dot = mp;
124*7c478bd9Sstevel@tonic-gate if (mp->m_text) {
125*7c478bd9Sstevel@tonic-gate if (!access(tempZedit, 2)) {
126*7c478bd9Sstevel@tonic-gate printf(gettext("%s: file exists\n"), tempZedit);
127*7c478bd9Sstevel@tonic-gate goto out;
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate * Copy the message into the edit file.
132*7c478bd9Sstevel@tonic-gate */
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate if ((fd = open(tempZedit, O_RDWR|O_CREAT|
135*7c478bd9Sstevel@tonic-gate O_EXCL, 0600)) < 0 ||
136*7c478bd9Sstevel@tonic-gate (obuf = fdopen(fd, "w")) == NULL) {
137*7c478bd9Sstevel@tonic-gate perror(tempZedit);
138*7c478bd9Sstevel@tonic-gate goto out;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate if (msend(mp, obuf, 0, fputs) < 0) {
141*7c478bd9Sstevel@tonic-gate perror(tempZedit);
142*7c478bd9Sstevel@tonic-gate fclose(obuf);
143*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
144*7c478bd9Sstevel@tonic-gate goto out;
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate fflush(obuf);
147*7c478bd9Sstevel@tonic-gate if (fferror(obuf)) {
148*7c478bd9Sstevel@tonic-gate perror(tempZedit);
149*7c478bd9Sstevel@tonic-gate fclose(obuf);
150*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
151*7c478bd9Sstevel@tonic-gate goto out;
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate fclose(obuf);
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate * If we are in read only mode, make the
157*7c478bd9Sstevel@tonic-gate * temporary message file readonly as well.
158*7c478bd9Sstevel@tonic-gate */
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate if (readonly)
161*7c478bd9Sstevel@tonic-gate chmod(tempZedit, 0400);
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate * Fork/execl the editor on the edit file.
165*7c478bd9Sstevel@tonic-gate */
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0)
168*7c478bd9Sstevel@tonic-gate modtime = 0;
169*7c478bd9Sstevel@tonic-gate else
170*7c478bd9Sstevel@tonic-gate modtime = statb.st_mtime;
171*7c478bd9Sstevel@tonic-gate pid = vfork();
172*7c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) {
173*7c478bd9Sstevel@tonic-gate perror("fork");
174*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
175*7c478bd9Sstevel@tonic-gate goto out;
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate if (pid == 0) {
178*7c478bd9Sstevel@tonic-gate sigchild();
179*7c478bd9Sstevel@tonic-gate if (sigint != SIG_IGN)
180*7c478bd9Sstevel@tonic-gate sigset(SIGINT, SIG_DFL);
181*7c478bd9Sstevel@tonic-gate if (sigquit != SIG_IGN)
182*7c478bd9Sstevel@tonic-gate sigset(SIGQUIT, SIG_DFL);
183*7c478bd9Sstevel@tonic-gate execlp(ed, ed, tempZedit, (char *)0);
184*7c478bd9Sstevel@tonic-gate perror(ed);
185*7c478bd9Sstevel@tonic-gate _exit(1);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate while (wait(&mesg) != pid)
188*7c478bd9Sstevel@tonic-gate ;
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate * If in read only mode, just remove the editor
192*7c478bd9Sstevel@tonic-gate * temporary and return.
193*7c478bd9Sstevel@tonic-gate */
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate if (readonly) {
196*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
197*7c478bd9Sstevel@tonic-gate continue;
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate /*
201*7c478bd9Sstevel@tonic-gate * Now copy the message to the end of the
202*7c478bd9Sstevel@tonic-gate * temp file.
203*7c478bd9Sstevel@tonic-gate */
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0) {
206*7c478bd9Sstevel@tonic-gate perror(tempZedit);
207*7c478bd9Sstevel@tonic-gate continue;
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate if (modtime == statb.st_mtime) {
210*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
211*7c478bd9Sstevel@tonic-gate continue;
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate if ((ibuf = fopen(tempZedit, "r")) == NULL) {
214*7c478bd9Sstevel@tonic-gate perror(tempZedit);
215*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
216*7c478bd9Sstevel@tonic-gate continue;
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
219*7c478bd9Sstevel@tonic-gate fseek(otf, (long) 0, 2);
220*7c478bd9Sstevel@tonic-gate size = fsize(otf);
221*7c478bd9Sstevel@tonic-gate mp->m_flag |= MODIFY;
222*7c478bd9Sstevel@tonic-gate mp->m_offset = size;
223*7c478bd9Sstevel@tonic-gate ms = 0L;
224*7c478bd9Sstevel@tonic-gate lines = 0;
225*7c478bd9Sstevel@tonic-gate while ((c = getc(ibuf)) != EOF) {
226*7c478bd9Sstevel@tonic-gate if (c == '\n') {
227*7c478bd9Sstevel@tonic-gate lines++;
228*7c478bd9Sstevel@tonic-gate blank = lastc == '\n';
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate lastc = c;
231*7c478bd9Sstevel@tonic-gate putc(c, otf);
232*7c478bd9Sstevel@tonic-gate if (ferror(otf))
233*7c478bd9Sstevel@tonic-gate break;
234*7c478bd9Sstevel@tonic-gate ms++;
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate if (!blank) {
237*7c478bd9Sstevel@tonic-gate putc('\n', otf);
238*7c478bd9Sstevel@tonic-gate ms++;
239*7c478bd9Sstevel@tonic-gate lines++;
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate mp->m_size = ms;
242*7c478bd9Sstevel@tonic-gate mp->m_lines = lines;
243*7c478bd9Sstevel@tonic-gate fflush(otf);
244*7c478bd9Sstevel@tonic-gate if (fferror(otf))
245*7c478bd9Sstevel@tonic-gate perror("/tmp");
246*7c478bd9Sstevel@tonic-gate fclose(ibuf);
247*7c478bd9Sstevel@tonic-gate setclen(mp);
248*7c478bd9Sstevel@tonic-gate } else {
249*7c478bd9Sstevel@tonic-gate printf("\n%s\n", gettext(
250*7c478bd9Sstevel@tonic-gate "*** Message content is not printable: pipe to command or save to a file ***"));
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate /*
255*7c478bd9Sstevel@tonic-gate * Restore signals and return.
256*7c478bd9Sstevel@tonic-gate */
257*7c478bd9Sstevel@tonic-gate
258*7c478bd9Sstevel@tonic-gate out:
259*7c478bd9Sstevel@tonic-gate sigset(SIGINT, sigint);
260*7c478bd9Sstevel@tonic-gate sigset(SIGQUIT, sigquit);
261*7c478bd9Sstevel@tonic-gate }
262