home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / os2file.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  4KB  |  168 lines

  1. /* -*-C-*-
  2.  
  3. $Id: os2file.c,v 1.3 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1994-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "os2.h"
  23. #include "osfile.h"
  24.  
  25. static ULONG set_file_pointer (Tchannel, ULONG, LONG);
  26.  
  27. #define OS2_OPEN_MODE(m)                        \
  28.   (((((m) & CHANNEL_READ) == 0)                        \
  29.     ? (OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE)            \
  30.     : (((m) & CHANNEL_WRITE) == 0)                    \
  31.     ? (OPEN_ACCESS_READONLY  | OPEN_SHARE_DENYNONE)            \
  32.     : (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE))            \
  33.    | OPEN_FLAGS_NOINHERIT)
  34.  
  35. static Tchannel
  36. open_file (const char * filename, ULONG attr, ULONG flags, unsigned int mode)
  37. {
  38.   HFILE handle;
  39.   ULONG action;
  40.   STD_API_CALL
  41.     (dos_open, (((char *) filename), (&handle), (&action), 0, attr, flags,
  42.         (OS2_OPEN_MODE (mode)), 0));
  43.   return (OS2_make_channel (handle, mode));
  44. }
  45.  
  46. Tchannel
  47. OS_open_input_file (const char * filename)
  48. {
  49.   return
  50.     (open_file (filename,
  51.         FILE_NORMAL,
  52.         (OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW),
  53.         CHANNEL_READ));
  54. }
  55.  
  56. Tchannel
  57. OS_open_output_file (const char * filename)
  58. {
  59.   return
  60.     (open_file (filename,
  61.         FILE_NORMAL,
  62.         (OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW),
  63.         CHANNEL_WRITE));
  64. }
  65.  
  66. Tchannel
  67. OS_open_io_file (const char * filename)
  68. {
  69.   return
  70.     (open_file (filename,
  71.         FILE_NORMAL,
  72.         (OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW),
  73.         (CHANNEL_READ | CHANNEL_WRITE)));
  74. }
  75.  
  76. Tchannel
  77. OS_open_append_file (const char * filename)
  78. {
  79.   Tchannel channel =
  80.     (open_file (filename,
  81.         FILE_NORMAL,
  82.         (OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW),
  83.         CHANNEL_WRITE));
  84.   transaction_begin ();
  85.   OS_channel_close_on_abort (channel);
  86.   (void) set_file_pointer (channel, FILE_END, 0);
  87.   transaction_commit ();
  88.   return (channel);
  89. }
  90.  
  91. static Tchannel
  92. open_file_noerror (const char * filename, ULONG attr, ULONG flags,
  93.            unsigned int mode)
  94. {
  95.   HFILE handle;
  96.   ULONG action;
  97.   if ((dos_open (((char *) filename), (&handle), (&action), 0, attr, flags,
  98.          (OS2_OPEN_MODE (mode)), 0))
  99.       != NO_ERROR)
  100.     return (NO_CHANNEL);
  101.   {
  102.     Tchannel channel = (OS2_make_channel (handle, mode));
  103.     if ((CHANNEL_TYPE (channel)) == channel_type_file)
  104.       return (channel);
  105.     OS_channel_close_noerror (channel);
  106.     return (NO_CHANNEL);
  107.   }
  108. }
  109.  
  110. Tchannel
  111. OS_open_load_file (const char * filename)
  112. {
  113.   return
  114.     (open_file_noerror
  115.      (filename,
  116.       FILE_NORMAL,
  117.       (OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW),
  118.       CHANNEL_READ));
  119. }
  120.  
  121. Tchannel
  122. OS_open_dump_file (const char * filename)
  123. {
  124.   return
  125.     (open_file_noerror
  126.      (filename,
  127.       FILE_NORMAL,
  128.       (OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW),
  129.       CHANNEL_WRITE));
  130. }
  131.  
  132. off_t
  133. OS_file_length (Tchannel channel)
  134. {
  135.   FILESTATUS3 buffer;
  136.   if ((CHANNEL_TYPE (channel)) != channel_type_file)
  137.     OS2_error_system_call (ERROR_INVALID_HANDLE, syscall_dos_query_file_info);
  138.   STD_API_CALL
  139.     (dos_query_file_info,
  140.      ((CHANNEL_HANDLE (channel)), FIL_STANDARD,
  141.       (&buffer), (sizeof (buffer))));
  142.   return (buffer.cbFile);
  143. }
  144.  
  145. off_t
  146. OS_file_position (Tchannel channel)
  147. {
  148.   return (set_file_pointer (channel, FILE_CURRENT, 0));
  149. }
  150.  
  151. void
  152. OS_file_set_position (Tchannel channel, off_t position)
  153. {
  154.   if ((set_file_pointer (channel, FILE_BEGIN, position)) != position)
  155.     OS2_error_anonymous ();
  156. }
  157.  
  158. static ULONG
  159. set_file_pointer (Tchannel channel, ULONG type, LONG distance)
  160. {
  161.   ULONG fp;
  162.   if ((CHANNEL_TYPE (channel)) != channel_type_file)
  163.     OS2_error_system_call (ERROR_INVALID_HANDLE, syscall_dos_set_file_ptr);
  164.   STD_API_CALL
  165.     (dos_set_file_ptr, ((CHANNEL_HANDLE (channel)), distance, type, (&fp)));
  166.   return (fp);
  167. }
  168.