home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / uemacs / part4 / sys / atari / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.9 KB  |  155 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Atari 520ST file I/O.
  4.  * Version:    30
  5.  * Last edit:    22-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  */
  9. #include    "def.h"
  10.  
  11. extern    FILE    *fopen();
  12. static    FILE    *ffp;
  13.  
  14. /*
  15.  * Open a file for reading.
  16.  */
  17. ffropen(fn)
  18. char    *fn;
  19. {
  20.     ffp = fopen(fn, "r");
  21.     adjustcase(fn);
  22.     if (ffp == NULL)
  23.         return (FIOFNF);
  24.     return (FIOSUC);
  25. }
  26.  
  27. /*
  28.  * Open a file for writing.
  29.  * Return TRUE if all is well, and
  30.  * FALSE on error (cannot create).
  31.  */
  32. ffwopen(fn)
  33. char    *fn;
  34. {
  35.     ffp = fopen(fn, "w");
  36.     adjustcase(fn);
  37.     if (ffp == NULL) {
  38.         eprintf("Cannot open file for writing");
  39.         return (FIOERR);
  40.     }
  41.     return (FIOSUC);
  42. }
  43.  
  44. /*
  45.  * Close a file.
  46.  * Should look at the status.
  47.  */
  48. ffclose()
  49. {
  50.     fclose(ffp);
  51.     return (FIOSUC);
  52. }
  53.  
  54. /*
  55.  * Write a line to the already
  56.  * opened file. The "buf" points to the
  57.  * buffer, and the "nbuf" is its length, less
  58.  * the free newline. Return the status.
  59.  * Check only at the newline.
  60.  */
  61. ffputline(buf, nbuf)
  62. register char    buf[];
  63. {
  64.     register int    i;
  65.  
  66.     for (i=0; i<nbuf; ++i)
  67.         putc(buf[i]&0xFF, ffp);
  68.     putc('\n', ffp);
  69.     if (ferror(ffp) != FALSE) {
  70.         eprintf("Write I/O error");
  71.         return (FIOERR);
  72.     }
  73.     return (FIOSUC);
  74. }
  75.  
  76. /*
  77.  * Read a line from a file, and store the bytes
  78.  * in the supplied buffer. Stop on end of file or end of
  79.  * line. Don't get upset by files that don't have an end of
  80.  * line on the last line; this seem to be common on CP/M-86 and
  81.  * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
  82.  * has not been confirmed. If this is sufficiently researched
  83.  * it may be possible to pull this kludge). Delete any CR
  84.  * followed by an LF. This is mainly for runoff documents,
  85.  * both on VMS and on Ultrix (they get copied over from
  86.  * VMS systems with DECnet).
  87.  */
  88. ffgetline(buf, nbuf)
  89. register char    buf[];
  90. {
  91.     register int    c;
  92.     register int    i;
  93.  
  94.     i = 0;
  95.     for (;;) {
  96.         c = getc(ffp);
  97.         if (c == '\r') {        /* Delete any non-stray    */
  98.             c = getc(ffp);        /* carriage returns.    */
  99.             if (c != '\n') {
  100.                 if (i >= nbuf-1) {
  101.                     eprintf("File has long line");
  102.                     return (FIOERR);
  103.                 }
  104.                 buf[i++] = '\r';
  105.             }
  106.         }
  107.         if (c==EOF || c=='\n')        /* End of line.        */
  108.             break;
  109.         if (i >= nbuf-1) {
  110.             eprintf("File has long line");
  111.             return (FIOERR);
  112.         }
  113.         buf[i++] = c;
  114.     }
  115.     if (c == EOF) {                /* End of file.        */
  116.         if (ferror(ffp) != FALSE) {
  117.             eprintf("File read error");
  118.             return (FIOERR);
  119.         }
  120.         if (i == 0)            /* Don't get upset if    */
  121.             return (FIOEOF);    /* no newline at EOF.    */
  122.     }
  123.     buf[i] = 0;
  124.     return (FIOSUC);
  125. }
  126.  
  127. /*
  128.  * Finish this routine when you decide
  129.  * what the right thing to do when renaming a
  130.  * file for backup purposes.
  131.  */
  132. fbackupfile(fname)
  133. char    *fname;
  134. {
  135.     return (TRUE);
  136. }
  137.  
  138. /*
  139.  * Zap file name to lower case, since
  140.  * the system on the 520ST has case insensitive
  141.  * file names, and lower looks better in the
  142.  * modelines than upper.
  143.  */
  144. adjustcase(fn)
  145. register char    *fn;
  146. {
  147.     register int    c;
  148.  
  149.     while ((c = *fn) != 0) {
  150.         if (ISUPPER(c) != FALSE)
  151.             *fn = TOLOWER(c);
  152.         ++fn;
  153.     }
  154. }
  155.