home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / pcmail / part06 / edit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.2 KB  |  118 lines

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