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

  1. /*
  2.  * Name:    MicroEMACS
  3.  * Version:    29
  4.  *        VAX/VMS file I/O.
  5.  * Last edit:    05-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  *
  9.  * Read and write ASCII files. All
  10.  * of the low level file I/O knowledge is here.
  11.  * VAX/VMS.
  12.  * Pretty much vanilla standard I/O, using
  13.  * the (traditional) funny open.
  14.  */
  15. #include    "def.h"
  16.  
  17. static    FILE    *ffp;
  18.  
  19. /*
  20.  * Open a file for reading.
  21.  */
  22. ffropen(fn)
  23. char    *fn;
  24. {
  25.     if ((ffp=fopen(fn, "r")) == NULL)
  26.         return (FIOFNF);
  27.     return (FIOSUC);
  28. }
  29.  
  30. /*
  31.  * Open a file for writing.
  32.  * Return TRUE if all is well, and
  33.  * FALSE on error (cannot create).
  34.  */
  35. ffwopen(fn)
  36. char    *fn;
  37. {
  38.     register int    fd;
  39.  
  40.     if ((fd=creat(fn, 0, "rfm=var", "rat=cr")) < 0
  41.     || (ffp=fdopen(fd, "w")) == NULL) {
  42.         eprintf("Cannot open file for writing");
  43.         return (FIOERR);
  44.     }
  45.     return (FIOSUC);
  46. }
  47.  
  48. /*
  49.  * Close a file.
  50.  * Should look at the status.
  51.  */
  52. ffclose()
  53. {
  54.     fclose(ffp);
  55.     return (FIOSUC);
  56. }
  57.  
  58. /*
  59.  * Write a line to the already
  60.  * opened file. The "buf" points to the
  61.  * buffer, and the "nbuf" is its length, less
  62.  * the free newline. Return the status.
  63.  * Check only at the newline.
  64.  */
  65. ffputline(buf, nbuf)
  66. register char    buf[];
  67. {
  68.     register int    i;
  69.  
  70.     for (i=0; i<nbuf; ++i)
  71.         putc(buf[i]&0xFF, ffp);
  72.     putc('\n', ffp);
  73.     if (ferror(ffp) != FALSE) {
  74.         eprintf("Write I/O error");
  75.         return (FIOERR);
  76.     }
  77.     return (FIOSUC);
  78. }
  79.  
  80. /*
  81.  * Read a line from a file, and store the bytes
  82.  * in the supplied buffer. Stop on end of file or end of
  83.  * line. Don't get upset by files that don't have an end of
  84.  * line on the last line; this seem to be common on CP/M-86 and
  85.  * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
  86.  * has not been confirmed. If this is sufficiently researched
  87.  * it may be possible to pull this kludge). Delete any CR
  88.  * followed by an LF. This is mainly for runoff documents,
  89.  * both on VMS and on Ultrix (they get copied over from
  90.  * VMS systems with DECnet).
  91.  */
  92. ffgetline(buf, nbuf)
  93. register char    buf[];
  94. {
  95.     register int    c;
  96.     register int    i;
  97.  
  98.     i = 0;
  99.     for (;;) {
  100.         c = getc(ffp);
  101.         if (c == '\r') {        /* Delete any non-stray    */
  102.             c = getc(ffp);        /* carriage returns.    */
  103.             if (c != '\n') {
  104.                 if (i >= nbuf-1) {
  105.                     eprintf("File has long line");
  106.                     return (FIOERR);
  107.                 }
  108.                 buf[i++] = '\r';
  109.             }
  110.         }
  111.         if (c==EOF || c=='\n')        /* End of line.        */
  112.             break;
  113.         if (i >= nbuf-1) {
  114.             eprintf("File has long line");
  115.             return (FIOERR);
  116.         }
  117.         buf[i++] = c;
  118.     }
  119.     if (c == EOF) {                /* End of file.        */
  120.         if (ferror(ffp) != FALSE) {
  121.             eprintf("File read error");
  122.             return (FIOERR);
  123.         }
  124.         if (i == 0)            /* Don't get upset if    */
  125.             return (FIOEOF);    /* no newline at EOF.    */
  126.     }
  127.     buf[i] = 0;
  128.     return (FIOSUC);
  129. }
  130.  
  131. /*
  132.  * VMS has version numbers, so there is
  133.  * no need for MicroEMACS to bother making its own
  134.  * flavour of backup copy. Return TRUE so the
  135.  * caller doesn't quit.
  136.  */
  137. fbackupfile(fname)
  138. char    *fname;
  139. {
  140.     return (TRUE);
  141. }
  142.  
  143. /*
  144.  * The string "fn" is a file name.
  145.  * Perform any required case adjustments. All sustems
  146.  * we deal with so far have case insensitive file systems.
  147.  * We zap everything to lower case. The problem we are trying
  148.  * to solve is getting 2 buffers holding the same file if
  149.  * you visit one of them with the "caps lock" key down.
  150.  * On UNIX file names are dual case, so we leave
  151.  * everything alone.
  152.  */
  153. adjustcase(fn)
  154. register char    *fn;
  155. {
  156.     register int    c;
  157.  
  158.     while ((c = *fn) != 0) {
  159.         if (c>='A' && c<='Z')
  160.             *fn = c + 'a' - 'A';
  161.         ++fn;
  162.     }
  163. }
  164.