home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / Mail / edit.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  3KB  |  180 lines

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