Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 612 → Rev 613

/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 <stdio.h>
#include <errno.h>
#include "fileacc.h"
//#include <windows.h>
#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 );
}
/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 <errno.h>
#if defined(__DOS__) || defined(__WINDOWS__)
#elif defined(__NT__)
#elif defined(__OS2__)
#elif defined(__NETWARE__)
#endif
 
#include <unistd.h>
#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) */
/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 <stdio.h>
#include <stdlib.h>
#include "liballoc.h"
#include <string.h>
#ifdef _M_IX86
#include <i86.h>
#endif
#include <unistd.h>
#include <errno.h>
#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 );
}
/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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#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
 
/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 <unistd.h>
#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 ) );
}
/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 <stdio.h>
#include <unistd.h>
#ifdef __NT__
#include <windows.h>
#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 ) );
}
/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 <stdio.h>
#include <unistd.h>
#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 );
}
 
/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 <stdio.h>
 
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 );
};
 
 
 
/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 <stdio.h>
#include <errno.h>
 
#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;
}
 
*******************/
/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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#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 );
}
}