home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8708 / 28 / sys / msdos / fileio.c next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  4.4 KB  |  229 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *         MSDOS file I/O (MSC4.0)
  4.  */
  5. #include    "def.h"
  6.  
  7. #ifndef F_OK
  8. #define F_OK    0
  9. #define X_OK    1
  10. #define W_OK    2
  11. #define R_OK    4
  12. #endif
  13.  
  14. /*
  15.  * Move bytes, overlaps OK (daveb).
  16.  *
  17.  * MS/C 1.0 provides "memcpy", rather than bcopy.
  18.  */
  19. bcopy( from, to, cnt )
  20. char *from;
  21. char *to;
  22. int cnt;
  23. {
  24.     memcpy(to, from, cnt);
  25. }
  26.  
  27.  
  28. static    FILE    *ffp;
  29.  
  30. /*
  31.  * Open a file for reading.
  32.  */
  33. ffropen(fn)
  34. char    *fn;
  35. {
  36.     if ((ffp=fopen(fn, "rb")) == NULL)
  37.         return (FIOFNF);
  38.     return (FIOSUC);
  39. }
  40.  
  41. /*
  42.  * Open a file for writing.
  43.  * Return TRUE if all is well, and
  44.  * FALSE on error (cannot create).
  45.  */
  46. ffwopen(fn)
  47. char    *fn;
  48. {
  49.     if ((ffp=fopen(fn, "wb")) == NULL) {
  50.         ewprintf("Cannot open file for writing");
  51.         return (FIOERR);
  52.     }
  53.     return (FIOSUC);
  54. }
  55.  
  56. /*
  57.  * Close a file.
  58.  * Should look at the status.
  59.  */
  60. ffclose()
  61. {
  62.     (VOID) fclose(ffp);
  63.     return (FIOSUC);
  64. }
  65.  
  66. /*
  67.  * Write a line to the already
  68.  * opened file. The "buf" points to the
  69.  * buffer, and the "nbuf" is its length, less
  70.  * the free newline. Return the status.
  71.  * Check only at the newline.
  72.  */
  73. ffputline(buf, nbuf)
  74. register char    buf[];
  75. {
  76.     register int    i;
  77.  
  78.     for (i=0; i<nbuf; ++i)
  79.         putc(buf[i]&0xFF, ffp);
  80.     putc('\r', ffp);    /* MSDOS wants \r\n line seperators */
  81.     putc('\n', ffp);
  82.     if (ferror(ffp) != FALSE) {
  83.         ewprintf("Write I/O error");
  84.         return (FIOERR);
  85.     }
  86.     return (FIOSUC);
  87. }
  88.  
  89. /*
  90.  * Read a line from a file, and store the bytes
  91.  * in the supplied buffer. Stop on end of file or end of
  92.  * line. Don't get upset by files that don't have an end of
  93.  * line on the last line; this seem to be common on CP/M-86 and
  94.  * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
  95.  * has not been confirmed. If this is sufficiently researched
  96.  * it may be possible to pull this kludge). Delete any CR
  97.  * followed by an LF. This is mainly for runoff documents,
  98.  * both on VMS and on Ultrix (they get copied over from
  99.  * VMS systems with DECnet).
  100.  */
  101. ffgetline(buf, nbuf)
  102. register char    buf[];
  103. {
  104.     register int    c;
  105.     register int    i;
  106.  
  107.     i = 0;
  108.     for (;;) {
  109.         c = getc(ffp);
  110.         if (c == '\r') {        /* Delete any non-stray    */
  111.             c = getc(ffp);        /* carriage returns.    */
  112.             if (c != '\n') {
  113.                 if (i >= nbuf-1) {
  114.                     ewprintf("File has long line");
  115.                     return (FIOERR);
  116.                 }
  117.                 buf[i++] = '\r';
  118.             }
  119.         }
  120.         if (c==EOF || c=='\n')        /* End of line.        */
  121.             break;
  122.         if (i >= nbuf-1) {
  123.             ewprintf("File has long line");
  124.             return (FIOERR);
  125.         }
  126.         buf[i++] = c;
  127.     }
  128.     if (c == EOF) {                /* End of file.        */
  129.         if (ferror(ffp) != FALSE) {
  130.             ewprintf("File read error");
  131.             return (FIOERR);
  132.         }
  133.         if (i == 0)            /* Don't get upset if    */
  134.             return (FIOEOF);    /* no newline at EOF.    */
  135.     }
  136.     buf[i] = 0;
  137.     return (FIOSUC);
  138. }
  139.  
  140. #ifdef    BACKUP
  141. /*
  142.  * Rename the file "fname" into a backup copy.
  143.  * On Unix the backup has the same name as the
  144.  * original file, with a "~" on the end - unfortunately
  145.  * this does not map well to MS-DOS - the old .bak convention
  146.  * is used.
  147.  */
  148. fbackupfile(fname)
  149. char    *fname;
  150. {
  151.     register char    *nname, *ptr;
  152.     char *strchr();
  153.  
  154.     if ((nname=malloc(strlen(fname)+3+1)) == NULL)
  155.         return (ABORT);
  156.     (void) strcpy(nname, fname);
  157.     if (ptr = strchr(nname, '.'))
  158.         strcpy(ptr, ".bak")
  159.     else
  160.         strcat(ptr, ".bak")
  161.  
  162.     if (strcmp(fname, nname) == 0) {
  163.         free(nname);
  164.         return FALSE;
  165.     }
  166.  
  167.     (void) unlink(nname);        /* Ignore errors.    */
  168.     (void) rename(fname, nname);
  169.     free(nname);
  170.     return (TRUE);
  171. }
  172. #endif
  173.  
  174. /*
  175.  * The string "fn" is a file name.
  176.  * Perform any required case adjustments. All sustems
  177.  * we deal with so far have case insensitive file systems.
  178.  * We zap everything to lower case. The problem we are trying
  179.  * to solve is getting 2 buffers holding the same file if
  180.  * you visit one of them with the "caps lock" key down.
  181.  * On UNIX file names are dual case, so we leave
  182.  * everything alone.
  183.  */
  184. /*ARGSUSED*/
  185. adjustcase(fn)
  186. register char    *fn;
  187. {
  188.     register int    c;
  189.  
  190.     while ((c = *fn) != 0) {
  191.         if (c>='A' && c<='Z')
  192.             *fn = c + 'a' - 'A';
  193.         ++fn;
  194.     }
  195. }
  196.  
  197.  
  198.  
  199. #ifdef    STARTUP
  200. #include <sys/types.h>
  201. #define STARTUPNAME ".mg"
  202. /*
  203.  * find the users startup file, and return it's name. Check for
  204.  * if MGSTARTUP is defined, then use that.   Otherwise, look
  205.  * for .mg in the current directory, then in the root directory.
  206.  */
  207. char *
  208. startupfile() 
  209. {
  210.     register char    *file;
  211.     static char    temp[NFILEN];
  212.     char        *getenv();
  213.  
  214.     if ((file = getenv("MGSTARTUP")) != NULL )
  215.         {
  216.         if (access(file, F_OK ) == 0)
  217.             return file;
  218.         return NULL;
  219.         }
  220.     if (access (STARTUPNAME, F_OK) == 0)
  221.         return STARTUPNAME;
  222.     strcpy(temp, "/");
  223.     strcat(temp, STARTUPNAME);
  224.     if (access (temp, F_OK) == 0)
  225.         return temp;
  226.     return NULL;
  227. }
  228. #endif
  229.