home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2872 / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  4.6 KB  |  254 lines

  1. /*
  2. *    MS-DOS file I/O.
  3. */
  4. #include    <sys/types.h> 
  5. #include        "def.h"
  6. #if UNIX
  7. #include    <fcntl.h> 
  8. #include    <sys/stat.h> 
  9. #endif
  10.  
  11. extern    char    MSG_cnt_wr[];
  12. extern    char    MSG_wr_io_er[];
  13. extern    char    MSG_rd_er[];
  14. extern    char    MSG_bak[];
  15. extern    char    MSG_backup[];
  16. extern    char    MSG_back_er[];
  17. extern    char    MSG_back_of[];
  18.  
  19. #include    "lintfunc.dec"
  20.  
  21. #if MSDOS
  22. static  FILE * ffp;
  23. #endif
  24.  
  25. #if UNIX
  26. static  int ffp;
  27. #endif
  28.  
  29. /*
  30. * Open a file for reading.
  31. */
  32. char    ffropen (fn)
  33. char   *fn;
  34. {
  35. #if MSDOS
  36.     if ((ffp = fopen (fn, "rb")) == NULL)/* pvr */
  37.         return (FIOERR);;
  38.     return (FIOSUC);
  39. #endif
  40. #if UNIX
  41.     if ((ffp = open (fn, O_RDONLY)) == -1)/* pvr */
  42.         return (FIOERR);;
  43.     return (FIOSUC);
  44. #endif
  45. }
  46. /*
  47. *   Get the file length
  48. */
  49. A32 file_len ()
  50.     {
  51. #if MSDOS
  52.     return (filelength (fileno (ffp)));
  53. #endif
  54. #if UNIX
  55.     struct    stat    st;
  56.     
  57.     if (fstat (ffp, &st) == -1)
  58.         return (-1);
  59.     return (st.st_size);
  60. #endif
  61.     }
  62.  
  63. /*
  64. * Open a file for writing.
  65. * Return TRUE if all is well, and
  66. * FALSE on error (cannot create).
  67. */
  68. char    ffwopen (fn)
  69. char   *fn;
  70. {
  71. #if MSDOS
  72.     if ((ffp = fopen (fn, "wb")) == NULL)/* pvr */
  73.         {
  74.         writ_echo (MSG_cnt_wr);
  75.         return (FIOERR);
  76.         }
  77.     return (FIOSUC);
  78. #endif
  79. #if UNIX
  80.     if ((ffp = open (fn, O_WRONLY | O_CREAT)) == -1)/* pvr */
  81.         return (FIOERR);;
  82.     return (FIOSUC);
  83. #endif
  84. }
  85.  
  86. /*
  87. * Close a file.
  88. * Should look at the status.
  89. */
  90. char    ffclose ()
  91. {
  92. #if MSDOS
  93.     fclose (ffp);
  94. #endif
  95. #if UNIX
  96.     close (ffp);
  97. #endif
  98.     return (FIOSUC);
  99. }
  100.  
  101. /*
  102. * Write a line to the already
  103. * opened file. The "buf" points to the
  104. * buffer, and the "nbuf" is its length.   pvr
  105. * Return the status.
  106. */
  107. char    ffputline (buf, nbuf)
  108. register char   buf[];
  109. int     nbuf;
  110. {
  111.     register int    i;
  112.  
  113. #if MSDOS
  114.     i = fwrite (buf, 1, nbuf, ffp);
  115. #endif
  116. #if UNIX 
  117.     i = write (ffp, buf, nbuf);
  118. #endif
  119.  
  120.     if ((i != nbuf)
  121. #if MSDOS    
  122.          || (ferror (ffp) != FALSE))
  123. #else
  124.         )
  125. #endif     
  126.         {
  127.         writ_echo (MSG_wr_io_er);
  128.         return (FIOERR);
  129.         }
  130.     return (FIOSUC);
  131. }
  132.  
  133. /*
  134. * Read a line from a file, and store the bytes
  135. * in the supplied buffer. Stop on end of file or after 'nbuf' characters. pvr
  136. * the first byte in the buffer is the length in bytes.
  137. */
  138. char    ffgetline (buf, nbuf, rbuf)
  139. register char   *buf;
  140. register int    *rbuf, nbuf;
  141. {
  142.  
  143.     register int    c;
  144.     register int    i;
  145.  
  146. #if MSDOS
  147.     *rbuf = fread (buf, 1, nbuf, ffp); 
  148. #endif
  149.  
  150. #if UNIX
  151.     *rbuf = read (ffp, buf, nbuf); 
  152. #endif
  153.  
  154.     /* End of file.         */
  155. #if MSDOS
  156.     if (ferror (ffp) != FALSE)
  157.         {
  158.         writ_echo (MSG_rd_er);
  159.         return (FIOERR);
  160.         }
  161. #endif
  162.     if (*rbuf == 0)
  163.         return (FIOEOF);
  164.  
  165.     return (FIOSUC);
  166. }
  167.  
  168. /*
  169. *   Seek to specified position in file.
  170. *   Return the actual position in the file.
  171. */
  172. A32     ffseek (posn)
  173.     A32     posn;
  174.     {
  175. #if MSDOS
  176.     fseek (ffp, posn, SEEK_SET);
  177.     return (ftell (ffp));
  178. #endif
  179. #if UNIX
  180.     return (lseek (ffp, posn, 0));
  181. #endif
  182.     }
  183.  
  184. /*
  185. * Some backup user on MS-DOS might want
  186. * to determine some rule for doing backups on that
  187. * system, and fix this. I don't use MS-DOS, so I don't
  188. * know what the right rules would be. Return TRUE so
  189. * the caller does not abort a write.
  190. */
  191. #if BACKUP
  192. bool    fbackupfile (fname)
  193. char   *fname;
  194. {
  195.     FILE * backupfile;
  196.     char    backname[128];
  197.     char   *source,
  198.            *backup;
  199.     extern char time_string[];
  200.     char    buf[80];
  201.  
  202.     source = fname;
  203.     backup = backname;
  204.     while ((*source > 0) && (*source != '.'))
  205.         {
  206.         *backup = *source;
  207.         backup++;
  208.         source++;
  209.         *backup = 0;
  210.         }
  211.     strcat (backname, MSG_bak);
  212.     sprintf (buf, MSG_backup, fname, backname);
  213.     writ_echo (buf);
  214.     unlink (backname);
  215. #if MSDOS
  216.     if (rename (backname, fname) > 0)
  217. #else
  218.     if ((link (fname, backname) != 0) || (unlink (fname) != 0))
  219. #endif
  220.         {
  221.         sprintf (buf, MSG_back_er, fname, backname);
  222.         writ_echo (buf);
  223.         return (FALSE);
  224.         }
  225.     return (TRUE);              /* Hack.                */
  226. }
  227.  
  228. #endif
  229.  
  230. /*
  231. * The string "fn" is a file name.
  232. * Perform any required case adjustments. All systems
  233. * we deal with so far have case insensitive file systems.
  234. * We zap everything to lower case. The problem we are trying
  235. * to solve is getting 2 buffers holding the same file if
  236. * you visit one of them with the "caps lock" key down.
  237. * On UNIX file names are dual case, so we leave
  238. * everything alone.
  239. */
  240. void    adjustcase (fn)
  241. register char  *fn;
  242. {
  243.     register int    c;
  244.  
  245.     while ((c = *fn) != 0)
  246.         {
  247.         if (c >= 'A' && c <= 'Z')
  248.             *fn = c + 'a' - 'A';
  249.         ++fn;
  250.         }
  251. }
  252.  
  253.  
  254.