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