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

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