Annotation of 40BSD/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:        struct stat statb;
                     62:        long modtime;
                     63: 
                     64:        /*
                     65:         * Set signals; locate editor.
                     66:         */
                     67: 
                     68:        sigint = signal(SIGINT, SIG_IGN);
                     69:        sigquit = signal(SIGQUIT, SIG_IGN);
                     70: 
                     71:        /*
                     72:         * Deal with each message to be edited . . .
                     73:         */
                     74: 
                     75:        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
                     76:                mesg = *ip;
                     77:                mp = &message[mesg-1];
                     78:                mp->m_flag |= MODIFY;
                     79: 
                     80:                /*
                     81:                 * Make up a name for the edit file of the
                     82:                 * form "Message%d" and make sure it doesn't
                     83:                 * already exist.
                     84:                 */
                     85: 
                     86:                cp = &nbuf[10];
                     87:                *--cp = 0;
                     88:                while (mesg) {
                     89:                        *--cp = mesg % 10 + '0';
                     90:                        mesg /= 10;
                     91:                }
                     92:                cp2 = copy("Message", edname);
                     93:                while (*cp2++ = *cp++)
                     94:                        ;
                     95:                if (!access(edname, 2)) {
                     96:                        printf("%s: file exists\n", edname);
                     97:                        goto out;
                     98:                }
                     99: 
                    100:                /*
                    101:                 * Copy the message into the edit file.
                    102:                 */
                    103: 
                    104:                close(creat(edname, 0600));
                    105:                if ((obuf = fopen(edname, "w")) == NULL) {
                    106:                        perror(edname);
                    107:                        goto out;
                    108:                }
                    109:                if (send(mp, obuf) < 0) {
                    110:                        perror(edname);
                    111:                        fclose(obuf);
                    112:                        remove(edname);
                    113:                        goto out;
                    114:                }
                    115:                fflush(obuf);
                    116:                if (ferror(obuf)) {
                    117:                        remove(edname);
                    118:                        fclose(obuf);
                    119:                        goto out;
                    120:                }
                    121:                fclose(obuf);
                    122: 
                    123:                /*
                    124:                 * Fork/execl the editor on the edit file.
                    125:                 */
                    126: 
                    127:                if (stat(edname, &statb) < 0)
                    128:                        modtime = 0;
                    129:                modtime = statb.st_mtime;
                    130:                pid = vfork();
                    131:                if (pid == -1) {
                    132:                        perror("fork");
                    133:                        remove(edname);
                    134:                        goto out;
                    135:                }
                    136:                if (pid == 0) {
                    137:                        if (sigint != SIG_IGN)
                    138:                                signal(SIGINT, SIG_DFL);
                    139:                        if (sigquit != SIG_IGN)
                    140:                                signal(SIGQUIT, SIG_DFL);
                    141:                        execl(ed, ed, edname, 0);
                    142:                        perror(ed);
                    143:                        _exit(1);
                    144:                }
                    145:                while (wait(&mesg) != pid)
                    146:                        ;
                    147: 
                    148:                /*
                    149:                 * Now copy the message to the end of the
                    150:                 * temp file.
                    151:                 */
                    152: 
                    153:                if (stat(edname, &statb) < 0) {
                    154:                        perror(edname);
                    155:                        goto out;
                    156:                }
                    157:                if (modtime == statb.st_mtime) {
                    158:                        remove(edname);
                    159:                        goto out;
                    160:                }
                    161:                if ((ibuf = fopen(edname, "r")) == NULL) {
                    162:                        perror(edname);
                    163:                        remove(edname);
                    164:                        goto out;
                    165:                }
                    166:                remove(edname);
                    167:                fseek(otf, (long) 0, 2);
                    168:                size = fsize(otf);
                    169:                mp->m_block = blockof(size);
                    170:                mp->m_offset = offsetof(size);
                    171:                ms = 0;
                    172:                lines = 0;
                    173:                while ((c = getc(ibuf)) != EOF) {
                    174:                        if (c == '\n')
                    175:                                lines++;
                    176:                        putc(c, otf);
                    177:                        if (ferror(otf))
                    178:                                break;
                    179:                        ms++;
                    180:                }
                    181:                mp->m_size = ms;
                    182:                mp->m_lines = lines;
                    183:                if (ferror(otf))
                    184:                        perror("/tmp");
                    185:                fclose(ibuf);
                    186:        }
                    187: 
                    188:        /*
                    189:         * Restore signals and return.
                    190:         */
                    191: 
                    192: out:
                    193:        signal(SIGINT, sigint);
                    194:        signal(SIGQUIT, sigquit);
                    195: }

unix.superglobalmegacorp.com

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