home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / beav.1.40.lzh / BEAV140 / fileio.c < prev    next >
Text File  |  1995-06-14  |  6KB  |  372 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. #ifndef OSK
  116.     if ((ffp = open (fn, O_WRONLY | O_CREAT, mode)) == -1)
  117. #else
  118.     if ((ffp = creat (fn, S_IREAD | S_IWRITE)) == -1)/* pvr */
  119. #endif
  120. #endif
  121. #endif
  122. #ifdef AMIGA
  123.         /* set perms as in original file 1.31 */
  124.         if ((ffp = open (fn, O_WRONLY | O_CREAT, mode)) == -1)    /* pvr */
  125. #endif
  126.         {
  127.         err_echo (MSG_cnt_wr);
  128.         return (FIOERR);
  129.         }
  130.     return (FIOSUC);
  131. }
  132.  
  133. /*
  134. * Close a file.
  135. * Should look at the status.
  136. */
  137. char
  138. ffclose ()
  139. {
  140. #ifdef MSDOS
  141.     fclose (ffp);
  142. #endif
  143. #ifdef UNIX
  144.     close (ffp);
  145. #endif
  146. #ifdef AMIGA
  147.     close (ffp);
  148. #endif
  149.     return (FIOSUC);
  150. }
  151.  
  152. /*
  153. * Write a line to the already
  154. * opened file. The "buf" points to the
  155. * buffer, and the "nbuf" is its length.   pvr
  156. * Return the status.
  157. */
  158. char
  159. ffputline (buf, nbuf)
  160.     register char buf[];
  161.     int nbuf;
  162. {
  163.     register int i;
  164.  
  165. #ifdef MSDOS
  166.     i = fwrite (buf, 1, nbuf, ffp);
  167. #endif
  168. #ifdef UNIX
  169.     i = write (ffp, buf, nbuf);
  170. #endif
  171. #ifdef AMIGA
  172.     i = write (ffp, buf, nbuf);
  173. #endif
  174.  
  175.     if ((i != nbuf)
  176. #ifdef MSDOS
  177.     || (ferror (ffp) != FALSE))
  178. #else
  179.     )
  180. #endif
  181.     {
  182.     err_echo (MSG_wr_io_er);
  183.     return (FIOERR);
  184.     }
  185.     return (FIOSUC);
  186. }
  187.  
  188. /*
  189. * Read a line from a file, and store the bytes
  190. * in the supplied buffer. Stop on end of file or after 'nbuf' characters. pvr
  191. * the first byte in the buffer is the length in bytes.
  192. */
  193. char
  194. 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
  228. ffseek (posn)
  229.     A32 posn;
  230. {
  231. #ifdef MSDOS
  232.     fseek (ffp, posn, SEEK_SET);
  233.     return (ftell (ffp));
  234. #endif
  235. #ifdef UNIX
  236.     return (lseek (ffp, posn, 0));
  237. #endif
  238. #ifdef AMIGA
  239.     return (lseek (ffp, posn, 0));
  240. #endif
  241. }
  242.  
  243. /*
  244. * Some backup user on MS-DOS might want
  245. * to determine some rule for doing backups on that
  246. * system, and fix this. I don't use MS-DOS, so I don't
  247. * know what the right rules would be. Return TRUE so
  248. * the caller does not abort a write.
  249. * Under UNIX just append the .bak postfix.
  250. */
  251. #ifdef BACKUP
  252. bool
  253. fbackupfile (fname)
  254.     char *fname;
  255. {
  256.     char backname[NFILEN];
  257.     char *source, *backup;
  258.     char buf[NCOL];
  259.  
  260.     source = fname;
  261.     backup = backname;
  262.     while ((*source > 0)
  263. #if defined(MSDOS) || defined(OS2)
  264.        && (*source != '.'))
  265. #else
  266.     )
  267. #endif
  268.     {
  269.     *backup = *source;
  270.     backup++;
  271.     source++;
  272.     *backup = 0;
  273.     }
  274. #ifdef OS2
  275.     strcpy (backup, source);
  276.     strcat (backup, "~");
  277.     if (!isvalid (backname))
  278.     strcpy (backup, ".bak");
  279. #else
  280.     strcat (backname, MSG_bak);
  281. #endif
  282.     sprintf (buf, MSG_backup, fname, backname);
  283.     writ_echo (buf);
  284.     unlink (backname);
  285. #ifdef NORENAME
  286.     if ((link (fname, backname) != 0) || (unlink (fname) != 0))
  287. #else
  288.     if (rename (fname, backname) > 0)
  289. #endif
  290.     {
  291.     sprintf (buf, MSG_back_er, fname, backname);
  292.     err_echo (buf);
  293.     return (FALSE);
  294.     }
  295.     return (TRUE);        /* Hack.                */
  296. }
  297.  
  298. #endif
  299.  
  300. /*
  301. * The string "fn" is a file name.
  302. * Perform any required case adjustments. All systems
  303. * we deal with so far have case insensitive file systems.
  304. * We zap everything to lower case. The problem we are trying
  305. * to solve is getting 2 buffers holding the same file if
  306. * you visit one of them with the "caps lock" key down.
  307. * On UNIX and AMIGA file names are dual case, so we leave
  308. * everything alone.
  309. */
  310. void
  311. adjustcase (fn)
  312.     register char *fn;
  313. {
  314.     register int c;
  315.  
  316.     while ((c = *fn) != 0)
  317.     {
  318.     if (c >= 'A' && c <= 'Z')
  319.         *fn = c + 'a' - 'A';
  320.     ++fn;
  321.     }
  322. }
  323.  
  324. #ifdef OS2
  325.  
  326. #define INCL_NOPM
  327. #define INCL_ERRORS
  328. #include <os2.h>
  329.  
  330. int 
  331. isvalid (char *name)
  332. {
  333.     HFILE hf;
  334. #ifdef __32BIT__
  335.     ULONG uAction;
  336. #else
  337.     USHORT uAction;
  338. #endif
  339.  
  340.     switch (DosOpen (name, &hf, &uAction, 0, 0, FILE_OPEN,
  341.              OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0))
  342.     {
  343.     case ERROR_INVALID_NAME:
  344.     case ERROR_FILENAME_EXCED_RANGE:
  345.     return FALSE;
  346.     case NO_ERROR:
  347.     DosClose (hf);
  348.     default:
  349.     return TRUE;
  350.     }
  351. }
  352.  
  353. #endif
  354.  
  355. #ifdef OSK
  356. rename(from,to)
  357. char *from,*to;
  358. {
  359.   char *cmd = (char*) malloc(strlen(from)+strlen(to)+10);
  360.   unsigned status;
  361.   char *pos = (char*)rindex(to,'/');
  362.  
  363.   unlink(to);
  364.   sprintf(cmd,"%s %s\n",from,pos ? pos+1 : to);
  365.   if (os9fork("rename",strlen(cmd),cmd,0,0,0,0) <= 0)
  366.     return(FALSE);
  367.   wait(&status);
  368.   return(status);
  369. }
  370. #endif
  371.  
  372.