Annotation of src/usr.bin/mail/edit.c, Revision 1.1.1.1
1.1 deraadt 1: /*
2: * Copyright (c) 1980, 1993
3: * The Regents of the University of California. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: */
33:
34: #ifndef lint
35: static char sccsid[] = "from: @(#)edit.c 8.1 (Berkeley) 6/6/93";
36: static char rcsid[] = "$Id: edit.c,v 1.4 1994/11/28 20:03:32 jtc Exp $";
37: #endif /* not lint */
38:
39: #include "rcv.h"
40: #include <fcntl.h>
41: #include "extern.h"
42:
43: /*
44: * Mail -- a mail program
45: *
46: * Perform message editing functions.
47: */
48:
49: /*
50: * Edit a message list.
51: */
52: int
53: editor(msgvec)
54: int *msgvec;
55: {
56:
57: return edit1(msgvec, 'e');
58: }
59:
60: /*
61: * Invoke the visual editor on a message list.
62: */
63: int
64: visual(msgvec)
65: int *msgvec;
66: {
67:
68: return edit1(msgvec, 'v');
69: }
70:
71: /*
72: * Edit a message by writing the message into a funnily-named file
73: * (which should not exist) and forking an editor on it.
74: * We get the editor from the stuff above.
75: */
76: int
77: edit1(msgvec, type)
78: int *msgvec;
79: int type;
80: {
81: register int c;
82: int i;
83: FILE *fp;
84: register struct message *mp;
85: off_t size;
86:
87: /*
88: * Deal with each message to be edited . . .
89: */
90: for (i = 0; msgvec[i] && i < msgCount; i++) {
91: sig_t sigint;
92:
93: if (i > 0) {
94: char buf[100];
95: char *p;
96:
97: printf("Edit message %d [ynq]? ", msgvec[i]);
98: if (fgets(buf, sizeof buf, stdin) == 0)
99: break;
100: for (p = buf; *p == ' ' || *p == '\t'; p++)
101: ;
102: if (*p == 'q')
103: break;
104: if (*p == 'n')
105: continue;
106: }
107: dot = mp = &message[msgvec[i] - 1];
108: touch(mp);
109: sigint = signal(SIGINT, SIG_IGN);
110: fp = run_editor(setinput(mp), mp->m_size, type, readonly);
111: if (fp != NULL) {
112: (void) fseek(otf, 0L, 2);
113: size = ftell(otf);
114: mp->m_block = blockof(size);
115: mp->m_offset = offsetof(size);
116: mp->m_size = fsize(fp);
117: mp->m_lines = 0;
118: mp->m_flag |= MODIFY;
119: rewind(fp);
120: while ((c = getc(fp)) != EOF) {
121: if (c == '\n')
122: mp->m_lines++;
123: if (putc(c, otf) == EOF)
124: break;
125: }
126: if (ferror(otf))
127: perror("/tmp");
128: (void) Fclose(fp);
129: }
130: (void) signal(SIGINT, sigint);
131: }
132: return 0;
133: }
134:
135: /*
136: * Run an editor on the file at "fpp" of "size" bytes,
137: * and return a new file pointer.
138: * Signals must be handled by the caller.
139: * "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI.
140: */
141: FILE *
142: run_editor(fp, size, type, readonly)
143: register FILE *fp;
144: off_t size;
145: int type, readonly;
146: {
147: register FILE *nf = NULL;
148: register int t;
149: time_t modtime;
150: char *edit;
151: struct stat statb;
152: extern char *tempEdit;
153:
154: if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) {
155: perror(tempEdit);
156: goto out;
157: }
158: if ((nf = Fdopen(t, "w")) == NULL) {
159: perror(tempEdit);
160: (void) unlink(tempEdit);
161: goto out;
162: }
163: if (size >= 0)
164: while (--size >= 0 && (t = getc(fp)) != EOF)
165: (void) putc(t, nf);
166: else
167: while ((t = getc(fp)) != EOF)
168: (void) putc(t, nf);
169: (void) fflush(nf);
170: if (fstat(fileno(nf), &statb) < 0)
171: modtime = 0;
172: else
173: modtime = statb.st_mtime;
174: if (ferror(nf)) {
175: (void) Fclose(nf);
176: perror(tempEdit);
177: (void) unlink(tempEdit);
178: nf = NULL;
179: goto out;
180: }
181: if (Fclose(nf) < 0) {
182: perror(tempEdit);
183: (void) unlink(tempEdit);
184: nf = NULL;
185: goto out;
186: }
187: nf = NULL;
188: if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
189: edit = type == 'e' ? _PATH_EX : _PATH_VI;
190: if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) {
191: (void) unlink(tempEdit);
192: goto out;
193: }
194: /*
195: * If in read only mode or file unchanged, just remove the editor
196: * temporary and return.
197: */
198: if (readonly) {
199: (void) unlink(tempEdit);
200: goto out;
201: }
202: if (stat(tempEdit, &statb) < 0) {
203: perror(tempEdit);
204: goto out;
205: }
206: if (modtime == statb.st_mtime) {
207: (void) unlink(tempEdit);
208: goto out;
209: }
210: /*
211: * Now switch to new file.
212: */
213: if ((nf = Fopen(tempEdit, "a+")) == NULL) {
214: perror(tempEdit);
215: (void) unlink(tempEdit);
216: goto out;
217: }
218: (void) unlink(tempEdit);
219: out:
220: return nf;
221: }