From 52facee6d5c0480111d6344a8cec66a0d446a490 Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Sun, 26 Aug 2007 09:08:14 +0000 Subject: [PATCH] handleio git-svn-id: svn://kolibrios.org@613 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../trunk/clib/handleio/_clsewnt.c | 64 +++++ .../open watcom/trunk/clib/handleio/fsync.c | 74 +++++ .../open watcom/trunk/clib/handleio/hdlman.c | 257 ++++++++++++++++++ .../open watcom/trunk/clib/handleio/iomode.c | 80 ++++++ .../trunk/clib/handleio/iomodtty.c | 59 ++++ .../open watcom/trunk/clib/handleio/lseek.c | 55 ++++ .../trunk/clib/handleio/lseekwnt.c | 105 +++++++ .../open watcom/trunk/clib/handleio/qread.c | 68 +++++ .../open watcom/trunk/clib/handleio/qwrit.c | 132 +++++++++ .../trunk/clib/handleio/stiomode.c | 175 ++++++++++++ 10 files changed, 1069 insertions(+) create mode 100644 programs/develop/open watcom/trunk/clib/handleio/_clsewnt.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/fsync.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/hdlman.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/iomode.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/iomodtty.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/lseek.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/lseekwnt.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/qread.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/qwrit.c create mode 100644 programs/develop/open watcom/trunk/clib/handleio/stiomode.c diff --git a/programs/develop/open watcom/trunk/clib/handleio/_clsewnt.c b/programs/develop/open watcom/trunk/clib/handleio/_clsewnt.c new file mode 100644 index 0000000000..68262d7079 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/_clsewnt.c @@ -0,0 +1,64 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "fileacc.h" +//#include +#include "rtcheck.h" +#include "rtdata.h" +#include "iomode.h" +#include "seterrno.h" +//#include "defwin.h" +#include "close.h" + +int __close( int hid ) +{ + int is_closed; + int rc; + HANDLE h; + + __handle_check( hid, -1 ); + + is_closed = 0; + rc = 0; + h = __getOSHandle( hid ); + +// if( !is_closed && !CloseHandle( h ) ) +// { +// rc = __set_errno_nt(); +// } + __freePOSIXHandle( hid ); + __SetIOMode_nogrow( hid, 0 ); + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/fsync.c b/programs/develop/open watcom/trunk/clib/handleio/fsync.c new file mode 100644 index 0000000000..4c7b859ac0 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/fsync.c @@ -0,0 +1,74 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include +#if defined(__DOS__) || defined(__WINDOWS__) +#elif defined(__NT__) +#elif defined(__OS2__) +#elif defined(__NETWARE__) +#endif + +#include +#include "iomode.h" +#include "rtcheck.h" +#include "seterrno.h" + +/* +// take fsync from LIBC import file +*/ +#if !defined (_NETWARE_LIBC) + +_WCRTLINK int fsync( int handle ) +/*******************************/ +{ + int ret = 0; + + __handle_check( handle, -1 ); + + #if defined(__DOS__) || defined(__WINDOWS__) + #elif defined(__NT__) +// if( !FlushFileBuffers( __getOSHandle( handle ) ) ) +// { +// __set_errno_nt(); +// ret = -1; +// } + #elif defined(__OS2__) + #elif defined(__NETWARE__) + #else + #error Unknown target system + #endif + + return( ret ); +} + +#endif /* !defined (_NETWARE_LIBC) */ diff --git a/programs/develop/open watcom/trunk/clib/handleio/hdlman.c b/programs/develop/open watcom/trunk/clib/handleio/hdlman.c new file mode 100644 index 0000000000..da9b11d32a --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/hdlman.c @@ -0,0 +1,257 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: Handle manager routines. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "liballoc.h" +#include +#ifdef _M_IX86 + #include +#endif +#include +#include +#if defined(__OS2__) +#elif defined(__WINDOWS__) || defined(__NT__) +#endif +#include "iomode.h" +#include "fileacc.h" +#include "rtinit.h" +#include "seterrno.h" +#include "handleio.h" + +#undef __getOSHandle + +extern unsigned __NFiles; // the size of the iomode array +extern void __grow_iomode( int num ); + unsigned __NHandles = 0; + +#if defined(__NT__) + +HANDLE *__OSHandles = NULL; + +unsigned __growPOSIXHandles( unsigned num ) +{ + HANDLE *new2; + unsigned i; + + if( num > __NHandles ) + { + _AccessFList(); + if( __OSHandles == NULL ) + { + new2 = lib_malloc( num * sizeof( int ) ); + } + else + { + new2 = lib_realloc( __OSHandles, num * sizeof( int ) ); + } + if( new2 == NULL ) + { + __set_errno( ENOMEM ); + num = __NHandles; + } + else + { + for( i = __NHandles; i < num; i++ ) + { + new2[ i ] = NULL_HANDLE; + } + __OSHandles = new2; + __NHandles = num; + } + _ReleaseFList(); + } + return( __NHandles ); +} + +int __allocPOSIXHandle( HANDLE hdl ) +{ + int i; + + _AccessFList(); + for( i = 0; i < __NHandles; i++ ) + { + if( __OSHandles[i] == NULL_HANDLE ) break; + } + if( i >= __NHandles ) + { + // 20 -> (20+10+1) -> 31 + // 31 -> (31+15+1) -> 47 + // 47 -> (47+23+1) -> 71 + __growPOSIXHandles( i + (i >> 1) + 1 ); + // keep iomode array in sync + if( __NHandles > __NFiles ) __grow_iomode( __NHandles ); + for( ; i < __NHandles; i++ ) + { + if( __OSHandles[i] == NULL_HANDLE ) break; + } + } + if( i >= __NHandles ) + { + i = -1; + } else { + __OSHandles[i] = hdl; + } + _ReleaseFList(); + return( i ); +} + +void __freePOSIXHandle( int hid ) +{ + __OSHandles[ hid ] = NULL_HANDLE; +} + + +HANDLE __getOSHandle( int hid ) +{ + return( __OSHandles[ hid ] ); +} + + +int __setOSHandle( unsigned hid, HANDLE hdl ) +{ + // call the Win32 API for a standard file handle + switch( hid ) { + case STDIN_FILENO: +// SetStdHandle( STD_INPUT_HANDLE, hdl ); + break; + case STDOUT_FILENO: +// SetStdHandle( STD_OUTPUT_HANDLE, hdl ); + break; + case STDERR_FILENO: +// SetStdHandle( STD_ERROR_HANDLE, hdl ); + break; + } + if( hid < __NHandles ) + { + __OSHandles[ hid ] = hdl; + } + else + { + hid = (unsigned)-1; // this should never happen + } + return( hid ); +} + +HANDLE *__FakeHandles = 0; +static int __topFakeHandle = 0; + +HANDLE __NTGetFakeHandle( void ) +{ + HANDLE os_handle; + static DWORD fakeHandle = 0x80000000L; + _AccessFList(); + + fakeHandle++; + os_handle = (HANDLE)fakeHandle; + _ReleaseFList(); + return( os_handle ); +} + +// called from library startup code + +void __initPOSIXHandles( void ) +{ + HANDLE h; + + // __OSHandles = NULL; + // __NHandles = 0; + + __growPOSIXHandles( __NFiles ); + h = 0; //GetStdHandle( STD_INPUT_HANDLE ); + if( h == 0 || h == INVALID_HANDLE_VALUE ) { + h = (HANDLE)__NTGetFakeHandle(); + } + __allocPOSIXHandle( h ); // should return 0==STDIN_FILENO + h = 0; //GetStdHandle( STD_OUTPUT_HANDLE ); + if( h == 0 || h == INVALID_HANDLE_VALUE ) { + h = (HANDLE)__NTGetFakeHandle(); + } + __allocPOSIXHandle( h ); // should return 1==STDOUT_FILENO + h = 0; //GetStdHandle( STD_ERROR_HANDLE ); + if( h == 0 || h == INVALID_HANDLE_VALUE ) { + h = (HANDLE)__NTGetFakeHandle(); + } + __allocPOSIXHandle( h ); // should return 3==STDERR_FILENO +} + +static void __finiPOSIXHandles( void ) +{ + if( __OSHandles != NULL ) { + lib_free( __OSHandles ); + __OSHandles = NULL; + } + if( __FakeHandles != NULL ) + { + int i; + for( i = 0 ; i < __topFakeHandle ; i++ ) + { + // CloseHandle( __FakeHandles[i] ); + } + lib_free( __FakeHandles ); + __FakeHandles = 0; + } +} + +AYI( __finiPOSIXHandles, INIT_PRIORITY_LIBRARY-1 ) + +#endif + + +void __set_handles( int num ) +{ + __NHandles = num; +} + +_WCRTLINK int _grow_handles( int num ) +{ + if( num > __NHandles ) + { + #if defined(MSDOS) + #elif defined( __OS2_286__ ) + #elif defined( __WARP__ ) + #elif defined(__WINDOWS__) + #elif defined(__NT__) + { + num = __growPOSIXHandles( num ); + } + #elif defined(__NETWARE__) + #elif defined(__UNIX__) + #endif + + if( num > __NFiles ) { + __grow_iomode( num ); // sets new __NFiles if successful + } + __NHandles = num; + } + return( __NHandles ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/iomode.c b/programs/develop/open watcom/trunk/clib/handleio/iomode.c new file mode 100644 index 0000000000..73bbd88a36 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/iomode.c @@ -0,0 +1,80 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: io_mode handle information array manipulation +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include "rtdata.h" +//#include "liballoc.h" +#include "fileacc.h" +#include "rtinit.h" +#include "seterrno.h" +#include "iomode.h" + +unsigned __NFiles = _NFILES; /* maximum # of files we can open */ + +#if defined(__NETWARE__) +#error NO IO MODE MANAGER UNDER NETWARE +#endif + +#if !defined(__UNIX__) + +unsigned _HUGEDATA __init_mode[_NFILES] = { /* file mode information (flags) */ + _READ, /* stdin */ + _WRITE, /* stdout */ + _WRITE, /* stderr */ + _READ|_WRITE, /* stdaux */ + _WRITE /* stdprn */ +}; + +unsigned *__io_mode = __init_mode; /* initially points to static array */ + +unsigned __GetIOMode( int handle ) +{ + if( handle >= __NFiles ) + { + return( 0 ); + } + return( __io_mode[handle] ); +} + +void __SetIOMode_nogrow( int handle, unsigned value ) +{ + if( handle < __NFiles ) + { + __io_mode[handle] = value; /* we're closing it; smite _INITIALIZED */ + } +} + +#endif + diff --git a/programs/develop/open watcom/trunk/clib/handleio/iomodtty.c b/programs/develop/open watcom/trunk/clib/handleio/iomodtty.c new file mode 100644 index 0000000000..9543f778dd --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/iomodtty.c @@ -0,0 +1,59 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: check tty for standard handles in the iomode array +* +****************************************************************************/ + +#include "variety.h" +#include +#include "rtdata.h" +//#include "liballoc.h" +#include "fileacc.h" +#include "handleio.h" +#include "iomode.h" + +#define _INITIALIZED _DYNAMIC + +void __ChkTTYIOMode( int handle ) +{ + if( handle < NUM_STD_STREAMS && !(__io_mode[handle] & _INITIALIZED) ) + { + __io_mode[handle] |= _INITIALIZED; + if( isatty( handle ) ) + { + __io_mode[handle] |= _ISTTY; + } + } +} + +// For F77 to call + +unsigned __IOMode( int handle ) +{ + __ChkTTYIOMode( handle ); + return( __GetIOMode( handle ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/lseek.c b/programs/develop/open watcom/trunk/clib/handleio/lseek.c new file mode 100644 index 0000000000..3f0813d73a --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/lseek.c @@ -0,0 +1,55 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: lseek wrapper with positive -> extend file check +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#ifdef __NT__ +#include +#endif +#include "iomode.h" +#include "rtcheck.h" +#include "seterrno.h" +#include "lseek.h" + +_WCRTLINK long lseek( int handle, long offset, int origin ) +{ + unsigned iomode_flags; + + __handle_check( handle, -1 ); + + /*** Set the _FILEEXT iomode_flags bit if positive offset ***/ + iomode_flags = __GetIOMode( handle ); + + if( offset > 0 && !(iomode_flags & _APPEND) ) + __SetIOMode( handle, iomode_flags | _FILEEXT ); + return( __lseek( handle, offset, origin ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/lseekwnt.c b/programs/develop/open watcom/trunk/clib/handleio/lseekwnt.c new file mode 100644 index 0000000000..0d27479be3 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/lseekwnt.c @@ -0,0 +1,105 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License.* +* ======================================================================== +* +* Description: low level lseek without file extend for Windows NT +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "iomode.h" +#include "rtcheck.h" +#include "seterrno.h" +#include "lseek.h" +#include "handleio.h" + +/* + DWORD SetFilePointer( + HANDLE hFile, // handle to file + LONG lDistanceToMove, // bytes to move pointer + PLONG lpDistanceToMoveHigh, // bytes to move pointer + DWORD dwMoveMethod // starting point + ); + */ + +#ifndef INVALID_SET_FILE_POINTER +#define INVALID_SET_FILE_POINTER 0xFFFFFFFF +#endif + +typedef struct +{ + char *name; + unsigned int offset; +}__file_handle; + +typedef struct +{ DWORD attr; + DWORD flags; + DWORD cr_time; + DWORD cr_date; + DWORD acc_time; + DWORD acc_date; + DWORD mod_time; + DWORD mod_date; + DWORD size; + DWORD size_high; +} FILEINFO; + +int _stdcall get_fileinfo(const char *name,FILEINFO* pinfo); + +_WCRTLINK long __lseek( int hid, long offset, int origin ) +{ + __file_handle *fh; + long rc; + + __handle_check( hid, -1 ); + fh = (__file_handle*) __getOSHandle( hid ); + + switch(origin) + { + case SEEK_SET: + rc = offset; + break; + case SEEK_CUR: + rc = fh->offset + offset; + break; + case SEEK_END: + { + FILEINFO info; + get_fileinfo(fh->name,&info); + rc = offset + info.size; + break; + } + default: + return -1; + }; + + fh->offset = rc; + + return( rc ); +} + diff --git a/programs/develop/open watcom/trunk/clib/handleio/qread.c b/programs/develop/open watcom/trunk/clib/handleio/qread.c new file mode 100644 index 0000000000..51365cac07 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/qread.c @@ -0,0 +1,68 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include "iomode.h" +#include "rtcheck.h" +#include "seterrno.h" +#include "qread.h" +#include + +typedef struct +{ + char *name; + unsigned int offset; +}__file_handle; + + +int _stdcall read_file (const char *name,char *buff,unsigned offset, unsigned count,unsigned *reads); + +int __qread( int handle, void *buffer, unsigned len ) +{ + __file_handle *fh; + unsigned amount_read=0; + + __handle_check( handle, -1 ); + fh = (__file_handle*) __getOSHandle( handle ); + + if(read_file(fh->name,buffer,fh->offset,len,&amount_read)) + { + if ( amount_read == 0) + return (-1); + + } + fh->offset+=amount_read; + return( amount_read ); +}; + + + diff --git a/programs/develop/open watcom/trunk/clib/handleio/qwrit.c b/programs/develop/open watcom/trunk/clib/handleio/qwrit.c new file mode 100644 index 0000000000..33cfec3244 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/qwrit.c @@ -0,0 +1,132 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include +#include + +#include "iomode.h" +#include "fileacc.h" +#include "rtcheck.h" +#include "rtdata.h" +#include "seterrno.h" +#include "qwrite.h" +#include "liballoc.h" + + +/* + Use caution when setting the file pointer in a multithreaded + application. You must synchronize access to shared resources. For + example, an application whose threads share a file handle, update the + file pointer, and read from the file must protect this sequence by + using a critical section object or a mutex object. + */ + +typedef struct +{ DWORD attr; + DWORD flags; + DWORD cr_time; + DWORD cr_date; + DWORD acc_time; + DWORD acc_date; + DWORD mod_time; + DWORD mod_date; + DWORD size; + DWORD size_high; +} FILEINFO; + +typedef struct +{ + char *name; + unsigned int offset; +}__file_handle; + +int _stdcall get_fileinfo(const char *name,FILEINFO* pinfo); +int _stdcall write_file(const char *name,const void *buff,unsigned offset,unsigned count,unsigned *writes); +char* getfullpath(const char* path); + +int __qwrite( int handle, const void *buffer, unsigned len ) +{ + int atomic; + __file_handle *fh; + unsigned len_written; + + + __handle_check( handle, -1 ); + + fh = (__file_handle*) __getOSHandle( handle ); + + atomic = 0; + if( __GetIOMode( handle ) & _APPEND ) + { + FILEINFO info; + + _AccessFileH( handle ); + atomic = 1; + get_fileinfo(fh->name,&info); + fh->offset = info.size; + }; + + if(write_file(fh->name,buffer,fh->offset,len,&len_written)) + { + if ( len_written == 0) + { + if( atomic == 1 ) + _ReleaseFileH( handle ); + + return (-1); + }; + }; + + fh->offset+=len_written; + + + if( atomic == 1 ) + { + _ReleaseFileH( handle ); + } + return( len_written ); +} + +/******************** +int write_once(const char *name, void *buffer, unsigned len) +{ char *path; + unsigned count; + + path= getfullpath(name); + write_file(path,buffer,0,len,&count); + lib_free(path); + return count; + +} + +*******************/ diff --git a/programs/develop/open watcom/trunk/clib/handleio/stiomode.c b/programs/develop/open watcom/trunk/clib/handleio/stiomode.c new file mode 100644 index 0000000000..32f4391a91 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/stiomode.c @@ -0,0 +1,175 @@ +/**************************************************************************** +* +* Open Watcom Project +* +* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. +* +* ======================================================================== +* +* This file contains Original Code and/or Modifications of Original +* Code as defined in and that are subject to the Sybase Open Watcom +* Public License version 1.0 (the 'License'). You may not use this file +* except in compliance with the License. BY USING THIS FILE YOU AGREE TO +* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is +* provided with the Original Code and Modifications, and is also +* available at www.sybase.com/developer/opensource. +* +* The Original Code and all software distributed under the License are +* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER +* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM +* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR +* NON-INFRINGEMENT. Please see the License for the specific language +* governing rights and limitations under the License. +* +* ======================================================================== +* +* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include "rtdata.h" +#include "liballoc.h" +#include "fileacc.h" +#include "rtinit.h" +#include "seterrno.h" +#include "iomode.h" +#include "handleio.h" + +#ifdef DLHEAP + +void* _cdecl dlmalloc(size_t); +void _cdecl dlfree(void*); +void _cdecl mf_init(); + +#define malloc dlmalloc +#define free dlfree +#define realloc dlrealloc + +#define lib_malloc dlmalloc +#define lib_free dlfree +#define lib_realloc dlrealloc + +#endif + +#if defined(__NETWARE__) +#error NO IO MODE MANAGER UNDER NETWARE +#endif + +extern unsigned _HUGEDATA __init_mode[_NFILES]; +extern unsigned __NFiles; /* maximum # of files we can open */ + +static unsigned _init_NFiles; // original __NFiles value; + +void __grow_iomode( int num ) +{ + unsigned *new; + + _AccessIOB(); + if( __io_mode == __init_mode ) + { + _init_NFiles = __NFiles; + new = (unsigned *) lib_malloc( num * sizeof( unsigned ) ); + if( new != NULL ) { + memcpy( new, __init_mode, __NFiles * sizeof(unsigned) ); + } + } + else + { + #if defined(__NETWARE__) + #else + new = (unsigned *) lib_realloc( __io_mode, num * sizeof( unsigned ) ); + #endif + } + if( new == NULL ) + { + __set_errno( ENOMEM ); + } + else + { + memset( &new[__NFiles], 0, (num-__NFiles)*sizeof(unsigned) ); + __io_mode = new; + __NFiles = num; + } + _ReleaseIOB(); +} + +void __shrink_iomode( void ) +{ + _AccessIOB(); + // free any malloc'd iomode array + if( __io_mode != __init_mode ) + { + lib_free( __io_mode ); + __io_mode = __init_mode; + __NFiles = _init_NFiles; + } + _ReleaseIOB(); +} + +AYI(__shrink_iomode,INIT_PRIORITY_IOSTREAM); + + +#if defined(__WARP__) + +extern unsigned __NHandles; + +static void __preinit_iomode_os2(void) +{ + LONG req_count; + ULONG curr_max_fh; + APIRET rc; + + // Ensure that the clib and OS file handle limits match + req_count = 0; + rc = DosSetRelMaxFH( &req_count, &curr_max_fh ); + if( rc == 0 ) { + __grow_iomode( curr_max_fh ); + __NHandles = curr_max_fh; // same as __set_handles + } + +} + +AXI( __preinit_iomode_os2, INIT_PRIORITY_RUNTIME ); + +#endif + +#define _INITIALIZED _DYNAMIC + +signed __SetIOMode( int handle, unsigned value ) +{ + int i; + + if( handle >= __NFiles ) + { + i = __NFiles; // 20 -> (20+10+1) -> 31 + // 31 -> (31+15+1) -> 47 + // 47 -> (47+23+1) -> 71 + __grow_iomode( i + (i > 1) + 1 ); + } + if( handle >= __NFiles ) + { + // return an error indication (errno should be set to ENOMEM) + return( -1 ); + } + else + { + if( value != 0 ) + { + __ChkTTYIOMode( handle ); + __io_mode[handle] = value | _INITIALIZED; + } + else + { + __io_mode[handle] = value; /* we're closing it; smite _INITIALIZED */ + } + return( handle ); + } +}