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