home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / pcmail / main / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.4 KB  |  124 lines

  1. /*++
  2. /* NAME
  3. /*    edit 3
  4. /* SUMMARY
  5. /*    edit a file
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    mail
  10. /* SYNOPSIS
  11. /*    #include "mail.h"
  12. /*
  13. /*    int edit(fname,tname)
  14. /*    char *fname,*tname;
  15. /* DESCRIPTION
  16. /*    edit() copies the file in fname to the file tname,
  17. /*    invokes the editor, and copies the result back.
  18. /* COMMANDS
  19. /*    the program specified in the EDITOR environment variable,
  20. /*    or a system-dependent default.
  21. /* AUTHOR(S)
  22. /*      W.Z. Venema
  23. /*      Eindhoven University of Technology
  24. /*      Department of Mathematics and Computer Science
  25. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  26. /* CREATION DATE
  27. /*    Wed Apr  6 20:21:35 MET 1988
  28. /* LAST MODIFICATION
  29. /*    90/01/22 13:01:34
  30. /* VERSION/RELEASE
  31. /*    2.1
  32. /*--*/
  33.  
  34. #include <stdio.h>
  35. #include <errno.h>
  36.  
  37. #include "defs.h"
  38. #include "path.h"
  39. #include "mail.h"
  40. #include "status.h"
  41.  
  42. #ifdef MSDOS
  43. #   include <fcntl.h>
  44. #endif
  45.  
  46.  /*
  47.   * Some editors (wordstar) do not accept path names. grrrr. So we copy the
  48.   * edit file to a temp file in the current directory. After editing, the
  49.   * temp file is copied back. We do not move files, since current dir and the
  50.   * spool dir may be in different file systems, and people might turn off the
  51.   * machine and lose their work.
  52.   */
  53.  
  54. /* edit - edit or create a file */
  55.  
  56. public int edit(fname, tname)
  57. char   *fname,
  58.        *tname;
  59. {
  60.     register int stat = 0;
  61.     register FILE *fp;
  62.  
  63.     /*
  64.      * First make sure that we can get write permission on the permanent file
  65.      * and temporary file (if they exist). Create an empty temp file if we
  66.      * are not editing an existing file.
  67.      */
  68.  
  69.     if (chmod(fname, 0666) && errno != ENOENT) {
  70.     stat = E_WRITERR;            /* original file is protected */
  71.     } else if (chmod(tname, 0666) && errno != ENOENT) {
  72.     stat = E_WRITERR;            /* scratch file is protected */
  73.     } else if ((fp = fopen(fname, "a")) == 0) {
  74.     stat = E_WRITERR;            /* file system is protected? */
  75.     } else if (fclose(fp), stat = cpfile(fname, tname)) {
  76.      /* void */ ;                /* could not make edit copy */
  77.     } else {
  78.     patience();                /* this may take some time */
  79.     kbdrest();                /* reset tty modes */
  80.     if (stat = invokelp(editor, tname, (char *) 0))    /* call editor */
  81.         stat = (stat == E_NOPROG ? stat : E_UNKNOWN);
  82.     else
  83.         stat = cpfile(tname, fname);    /* copy back */
  84.     kbdinit();                /* set tty modes */
  85.     unlink(tname);                /* don't check status */
  86.     }
  87.     chmod(fname, 0444);                /* protect destination file */
  88.     return (stat);
  89. }
  90.  
  91. /* cpfile - yet another file copy function */
  92.  
  93. hidden int cpfile(from, to)
  94. char   *from;
  95. char   *to;
  96. {
  97.     register FILE *in,
  98.            *out;            /* file pointers */
  99.     int     stat = 0;            /* error status */
  100.     register int c;            /* character buffer */
  101.  
  102.     if ((in = fopen(from, "r")) == 0) {        /* cannot read source */
  103.     return (E_READERR);
  104.     } else if ((out = fopen(to, "w")) == 0) {    /* cannot write destination */
  105.     fclose(in);
  106.     return (E_WRITERR);
  107.     } else {
  108.  
  109. #ifdef O_BINARY
  110.     setmode(fileno(in), O_BINARY);        /* get rid of the */
  111.     setmode(fileno(out), O_BINARY);        /* crlf translation */
  112. #endif
  113.     while ((c = getc(in)) != EOF)
  114.         putc(c, out);
  115.     if (ferror(in))                /* check read error status */
  116.         stat = E_READERR;
  117.     else if (ferror(out))            /* check write error status */
  118.         stat = E_WRITERR;
  119.     fclose(in);
  120.     fclose(out);
  121.     return (stat);                /* at most one type of error */
  122.     }
  123. }
  124.