home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / OS2 / BEAV132X.ZIP / FILEIO.C < prev    next >
C/C++ Source or Header  |  1992-01-06  |  6KB  |  314 lines

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