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