edit.c (67496) | edit.c (74769) |
---|---|
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 --- 16 unchanged lines hidden (view full) --- 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 * | 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 --- 16 unchanged lines hidden (view full) --- 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 * $FreeBSD: head/usr.bin/mail/edit.c 67496 2000-10-24 13:54:31Z phk $ | 33 * $FreeBSD: head/usr.bin/mail/edit.c 74769 2001-03-25 04:57:05Z mikeh $ |
34 */ 35 36#ifndef lint | 34 */ 35 36#ifndef lint |
37#if 0 |
|
37static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93"; | 38static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93"; |
39#endif 40static const char rcsid[] = 41 "$FreeBSD: head/usr.bin/mail/edit.c 74769 2001-03-25 04:57:05Z mikeh $"; |
|
38#endif /* not lint */ 39 40#include "rcv.h" 41#include <fcntl.h> 42#include "extern.h" 43 44/* 45 * Mail -- a mail program --- 74 unchanged lines hidden (view full) --- 120 rewind(fp); 121 while ((c = getc(fp)) != EOF) { 122 if (c == '\n') 123 mp->m_lines++; 124 if (putc(c, otf) == EOF) 125 break; 126 } 127 if (ferror(otf)) | 42#endif /* not lint */ 43 44#include "rcv.h" 45#include <fcntl.h> 46#include "extern.h" 47 48/* 49 * Mail -- a mail program --- 74 unchanged lines hidden (view full) --- 124 rewind(fp); 125 while ((c = getc(fp)) != EOF) { 126 if (c == '\n') 127 mp->m_lines++; 128 if (putc(c, otf) == EOF) 129 break; 130 } 131 if (ferror(otf)) |
128 perror("/tmp"); | 132 warnx("/tmp"); |
129 (void) Fclose(fp); 130 } 131 (void) signal(SIGINT, sigint); 132 } 133 return 0; 134} 135 136/* --- 6 unchanged lines hidden (view full) --- 143run_editor(fp, size, type, readonly) 144 register FILE *fp; 145 off_t size; 146 int type, readonly; 147{ 148 register FILE *nf = NULL; 149 register int t; 150 time_t modtime; | 133 (void) Fclose(fp); 134 } 135 (void) signal(SIGINT, sigint); 136 } 137 return 0; 138} 139 140/* --- 6 unchanged lines hidden (view full) --- 147run_editor(fp, size, type, readonly) 148 register FILE *fp; 149 off_t size; 150 int type, readonly; 151{ 152 register FILE *nf = NULL; 153 register int t; 154 time_t modtime; |
151 char *edit; | 155 char *edit, tempname[PATHSIZE]; |
152 struct stat statb; | 156 struct stat statb; |
153 extern char *tempEdit; | |
154 | 157 |
155 if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) { 156 perror(tempEdit); | 158 snprintf(tempname, sizeof(tempname), "%s/mail.ReXXXXXXXXXX", tmpdir); 159 if ((t = mkstemp(tempname)) == -1 || 160 (nf = Fdopen(t, "w")) == NULL) { 161 warn("%s", tempname); |
157 goto out; 158 } | 162 goto out; 163 } |
159 if ((nf = Fdopen(t, "w")) == NULL) { 160 perror(tempEdit); 161 (void) unlink(tempEdit); | 164 if (readonly && fchmod(t, 0400) == -1) { 165 warn("%s", tempname); 166 (void) rm(tempname); |
162 goto out; 163 } 164 if (size >= 0) 165 while (--size >= 0 && (t = getc(fp)) != EOF) 166 (void) putc(t, nf); 167 else 168 while ((t = getc(fp)) != EOF) 169 (void) putc(t, nf); 170 (void) fflush(nf); 171 if (fstat(fileno(nf), &statb) < 0) 172 modtime = 0; 173 else 174 modtime = statb.st_mtime; 175 if (ferror(nf)) { 176 (void) Fclose(nf); | 167 goto out; 168 } 169 if (size >= 0) 170 while (--size >= 0 && (t = getc(fp)) != EOF) 171 (void) putc(t, nf); 172 else 173 while ((t = getc(fp)) != EOF) 174 (void) putc(t, nf); 175 (void) fflush(nf); 176 if (fstat(fileno(nf), &statb) < 0) 177 modtime = 0; 178 else 179 modtime = statb.st_mtime; 180 if (ferror(nf)) { 181 (void) Fclose(nf); |
177 perror(tempEdit); 178 (void) unlink(tempEdit); | 182 warnx("%s", tempname); 183 (void) rm(tempname); |
179 nf = NULL; 180 goto out; 181 } 182 if (Fclose(nf) < 0) { | 184 nf = NULL; 185 goto out; 186 } 187 if (Fclose(nf) < 0) { |
183 perror(tempEdit); 184 (void) unlink(tempEdit); | 188 warn("%s", tempname); 189 (void) rm(tempname); |
185 nf = NULL; 186 goto out; 187 } 188 nf = NULL; 189 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR) 190 edit = type == 'e' ? _PATH_EX : _PATH_VI; | 190 nf = NULL; 191 goto out; 192 } 193 nf = NULL; 194 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR) 195 edit = type == 'e' ? _PATH_EX : _PATH_VI; |
191 if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) { 192 (void) unlink(tempEdit); | 196 if (run_command(edit, 0, -1, -1, tempname, NOSTR, NOSTR) < 0) { 197 (void) rm(tempname); |
193 goto out; 194 } 195 /* 196 * If in read only mode or file unchanged, just remove the editor 197 * temporary and return. 198 */ 199 if (readonly) { | 198 goto out; 199 } 200 /* 201 * If in read only mode or file unchanged, just remove the editor 202 * temporary and return. 203 */ 204 if (readonly) { |
200 (void) unlink(tempEdit); | 205 (void) rm(tempname); |
201 goto out; 202 } | 206 goto out; 207 } |
203 if (stat(tempEdit, &statb) < 0) { 204 perror(tempEdit); | 208 if (stat(tempname, &statb) < 0) { 209 warn("%s", tempname); |
205 goto out; 206 } 207 if (modtime == statb.st_mtime) { | 210 goto out; 211 } 212 if (modtime == statb.st_mtime) { |
208 (void) unlink(tempEdit); | 213 (void) rm(tempname); |
209 goto out; 210 } 211 /* 212 * Now switch to new file. 213 */ | 214 goto out; 215 } 216 /* 217 * Now switch to new file. 218 */ |
214 if ((nf = Fopen(tempEdit, "a+")) == NULL) { 215 perror(tempEdit); 216 (void) unlink(tempEdit); | 219 if ((nf = Fopen(tempname, "a+")) == NULL) { 220 warn("%s", tempname); 221 (void) rm(tempname); |
217 goto out; 218 } | 222 goto out; 223 } |
219 (void) unlink(tempEdit); | 224 (void) rm(tempname); |
220out: 221 return nf; 222} | 225out: 226 return nf; 227} |