home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 001-099 / ff093.lzh / MicroEmacs / source / src.arc / fileio.c < prev    next >
C/C++ Source or Header  |  1987-08-16  |  4KB  |  188 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here. A better message writing scheme should
  4.  * be used.
  5.  */
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. FILE    *ffp;                           /* File pointer, all functions. */
  11.  
  12. /*
  13.  * Open a file for reading.
  14.  */
  15. ffropen(fn)
  16. char    *fn;
  17. {
  18.         if ((ffp=fopen(fn, "r")) == NULL)
  19.                 return (FIOFNF);
  20.         return (FIOSUC);
  21. }
  22.  
  23. /*
  24.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  25.  * (cannot create).
  26.  */
  27. ffwopen(fn)
  28. char    *fn;
  29. {
  30. #if     VMS
  31.         register int    fd;
  32.  
  33.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  34.         || (ffp=fdopen(fd, "w")) == NULL) {
  35. #else
  36.         if ((ffp=fopen(fn, "w")) == NULL) {
  37. #endif
  38.                 mlwrite("Cannot open file for writing");
  39.                 return (FIOERR);
  40.         }
  41.         return (FIOSUC);
  42. }
  43.  
  44. /*
  45.  * Close a file. Should look at the status in all systems.
  46.  */
  47. ffclose()
  48. {
  49. #if    MSDOS & CTRLZ
  50.     fputc(26, ffp);        /* add a ^Z at the end of the file */
  51. #endif
  52.     
  53. #if     V7 | USG | BSD | (MSDOS & (LATTICE | MSC))
  54.         if (fclose(ffp) != FALSE) {
  55.                 mlwrite("Error closing file");
  56.                 return(FIOERR);
  57.         }
  58.         return(FIOSUC);
  59. #else
  60.         fclose(ffp);
  61.         return (FIOSUC);
  62. #endif
  63. }
  64.  
  65. /*
  66.  * Write a line to the already opened file. The "buf" points to the buffer,
  67.  * and the "nbuf" is its length, less the free newline. Return the status.
  68.  * Check only at the newline.
  69.  */
  70. ffputline(buf, nbuf)
  71. char    buf[];
  72. {
  73.         register int    i;
  74. #if    CRYPT
  75.     char c;        /* character to translate */
  76.  
  77.     if (cryptflag) {
  78.             for (i = 0; i < nbuf; ++i) {
  79.             c = buf[i] & 0xff;
  80.             crypt(&c, 1);
  81.             fputc(c, ffp);
  82.         }
  83.     } else
  84.             for (i = 0; i < nbuf; ++i)
  85.                     fputc(buf[i]&0xFF, ffp);
  86. #else
  87.         for (i = 0; i < nbuf; ++i)
  88.                 fputc(buf[i]&0xFF, ffp);
  89. #endif
  90.  
  91. #if    ST520
  92.         fputc('\r', ffp);
  93. #endif        
  94.         fputc('\n', ffp);
  95.  
  96.         if (ferror(ffp)) {
  97.                 mlwrite("Write I/O error");
  98.                 return (FIOERR);
  99.         }
  100.  
  101.         return (FIOSUC);
  102. }
  103.  
  104. /*
  105.  * Read a line from a file, and store the bytes in the supplied buffer. The
  106.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  107.  * at the end of the file that don't have a newline present. Check for I/O
  108.  * errors too. Return status.
  109.  */
  110. ffgetline(buf, nbuf)
  111. register char   buf[];
  112. {
  113.         register int    c;
  114.         register int    i;
  115.  
  116.         i = 0;
  117.  
  118.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  119.                 if (i >= nbuf-2) {
  120.             buf[nbuf - 2] = c;    /* store last char read */
  121.             buf[nbuf - 1] = 0;    /* and terminate it */
  122.                         mlwrite("File has long line");
  123. #if    CRYPT
  124.             if (cryptflag)
  125.                 crypt(buf, strlen(buf));
  126. #endif
  127.                         return (FIOLNG);
  128.                 }
  129.                 buf[i++] = c;
  130.         }
  131.  
  132. #if    ST520
  133.     if(buf[i-1] == '\r')
  134.         i--;
  135. #endif
  136.         if (c == EOF) {
  137.                 if (ferror(ffp)) {
  138.                         mlwrite("File read error");
  139.                         return (FIOERR);
  140.                 }
  141.  
  142.                 if (i != 0) {
  143.                     buf[i] = 0;
  144.                         return(FIOFUN);
  145.                 }
  146.  
  147.                 return (FIOEOF);
  148.         }
  149.  
  150.         buf[i] = 0;
  151. #if    CRYPT
  152.     if (cryptflag)
  153.         crypt(buf, strlen(buf));
  154. #endif
  155.         return (FIOSUC);
  156. }
  157.  
  158. #if    AZTEC & MSDOS
  159. #undef    fgetc
  160. /*    a1getc:        Get an ascii char from the file input stream
  161.             but DO NOT strip the high bit
  162. */
  163.  
  164. int a1getc(fp)
  165.  
  166. FILE *fp;
  167.  
  168. {
  169.     int c;        /* translated character */
  170.  
  171.     c = getc(fp);    /* get the character */
  172.  
  173.     /* if its a <LF> char, throw it out  */
  174.     while (c == 10)
  175.         c = getc(fp);
  176.  
  177.     /* if its a <RETURN> char, change it to a LF */
  178.     if (c == '\r')
  179.         c = '\n';
  180.  
  181.     /* if its a ^Z, its an EOF */
  182.     if (c == 26)
  183.         c = EOF;
  184.  
  185.     return(c);
  186. }
  187. #endif
  188.