Annotation of 3BSD/cmd/ucbmail/edit.c, revision 1.1.1.1

1.1       root        1: #
                      2: 
                      3: #include "rcv.h"
                      4: #include <stdio.h>
                      5: #include <sys/stat.h>
                      6: 
                      7: /*
                      8:  * Mail -- a mail program
                      9:  *
                     10:  * Perform message editing functions.
                     11:  */
                     12: 
                     13: /*
                     14:  * Edit a message list.
                     15:  */
                     16: 
                     17: editor(msgvec)
                     18:        int *msgvec;
                     19: {
                     20:        char *edname;
                     21: 
                     22:        if ((edname = value("EDITOR")) == NOSTR)
                     23:                edname = EDITOR;
                     24:        return(edit1(msgvec, edname));
                     25: }
                     26: 
                     27: /*
                     28:  * Invoke the visual editor on a message list.
                     29:  */
                     30: 
                     31: visual(msgvec)
                     32:        int *msgvec;
                     33: {
                     34:        char *edname;
                     35: 
                     36:        if ((edname = value("VISUAL")) == NOSTR)
                     37:                edname = VISUAL;
                     38:        return(edit1(msgvec, edname));
                     39: }
                     40: 
                     41: /*
                     42:  * Edit a message by writing the message into a funnily-named file
                     43:  * (which should not exist) and forking an editor on it.
                     44:  * We get the editor from the stuff above.
                     45:  */
                     46: 
                     47: edit1(msgvec, ed)
                     48:        int *msgvec;
                     49:        char *ed;
                     50: {
                     51:        register char *cp, *cp2;
                     52:        register int c;
                     53:        int *ip, pid, mesg, lines;
                     54:        unsigned int ms;
                     55:        int (*sigint)(), (*sigquit)();
                     56:        FILE *ibuf, *obuf;
                     57:        char edname[15], nbuf[10];
                     58:        struct message *mp;
                     59:        extern char tempEdit[];
                     60:        off_t fsize(), size;
                     61: 
                     62:        /*
                     63:         * Set signals; locate editor.
                     64:         */
                     65: 
                     66:        sigint = signal(SIGINT, SIG_IGN);
                     67:        sigquit = signal(SIGQUIT, SIG_IGN);
                     68: 
                     69:        /*
                     70:         * Deal with each message to be edited . . .
                     71:         */
                     72: 
                     73:        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
                     74:                mesg = *ip;
                     75:                mp = &message[mesg-1];
                     76:                mp->m_flag |= MODIFY;
                     77: 
                     78:                /*
                     79:                 * Make up a name for the edit file of the
                     80:                 * form "Message%d" and make sure it doesn't
                     81:                 * already exist.
                     82:                 */
                     83: 
                     84:                cp = &nbuf[10];
                     85:                *--cp = 0;
                     86:                while (mesg) {
                     87:                        *--cp = mesg % 10 + '0';
                     88:                        mesg /= 10;
                     89:                }
                     90:                cp2 = copy("Message", edname);
                     91:                while (*cp2++ = *cp++)
                     92:                        ;
                     93:                if (!access(edname, 2)) {
                     94:                        printf("%s: file exists\n", edname);
                     95:                        goto out;
                     96:                }
                     97: 
                     98:                /*
                     99:                 * Copy the message into the edit file.
                    100:                 */
                    101: 
                    102:                close(creat(edname, 0600));
                    103:                if ((obuf = fopen(edname, "w")) == NULL) {
                    104:                        perror(edname);
                    105:                        goto out;
                    106:                }
                    107:                if (send(mp, obuf) < 0) {
                    108:                        perror(edname);
                    109:                        fclose(obuf);
                    110:                        remove(edname);
                    111:                        goto out;
                    112:                }
                    113:                fflush(obuf);
                    114:                if (ferror(obuf)) {
                    115:                        remove(edname);
                    116:                        fclose(obuf);
                    117:                        goto out;
                    118:                }
                    119:                fclose(obuf);
                    120: 
                    121:                /*
                    122:                 * Fork/execl the editor on the edit file.
                    123:                 */
                    124: 
                    125:                pid = vfork();
                    126:                if (pid == -1) {
                    127:                        perror("fork");
                    128:                        remove(edname);
                    129:                        goto out;
                    130:                }
                    131:                if (pid == 0) {
                    132:                        if (sigint != SIG_IGN)
                    133:                                signal(SIGINT, SIG_DFL);
                    134:                        if (sigquit != SIG_IGN)
                    135:                                signal(SIGQUIT, SIG_DFL);
                    136:                        execl(ed, ed, edname, 0);
                    137:                        perror(ed);
                    138:                        _exit(1);
                    139:                }
                    140:                while (wait(&mesg) != pid)
                    141:                        ;
                    142: 
                    143:                /*
                    144:                 * Now copy the message to the end of the
                    145:                 * temp file.
                    146:                 */
                    147: 
                    148:                if ((ibuf = fopen(edname, "r")) == NULL) {
                    149:                        perror(edname);
                    150:                        remove(edname);
                    151:                        goto out;
                    152:                }
                    153:                remove(edname);
                    154:                fseek(otf, (long) 0, 2);
                    155:                size = fsize(otf);
                    156:                mp->m_block = blockof(size);
                    157:                mp->m_offset = offsetof(size);
                    158:                ms = 0;
                    159:                lines = 0;
                    160:                while ((c = getc(ibuf)) != EOF) {
                    161:                        if (c == '\n')
                    162:                                lines++;
                    163:                        putc(c, otf);
                    164:                        if (ferror(otf))
                    165:                                break;
                    166:                        ms++;
                    167:                }
                    168:                mp->m_size = ms;
                    169:                mp->m_lines = lines;
                    170:                if (ferror(otf))
                    171:                        perror("/tmp");
                    172:                fclose(ibuf);
                    173:        }
                    174: 
                    175:        /*
                    176:         * Restore signals and return.
                    177:         */
                    178: 
                    179: out:
                    180:        signal(SIGINT, sigint);
                    181:        signal(SIGQUIT, sigquit);
                    182: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.