[BACK]Return to edit.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/edit.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: edit.c,v 1.11 2001/11/21 15:26:39 millert Exp $       */
1.2       deraadt     2: /*     $NetBSD: edit.c,v 1.5 1996/06/08 19:48:20 christos Exp $        */
                      3:
1.1       deraadt     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.
1.12    ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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
1.2       deraadt    34: #if 0
1.11      millert    35: static const char sccsid[] = "@(#)edit.c       8.1 (Berkeley) 6/6/93";
1.2       deraadt    36: #else
1.12    ! millert    37: static const char rcsid[] = "$OpenBSD: edit.c,v 1.11 2001/11/21 15:26:39 millert Exp $";
1.2       deraadt    38: #endif
1.1       deraadt    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
1.11      millert    55: editor(void *v)
1.1       deraadt    56: {
1.2       deraadt    57:        int *msgvec = v;
1.1       deraadt    58:
1.3       millert    59:        return(edit1(msgvec, 'e'));
1.1       deraadt    60: }
                     61:
                     62: /*
                     63:  * Invoke the visual editor on a message list.
                     64:  */
                     65: int
1.11      millert    66: visual(void *v)
1.1       deraadt    67: {
1.2       deraadt    68:        int *msgvec = v;
1.1       deraadt    69:
1.3       millert    70:        return(edit1(msgvec, 'v'));
1.1       deraadt    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
1.11      millert    79: edit1(int *msgvec, int type)
1.1       deraadt    80: {
1.7       millert    81:        int c, i;
1.1       deraadt    82:        FILE *fp;
1.10      millert    83:        struct sigaction oact;
                     84:        sigset_t oset;
1.7       millert    85:        struct message *mp;
1.1       deraadt    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]);
1.3       millert    97:                        if (fgets(buf, sizeof(buf), stdin) == 0)
1.1       deraadt    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);
1.10      millert   108:                (void)ignoresig(SIGINT, &oact, &oset);
1.1       deraadt   109:                fp = run_editor(setinput(mp), mp->m_size, type, readonly);
                    110:                if (fp != NULL) {
1.4       millert   111:                        (void)fseek(otf, 0L, 2);
1.1       deraadt   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))
1.3       millert   126:                                warn("/tmp");
                    127:                        (void)Fclose(fp);
1.1       deraadt   128:                }
1.10      millert   129:                (void)sigprocmask(SIG_SETMASK, &oset, NULL);
                    130:                (void)sigaction(SIGINT, &oact, NULL);
1.1       deraadt   131:        }
1.3       millert   132:        return(0);
1.1       deraadt   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 *
1.11      millert   142: run_editor(FILE *fp, off_t size, int type, int readonly)
1.1       deraadt   143: {
1.7       millert   144:        FILE *nf = NULL;
                    145:        int t;
1.1       deraadt   146:        time_t modtime;
1.6       millert   147:        char *edit, tempname[PATHSIZE];
1.1       deraadt   148:        struct stat statb;
                    149:
1.6       millert   150:        (void)snprintf(tempname, sizeof(tempname),
                    151:            "%s/mail.ReXXXXXXXXXX", tmpdir);
                    152:        if ((t = mkstemp(tempname)) == -1 ||
                    153:            (nf = Fdopen(t, "w")) == NULL) {
1.8       millert   154:                warn("%s", tempname);
1.1       deraadt   155:                goto out;
                    156:        }
1.6       millert   157:        if (readonly && fchmod(t, 0400) == -1) {
1.8       millert   158:                warn("%s", tempname);
1.6       millert   159:                (void)rm(tempname);
1.1       deraadt   160:                goto out;
                    161:        }
                    162:        if (size >= 0)
                    163:                while (--size >= 0 && (t = getc(fp)) != EOF)
1.4       millert   164:                        (void)putc(t, nf);
1.1       deraadt   165:        else
                    166:                while ((t = getc(fp)) != EOF)
1.4       millert   167:                        (void)putc(t, nf);
                    168:        (void)fflush(nf);
1.1       deraadt   169:        if (fstat(fileno(nf), &statb) < 0)
                    170:                modtime = 0;
                    171:        else
                    172:                modtime = statb.st_mtime;
                    173:        if (ferror(nf)) {
1.4       millert   174:                (void)Fclose(nf);
1.8       millert   175:                warn("%s", tempname);
1.6       millert   176:                (void)rm(tempname);
1.1       deraadt   177:                nf = NULL;
                    178:                goto out;
                    179:        }
                    180:        if (Fclose(nf) < 0) {
1.8       millert   181:                warn("%s", tempname);
1.6       millert   182:                (void)rm(tempname);
1.1       deraadt   183:                nf = NULL;
                    184:                goto out;
                    185:        }
                    186:        nf = NULL;
1.5       millert   187:        if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
1.1       deraadt   188:                edit = type == 'e' ? _PATH_EX : _PATH_VI;
1.9       millert   189:        if (run_command(edit, 0, 0, -1, tempname, NULL, NULL) < 0) {
1.6       millert   190:                (void)rm(tempname);
1.1       deraadt   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) {
1.6       millert   198:                (void)rm(tempname);
1.1       deraadt   199:                goto out;
                    200:        }
1.6       millert   201:        if (stat(tempname, &statb) < 0) {
1.8       millert   202:                warn("%s", tempname);
1.1       deraadt   203:                goto out;
                    204:        }
                    205:        if (modtime == statb.st_mtime) {
1.6       millert   206:                (void)rm(tempname);
1.1       deraadt   207:                goto out;
                    208:        }
                    209:        /*
                    210:         * Now switch to new file.
                    211:         */
1.6       millert   212:        if ((nf = Fopen(tempname, "a+")) == NULL) {
1.8       millert   213:                warn("%s", tempname);
1.6       millert   214:                (void)rm(tempname);
1.1       deraadt   215:                goto out;
                    216:        }
1.6       millert   217:        (void)rm(tempname);
1.1       deraadt   218: out:
1.3       millert   219:        return(nf);
1.1       deraadt   220: }