diff --git a/programs/develop/open watcom/trunk/clib/direct/gtcwdwnt.c b/programs/develop/open watcom/trunk/clib/direct/gtcwdwnt.c new file mode 100644 index 0000000000..1a9281f242 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/direct/gtcwdwnt.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 "widechar.h" +#include +#include +#include +#include +#include +#include +#include +#include "rtdata.h" +#include "seterrno.h" +#include "liballoc.h" + +int _stdcall sys_getcwd(char *buf, size_t size); + +_WCRTLINK CHAR_TYPE *__F_NAME(getcwd,_wgetcwd)( CHAR_TYPE *buf, size_t size ) +{ + char path[1024]; + size_t realsize; + + /*** Get the current directory ***/ + realsize = sys_getcwd( path, 1024 ); + + if( realsize == 0 ) { + return( NULL ); + } + if( buf == NULL ) { + buf = lib_malloc( max(size,realsize) * CHARSIZE ); + if( buf == NULL ) { + __set_errno( ENOMEM ); + return( NULL ); + } + } else { + if( realsize > size ) { + __set_errno( ERANGE ); + return( NULL ); + } + } + + memcpy( buf, path, realsize*CHARSIZE ); + return( buf ); +} diff --git a/programs/develop/open watcom/trunk/clib/environ/clearenv.c b/programs/develop/open watcom/trunk/clib/environ/clearenv.c new file mode 100644 index 0000000000..c0f8f42247 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/clearenv.c @@ -0,0 +1,69 @@ +/**************************************************************************** +* +* 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: Implementation of clearenv() - clear the environment. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "liballoc.h" +#include "rtdata.h" + +/* Note - clearenv() is always called at library exit */ + +_WCRTLINK int (clearenv)( void ) +{ +#ifndef __NETWARE__ + char **envp; + char *env_str; + int index; + + if( _RWD_environ != NULL ) { + for( envp = _RWD_environ; env_str = *envp; ++envp ) { + if( _RWD_env_mask != NULL ) { + index = envp - _RWD_environ; + if( _RWD_env_mask[ index ] != 0 ) { + lib_free( (void *)env_str ); + } + *envp = NULL; + } + } + if( _RWD_env_mask != NULL ) { + lib_free( _RWD_environ ); + } + envp = lib_malloc( sizeof(char *) + sizeof(char) ); + if( envp == NULL ) return( -1 ); + _RWD_environ = envp; + *_RWD_environ = NULL; + _RWD_env_mask = ((char *)envp) + sizeof(char *); + *_RWD_env_mask = 0; + } +#endif + return( 0 ); /* success */ +} diff --git a/programs/develop/open watcom/trunk/clib/environ/environ.c b/programs/develop/open watcom/trunk/clib/environ/environ.c new file mode 100644 index 0000000000..cddf4856ae --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/environ.c @@ -0,0 +1,51 @@ +/**************************************************************************** +* +* 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: Environment pointers. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "rtdata.h" +#include "rtinit.h" + +_WCRTLINKD char ** _WCNEAR environ; /* pointer to environment table */ +_WCRTLINKD wchar_t ** _WCNEAR _wenviron; /* pointer to wide char environment */ +char * _WCNEAR __env_mask; /* ptr to char array of flags */ + +extern void __setenvp( void ); + +AXI( __setenvp, INIT_PRIORITY_LIBRARY ) + +#if !defined(__NETWARE__) && !defined(__LINUX__) +/* Environment needs to be freed after files are shut down; __MkTmpFile() + * is used to delete temp files and needs access to environment. + */ +extern void __freeenvp( void ); +AYI( __freeenvp, INIT_PRIORITY_LIBRARY - 1 ) +#endif diff --git a/programs/develop/open watcom/trunk/clib/environ/filestr.h b/programs/develop/open watcom/trunk/clib/environ/filestr.h new file mode 100644 index 0000000000..303789c9e9 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/filestr.h @@ -0,0 +1,54 @@ +/**************************************************************************** +* +* 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! +* +****************************************************************************/ + + +#ifndef _FILNMTYP_H_INCLUDED +#define _FILNMTYP_H_INCLUDED + +#include "variety.h" +#include + + +_WCRTLINK extern char * __FormPosixHandleStr( void ); +_WCRTLINK extern void __ParsePosixHandleStr( void ); + +_WCRTLINK extern wchar_t * __wFormPosixHandleStr( void ); +_WCRTLINK extern void __wParsePosixHandleStr( void ); + + +#ifdef __NT__ + #define __USE_POSIX_HANDLE_STRINGS +#else + #undef __USE_POSIX_HANDLE_STRINGS +#endif + + +#endif diff --git a/programs/develop/open watcom/trunk/clib/environ/getenv.c b/programs/develop/open watcom/trunk/clib/environ/getenv.c new file mode 100644 index 0000000000..c070c4cd2f --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/getenv.c @@ -0,0 +1,111 @@ +/**************************************************************************** +* +* 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: Implementation of getenv(). +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include +#include "rtdata.h" +#ifdef __WIDECHAR__ + #include "wenviron.h" +#endif + +#if defined(__UNIX__) + #ifdef __WIDECHAR__ + #define CMP_FUNC wcsncmp + #else + #define CMP_FUNC strncmp + #endif +#else + #define CMP_FUNC _wcsnicmp +#endif + +#if !defined(__UNIX__) && !defined(__WIDECHAR__) && !defined(__NETWARE__) + +_WCRTLINK char *getenv( const char *name ) +{ + char **envp; + char *p; + + /*** Find the environment string ***/ + __ptr_check( name, 0 ); + envp = _RWD_environ; + if( (envp != NULL) && (name != NULL) ) { + for( ; p = *envp; ++envp ) { + const char *s = name; + + while( *p != '\0' ) { /* simple check is sufficient for p, not s */ + if ( _mbterm( s ) ) { + if( *p == '=' ) return( p + 1 ); + break; + } + if ( _mbctoupper( _mbsnextc( p ) ) != _mbctoupper( _mbsnextc( s ) ) ) + break; + p = _mbsinc( p ); /* skip over character */ + s = _mbsinc( s ); /* skip over character */ + } + } + } + return( NULL ); /* not found */ +} + +#else + +_WCRTLINK CHAR_TYPE *__F_NAME(getenv,_wgetenv)( const CHAR_TYPE *name ) +{ +#ifdef __NETWARE__ + name = name; +#else + CHAR_TYPE **envp; + CHAR_TYPE *p; + int len; + + #ifdef __WIDECHAR__ + if( _RWD_wenviron == NULL ) __create_wide_environment(); + #endif + + /*** Find the environment string ***/ + __ptr_check( name, 0 ); + envp = __F_NAME(_RWD_environ,_RWD_wenviron); + if( (envp != NULL) && (name != NULL) ) { + len = __F_NAME(strlen,wcslen)( name ); + for( ; p = *envp; ++envp ) { + if( CMP_FUNC( p, name, len ) == 0 ) { + if( p[len] == __F_NAME('=',L'=') ) return( &p[len+1] ); + } + } + } +#endif + return( NULL ); /* not found */ +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/environ/gtcmd.c b/programs/develop/open watcom/trunk/clib/environ/gtcmd.c new file mode 100644 index 0000000000..f888af7dd1 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/gtcmd.c @@ -0,0 +1,85 @@ +/**************************************************************************** +* +* 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: Multi-platform implementation of getcmd() and _bgetcmd(). +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#if defined(__NT__) +#elif defined(__OS2__) +#endif + +#if !defined( __NETWARE__ ) || defined( _NETWARE_LIBC ) + #include +#endif + +extern char *_LpCmdLine; + +_WCRTLINK int _bgetcmd( char *buffer, int len ) +{ + int cmdlen; + char *cmd; + char *tmp; + + if( buffer && (len > 0) ) + *buffer = '\0'; + + cmd = _LpCmdLine; + if( !cmd ) + return( 0 ); + + while( *cmd == ' ' || *cmd == '\t' ) + ++cmd; + + for( cmdlen = 0, tmp = cmd; *tmp; ++tmp, ++cmdlen ) + ; + + if( !buffer || (len <= 0) ) + return( cmdlen ); + + len--; + len = (len < cmdlen) ? len : cmdlen; + + while( len ) { + *buffer++ = *cmd++; + --len; + } + buffer[len] = '\0'; + + return( cmdlen ); +} /* _bgetcmd() */ + + +_WCRTLINK char *getcmd( char *buffer ) +{ + _bgetcmd( buffer, INT_MAX ); + return( buffer ); +} /* getcmd() */ diff --git a/programs/develop/open watcom/trunk/clib/environ/searchen.c b/programs/develop/open watcom/trunk/clib/environ/searchen.c new file mode 100644 index 0000000000..f1d6314ca1 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/searchen.c @@ -0,0 +1,151 @@ +/**************************************************************************** +* +* 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: This function searches for the specified file in the +* 1) current directory or, failing that, +* 2) the paths listed in the specified environment variable +* until it finds the first occurrence of the file. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#undef __INLINE_FUNCTIONS__ +#include +#include +#include +#ifndef __UNIX__ + #include +#endif +#include +#include "rtdata.h" +#include "seterrno.h" +#ifdef __WIDECHAR__ + #include "wenviron.h" +#endif + +#if defined(__UNIX__) + #ifdef __WIDECHAR__ + #define PATH_SEPARATOR L'/' + #define LIST_SEPARATOR L':' + #else + #define PATH_SEPARATOR '/' + #define LIST_SEPARATOR ':' + #endif +#else + #ifdef __WIDECHAR__ + #define PATH_SEPARATOR L'\\' + #define LIST_SEPARATOR L';' + #else + #define PATH_SEPARATOR '\\' + #define LIST_SEPARATOR ';' + #endif +#endif + + +_WCRTLINK void __F_NAME(_searchenv,_wsearchenv)( const CHAR_TYPE *name, const CHAR_TYPE *env_var, CHAR_TYPE *buffer ) +{ + CHAR_TYPE *p, *p2; + int prev_errno; + size_t len; + +#ifdef __WIDECHAR__ + if( _RWD_wenviron == NULL ) __create_wide_environment(); +#endif + + prev_errno = _RWD_errno; + if( __F_NAME(access,_waccess)( name, F_OK ) == 0 ) { + p = buffer; /* JBS 90/3/30 */ + len = 0; /* JBS 04/1/06 */ + for( ;; ) { + if( name[0] == PATH_SEPARATOR ) break; + if( name[0] == __F_NAME('.',L'.') ) break; +#ifndef __UNIX__ + if( name[0] == __F_NAME('/',L'/') ) break; + if( (name[0] != __F_NAME('\0',L'\0')) && (name[1] == __F_NAME(':',L':')) ) break; +#endif + __F_NAME(getcwd,_wgetcwd)( buffer, _MAX_PATH ); + len = __F_NAME(strlen,wcslen)( buffer ); + p = &buffer[ len ]; + if( p[-1] != PATH_SEPARATOR ) { + if( len < (_MAX_PATH - 1) ) { + *p++ = PATH_SEPARATOR; + len++; + } + } + break; + } + *p = __F_NAME('\0',L'\0'); + __F_NAME(strncat,wcsncat)( p, name, (_MAX_PATH - 1) - len ); + return; + } + p = __F_NAME(getenv,_wgetenv)( env_var ); + if( p != NULL ) { + for( ;; ) { + if( *p == __F_NAME('\0',L'\0') ) break; + p2 = buffer; + len = 0; /* JBS 04/1/06 */ + while( *p ) { + if( *p == LIST_SEPARATOR ) break; + if( *p != __F_NAME('"',L'"') ) { + if( len < (_MAX_PATH-1) ) { + *p2++ = *p; /* JBS 00/9/29 */ + len++; + } + } + p++; + } + /* check for zero-length prefix which represents CWD */ + if( p2 != buffer ) { /* JBS 90/3/30 */ + if( p2[-1] != PATH_SEPARATOR +#ifndef __UNIX__ + && p2[-1] != __F_NAME('/','/') + && p2[-1] != __F_NAME(':',':') +#endif + ) { + if( len < (_MAX_PATH - 1) ) { + *p2++ = PATH_SEPARATOR; + len++; + } + } + *p2 = __F_NAME('\0',L'\0'); + len += __F_NAME(strlen,wcslen)( name );/* JBS 04/12/23 */ + if( len < _MAX_PATH ) { + __F_NAME(strcat,wcscat)( p2, name ); + /* check to see if file exists */ + if( __F_NAME(access,_waccess)( buffer, 0 ) == 0 ) { + __set_errno( prev_errno ); + return; + } + } + } + if( *p == '\0' ) break; + ++p; + } + } + buffer[0] = __F_NAME( '\0',L'\0' ); +} diff --git a/programs/develop/open watcom/trunk/clib/environ/setenvp.c b/programs/develop/open watcom/trunk/clib/environ/setenvp.c new file mode 100644 index 0000000000..bcbc16d146 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/environ/setenvp.c @@ -0,0 +1,127 @@ +/**************************************************************************** +* +* 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: Routines to create/destroy clib copy of OS environment. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include "liballoc.h" +#include +#include "filestr.h" +#include "rtdata.h" + +extern char _WCI86FAR *_Envptr; + +static char *_free_ep; + +#define allocate lib_malloc + +void __setenvp( void ) +{ +#if defined(__NETWARE__) + // no environment support +#elif defined(__LINUX__) +#else + char _WCI86FAR *startp; + char _WCI86FAR *p; + char *ep; + char *my_env_mask; + char **my_environ; + int count; + size_t ep_size; + size_t env_size; + + /* if we are already initialized, then return */ + if( _RWD_environ != NULL ) return; /* 10-jun-90 */ + + // env_size = sys_get_environ(0,0)+1; + // _Envptr = lib_malloc(env_size); + // sys_get_environ(_Envptr, env_size) + + startp = _Envptr; + + count = 0; + p = startp; + while( *p ) { + while( *++p ); + ++count; + ++p; + } + ep_size = p - startp; + if( ep_size == 0 ) { + ep_size = 1; + } + ep = (char *)allocate( ep_size ); + if( ep ) { + env_size = (count + 1) * sizeof(char *) + count * sizeof(char); + my_environ = (char **)allocate( env_size ); + if( my_environ ) { + _RWD_environ = my_environ; + p = startp; + _free_ep = ep; + while( *p ) { + *my_environ++ = ep; + while( *ep++ = *p++ ) + ; + } + *my_environ++ = NULL; + _RWD_env_mask = my_env_mask = (char *) my_environ; + for( ; count; count-- ) + *my_env_mask++ = 0; + } else { + lib_free( ep ); + } + } + + /*** Handle the C_FILE_INFO entry ***/ + #ifdef __USE_POSIX_HANDLE_STRINGS + // __ParsePosixHandleStr(); + #endif +#endif +} + +#if !defined(__NETWARE__) && !defined(__LINUX__) + +void __freeenvp( void ) +{ + clearenv(); + if( _RWD_environ ) { + lib_free( _RWD_environ ); + _RWD_environ = NULL; + } + if( _free_ep ) { + lib_free( _free_ep ); + _free_ep = NULL; + } +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/file/remove.c b/programs/develop/open watcom/trunk/clib/file/remove.c new file mode 100644 index 0000000000..1a2531e382 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/file/remove.c @@ -0,0 +1,48 @@ +/**************************************************************************** +* +* 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 "widechar.h" +#ifndef __UNIX__ + #include +#endif +#include +#ifdef __UNIX__ + #include +#endif + + +_WCRTLINK int __F_NAME(remove,_wremove)( CHAR_TYPE const *filename ) +{ + __ptr_check( filename, 0 ); + return( __F_NAME(unlink,_wunlink)( filename ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/close.c b/programs/develop/open watcom/trunk/clib/handleio/close.c new file mode 100644 index 0000000000..3462c7862f --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/close.c @@ -0,0 +1,50 @@ +/**************************************************************************** +* +* 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 "rtdata.h" +#include "rtcheck.h" +#include "seterrno.h" +#include +#include "close.h" + +_WCRTLINK int close( int handle ) +{ + int rc; + + __handle_check( handle, -1 ); + + rc = __close( handle ); + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/handleio/read.c b/programs/develop/open watcom/trunk/clib/handleio/read.c new file mode 100644 index 0000000000..a41d654950 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/read.c @@ -0,0 +1,135 @@ +/**************************************************************************** +* +* 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 "iomode.h" +#include "fileacc.h" +#include "rtcheck.h" +#include "rtdata.h" +#include "seterrno.h" +#include "lseek.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); + +_WCRTLINK int read( int handle, void *buf, unsigned len ) +{ + unsigned read_len, total_len; + unsigned reduce_idx, finish_idx; + unsigned iomode_flags; + char *buffer = buf; + BOOL rc; + HANDLE h; + unsigned amount_read; + __file_handle *fh; + + __handle_check( handle, -1 ); + __ChkTTYIOMode( handle ); + iomode_flags = __GetIOMode( handle ); + if( iomode_flags == 0 ) + { + __set_errno( EBADF ); + return( -1 ); + } + if( !(iomode_flags & _READ) ) + { + __set_errno( EACCES ); /* changed from EBADF to EACCES 23-feb-89 */ + return( -1 ); + } + + + fh = (__file_handle*) __getOSHandle( handle ); + + if( iomode_flags & _BINARY ) /* if binary mode */ + { + + if(read_file(fh->name,buffer,fh->offset,len,&amount_read)) + { + if ( amount_read == 0) + return (-1); + } + fh->offset+=amount_read; + total_len = amount_read; + } + else + { + total_len = 0; + read_len = len; + do + { + if(read_file(fh->name,buffer,fh->offset,len,&amount_read)) + { + if( amount_read == 0 ) + { /* EOF */ + break; + } + } + reduce_idx = 0; + finish_idx = reduce_idx; + for( ; reduce_idx < amount_read; ++reduce_idx ) + { + if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ + { + __lseek( handle, + ((long)reduce_idx - (long)amount_read)+1L, + SEEK_CUR ); + total_len += finish_idx; + return( total_len ); + } + if( buffer[ reduce_idx ] != '\r' ) + { + buffer[ finish_idx++ ] = buffer[ reduce_idx ]; + }; + } + + total_len += finish_idx; + buffer += finish_idx; + read_len -= finish_idx; + if( iomode_flags & _ISTTY ) + { + break; /* 04-feb-88, FWC */ + } + } while( read_len != 0 ); + } + return( total_len ); +} + diff --git a/programs/develop/open watcom/trunk/clib/handleio/unlnkwnt.c b/programs/develop/open watcom/trunk/clib/handleio/unlnkwnt.c new file mode 100644 index 0000000000..8a8f4f7704 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/handleio/unlnkwnt.c @@ -0,0 +1,46 @@ +/**************************************************************************** +* +* 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 "widechar.h" +#include "seterrno.h" +#include + +int _stdcall sys_delete_file(const CHAR_TYPE *filename); + +_WCRTLINK int __F_NAME(unlink,_wunlink)( const CHAR_TYPE *filename ) +{ + if( sys_delete_file( filename )) { + return( -1 ); + } + return( 0 ); +} diff --git a/programs/develop/open watcom/trunk/clib/kanji/mbdtoupp.c b/programs/develop/open watcom/trunk/clib/kanji/mbdtoupp.c new file mode 100644 index 0000000000..6748f56e58 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/kanji/mbdtoupp.c @@ -0,0 +1,41 @@ +/**************************************************************************** +* +* 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 + + +_WCRTLINK unsigned int _mbdtoupper( unsigned int ch ) +{ + return( ch >= 0x8281 && ch <= 0x829A ? ( ch - 0x21 ) : ch ); +} diff --git a/programs/develop/open watcom/trunk/clib/mbyte/mbcupper.c b/programs/develop/open watcom/trunk/clib/mbyte/mbcupper.c new file mode 100644 index 0000000000..f04a22e0d7 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/mbyte/mbcupper.c @@ -0,0 +1,52 @@ +/**************************************************************************** +* +* 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 "mbchar.h" +#include + +/** + * Convert a single character to uppercase. Equivalent to toupper(). + */ + +_WCRTLINK unsigned int _mbctoupper( unsigned int ch ) +{ + if( ch & 0xFF00 ) { + if( __MBCodePage == 932 ) { + return( _mbdtoupper( ch ) ); + } + return( ch ); // we don't know what to do - just return it + } else { + return( toupper( ch ) ); + } +} diff --git a/programs/develop/open watcom/trunk/clib/mbyte/mbsinc.c b/programs/develop/open watcom/trunk/clib/mbyte/mbsinc.c new file mode 100644 index 0000000000..a9ec3c04d8 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/mbyte/mbsinc.c @@ -0,0 +1,49 @@ +/**************************************************************************** +* +* 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 "farfunc.h" + + + +/**** +***** Return the address of the next character in a string. +****/ + +_WCRTLINK unsigned char _FFAR *_NEARFAR((_mbsinc),(_fmbsinc))( const unsigned char _FFAR *string ) +{ + if( _ismbblead(*string) && *(string+1)!='\0' ) /* valid DBCS char? */ + return( (unsigned char _FFAR*) (string+2) );/* yes, skip two bytes */ + else + return( (unsigned char _FFAR*) (string+1) );/* no, just skip one */ +} diff --git a/programs/develop/open watcom/trunk/clib/mbyte/mbsnextc.c b/programs/develop/open watcom/trunk/clib/mbyte/mbsnextc.c new file mode 100644 index 0000000000..6cb3945e90 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/mbyte/mbsnextc.c @@ -0,0 +1,43 @@ +/**************************************************************************** +* +* 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: Return the value of the next character in the string. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "farfunc.h" + + +_WCRTLINK unsigned int _NEARFAR(_mbsnextc,_fmbsnextc)( const unsigned char _FFAR *string ) +{ + unsigned int retval = string[0]; + if( _ismbblead(retval) ) + retval = (retval << 8) | string[1]; + return( retval ); +} diff --git a/programs/develop/open watcom/trunk/clib/mbyte/mbterm.c b/programs/develop/open watcom/trunk/clib/mbyte/mbterm.c new file mode 100644 index 0000000000..c11aed29bd --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/mbyte/mbterm.c @@ -0,0 +1,50 @@ +/**************************************************************************** +* +* 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 "farfunc.h" + + + +/**** +***** Return true if *ch=='\0' or if *ch is a lead byte and *(ch+1)=='\0'. +***** If ch points to a single-byte character or a valid DBCS character, +***** returns false. +****/ + +_WCRTLINK int _NEARFAR(_mbterm,_fmbterm)( const unsigned char _FFAR *ch ) +{ + if( *ch == '\0' ) return( 1 ); + if( _ismbblead(*ch) && *(ch+1)=='\0' ) return( 2 ); + return( 0 ); +} diff --git a/programs/develop/open watcom/trunk/clib/mbyte/setmbcp.c b/programs/develop/open watcom/trunk/clib/mbyte/setmbcp.c new file mode 100644 index 0000000000..135bcb9ee6 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/mbyte/setmbcp.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: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "mbchar.h" + + +_WCRTLINK int _setmbcp( int i ) +{ + if( i == _MB_CP_ANSI ) { + i = _MBINIT_CP_ANSI; + } else if( i == _MB_CP_OEM ) { + i = _MBINIT_CP_OEM; + } else if( i == _MB_CP_SBCS ) { + i = _MBINIT_CP_SBCS; + } else if( i == 932 ) { + i = _MBINIT_CP_932; + } + + if( __mbinit( i ) == 0 ) { + return( 0 ); /* success */ + } else { + return( -1 ); /* failure */ + } +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/allocfp.c b/programs/develop/open watcom/trunk/clib/streamio/allocfp.c new file mode 100644 index 0000000000..14fa1cc2dc --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/allocfp.c @@ -0,0 +1,92 @@ +/**************************************************************************** +* +* 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: Platform independent __allocfp() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "liballoc.h" +#include "fileacc.h" +#include "rtdata.h" +#include "seterrno.h" +#include "streamio.h" + + +#define KEEP_FLAGS (_READ | _WRITE | _DYNAMIC) + +FILE *__allocfp( int handle ) +{ + FILE *end; + FILE *fp; + __stream_link *link; + unsigned flags; + + handle = handle; + _AccessIOB(); + /* Try and take one off the recently closed list */ + link = _RWD_cstream; + if( link != NULL ) { + _RWD_cstream = link->next; + fp = link->stream; + flags = (fp->_flag & KEEP_FLAGS) | (_READ | _WRITE); + goto got_one; + } + /* See if there is a static FILE structure available. */ + end = &_RWD_iob[_NFILES]; + for( fp = _RWD_iob; fp < end; ++fp ) { + if( (fp->_flag & (_READ | _WRITE)) == 0 ) { + link = lib_malloc( sizeof( __stream_link ) ); + if( link == NULL ) + goto no_mem; + flags = _READ | _WRITE; + goto got_one; + } + } + /* Allocate a new dynamic structure */ + flags = _DYNAMIC | _READ | _WRITE; + link = lib_malloc( sizeof( __stream_link ) + sizeof( FILE ) ); + if( link == NULL ) + goto no_mem; + fp = (FILE *)(link + 1); +got_one: + memset( fp, 0, sizeof( *fp ) ); + fp->_flag = flags; + link->stream = fp; + link->stream->_link = link; /* point back to link structure */ + link->next = _RWD_ostream; + _RWD_ostream = link; + _ReleaseIOB(); + return( fp ); +no_mem: + __set_errno( ENOMEM ); + _ReleaseIOB(); + return( NULL ); /* no free slots */ +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/chktty.c b/programs/develop/open watcom/trunk/clib/streamio/chktty.c new file mode 100644 index 0000000000..bc0f41d629 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/chktty.c @@ -0,0 +1,52 @@ +/**************************************************************************** +* +* 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: Implementation of chktty() - check if stream is teletype. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "rtdata.h" +#include "fileacc.h" +#include "streamio.h" + + +void __chktty( FILE *fp ) +{ + /* if we have not determined that we've got a tty then check for one */ + if( !(fp->_flag & _ISTTY) ) { + if( isatty( fileno( fp ) ) ) { + fp->_flag |= _ISTTY; + if( ( fp->_flag & (_IONBF | _IOLBF | _IOFBF) ) == 0 ) { + fp->_flag |= _IOLBF; + } + } + } +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/clearerr.c b/programs/develop/open watcom/trunk/clib/streamio/clearerr.c new file mode 100644 index 0000000000..2e7ed70e1d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/clearerr.c @@ -0,0 +1,45 @@ +/**************************************************************************** +* +* 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: Implementation of clearerr() - clear stream error indicator. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "variety.h" +#include "fileacc.h" + + +_WCRTLINK void (clearerr)( FILE *fp ) +{ + __stream_check( fp, 0 ); + _ValidFile( fp, 0 ); + _AccessFile( fp ); + fp->_flag &= ~(_SFERR | _EOF); + _ReleaseFile( fp ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/comtflag.c b/programs/develop/open watcom/trunk/clib/streamio/comtflag.c new file mode 100644 index 0000000000..d32fc28537 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/comtflag.c @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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: Commit mode state variable and accessor. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "commode.h" + + +int _commode = 0; + + +/* + * Before changing any of this, check startup/c/commode.c! + */ + +_WCRTLINK void _WCI86FAR __set_commode( void ) +{ + _commode = _COMMIT; +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/dsetefg.c b/programs/develop/open watcom/trunk/clib/streamio/dsetefg.c new file mode 100644 index 0000000000..e5e8dad0db --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/dsetefg.c @@ -0,0 +1,35 @@ +/**************************************************************************** +* +* 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: DLL version of setefg module. +* +****************************************************************************/ + + +// this file should remain an indirected file +// it is done this way to support the reuse of the source file +#define __SW_BR +#include "setefg.c" diff --git a/programs/develop/open watcom/trunk/clib/streamio/fclose.c b/programs/develop/open watcom/trunk/clib/streamio/fclose.c new file mode 100644 index 0000000000..473fabc94e --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fclose.c @@ -0,0 +1,122 @@ +/**************************************************************************** +* +* 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: Platform independent fclose() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "liballoc.h" +#include "mf.h" +#include +#include "fileacc.h" +#include "tmpfname.h" +#include "rtdata.h" +#include "lseek.h" +#include "streamio.h" +#include "close.h" +#include "flush.h" + + +#ifndef __UNIX__ +void (*__RmTmpFileFn)( FILE *fp ); +#endif + + +int __doclose( FILE *fp, int close_handle ) +{ + int ret; + + if( fp->_flag == 0 ) { + return( -1 ); /* file already closed */ + } + ret = 0; + if( fp->_flag & _DIRTY ) { + ret = __flush( fp ); + } + _AccessFile( fp ); +/* + * 02-nov-92 G.Turcotte Syncronize buffer pointer with the file pointer + * IEEE Std 1003.1-1988 B.8.2.3.2 + * 03-nov-03 B.Oldeman Inlined ftell; we already know the buffer isn't + * dirty (because of the flush), so only a "get" applies + */ + if( fp->_cnt != 0 ) { /* if something in buffer */ + __lseek( fileno( fp ), -fp->_cnt, SEEK_CUR ); + } + + if( close_handle ) { +#if defined( __UNIX__ ) || defined( __NETWARE__ ) + // we don't get to implement the close function on these systems + ret |= close( fileno( fp ) ); +#else + ret |= __close( fileno( fp ) ); +#endif + } + if( fp->_flag & _BIGBUF ) { /* if we allocated the buffer */ + lib_free( _FP_BASE(fp) ); + _FP_BASE(fp) = NULL; + } +#ifndef __UNIX__ + /* this never happens under UNIX */ + if( fp->_flag & _TMPFIL ) { /* if this is a temporary file */ + __RmTmpFileFn( fp ); + } +#endif + _ReleaseFile( fp ); + return( ret ); +} + +int __shutdown_stream( FILE *fp, int close_handle ) +{ + int ret; + + ret = __doclose( fp, close_handle ); + __freefp( fp ); + return( ret ); +} + +_WCRTLINK int fclose( FILE *fp ) +{ + __stream_link *link; + + _AccessIOB(); + link = _RWD_ostream; + for( ;; ) { + if( link == NULL ) { + _ReleaseIOB(); + return( -1 ); /* file not open */ + } + if( link->stream == fp ) break; + link = link->next; + } + _ReleaseIOB(); + return( __shutdown_stream( fp, 1 ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fdopen.c b/programs/develop/open watcom/trunk/clib/streamio/fdopen.c new file mode 100644 index 0000000000..80bbcc6a54 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fdopen.c @@ -0,0 +1,144 @@ +/**************************************************************************** +* +* 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: Platform independent fdopen() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#ifdef __WIDECHAR__ + #include +#endif +#include +#include +#ifdef __NT__ + #include +#endif +#include "iomode.h" +#include "rtdata.h" +#include "seterrno.h" +#include "streamio.h" + + +extern int __F_NAME(__open_flags,__wopen_flags)( const CHAR_TYPE *, int * ); + + +#ifndef __NETWARE__ + +static int __iomode( int handle, int amode ) +{ + int flags; + int __errno; + +#ifdef __UNIX__ + if( (flags = fcntl( handle, F_GETFL )) == -1 ) { + return( -1 ); + } + + __errno = EOK; + if( (flags & O_APPEND) && !(amode & _APPEND) ) { + __errno = EACCES; + } + if( (flags & O_ACCMODE) == O_RDONLY ) { + if( amode & _WRITE ) { + __errno = EACCES; + } + } else if( (flags & O_ACCMODE) == O_WRONLY ) { + if( amode & _READ ) { + __errno = EACCES; + } + } +#else + /* make sure the handle has the same text/binary mode */ + flags = __GetIOMode( handle ); + __errno = 0; + if( (amode ^ flags) & (_BINARY | _APPEND) ) { + __errno = EACCES; + } + if( ( amode & _READ ) && !(flags & _READ) ) { + __errno = EACCES; + } + if( ( amode & _WRITE ) && !(flags & _WRITE) ) { + __errno = EACCES; + } +#endif + if( __errno == EACCES ) { + __set_errno( __errno ); + return( -1 ); + } + return( 0 ); +} + +#endif + +_WCRTLINK FILE *__F_NAME(fdopen,_wfdopen)( int handle, const CHAR_TYPE *access_mode ) +{ + unsigned flags; + FILE *fp; + int extflags; + + if( handle == -1 ) { + __set_errno( EBADF ); /* 5-dec-90 */ + return( NULL ); /* 19-apr-90 */ + } + flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags ); + if( flags == 0 ) return( NULL ); + +#ifndef __NETWARE__ + /* make sure the handle has the same text/binary mode */ + if( __iomode( handle, flags ) == -1 ) { + return( NULL ); + } +#endif + fp = __allocfp( handle ); /* JBS 30-aug-91 */ + if( fp ) { + fp->_flag &= ~(_READ | _WRITE); /* 2-dec-90 */ + fp->_flag |= flags; + fp->_cnt = 0; + _FP_BASE(fp) = NULL; + fp->_bufsize = 0; /* was BUFSIZ JBS 91/05/31 */ +#ifndef __NETWARE__ + _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */ + _FP_EXTFLAGS(fp) = extflags; +#endif +#if defined( __NT__ ) || defined( __OS2__ ) + _FP_PIPEDATA(fp).isPipe = 0; /* not a pipe */ +#endif + fp->_handle = handle; /* BJS 91-07-23 */ + if( __F_NAME(tolower,towlower)( *access_mode ) == 'a' ) { + fseek( fp, 0, SEEK_END ); + } + __chktty( fp ); /* JBS 31-may-91 */ +#if !defined( __UNIX__ ) && !defined( __NETWARE__ ) + __SetIOMode( handle, flags ); +#endif + } + return( fp ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/feof.c b/programs/develop/open watcom/trunk/clib/streamio/feof.c new file mode 100644 index 0000000000..f54df0fa91 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/feof.c @@ -0,0 +1,41 @@ +/**************************************************************************** +* +* 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: Implementation of feof() - check for stream end-of-file. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "fileacc.h" + + +_WCRTLINK int (feof)( FILE *fp ) +{ + _ValidFile( fp, 0 ); + return( fp->_flag & _EOF ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/ferror.c b/programs/develop/open watcom/trunk/clib/streamio/ferror.c new file mode 100644 index 0000000000..fc2702d1de --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/ferror.c @@ -0,0 +1,41 @@ +/**************************************************************************** +* +* 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: Implementation of ferror() - check for stream error. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "fileacc.h" + + +_WCRTLINK int (ferror)( FILE *fp ) +{ + _ValidFile( fp, 0 ); + return( fp->_flag & _SFERR ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fflush.c b/programs/develop/open watcom/trunk/clib/streamio/fflush.c new file mode 100644 index 0000000000..5aa82d30bf --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fflush.c @@ -0,0 +1,48 @@ +/**************************************************************************** +* +* 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: Implementation of fflush() - flush streams. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "rtdata.h" +#include "fileacc.h" +#include "flush.h" + +_WCRTLINK int fflush( FILE *fp ) +{ + if( fp == NULL ) { + flushall(); + return( 0 ); + } + _ValidFile( fp, EOF ); + return( __flush( fp ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fgetc.c b/programs/develop/open watcom/trunk/clib/streamio/fgetc.c new file mode 100644 index 0000000000..b3998013b6 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fgetc.c @@ -0,0 +1,227 @@ +/**************************************************************************** +* +* 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: Platform independent fgetc() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#ifndef __UNIX__ + #include +#endif +#include "fileacc.h" +#include +#include "rtdata.h" +#include "seterrno.h" +#ifdef __WIDECHAR__ + #include + #include +#endif +#include "qread.h" +#include "orient.h" +#include "flush.h" +#include "streamio.h" + + +#define DOS_EOF_CHAR 0x1a + +#ifdef __WIDECHAR__ + #define CHARMASK 0xffff +#else + #define CHARMASK 0xff +#endif + +int __F_NAME(__fill_buffer,__wfill_buffer)( FILE *fp ) +{ + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); + } + if( fp->_flag & _ISTTY ) { /* 20-aug-90 */ + if( fp->_flag & (_IONBF | _IOLBF) ) { + __flushall( _ISTTY ); /* flush all TTY output */ + } + } + fp->_flag &= ~_UNGET; /* 10-mar-90 */ + fp->_ptr = _FP_BASE(fp); +#ifdef __UNIX__ + fp->_cnt = __qread( fileno( fp ), fp->_ptr, + (fp->_flag & _IONBF) ? CHARSIZE : fp->_bufsize ); +#else + if(( fp->_flag & (_IONBF | _ISTTY)) == (_IONBF | _ISTTY) && + ( fileno( fp ) == STDIN_FILENO )) + { + int c; /* JBS 31-may-91 */ + + fp->_cnt = 0; + // c = getche(); + if( c != EOF ) { + *(CHAR_TYPE *)fp->_ptr = c; + fp->_cnt = CHARSIZE; + } + } else { + fp->_cnt = __qread( fileno( fp ), fp->_ptr, + (fp->_flag & _IONBF) ? CHARSIZE : fp->_bufsize ); + } +#endif + if( fp->_cnt <= 0 ) { + if( fp->_cnt == 0 ) { + fp->_flag |= _EOF; + } else { + fp->_flag |= _SFERR; + fp->_cnt = 0; + } + } + return( fp->_cnt ); +} + +int __F_NAME(__filbuf,__wfilbuf)( FILE *fp ) +{ + if( __F_NAME(__fill_buffer,__wfill_buffer)( fp ) == 0 ) { + return( EOF ); + } + else { + fp->_cnt -= CHARSIZE; + fp->_ptr += CHARSIZE; + return( *(CHAR_TYPE *)(fp->_ptr - CHARSIZE) & CHARMASK ); + } +} + + +#ifndef __WIDECHAR__ + +_WCRTLINK int fgetc( FILE *fp ) +{ + int c; + + _ValidFile( fp, EOF ); + _AccessFile( fp ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(fp,EOF); + + if( (fp->_flag & _READ) == 0 ) { + __set_errno( EBADF ); + fp->_flag |= _SFERR; + c = EOF; + } else { + fp->_cnt--; + // it is important that this remain a relative comparison + // to ensure that the getc() macro works properly + if( fp->_cnt < 0 ) { + c = __F_NAME(__filbuf,__wfilbuf)( fp ); + } else { + c = *(char *)fp->_ptr; + fp->_ptr++; + } + } +#ifndef __UNIX__ + if( !(fp->_flag & _BINARY) ) { + if( c == '\r' ) { + fp->_cnt--; + // it is important that this remain a relative comparison + // to ensure that the getc() macro works properly + if( fp->_cnt < 0 ) { + c = __F_NAME(__filbuf,__wfilbuf)( fp ); + } else { + c = *(CHAR_TYPE*)fp->_ptr & CHARMASK; + fp->_ptr += CHARSIZE; + } + } + if( c == DOS_EOF_CHAR ) { + fp->_flag |= _EOF; + c = EOF; + } + } +#endif + _ReleaseFile( fp ); + return( c ); +} + +#else + +static int __read_wide_char( FILE *fp, wchar_t *wc ) +/**************************************************/ +{ + if( fp->_flag & _BINARY ) { + /*** Read a wide character ***/ + return( fread( wc, sizeof( wchar_t ), 1, fp ) ); + } else { + char mbc[MB_CUR_MAX]; + wchar_t wcTemp; + int rc; + + /*** Read the multibyte character ***/ + if( !fread( &mbc[0], 1, 1, fp ) ) + return( 0 ); + + if( _ismbblead( mbc[0] ) ) { + if( !fread( &mbc[1], 1, 1, fp ) ) + return( 0 ); + } + + /*** Convert it to wide form ***/ + rc = mbtowc( &wcTemp, mbc, MB_CUR_MAX ); + if( rc >= 0 ) { + *wc = wcTemp; + return( 1 ); + } else { + __set_errno( EILSEQ ); + return( 0 ); + } + } +} + +_WCRTLINK wint_t fgetwc( FILE *fp ) +{ + wchar_t c; + + _ValidFile( fp, WEOF ); + _AccessFile( fp ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(fp,WEOF); + + /*** Read the character ***/ + if( !__read_wide_char( fp, &c ) ) { + _ReleaseFile( fp ); + return( WEOF ); + } + if( !(fp->_flag & _BINARY) && (c == L'\r') ) { + if( !__read_wide_char( fp, &c ) ) { + _ReleaseFile( fp ); + return( WEOF ); + } + } + + _ReleaseFile( fp ); + return( (wint_t)c ); +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/fgetchar.c b/programs/develop/open watcom/trunk/clib/streamio/fgetchar.c new file mode 100644 index 0000000000..cdd06912df --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fgetchar.c @@ -0,0 +1,40 @@ +/**************************************************************************** +* +* 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: Implementation of fgetchar() - read character from stdin. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK INTCHAR_TYPE __F_NAME(fgetchar,fgetwchar)( void ) +{ + return( __F_NAME(fgetc,fgetwc)( stdin ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fgetpos.c b/programs/develop/open watcom/trunk/clib/streamio/fgetpos.c new file mode 100644 index 0000000000..340806e608 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fgetpos.c @@ -0,0 +1,42 @@ +/**************************************************************************** +* +* 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: Implementation of fgetpos() - return stream position. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "fileacc.h" + + +_WCRTLINK int fgetpos( FILE *fp, fpos_t *pos ) +{ + _ValidFile( fp, -1 ); + *pos = ftell( fp ); + return( *pos == -1L ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fgets.c b/programs/develop/open watcom/trunk/clib/streamio/fgets.c new file mode 100644 index 0000000000..8160fe8201 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fgets.c @@ -0,0 +1,65 @@ +/**************************************************************************** +* +* 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: Implementation of fgets() - read string from stream. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include "fileacc.h" + + +_WCRTLINK CHAR_TYPE *__F_NAME(fgets,fgetws)( CHAR_TYPE *s, int n, FILE *fp ) +{ + INTCHAR_TYPE c; + CHAR_TYPE *cs; + unsigned oflag; + + _ValidFile( fp, 0 ); + _AccessFile( fp ); + + oflag = fp->_flag & (_SFERR | _EOF); /* 06-sep-91 */ + fp->_flag &= ~(_SFERR | _EOF); + cs = s; + + /* don't use macro version of getc: multi-threading issues */ + while( (--n > 0) && (c = __F_NAME(fgetc,fgetwc)( fp )) != __F_NAME(EOF,WEOF) ) { + if( (*cs++ = c) == __F_NAME('\n',L'\n') ) + break; + } + + if( c == __F_NAME(EOF,WEOF) && (cs == s || ferror( fp )) ) { + s = NULL; + } else { + *cs = NULLCHAR; + } + fp->_flag |= oflag; /* 06-sep-91 */ + _ReleaseFile( fp ); + return( s ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/flush.c b/programs/develop/open watcom/trunk/clib/streamio/flush.c new file mode 100644 index 0000000000..541eba709c --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/flush.c @@ -0,0 +1,113 @@ +/**************************************************************************** +* +* 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: Implementation of fflush() helper routine. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include "fileacc.h" +#include "rtdata.h" +#include "seterrno.h" +#include "qwrite.h" +#include "lseek.h" +#include "flush.h" + +#if defined( __NETWARE__ ) && defined( _THIN_LIB ) + +/* Take flush from LibC */ +_WCRTLINK int __flush( FILE *fp ) +{ + return( fflush( fp ) ); +} + +#else + +_WCRTLINK int __flush( FILE *fp ) +{ + int len; + long offset; + int ret; + char *ptr; + unsigned amount; + + ret = 0; + _AccessFile( fp ); + if( fp->_flag & _DIRTY ) { + fp->_flag &= ~_DIRTY; + if( (fp->_flag & _WRITE) && (_FP_BASE(fp) != NULL) ) { + ptr = _FP_BASE(fp); + amount = fp->_cnt; + while( amount != 0 && ret == 0 ) { + len = __qwrite( fileno( fp ), ptr, amount ); /* 02-aug-90 */ + if( len == -1 ) { + fp->_flag |= _SFERR; + ret = EOF; + } +#ifndef __UNIX__ + else if( len == 0 ) { + __set_errno( ENOSPC ); /* 12-nov-88 */ + fp->_flag |= _SFERR; + ret = EOF; + } +#endif + ptr += len; + amount -= len; + } + } + } else if( _FP_BASE(fp) != NULL ) { /* not dirty */ + /* fseek( fp, ftell(fp), SEEK_SET ); */ + fp->_flag &= ~_EOF; + if( !(fp->_flag & _ISTTY) ) { + offset = fp->_cnt; + if( offset != 0 ) { /* 10-aug-89 JD */ + offset = __lseek( fileno( fp ), -offset, SEEK_CUR ); + } + if( offset == -1 ) { + fp->_flag |= _SFERR; + ret = EOF; + } + } + } + fp->_ptr = _FP_BASE(fp); /* reset ptr to start of buffer */ + fp->_cnt = 0; +#if !defined( __NETWARE__ ) && !defined( __OSI__ ) + if( ret == 0 && (_FP_EXTFLAGS(fp) & _COMMIT) ) { + if( fsync( fileno( fp ) ) == -1 ) { + ret = EOF; + } + } +#endif + _ReleaseFile( fp ); + return( ret ); +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/flushall.c b/programs/develop/open watcom/trunk/clib/streamio/flushall.c new file mode 100644 index 0000000000..8fffa7c179 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/flushall.c @@ -0,0 +1,65 @@ +/**************************************************************************** +* +* 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: Implementation of flushall() - flush all streams. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "rtdata.h" +#include "fileacc.h" +#include "flush.h" + + +/* __fill_buffer calls this routine with _ISTTY mask */ + +int __flushall( int mask ) +{ + __stream_link *link; + FILE *fp; + int number_of_open_files; + + _AccessIOB(); + number_of_open_files = 0; + for( link = _RWD_ostream; link != NULL; link = link->next ) { + fp = link->stream; + if( fp->_flag & mask ) { /* if file is a candidate */ + ++number_of_open_files; + if( fp->_flag & _DIRTY ) { + __flush( fp ); + } + } + } + _ReleaseIOB(); + return( number_of_open_files ); +} + +_WCRTLINK int flushall( void ) +{ + return( __flushall( ~0 ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fmode.h b/programs/develop/open watcom/trunk/clib/streamio/fmode.h new file mode 100644 index 0000000000..386ac1af38 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fmode.h @@ -0,0 +1,33 @@ +/**************************************************************************** +* +* 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! +* +****************************************************************************/ + + +/* define _fmode to be _fmode */ diff --git a/programs/develop/open watcom/trunk/clib/streamio/fopen.c b/programs/develop/open watcom/trunk/clib/streamio/fopen.c new file mode 100644 index 0000000000..e91ce17b24 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fopen.c @@ -0,0 +1,359 @@ +/**************************************************************************** +* +* 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: Platform independent fopen() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#ifdef __WIDECHAR__ + #include +#endif +#include +#include +#include +#include "fileacc.h" +#include "fmode.h" +#include "openmode.h" +#include "rtdata.h" +#include "seterrno.h" +//#include "defwin.h" +#include "streamio.h" + +#ifdef __UNIX__ + #define PMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) +#else + #define PMODE (S_IREAD | S_IWRITE) +#endif + + +int __F_NAME(__open_flags,__wopen_flags)( const CHAR_TYPE *modestr, int *extflags ) +{ + int flags; + int alive = 1; + int gotplus = 0; + int gottextbin = 0; +#ifndef __NETWARE__ + int gotcommit = 0; +#endif + + flags = 0; + if( extflags != NULL ) { +#ifdef __NETWARE__ + *extflags = 0; +#else + if( _commode == _COMMIT ) { + *extflags = _COMMIT; + } else { + *extflags = 0; + } +#endif + } + + /* + * The first character in modestr must be 'r', 'w', or 'a'. + */ + switch( *modestr ) { + case 'r': + flags |= _READ; + break; + case 'w': + flags |= _WRITE; + break; + case 'a': + flags |= _WRITE | _APPEND; + break; + default: + __set_errno( EINVAL ); + return( 0 ); + } + modestr++; + + /* + * Next we might have, in any order, some additional mode modifier + * characters: + * 1. A '+' character. + * 2. Either a 't' or a 'b'. + * 3. Either a 'c' or a 'n'. (Not available for Netware.) + * For MS compatability, scanning stops when any of the three groups + * is encountered twice; e.g., "wct+b$&!" is valid and will result in + * a text, not binary, stream. Also for MS compatability, scanning + * stops at any unrecognized character, without causing failure. + */ + while( (*modestr != NULLCHAR) && alive ) { + switch( *modestr ) { + case '+': + if( gotplus ) { + alive = 0; + } else { + flags |= _READ | _WRITE; + gotplus = 1; + } + break; + case 't': + if( gottextbin ) { + alive = 0; + } else { + gottextbin = 1; + } + break; + case 'b': + if( gottextbin ) { + alive = 0; + } else { +#ifndef __UNIX__ + flags |= _BINARY; +#endif + gottextbin = 1; + } + break; +#ifndef __NETWARE__ + case 'c': + if( gotcommit ) { + alive = 0; + } else { + *extflags |= _COMMIT; + gotcommit = 1; + } + break; + case 'n': + if( gotcommit ) { + alive = 0; + } else { + *extflags &= ~_COMMIT; + gotcommit = 1; + } + break; +#endif + default: + break; + } + modestr++; + } + + /* + * Handle defaults for any unspecified options. + */ +#ifndef __UNIX__ + if( !gottextbin ) { + if( _RWD_fmode == O_BINARY ) flags |= _BINARY; + } +#endif + + return( flags ); +} + + +static FILE *__F_NAME(__doopen,__wdoopen)( const CHAR_TYPE *name, + CHAR_TYPE mode, + int file_flags, + int extflags, + int shflag, /* sharing flag */ + FILE * fp ) +{ + int open_mode; + int p_mode; + + SetupTGCSandNCS( RETURN_ARG( FILE *, 0 ) ); /* for NW386 */ + fp->_flag &= ~(_READ | _WRITE); + fp->_flag |= file_flags; + + /* we need the mode character to indicate if the original */ + /* intention is to open for read or for write */ + mode = __F_NAME(tolower,towlower)( mode ); + if( mode == 'r' ) { + open_mode = O_RDONLY; + if( file_flags & _WRITE ) { /* if "r+" mode */ + open_mode = O_RDWR; + } +#if defined( __NETWARE__ ) + open_mode |= O_BINARY; +#elif defined( __UNIX__ ) +#else + if( file_flags & _BINARY ) { + open_mode |= O_BINARY; + } else { + open_mode |= O_TEXT; + } +#endif + p_mode = 0; + } else { /* mode == 'w' || mode == 'a' */ + if( file_flags & _READ ) { /* if "a+" or "w+" mode */ + open_mode = O_RDWR | O_CREAT; + } else { + open_mode = O_WRONLY | O_CREAT; + } + if( file_flags & _APPEND ) { + open_mode |= O_APPEND; + } else { /* mode == 'w' */ + open_mode |= O_TRUNC; + } +#if defined( __NETWARE__ ) + open_mode |= O_BINARY; +#elif defined( __UNIX__ ) +#else + if( file_flags & _BINARY ) { + open_mode |= O_BINARY; + } else { + open_mode |= O_TEXT; + } +#endif + p_mode = PMODE; + } + fp->_handle = __F_NAME(sopen,_wsopen)( name, open_mode, shflag, p_mode ); + if( fp->_handle == -1 ) { + // since we couldn't open the file, release the FILE struct + __freefp( fp ); + return( NULL ); + } + fp->_cnt = 0; + fp->_bufsize = 0; /* was BUFSIZ JBS 31-may-91 */ +#ifndef __NETWARE__ + _FP_ORIENTATION(fp) = _NOT_ORIENTED; /* initial orientation */ + _FP_EXTFLAGS(fp) = extflags; +#endif +#if defined( __NT__ ) || defined( __OS2__ ) + _FP_PIPEDATA(fp).isPipe = 0; /* not a pipe */ +#endif + _FP_BASE(fp) = NULL; + if( file_flags & _APPEND ) { + fseek( fp, 0L, SEEK_END ); + } + __chktty( fp ); /* JBS 28-aug-90 */ + return( fp ); +} + + +_WCRTLINK FILE *__F_NAME(_fsopen,_wfsopen)( const CHAR_TYPE *name, + const CHAR_TYPE *access_mode, int shflag ) +{ + FILE * fp; + int file_flags; + int extflags; + + /* validate access_mode */ + file_flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags ); + if( file_flags == 0 ) { + return( NULL ); + } + + /* specify dummy handle 0 */ + fp = __allocfp( 0 ); /* JBS 30-aug-91 */ + if( fp != NULL ) { + fp = __F_NAME(__doopen,__wdoopen)( name, *access_mode, + file_flags, extflags, + shflag, fp ); + } + return( fp ); +} + + +_WCRTLINK FILE *__F_NAME(fopen,_wfopen)( const CHAR_TYPE *name, const CHAR_TYPE *access_mode ) +{ + return( __F_NAME(_fsopen,_wfsopen)( name, access_mode, OPENMODE_DENY_COMPAT ) ); +} + +static FILE *close_file( FILE *fp ) +{ + __stream_link * link; + __stream_link **owner; + + _AccessIOB(); + /* See if the file pointer is a currently open file. */ + link = _RWD_ostream; + for( ;; ) { + if( link == NULL ) break; + if( link->stream == fp ) { + if( fp->_flag & (_READ|_WRITE) ) { + __doclose( fp, 1 ); + } + _ReleaseIOB(); + return( fp ); + } + link = link->next; + } + /* + It's not on the list of open files, so check the list of + recently closed ones. + */ + owner = &_RWD_cstream; + for( ;; ) { + link = *owner; + if( link == NULL ) break; + if( link->stream == fp ) { + /* remove from closed list and put on open */ + *owner = link->next; + link->next = _RWD_ostream; + _RWD_ostream = link; + _ReleaseIOB(); + return( fp ); + } + owner = &link->next; + } + /* We ain't seen that file pointer ever. Leave things be. */ + __set_errno( EBADF ); + _ReleaseIOB(); + return( NULL ); +} + + +_WCRTLINK FILE *__F_NAME(freopen,_wfreopen)( const CHAR_TYPE *name, + const CHAR_TYPE *access_mode, FILE *fp ) +{ + int hdl; + int file_flags; + int extflags; + + _ValidFile( fp, 0 ); + + /* validate access_mode */ + file_flags = __F_NAME(__open_flags,__wopen_flags)( access_mode, &extflags ); + if( file_flags == 0 ) { + return( NULL ); + } + + hdl = fileno( fp ); + _AccessFileH( hdl ); + +#ifdef DEFAULT_WINDOWING + if( _WindowsRemoveWindowedHandle != 0 ) { + _WindowsRemoveWindowedHandle( hdl ); + } +#endif + fp = close_file( fp ); + if( fp != NULL ) { + fp->_flag &= _DYNAMIC; /* 24-jul-92 */ + fp = __F_NAME(__doopen,__wdoopen)( name, *access_mode, + file_flags, extflags, + 0, fp ); + } + _ReleaseFileH( hdl ); + return( fp ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fopen_s.c b/programs/develop/open watcom/trunk/clib/streamio/fopen_s.c new file mode 100644 index 0000000000..cdbb0d2d7c --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fopen_s.c @@ -0,0 +1,89 @@ +/**************************************************************************** +* +* 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: Implementation of fopen_s() - safe version of fopen(). +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#ifdef __WIDECHAR__ + #include +#endif +// #include +#include +#include +#include "fileacc.h" +#include "fmode.h" +#include "openmode.h" +#include "rtdata.h" +// #include "seterrno.h" +// #include "defwin.h" + + +_WCRTLINK errno_t __F_NAME(fopen_s,_wfopen_s)( FILE * __restrict * __restrict streamptr, + const CHAR_TYPE * __restrict filename, + const CHAR_TYPE * __restrict mode) +/**************************************************************************************/ +{ + errno_t rc = -1; + const char *msg; + + /* check for runtime-constraints */ + /* streamptr not null */ + /* filename not null */ + /* mode not null */ + if( __check_constraint_nullptr_msg( msg, streamptr ) && + __check_constraint_nullptr_msg( msg, filename ) && + __check_constraint_nullptr_msg( msg, mode ) ) { + + /* ua.. and uw.. are treated as a.. and w.. */ + if( (*mode == __F_NAME('u',L'u')) && + ( (*(mode + 1) == __F_NAME('r',L'r')) || + (*(mode + 1) == __F_NAME('w',L'w')) || + (*(mode + 1) == __F_NAME('a',L'a'))) ) { + mode++; /* ignore u for now */ + } + *streamptr = __F_NAME(_fsopen,_wfsopen)( filename, mode, OPENMODE_DENY_COMPAT ); + + if( *streamptr != NULL ) { /* if open ok set rc = 0 */ + rc = 0; + } + } else { + /* Runtime-constraints found, store zero in streamptr */ + if( streamptr != NULL ) { + *streamptr = NULL; + } + /* Now call the handler */ + __rtct_fail( __func__, msg, NULL ); + } + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fprintf.c b/programs/develop/open watcom/trunk/clib/streamio/fprintf.c new file mode 100644 index 0000000000..6c82b16ab8 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fprintf.c @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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: Implementation of fprintf() - formatted stream output. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "farsupp.h" +#include "printf.h" +#include "fprtf.h" + + +_WCRTLINK int __F_NAME(fprintf,fwprintf)( FILE *io, const CHAR_TYPE *format, ... ) +{ + va_list args; + + va_start( args, format ); + return( __F_NAME(__fprtf,__fwprtf)( io, format, args ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fprntf_s.c b/programs/develop/open watcom/trunk/clib/streamio/fprntf_s.c new file mode 100644 index 0000000000..e97c7dbaa7 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fprntf_s.c @@ -0,0 +1,48 @@ +/**************************************************************************** +* +* 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: Implementation of fprintf_s() - safe formatted stream output. +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#include "fprtf_s.h" + + +_WCRTLINK int __F_NAME(fprintf_s,fwprintf_s)( FILE * __restrict io, + const CHAR_TYPE * __restrict format, ... ) +{ + va_list args; + + va_start( args, format ); + return( __F_NAME(__fprtf_s,__fwprtf_s)( io, format, args ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fprtf.c b/programs/develop/open watcom/trunk/clib/streamio/fprtf.c new file mode 100644 index 0000000000..b5c875260e --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fprtf.c @@ -0,0 +1,99 @@ +/**************************************************************************** +* +* 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: Implementation of printf() - formatted output. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "rtdata.h" +#include "fileacc.h" +#include "printf.h" +#include "fprtf.h" +#include "orient.h" +#include "flush.h" +#include "streamio.h" + + +/* + * file_putc -- write a character to a file + */ +static slib_callback_t file_putc; // setup calling convention +static void __SLIB_CALLBACK file_putc( SPECS __SLIB *specs, int op_char ) +{ + __F_NAME(fputc,fputwc)( op_char, (FILE *)specs->_dest ); + specs->_output_count++; +} + + +int __F_NAME(__fprtf,__fwprtf)( FILE *fp, const CHAR_TYPE *format, va_list arg ) +{ + int not_buffered; + int amount_written; + unsigned oflag; + slib_callback_t *tmp; + + _ValidFile( fp, 0 ); + _AccessFile( fp ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(fp,0); + + oflag = fp->_flag & (_SFERR | _EOF); /* 06-sep-91 */ + fp->_flag &= ~(_SFERR | _EOF); + + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); /* allocate buffer */ + } + not_buffered = 0; + if( fp->_flag & _IONBF ) { + not_buffered = 1; + fp->_flag &= ~_IONBF; + fp->_flag |= _IOFBF; + } +#if defined( __386__ ) && defined( __QNX__ ) + /* avoid some segment relocations for 32-bit QNX */ + tmp = (void (*)())file_putc; +#else + tmp = file_putc; +#endif + amount_written = __F_NAME(__prtf,__wprtf)( fp, format, arg, tmp ); + if( not_buffered ) { + fp->_flag &= ~_IOFBF; + fp->_flag |= _IONBF; + __flush( fp ); + } + if( ferror( fp ) ) + amount_written = -1; /* 06-sep-91 */ + fp->_flag |= oflag; + + _ReleaseFile( fp ); + return( amount_written ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.c b/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.c new file mode 100644 index 0000000000..82050e4bb2 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.c @@ -0,0 +1,108 @@ +/**************************************************************************** +* +* 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: Implementation of __fprtf_s() - safe formatted output. +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include "rtdata.h" +#include "fileacc.h" +#include "printf.h" +#include "fprtf_s.h" +#include "orient.h" + +extern void __ioalloc( FILE * ); +extern int __flush( FILE * ); + + +/* + * file_putc -- write a character to a file + */ +static slib_callback_t file_putc; // setup calling convention +static void __SLIB_CALLBACK file_putc( SPECS __SLIB *specs, int op_char ) +{ + __F_NAME(fputc,fputwc)( op_char, (FILE *)specs->_dest ); + specs->_output_count++; +} + + +int __F_NAME(__fprtf_s,__fwprtf_s)( FILE * __restrict stream, + const CHAR_TYPE * __restrict format, va_list arg ) +{ + int not_buffered; + int amount_written; + unsigned oflag; + const char *msg = NULL; /* doubles as error indicator */ + + /* Check for runtime-constraints before grabbing file lock */ + /* stream not null */ + /* format not null */ + if( __check_constraint_nullptr_msg( msg, stream ) && + __check_constraint_nullptr_msg( msg, format ) ) { + + _ValidFile( stream, 0 ); + _AccessFile( stream ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(stream,0); + + oflag = stream->_flag & (_SFERR | _EOF); + stream->_flag &= ~(_SFERR | _EOF); + + if( _FP_BASE(stream) == NULL ) { + __ioalloc( stream ); /* allocate buffer */ + } + not_buffered = 0; + if( stream->_flag & _IONBF ) { + not_buffered = 1; + stream->_flag &= ~_IONBF; + stream->_flag |= _IOFBF; + } + amount_written = __F_NAME(__prtf_s,__wprtf_s)( stream, format, arg, &msg, file_putc ); + if( not_buffered ) { + stream->_flag &= ~_IOFBF; + stream->_flag |= _IONBF; + __flush( stream ); + } + if( ferror( stream ) ) + amount_written = -1; + stream->_flag |= oflag; + + _ReleaseFile( stream ); + } + if( msg != NULL ) { + /* There was a constraint violation; call the handler */ + __rtct_fail( __func__, msg, NULL ); + amount_written = -1; + } + return( amount_written ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.h b/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.h new file mode 100644 index 0000000000..f4a0cd0323 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fprtf_s.h @@ -0,0 +1,32 @@ +/**************************************************************************** +* +* 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: Prototype for __fprtf_s() internal routine. +* +****************************************************************************/ + + +extern int __F_NAME(__fprtf_s,__fwprtf_s)( FILE *fp, const CHAR_TYPE *format, va_list arg ); diff --git a/programs/develop/open watcom/trunk/clib/streamio/fputc.c b/programs/develop/open watcom/trunk/clib/streamio/fputc.c new file mode 100644 index 0000000000..48bc652097 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fputc.c @@ -0,0 +1,145 @@ +/**************************************************************************** +* +* 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: Platform independent fputc() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "fileacc.h" +#include "rtdata.h" +#include "seterrno.h" +#include "orient.h" +#ifdef __WIDECHAR__ + #include + #include +#endif +#include "flush.h" +#include "streamio.h" + + +#ifndef __WIDECHAR__ + +_WCRTLINK int fputc( int c, FILE *fp ) +{ + int flags; + + _ValidFile( fp, EOF ); + _AccessFile( fp ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(fp,EOF); + + if( !(fp->_flag & _WRITE) ) { + __set_errno( EBADF ); + fp->_flag |= _SFERR; + _ReleaseFile( fp ); + return( EOF ); + } + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); + } + flags = _IONBF; + if( c == '\n' ) { + flags = _IONBF | _IOLBF; +#ifndef __UNIX__ + if( !(fp->_flag & _BINARY) ) { + fp->_flag |= _DIRTY; + *(char*)fp->_ptr = '\r'; /* '\n' -> '\r''\n' */ + fp->_ptr++; + fp->_cnt++; + if( fp->_cnt == fp->_bufsize ) { + if( __flush( fp ) ) { + _ReleaseFile( fp ); + return( EOF ); + } + } + } +#endif + } + fp->_flag |= _DIRTY; + *(char *)fp->_ptr = c; + fp->_ptr++; + fp->_cnt++; + if( (fp->_flag & flags) || (fp->_cnt == fp->_bufsize) ) { + if( __flush( fp ) ) { + _ReleaseFile( fp ); + return( EOF ); + } + } + _ReleaseFile( fp ); + return( (UCHAR_TYPE)c ); +} + + +#else + + +static int __write_wide_char( FILE *fp, wchar_t wc ) +/**************************************************/ +{ + if( fp->_flag & _BINARY ) { + /*** Dump the wide character ***/ + return( fwrite( &wc, sizeof( wchar_t ), 1, fp ) ); + } else { + char mbc[MB_CUR_MAX]; + int rc; + + /*** Convert the wide character to multibyte form and write it ***/ + rc = wctomb( mbc, wc ); + if( rc > 0 ) { + return( fwrite( mbc, rc, 1, fp ) ); + } else { + __set_errno( EILSEQ ); + return( 0 ); + } + } +} + + +_WCRTLINK wint_t fputwc( wint_t c, FILE *fp ) +{ + _ValidFile( fp, WEOF ); + _AccessFile( fp ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(fp,WEOF); + + /*** Write the character ***/ + if( !__write_wide_char( fp, c ) ) { + _ReleaseFile( fp ); + return( WEOF ); + } else { + _ReleaseFile( fp ); + return( c ); + } +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/fputchar.c b/programs/develop/open watcom/trunk/clib/streamio/fputchar.c new file mode 100644 index 0000000000..e38b027b42 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fputchar.c @@ -0,0 +1,40 @@ +/**************************************************************************** +* +* 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: Implementation of fputchar() - print character to stdout. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK INTCHAR_TYPE __F_NAME(fputchar,fputwchar)( INTCHAR_TYPE c ) +{ + return( __F_NAME(fputc,fputwc)( c, stdout ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fputs.c b/programs/develop/open watcom/trunk/clib/streamio/fputs.c new file mode 100644 index 0000000000..1279a78687 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fputs.c @@ -0,0 +1,91 @@ +/**************************************************************************** +* +* 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: Implementation of fputs() - put string to stream. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include "fileacc.h" +#include "rtdata.h" +#include "flush.h" +#include "streamio.h" + + +_WCRTLINK int __F_NAME(fputs,fputws)( const CHAR_TYPE *s, FILE *fp ) +{ + const CHAR_TYPE *start; + int c; + int not_buffered; + int rc; + + _ValidFile( fp, __F_NAME(EOF,WEOF) ); + _AccessFile( fp ); + + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); /* allocate buffer */ + } + not_buffered = 0; + if( fp->_flag & _IONBF ) { + not_buffered = 1; + fp->_flag &= ~_IONBF; + fp->_flag |= _IOLBF; + } + rc = 0; + start = s; + while( c = *s ) { + s++; +#ifndef __WIDECHAR__ + if( (fputc)( c, fp ) == EOF ) { /* 23-oct-91 */ + rc = EOF; + break; + } +#else + if( (fputwc)( c, fp ) == WEOF ) { /* 23-oct-91 */ + rc = -1; + break; + } +#endif + } + if( not_buffered ) { + fp->_flag &= ~_IOLBF; + fp->_flag |= _IONBF; + if( rc == 0 ) { + rc = __flush( fp ); /* 23-oct-91 */ + } + } + if( rc == 0 ) { + /* return the number of items written */ + /* this is ok by ANSI which says that success is */ + /* indicated by a non-negative return value */ + rc = s - start; + } + _ReleaseFile( fp ); + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fread.c b/programs/develop/open watcom/trunk/clib/streamio/fread.c new file mode 100644 index 0000000000..657366d424 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fread.c @@ -0,0 +1,165 @@ +/**************************************************************************** +* +* 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: Implementation of fread() - read data from stream. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "fileacc.h" +#include +#include +#include "rtdata.h" +#include "seterrno.h" +#include "qread.h" +#include "streamio.h" + + +#define DOS_EOF_CHAR 0x1a + +extern int __fill_buffer( FILE * ); /* located in fgetc */ + + +_WCRTLINK size_t fread( void *_buf, size_t size, size_t n, FILE *fp ) +{ + char *buf = _buf; + size_t len_read; + + _ValidFile( fp, 0 ); + _AccessFile( fp ); + if( (fp->_flag & _READ) == 0 ) { + __set_errno( EBADF ); + fp->_flag |= _SFERR; + _ReleaseFile( fp ); + return( 0 ); + } + +#if 0 + /*** If the buffer is _DIRTY, resync it before reading ***/ + if( fp->_flag & (_WRITE | _UNGET) ) { + if( fp->_flag & _DIRTY ) { + fseek( fp, 0, SEEK_CUR ); + } + } +#endif + + n *= size; + if( n == 0 ) { + _ReleaseFile( fp ); + return( n ); + } + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); /* allocate buffer */ + } + len_read = 0; +#if !defined( __UNIX__ ) + if( fp->_flag & _BINARY ) +#endif + { + size_t bytes_left = n, bytes; + for( ;; ) { + if( fp->_cnt != 0 ) { + bytes = fp->_cnt; + if( bytes > bytes_left ) { + bytes = bytes_left; + } + memcpy( buf, fp->_ptr, bytes ); + fp->_ptr += bytes; + buf += bytes; + fp->_cnt -= bytes; + bytes_left -= bytes; + len_read += bytes; + } + if( bytes_left == 0 ) break; + + /* if user's buffer is larger than our buffer, OR + _IONBF is set, then read directly into user's buffer. */ + + if( (bytes_left >= fp->_bufsize) || (fp->_flag & _IONBF) ) { + bytes = bytes_left; + fp->_ptr = _FP_BASE(fp); + fp->_cnt = 0; + if( !(fp->_flag & _IONBF) ) { + /* if more than a sector, set to multiple of sector size*/ + if( bytes > 512 ) { + bytes &= -512; + } + } + n = __qread( fileno(fp), buf, bytes ); + if( n == -1 ) { + fp->_flag |= _SFERR; + break; + } else if( n == 0 ) { + fp->_flag |= _EOF; + break; + } + buf += n; + bytes_left -= n; + len_read += n; + } else { + if( __fill_buffer( fp ) == 0 ) break; + } + } /* end for */ +#if !defined(__UNIX__) + } else { + for( ;; ) { + int c; + + // ensure non-empty buffer + if( fp->_cnt == 0 ) { + if( __fill_buffer( fp ) == 0 ) break; + } + // get character + --fp->_cnt; + c = *fp->_ptr++ & 0xff; + // perform new-line translation + if( c == '\r' ) { + // ensure non-empty buffer + if( fp->_cnt == 0 ) { + if( __fill_buffer( fp ) == 0 ) break; + } + // get character + --fp->_cnt; + c = *fp->_ptr++ & 0xff; + } + // check for DOS end of file marker + if( c == DOS_EOF_CHAR ) { + fp->_flag |= _EOF; + break; + } + // store chracter + buf[len_read] = (char)c; + ++len_read; + if( len_read == n ) break; + } +#endif + } + _ReleaseFile( fp ); + return( len_read / size ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/freefp.c b/programs/develop/open watcom/trunk/clib/streamio/freefp.c new file mode 100644 index 0000000000..550c58602b --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/freefp.c @@ -0,0 +1,92 @@ +/**************************************************************************** +* +* 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: Platform independent __allocfp() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "liballoc.h" +#include "fileacc.h" +#include "rtdata.h" +#include "streamio.h" + +#ifdef DLHEAP + +void _cdecl dlfree(void*); +void _cdecl mf_init(); + +#define malloc dlmalloc +#define free dlfree +#define realloc dlrealloc + +#define lib_free dlfree +#endif + +/* + NOTE: This routine does not actually free the link/FILE structures. + That is because code assumes that it can fclose the file and then + freopen is a short time later. The __purgefp routine can be called + to actually release the storage. +*/ + +void __freefp( FILE * fp ) +{ + __stream_link **owner; + __stream_link *link; + + _AccessIOB(); + owner = &_RWD_ostream; + for( ;; ) { + link = *owner; + if( link == NULL ) + return; + if( link->stream == fp ) + break; + owner = &link->next; + } + fp->_flag |= _READ | _WRITE; + (*owner) = link->next; + link->next = _RWD_cstream; + _RWD_cstream = link; + _ReleaseIOB(); +} + + +void __purgefp( void ) +{ + __stream_link *next; + + _AccessIOB(); + while( _RWD_cstream != NULL ) { + next = _RWD_cstream->next; + lib_free( _RWD_cstream ); + _RWD_cstream = next; + } + _ReleaseIOB(); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/freop_s.c b/programs/develop/open watcom/trunk/clib/streamio/freop_s.c new file mode 100644 index 0000000000..34cb25c2a6 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/freop_s.c @@ -0,0 +1,88 @@ +/**************************************************************************** +* +* 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: Implementation of freopen_s() - safe version of freopen(). +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#ifdef __WIDECHAR__ + #include +#endif +#include +#include +#include "fileacc.h" +#include "fmode.h" +#include "openmode.h" +#include "rtdata.h" + + +_WCRTLINK errno_t __F_NAME(freopen_s,_wfreopen_s)( + FILE * __restrict * __restrict newstreamptr, + const CHAR_TYPE * __restrict filename, + const CHAR_TYPE * __restrict mode, + FILE * __restrict stream ) +/********************************************************************/ +{ + errno_t rc = -1; + const char *msg; + + /* check for runtime-constraints */ + /* newstreamptr not null */ + /* mode not null */ + /* stream not null */ + if( __check_constraint_nullptr_msg( msg, newstreamptr ) && + __check_constraint_nullptr_msg( msg, mode ) && + __check_constraint_nullptr_msg( msg, stream ) ) { + + /* ua.. and uw.. are treated as a.. and w.. */ + if( (*mode == __F_NAME('u',L'u')) && + ( (*(mode + 1) == __F_NAME('r',L'r')) || + (*(mode + 1) == __F_NAME('w',L'w')) || + (*(mode + 1) == __F_NAME('a',L'a'))) ) { + mode++; /* ignore u for now */ + } + *newstreamptr = __F_NAME(freopen,_wfreopen)( filename, mode, stream ); + + if( *newstreamptr != NULL ) { /* if reopen ok set rc = 0 */ + rc = 0; + } + } else { + /* Runtime-constraints found, store zero in newstreamptr */ + if( newstreamptr != NULL ) { + *newstreamptr = NULL; + } + /* Now call the handler */ + __rtct_fail( __func__, msg, NULL ); + } + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fscanf.c b/programs/develop/open watcom/trunk/clib/streamio/fscanf.c new file mode 100644 index 0000000000..b0372e844f --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fscanf.c @@ -0,0 +1,84 @@ +/**************************************************************************** +* +* 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: Implementation of fscanf() - formatted stream input. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "rtdata.h" +#include "scanf.h" +#include "fileacc.h" +#include "orient.h" + + +static int cget_file( PTR_SCNF_SPECS specs ) +{ + int c; + + if( (c = __F_NAME(getc,getwc)( (FILE *)specs->ptr )) == __F_NAME(EOF,WEOF) ) { + specs->eoinp = 1; + } + return( c ); +} + + +static void uncget_file( int c, PTR_SCNF_SPECS specs ) +{ + __F_NAME(ungetc,ungetwc)( c, (FILE *)specs->ptr ); +} + + +_WCRTLINK int __F_NAME(vfscanf,vfwscanf)( FILE *io, const CHAR_TYPE *format, va_list args ) +{ + int rc; + SCNF_SPECS specs; + + _AccessFile( io ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(io,0); + + specs.ptr = (CHAR_TYPE *)io; + specs.cget_rtn = cget_file; + specs.uncget_rtn = uncget_file; + rc = __F_NAME(__scnf,__wscnf)( (PTR_SCNF_SPECS)&specs, format, args ); + _ReleaseFile( io ); + return( rc ); +} + + +_WCRTLINK int __F_NAME(fscanf,fwscanf)( FILE *io, const CHAR_TYPE *format, ... ) +{ + va_list args; + + va_start( args, format ); + return( __F_NAME(vfscanf,vfwscanf)( io, format, args ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fscanf_s.c b/programs/develop/open watcom/trunk/clib/streamio/fscanf_s.c new file mode 100644 index 0000000000..043b7a27bb --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fscanf_s.c @@ -0,0 +1,96 @@ +/**************************************************************************** +* +* 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: Implementation of fscanf_s() - safe formatted stream input. +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#include "rtdata.h" +#include "scanf.h" +#include "fileacc.h" +#include "orient.h" + + +static int cget_file( PTR_SCNF_SPECS specs ) +{ + int c; + + if( (c = __F_NAME(getc,getwc)( (FILE *)specs->ptr )) == __F_NAME(EOF,WEOF) ) { + specs->eoinp = 1; + } + return( c ); +} + + +static void uncget_file( int c, PTR_SCNF_SPECS specs ) +{ + __F_NAME(ungetc,ungetwc)( c, (FILE *)specs->ptr ); +} + + +_WCRTLINK int __F_NAME(fscanf_s,fwscanf_s)( FILE * __restrict stream, + const CHAR_TYPE * __restrict format, ... ) +{ + va_list arg; + SCNF_SPECS specs; + const char *msg; + int rc; + + /* Basic check for null pointers to see if we can continue */ + if( __check_constraint_nullptr_msg( msg, stream ) + && __check_constraint_nullptr_msg( msg, format ) ) { + + _AccessFile( stream ); + + /*** Deal with stream orientation ***/ + ORIENT_STREAM(stream,0); + + specs.ptr = (CHAR_TYPE *)stream; + specs.cget_rtn = cget_file; + specs.uncget_rtn = uncget_file; + msg = NULL; + + va_start( arg, format ); + rc = __F_NAME(__scnf_s,__wscnf_s)( (PTR_SCNF_SPECS)&specs, format, &msg, arg ); + va_end( arg ); + + _ReleaseFile( stream ); + + if( msg == NULL ) { + /* no rt-constraint violation inside worker routine */ + return( rc ); + } + } + __rtct_fail( __func__, msg, NULL ); + return( __F_NAME(EOF,WEOF) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fseek.c b/programs/develop/open watcom/trunk/clib/streamio/fseek.c new file mode 100644 index 0000000000..96e9dfe293 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fseek.c @@ -0,0 +1,157 @@ +/**************************************************************************** +* +* 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: Implementation of fseek - set stream position. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "fileacc.h" +#include "rtdata.h" +#include "seterrno.h" +#include "flush.h" + + +static int __update_buffer( long diff, FILE *fp ) +{ + /* + diff is relative to fp->_ptr + if diff is within the buffer update the pointers and return 0 + otherwise update the pointers and return 1 + */ + if( diff <= fp->_cnt && diff >= (_FP_BASE(fp) - fp->_ptr) ) { + fp->_flag &= ~(_EOF); + fp->_ptr += diff; + fp->_cnt -= diff; + return( 0 ); + } + return( 1 ); +} + + +/* + * This used to be in __update_buffer(), but we don't want to do this until + * AFTER we've made certain that lseek() will be a successful one. + */ +static void __reset_buffer( FILE *fp ) +{ + fp->_flag &= ~(_EOF); + fp->_ptr = _FP_BASE(fp); + fp->_cnt = 0; +} + + +_WCRTLINK int fseek( FILE *fp, long offset, int origin ) +{ + _ValidFile( fp, -1 ); + _AccessFile( fp ); + /* + if the file is open for any sort of writing we must ensure that + the buffer is flushed when dirty so that the integrity of the + data is preserved. + if there is an ungotten character in the buffer then the data must + be discarded to ensure the integrity of the data + */ + if( fp->_flag & (_WRITE | _UNGET) ) { + if( fp->_flag & _DIRTY ) { + /* + the __flush routine aligns the DOS file pointer with the + start of the resulting cleared buffer, as such, the SEEK_CUR + code used for the non-_DIRTY buffer case is not required + */ + if( __flush( fp ) ) { + // assume __flush set the errno value + // if erroneous input, override errno value + if( origin == SEEK_SET && offset < 0 ) { + __set_errno( EINVAL ); + } + _ReleaseFile( fp ); + return( -1 ); + } + } else { + if( origin == SEEK_CUR ) { + offset -= fp->_cnt; + } + fp->_ptr = _FP_BASE(fp); + fp->_cnt = 0; + } + fp->_flag &= ~(_EOF|_UNGET); + if( lseek( fileno( fp ), offset, origin ) == -1 ) { + _ReleaseFile( fp ); + return( -1 ); + } + } else { + // file is open for read only, + // no characters have been ungotten + // the OS file pointer is at fp->_ptr + fp->_cnt relative to the + // FILE* buffer + switch( origin ) { + case SEEK_CUR: + { long ptr_delta = fp->_cnt; + + if( __update_buffer( offset, fp ) ) { + offset -= ptr_delta; + if( lseek( fileno( fp ), offset, origin ) == -1 ) { + _ReleaseFile( fp ); + return( -1 ); + } + __reset_buffer(fp); + } + } break; + case SEEK_SET: + { long file_ptr = tell( fileno( fp ) ); + + file_ptr -= fp->_cnt; + if( __update_buffer( offset - file_ptr, fp ) ) { + if( lseek( fileno( fp ), offset, origin ) == -1 ) { + _ReleaseFile( fp ); + return( -1 ); + } + __reset_buffer(fp); + } + } break; + case SEEK_END: + fp->_flag &= ~(_EOF); + fp->_ptr = _FP_BASE(fp); + fp->_cnt = 0; + if( lseek( fileno( fp ), offset, origin ) == -1 ) { + _ReleaseFile( fp ); + return( -1 ); + } + break; + default: + __set_errno( EINVAL ); + _ReleaseFile( fp ); + return( -1 ); + } + } + _ReleaseFile( fp ); + return( 0 ); /* indicate success */ +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fsetpos.c b/programs/develop/open watcom/trunk/clib/streamio/fsetpos.c new file mode 100644 index 0000000000..36b1ebb16e --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fsetpos.c @@ -0,0 +1,39 @@ +/**************************************************************************** +* +* 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: Implementation of fsetpos() - set stream position. +* +****************************************************************************/ + + +#include "variety.h" +#include + + +_WCRTLINK int fsetpos( FILE *fp, const fpos_t *pos ) +{ + return( fseek( fp, *pos, SEEK_SET ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/ftell.c b/programs/develop/open watcom/trunk/clib/streamio/ftell.c new file mode 100644 index 0000000000..ca2c5a77ea --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/ftell.c @@ -0,0 +1,61 @@ +/**************************************************************************** +* +* 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: Platform independent ftell() implementaiton. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "fileacc.h" + + +_WCRTLINK long ftell( FILE *fp ) +{ + long pos; + + _ValidFile( fp, -1 ); + if( fp->_flag & _APPEND && fp->_flag & _DIRTY ) { + fflush( fp ); /* if data written in append mode, OS must know */ + } + pos = tell( fileno( fp ) ); + if( pos == -1 ) { + return( -1L ); + } + _AccessFile( fp ); + if( fp->_cnt != 0 ) { /* if something in buffer */ + if( fp->_flag & _DIRTY ) { /* last operation was a put */ + pos += fp->_cnt; + } else { /* last operation was a get */ + pos -= fp->_cnt; + } + } + _ReleaseFile( fp ); + return( pos ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fwide.c b/programs/develop/open watcom/trunk/clib/streamio/fwide.c new file mode 100644 index 0000000000..7774ab6582 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fwide.c @@ -0,0 +1,62 @@ +/**************************************************************************** +* +* 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: Platform independent fwide() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include "fileacc.h" +#include "rtdata.h" + + +_WCRTLINK int fwide( FILE *fp, int mode ) +{ + int new_mode; + + _ValidFile( fp, EOF ); + _AccessFile( fp ); + +#ifndef __NETWARE__ + /* Set orientation if possible */ + if( mode > 0 && _FP_ORIENTATION(fp) == _NOT_ORIENTED ) + _FP_ORIENTATION(fp) = _WIDE_ORIENTED; + else if( mode < 0 && _FP_ORIENTATION(fp) == _NOT_ORIENTED ) + _FP_ORIENTATION(fp) = _BYTE_ORIENTED; + + /* Find out what the current orientation is */ + new_mode = _FP_ORIENTATION(fp) == _WIDE_ORIENTED ? + 1 : (_FP_ORIENTATION(fp) == _BYTE_ORIENTED ? -1 : 0); +#else + new_mode = 0; +#endif + + _ReleaseFile( fp ); + return( new_mode ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/fwrite.c b/programs/develop/open watcom/trunk/clib/streamio/fwrite.c new file mode 100644 index 0000000000..eff6f9ec39 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/fwrite.c @@ -0,0 +1,161 @@ +/**************************************************************************** +* +* 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: Platform independent fwrite() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include "fileacc.h" +#include "rtdata.h" +#include "seterrno.h" +#include "qwrite.h" +#include "flush.h" +#include "streamio.h" + + +_WCRTLINK size_t fwrite( const void *buf, size_t size, size_t n, FILE *fp ) +{ + size_t count; + unsigned oflag; + + _ValidFile( fp, 0 ); + _AccessFile( fp ); + if( (fp->_flag & _WRITE) == 0 ) { + __set_errno( EBADF ); + fp->_flag |= _SFERR; + _ReleaseFile( fp ); + return( 0 ); /* POSIX says return 0 */ + } + n *= size; + if( n == 0 ) { + _ReleaseFile( fp ); + return( n ); + } + if( _FP_BASE(fp) == NULL ) { + __ioalloc( fp ); /* allocate buffer */ + } + oflag = fp->_flag & (_SFERR | _EOF); /* JBS 27-jan-92 */ + fp->_flag &= ~(_SFERR | _EOF); /* JBS 27-jan-92 */ + count = 0; +#if !defined( __UNIX__ ) + if( fp->_flag & _BINARY ) { /* binary I/O */ +#else + { +#endif + size_t bytes_left = n, bytes; + + do { + /* if our buffer is empty, and user's buffer is larger, + then write directly from user's buffer. 28-apr-90 */ + + if( fp->_cnt == 0 && bytes_left >= fp->_bufsize ) { + bytes = bytes_left & -512; /* multiple of 512 */ + if( bytes == 0 ) { + bytes = bytes_left; /* bufsize < 512 */ + } + n = __qwrite( fileno( fp ), buf, bytes ); + if( n == -1 ) { + fp->_flag |= _SFERR; + } +#if !defined( __UNIX__ ) + else if( n == 0 ) { + _RWD_errno = ENOSPC; + fp->_flag |= _SFERR; + } +#endif + bytes = n; + } else { + bytes = fp->_bufsize - fp->_cnt; + if( bytes > bytes_left ) { + bytes = bytes_left; + } + memcpy( fp->_ptr, buf, bytes ); + fp->_ptr += bytes; + fp->_cnt += bytes; + fp->_flag |= _DIRTY; + if( (fp->_cnt == fp->_bufsize) || (fp->_flag & _IONBF) ) { + __flush(fp); + } + } + buf = ((const char *)buf) + bytes; + count += bytes; + bytes_left -= bytes; + } while( bytes_left && !ferror( fp ) ); +#if !defined( __UNIX__ ) + } else { /* text I/O */ + const char *bufptr; + int not_buffered; + #ifndef __NETWARE__ + int old_orientation; + #endif + /* temporarily enable buffering saving the previous setting */ + not_buffered = 0; + if( fp->_flag & _IONBF ) { + not_buffered = 1; + fp->_flag &= ~_IONBF; + fp->_flag |= _IOFBF; + } + + /*** Use fputc, and make it think the stream is byte-oriented ***/ + #ifndef __NETWARE__ + old_orientation = _FP_ORIENTATION(fp); + _FP_ORIENTATION(fp) = _BYTE_ORIENTED; + #endif + bufptr = (const char *)buf; + do { + fputc( *(bufptr++), fp ); + if( fp->_flag & (_EOF | _SFERR) ) break; + ++count; + } while( count != n ); + #ifndef __NETWARE__ + _FP_ORIENTATION(fp) = old_orientation; + #endif + + if( not_buffered ) { /* if wasn't buffered, then reset */ + fp->_flag &= ~_IOFBF; + fp->_flag |= _IONBF; + __flush( fp ); + } +#endif + } + if( fp->_flag & _SFERR ) { + /* + * Quantum 11-17-92 Temporary buffering confuses the return + * value if the call is interrupted. + * kludge: return 0 on error + */ + count = 0; + } + fp->_flag |= oflag; /* JBS 27-jan-92 */ + _ReleaseFile( fp ); + return( count / size ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/getc.c b/programs/develop/open watcom/trunk/clib/streamio/getc.c new file mode 100644 index 0000000000..7e6152f742 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/getc.c @@ -0,0 +1,45 @@ +/**************************************************************************** +* +* 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: Implementation of getc() - read character from stream. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK INTCHAR_TYPE __F_NAME((getc),(getwc))( FILE *fp ) +{ + __stream_check( fp, 1 ); +#if !defined( __WIDECHAR__ ) && defined( getc ) + return( getc( fp ) ); +#else + return( __F_NAME(fgetc,fgetwc)( fp ) ); +#endif +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/getchar.c b/programs/develop/open watcom/trunk/clib/streamio/getchar.c new file mode 100644 index 0000000000..491fe5c6c1 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/getchar.c @@ -0,0 +1,46 @@ +/**************************************************************************** +* +* 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: Implementation of getchar() - read character from stdin. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + +#undef getchar + + +_WCRTLINK INTCHAR_TYPE __F_NAME(getchar,getwchar)( void ) +{ +#ifdef getc + return( __F_NAME(getc,getwc)( stdin ) ); +#else + return( __F_NAME(fgetc,fgetwc)( stdin ) ); +#endif +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/gets.c b/programs/develop/open watcom/trunk/clib/streamio/gets.c new file mode 100644 index 0000000000..9ad8ab1a8f --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/gets.c @@ -0,0 +1,57 @@ +/**************************************************************************** +* +* 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: Implementation of gets() - read string from stdin. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK CHAR_TYPE *__F_NAME(gets,getws)( CHAR_TYPE *s ) +{ + INTCHAR_TYPE c; + CHAR_TYPE *cs; + unsigned oflag; + + oflag = stdin->_flag & (_SFERR | _EOF); /* 06-sep-91 */ + stdin->_flag &= ~(_SFERR | _EOF); + cs = s; + while( (c = __F_NAME((getc),(getwc))( stdin )) != __F_NAME(EOF,WEOF) + && (c != __F_NAME('\n',L'\n')) ) { + *cs++ = c; + } + if( c == __F_NAME(EOF,WEOF) && (cs == s || ferror( stdin )) ) { + s = NULL; + } else { + *cs = NULLCHAR; + } + stdin->_flag |= oflag; /* 06-sep-91 */ + return( s ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/gets_s.c b/programs/develop/open watcom/trunk/clib/streamio/gets_s.c new file mode 100644 index 0000000000..05b0bba0fd --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/gets_s.c @@ -0,0 +1,106 @@ +/**************************************************************************** +* +* 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: Implementation of gets_s(). +* +****************************************************************************/ + +#include "variety.h" +#include "saferlib.h" +#include + +_WCRTLINK extern char *gets_s( char *s, rsize_t n ) +/*************************************************/ +{ + int c; + char *cs; + rsize_t len; + unsigned oflag; + + /* Check runtime constraint: s shall not be a null pointer */ + if( ! __check_constraint_nullptr( s ) ) { + /* Just trigger the constraint handler if necessary */ + } + + /* Check runtime constraint: n shall not be zero nor + greater than RSIZE_MAX */ + if( ! __check_constraint_zero( n ) + || ! __check_constraint_maxsize( n ) ) { + n = 0; + } + + /* Filter out stream error and EOF status during this function */ + oflag = stdin->_flag & (_SFERR | _EOF); + stdin->_flag &= ~(_SFERR | _EOF); + + cs = s; + len = n; + + /* + Read data until any of following conditions is met: + - Received EOF + - Read error + - New line character received + */ + while( (c = getc( stdin )) != EOF + && (c != '\n') ) { + if( cs && len ) { + *cs++ = c; + len--; + } + } + + /* Post-process runtime constraints, return NULL when catched */ + if( s == NULL || n == 0) { + stdin->_flag |= oflag; + return( NULL ); + } + + /* After this point, it is assumed that s is valid and + contains at least one character */ + + /* Catch too small buffer and I/O error */ + if( ! __check_constraint_toosmall( s, len ) + || ferror( stdin ) ) { + stdin->_flag |= oflag; + *s = 0; + return( NULL ); + } + + /* Restore stream error states if they were present previously */ + stdin->_flag |= oflag; + + /* If we got EOF and didn't get any characters, return NULL */ + if( c == EOF && cs == s ) { + *s = 0; + return( NULL ); + } + + /* Terminate string */ + *cs = 0; + + return( s ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/getw.c b/programs/develop/open watcom/trunk/clib/streamio/getw.c new file mode 100644 index 0000000000..83aa625819 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/getw.c @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of _getw(). +* +****************************************************************************/ + + +#include "variety.h" +#include + + +_WCRTLINK int _getw( FILE *fp ) +{ + int ch; + size_t rc; + + rc = fread( &ch, sizeof( int ), 1, fp ); + if( rc > 0 ) { + return( ch ); + } else { + return( EOF ); + } +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/initfile.c b/programs/develop/open watcom/trunk/clib/streamio/initfile.c new file mode 100644 index 0000000000..faf78576f7 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/initfile.c @@ -0,0 +1,67 @@ +/**************************************************************************** +* +* 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: Stream I/O initializer. +* +****************************************************************************/ + + +//#include "dll.h" // needs to be first +#include "variety.h" +#include +#include +#include "liballoc.h" +#include "mf.h" +#include "rtdata.h" +#include "exitwmsg.h" + +void __InitFiles( void ) +{ + __stream_link *link; + FILE *fp; + + fp = _RWD_iob; + + stderr->_flag &= ~(_IONBF | _IOLBF | _IOFBF); + stderr->_flag |= _IONBF; + for( fp = _RWD_iob; fp->_flag != 0; ++fp ) + { + link = lib_malloc( sizeof( __stream_link ) ); + if( link == NULL ) + { + __fatal_runtime_error( + "Not enough memory to allocate file structures\r\n", 1 ); + } + link->stream = fp; + link->next = _RWD_ostream; + _RWD_ostream = link; + fp->_link = link; + fp->_link->_base = NULL; + fp->_link->_tmpfchar = 0; + fp->_link->_orientation = _NOT_ORIENTED; + } + _RWD_cstream = NULL; +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/ioalloc.c b/programs/develop/open watcom/trunk/clib/streamio/ioalloc.c new file mode 100644 index 0000000000..ebba1213cf --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/ioalloc.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: Platform independent __ioalloc() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "liballoc.h" +#include +#include "rtdata.h" +#include "streamio.h" + + +void __ioalloc( FILE *fp ) +{ + __chktty( fp ); /* JBS 28-aug-90 */ + if( fp->_bufsize == 0 ) { + if( fp->_flag & _IOLBF ) { + fp->_bufsize = 134; + } else if( fp->_flag & _IONBF ) { + /* Use small but reasonably sized buffer; otherwise we will end + * up calling into the OS for every character, completely killing + * performance on unbuffered stream output through printf() etc., + * especially in extended DOS because of mode switches. + */ + fp->_bufsize = 64; + } else { + fp->_bufsize = BUFSIZ; + } + } + _FP_BASE(fp) = lib_malloc( fp->_bufsize ); + if( _FP_BASE(fp) == NULL ) { + fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF); + fp->_flag |= _IONBF; /* can't get big buffer */ + _FP_BASE(fp) = (char *)&(fp->_ungotten); + fp->_bufsize = 1; + } else { + fp->_flag |= _BIGBUF; /* got big buffer */ + } + fp->_ptr = _FP_BASE(fp); + fp->_cnt = 0; +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/iob.c b/programs/develop/open watcom/trunk/clib/streamio/iob.c new file mode 100644 index 0000000000..9454e345cb --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/iob.c @@ -0,0 +1,62 @@ +/**************************************************************************** +* +* 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: Definition of __iob array. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "rtdata.h" +#include "rtinit.h" +#include "tmpfname.h" + + +_WCRTLINKD FILE _WCNEAR __iob[_NFILES] = { + { NULL, 0, NULL, _READ, 0, 0, 0 } /* stdin */ + ,{ NULL, 0, NULL, _WRITE, 1, 0, 0 } /* stdout */ + ,{ NULL, 0, NULL, _WRITE, 2, 0, 0 } /* stderr */ +#if defined( __DOS__ ) || defined( __WINDOWS__ ) || defined( __OSI__ ) + ,{ NULL, 0, NULL, _READ|_WRITE, 3, 0, 0 } /* stdaux */ + ,{ NULL, 0, NULL, _WRITE, 4, 0, 0 } /* stdprn */ +#endif +}; + +__stream_link *__ClosedStreams; +__stream_link *__OpenStreams; + +#if !defined( __UNIX__ ) +_WCRTLINKD int _WCNEAR _fmode = O_TEXT; /* default file translation mode */ +#endif + +extern void __InitFiles(); +extern void __full_io_exit(); + +AXI(__InitFiles,INIT_PRIORITY_LIBRARY); +AYI(__full_io_exit,INIT_PRIORITY_LIBRARY); diff --git a/programs/develop/open watcom/trunk/clib/streamio/iobaddr.c b/programs/develop/open watcom/trunk/clib/streamio/iobaddr.c new file mode 100644 index 0000000000..e3e0de0f12 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/iobaddr.c @@ -0,0 +1,140 @@ +/**************************************************************************** +* +* 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: Standard stream/file accessor routines. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "rtdata.h" + + +#if !defined( __NETWARE__ ) && !defined( _THIN_LIB ) + +_WCRTLINK FILE *__get_std_stream( unsigned handle ) +{ + if( handle > NUM_STD_STREAMS ) { + return( NULL ); + } else { + return( &_RWD_iob[handle] ); + } +} + +_WCRTLINK FILE *__get_std_file( unsigned handle ) +{ + return( __get_std_stream( handle ) ); +} + +#else + +#include + +#if defined( _NETWARE_LIBC ) + extern FILE **___stdin ( void ); + extern FILE **___stdout( void ); + extern FILE **___stderr( void ); + extern FILE **___cin ( void ); + extern FILE **___cout ( void ); + + _WCRTLINK FILE *__get_std_stream( unsigned handle ) + { + FILE *pFile = NULL; + + switch( handle ) { + case STDIN_FILENO: + pFile = *___stdin(); + break; + case STDOUT_FILENO: + pFile = *___stdout(); + break; + case STDERR_FILENO: + pFile = *___stderr(); + break; + default: + break; + } + return( pFile ); + } +#elif defined( _NETWARE_CLIB ) + extern FILE **__get_stdin ( void ); + extern FILE **__get_stdout( void ); + extern FILE **__get_stderr( void ); + + _WCRTLINK FILE *__get_std_stream( unsigned handle ) + { + FILE *pFile = NULL; + + switch( handle ) { + case STDIN_FILENO: + pFile = *__get_stdin(); + break; + case STDOUT_FILENO: + pFile = *__get_stdout(); + break; + case STDERR_FILENO: + pFile = *__get_stderr(); + break; + default: + break; + } + return( pFile ); + } +#endif + +#endif + + +#if defined( __NETWARE__ ) && !defined( _THIN_LIB ) + +#include + +FILE **__get_stdin( void ) +{ + static FILE *stdin_ptr; + + stdin_ptr = __get_std_stream( STDIN_FILENO ); + return( &stdin_ptr ); +} + +FILE **__get_stdout( void ) +{ + static FILE *stdout_ptr; + + stdout_ptr = __get_std_stream( STDOUT_FILENO ); + return( &stdout_ptr ); +} + +FILE **__get_stderr( void ) +{ + static FILE *stderr_ptr; + + stderr_ptr = __get_std_stream( STDERR_FILENO ); + return( &stderr_ptr ); +} + +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/iobptr.c b/programs/develop/open watcom/trunk/clib/streamio/iobptr.c new file mode 100644 index 0000000000..cd1dd959fb --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/iobptr.c @@ -0,0 +1,40 @@ +/**************************************************************************** +* +* 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: Accessor function for __iob array. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "rtdata.h" + + +_WCRTLINK FILE (*__get_iob_ptr( void ))[] +{ + return( &__iob ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/noefgfmt.c b/programs/develop/open watcom/trunk/clib/streamio/noefgfmt.c new file mode 100644 index 0000000000..81157cf7cc --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/noefgfmt.c @@ -0,0 +1,46 @@ +/**************************************************************************** +* +* 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: Dummy floating-point formatting routines. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "rtdata.h" +#include "exitwmsg.h" +#include "ftos.h" +#include "farsupp.h" + + +static void _no_support_loaded( void ) +{ + __fatal_runtime_error( "Floating-point support not loaded\r\n", 1 ); +} + +_WCRTLINK FAR_STRING (*__EFG_printf)() = (FAR_STRING (*)())_no_support_loaded; +_WCRTLINK void (*__EFG_scanf)() = _no_support_loaded; diff --git a/programs/develop/open watcom/trunk/clib/streamio/perror.c b/programs/develop/open watcom/trunk/clib/streamio/perror.c new file mode 100644 index 0000000000..d18d093379 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/perror.c @@ -0,0 +1,51 @@ +/**************************************************************************** +* +* 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: Implementation of perror() - print error message. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include +#include +#include "rtdata.h" +#include "seterrno.h" + + +_WCRTLINK void __F_NAME(perror,_wperror)( const CHAR_TYPE *s ) +{ + __null_check( s, 0 ); + if( s != NULL && *s != '\0' ) { + __F_NAME(fputs,fputws)( s, stderr ); + __F_NAME(fputs,fputws)( __F_NAME(": ",L": "), stderr ); + } + __F_NAME(fputs,fputws)( __F_NAME(strerror,wcserror)( _RWD_errno ), stderr ); + __F_NAME(fputc,fputwc)( '\n', stderr ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/printf.c b/programs/develop/open watcom/trunk/clib/streamio/printf.c new file mode 100644 index 0000000000..3bf34cdd67 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/printf.c @@ -0,0 +1,73 @@ +/**************************************************************************** +* +* 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: Implementation of printf() - formatted output. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "farsupp.h" +#include "printf.h" +#include "fprtf.h" + +void debug_out_str(const char* str); +void _stdcall debug_out(int ch); + +static char _dest[512]; + +_WCRTLINK int __F_NAME(printf,wprintf)( const CHAR_TYPE *format, ... ) +{ + int retval; + auto va_list args; + + va_start( args, format ); +// return( __F_NAME(__fprtf,__fwprtf)( stdout, format, args ) ); + + va_start( args, format ); + #ifdef __WIDECHAR__ + retval = _vswprintf( _dest, format, args ); + #else + retval = vsprintf( _dest, format, args ); + #endif + debug_out_str(_dest); + return retval; +} + +void debug_out_str(const char* str) +{ + while (*str != 0) + { + debug_out(*str); + str++; + } +} + + + diff --git a/programs/develop/open watcom/trunk/clib/streamio/printf_s.c b/programs/develop/open watcom/trunk/clib/streamio/printf_s.c new file mode 100644 index 0000000000..694d6f206d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/printf_s.c @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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: Implementation of printf_s() - safe formatted output. +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#include "fprtf_s.h" + + +_WCRTLINK int __F_NAME(printf_s,wprintf_s)( const CHAR_TYPE * __restrict format, ... ) +{ + va_list args; + + va_start( args, format ); + return( __F_NAME(__fprtf_s,__fwprtf_s)( stdout, format, args ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/prtf.c b/programs/develop/open watcom/trunk/clib/streamio/prtf.c new file mode 100644 index 0000000000..2df55a0219 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/prtf.c @@ -0,0 +1,1100 @@ +/**************************************************************************** +* +* 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: __prtf() - low level string formatter. +* +****************************************************************************/ + + +#define __LONG_LONG_SUPPORT__ + +#if !defined( __NETWARE__ ) && !defined( __UNIX__ ) + #define USE_MBCS_TRANSLATION +#endif + +#include "variety.h" +#ifdef SAFE_PRINTF + #include "saferlib.h" +#endif +#include "widechar.h" +#include +#include +#include +#include +#include +#if defined( __WIDECHAR__ ) || defined( USE_MBCS_TRANSLATION ) + #include +#endif +#include "ftos.h" +#include "farsupp.h" +#include "printf.h" +#include "prtscncf.h" +#include "fixpoint.h" +#include "myvalist.h" + +#define BUF_SIZE 72 /* 64-bit ints formatted as binary can get big */ +#define TRUE 1 +#define FALSE 0 + +#define PASCAL_STRING 'S' /* for Novell */ +#define WIDE_CHAR_STRING 'S' + +#if defined( __QNX_386__ ) + /* for use in QNX 32-bit shared library */ + #pragma aux __prtf "_sl_*" far; +#endif + + +#if defined( __QNX__ ) + #define EFG_PRINTF __EFG_Format +#else + #define EFG_PRINTF (*__EFG_printf) +#endif + +extern FAR_STRING EFG_PRINTF( char *buffer, my_va_list *args, _mbcs_SPECS __SLIB *specs ); + + +#if defined( __WIDECHAR__ ) + #define _FAR_OTHER_STRING FAR_ASCII_STRING +#else + #define _FAR_OTHER_STRING FAR_UNI_STRING +#endif + + +#if defined( __WINDOWS_386__ ) + #ifdef __SW_3S + #pragma aux slib_callback_t modify [eax edx ecx fs gs]; + #else + #pragma aux slib_callback_t modify [fs gs]; + #endif +#endif + + +/* forward references */ +static const CHAR_TYPE *evalflags( const CHAR_TYPE *, SPECS __SLIB * ); +static FAR_STRING formstring( CHAR_TYPE *, my_va_list *, SPECS __SLIB *, CHAR_TYPE * ); +static const CHAR_TYPE * getprintspecs( const CHAR_TYPE *, my_va_list *, SPECS __SLIB * ); +#ifdef USE_MBCS_TRANSLATION +static void write_wide_string( FAR_UNI_STRING str, SPECS *specs, + slib_callback_t *out_putc ); +static void write_skinny_string( FAR_ASCII_STRING str, SPECS *specs, + slib_callback_t *out_putc ); +#endif + +#ifdef SAFE_PRINTF + int __F_NAME(__prtf_s,__wprtf_s) +#else + int __F_NAME(__prtf,__wprtf) +#endif + ( void __SLIB *dest, /* parm for use by out_putc */ + const CHAR_TYPE *format, /* pointer to format string */ + va_list args, /* pointer to pointer to args*/ +#ifdef SAFE_PRINTF + const char **msg, /* rt-constraint message */ +#endif + slib_callback_t *out_putc ) /* char output routine */ +{ + CHAR_TYPE buffer[ BUF_SIZE ]; + CHAR_TYPE null_char = '\0'; + CHAR_TYPE *a; + FAR_STRING arg; + const CHAR_TYPE *ctl; + SPECS specs; + + specs._dest = dest; + specs._flags = 0; + specs._version = SPECS_VERSION; + specs._output_count = 0; + ctl = format; + while( *ctl != NULLCHAR ) { + if( *ctl != '%' ) { + (out_putc)( &specs, *ctl++ ); + } else { + ++ctl; + { + my_va_list pargs; + pargs = MY_VA_LIST( args ); + ctl = getprintspecs( ctl, &pargs, &specs ); + MY_VA_LIST( args ) = pargs; + } + + specs._character = *ctl++; + if( specs._character == NULLCHAR ) + break; /* 05-jan-89 */ + + if( specs._character == 'n' ) { +#ifdef SAFE_PRINTF + /* The %n specifier is not allowed - too dangerous. */ + *msg = "%n"; + break; +#else + FAR_INT iptr; + +#if defined( __FAR_SUPPORT__ ) + if( specs._flags & SPF_FAR ) { + iptr = va_arg( args, int _WCFAR * ); + } else if( specs._flags & SPF_NEAR ) { + iptr = va_arg( args, int _WCNEAR * ); + } else { + iptr = va_arg( args, int * ); + } +#else + iptr = va_arg( args, int * ); +#endif + if( specs._flags & SPF_CHAR ) { + *((FAR_CHAR)iptr) = specs._output_count; + } else if( specs._flags & SPF_SHORT ) { + *((FAR_SHORT)iptr) = specs._output_count; + } else if( specs._flags & SPF_LONG ) { + *((FAR_LONG)iptr) = specs._output_count; +#if defined( __LONG_LONG_SUPPORT__ ) + } else if( specs._flags & SPF_LONG_LONG ) { + *((FAR_INT64)iptr) = specs._output_count; +#endif + } else { + *iptr = specs._output_count; + } +#endif /* SAFE_PRINTF */ + } else { +#ifdef SAFE_PRINTF + if( specs._character == 's' || specs._character == 'S' ) { + FAR_STRING str; + va_list args_copy; + + /* Make sure %s argument is not NULL. Note that near pointers + * in segmented models need special handling because only + * offset will be NULL, not segment. + */ + va_copy( args_copy, args ); +#if defined( __FAR_SUPPORT__ ) + if( specs._flags & SPF_FAR ) { + str = va_arg( args_copy, CHAR_TYPE _WCFAR * ); + } else if( specs._flags & SPF_NEAR ) { + CHAR_TYPE _WCNEAR *ptr; + + ptr = va_arg( args_copy, CHAR_TYPE _WCNEAR * ); + if( ptr == NULL ) { + str = NULL; + } else { + str = ptr; + } + } else { + CHAR_TYPE *ptr; + + ptr = va_arg( args_copy, CHAR_TYPE * ); + if( ptr == NULL ) { + str = NULL; + } else { + str = ptr; + } + } +#else + str = va_arg( args_copy, CHAR_TYPE * ); +#endif + va_end( args_copy ); + if( str == NULL ) { + *msg = "%s -> NULL"; + break; /* bail out */ + } + } +#endif /* SAFE_PRINTF */ + + { + my_va_list pargs; + pargs = MY_VA_LIST( args ); + arg = formstring( buffer, &pargs, &specs, &null_char ); + MY_VA_LIST( args ) = pargs; + } + specs._fld_width -= specs._n0 + + specs._nz0 + + specs._n1 + + specs._nz1 + + specs._n2 + + specs._nz2; + if( !(specs._flags & SPF_LEFT_ADJUST) ) { + if( specs._pad_char == ' ' ) { + while( specs._fld_width > 0 ) { + (out_putc)( &specs, ' ' ); + --specs._fld_width; + } + } + } + a = buffer; + while( specs._n0 > 0 ) { + (out_putc)( &specs, *a ); + ++a; + --specs._n0; + } + while( specs._nz0 > 0 ) { + (out_putc)( &specs, '0' ); + --specs._nz0; + } + if( specs._character == 's' ) { +#if defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + if( specs._flags & SPF_SHORT ) { + write_skinny_string( (FAR_ASCII_STRING)arg, &specs, out_putc ); + } else +#elif !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + if( specs._flags & SPF_LONG ) { + write_wide_string( (FAR_UNI_STRING)arg, &specs, out_putc ); + } else +#endif + { + while( specs._n1 > 0 ) { + (out_putc)( &specs, *arg++ ); + --specs._n1; + } + } + } +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + else if( specs._character == WIDE_CHAR_STRING ) { + write_wide_string( (FAR_UNI_STRING)arg, &specs, out_putc ); + } else +#elif !defined( __WIDECHAR__ ) && defined( __NETWARE__ ) + else if( specs._character == WIDE_CHAR_STRING ) { + } else +#endif + { + while( specs._n1 > 0 ) { + (out_putc)( &specs, *arg++ ); + --specs._n1; + } + } + while( specs._nz1 > 0 ) { + (out_putc)( &specs, '0' ); + --specs._nz1; + } + while( specs._n2 > 0 ) { + (out_putc)( &specs, *arg ); + ++arg; + --specs._n2; + } + while( specs._nz2 > 0 ) { + (out_putc)( &specs, '0' ); + --specs._nz2; + } + if( specs._flags & SPF_LEFT_ADJUST ) { + while( specs._fld_width > 0 ) { + (out_putc)( &specs, ' ' ); + --specs._fld_width; + } + } + } + } + } + return( specs._output_count ); +} + +static const CHAR_TYPE * getprintspecs( const CHAR_TYPE *ctl, + my_va_list *pargs, + SPECS __SLIB *specs ) +{ + specs->_pad_char = ' '; + ctl = evalflags( ctl, specs ); + specs->_fld_width = 0; + if( *ctl == '*' ) { + specs->_fld_width = va_arg( pargs->v, int ); + if( specs->_fld_width < 0 ) { + specs->_fld_width = - specs->_fld_width; + specs->_flags |= SPF_LEFT_ADJUST; + } + ctl++; + } else { + while(( *ctl >= '0' ) && ( *ctl <= '9' )) { + specs->_fld_width = specs->_fld_width * 10 + ( *ctl++ - '0' ); + } + } + specs->_prec = -1; + if( *ctl == '.' ) { + specs->_prec = 0; + ctl++; + if( *ctl == '*' ) { + specs->_prec = va_arg( pargs->v, int ); + if( specs->_prec < 0 ) + specs->_prec = -1; /* 19-jul-90 */ + ctl++; + } else { + while(( *ctl >= '0' ) && ( *ctl <= '9' )) { + specs->_prec = specs->_prec * 10 + ( *ctl++ - '0' ); + } + } + /* + "For b, d, i, o, u, x, X, e, E, f, g and G conversions, leading + zeros (following any indication of sign or base) are used to + pad the field width; no space padding is performed. If the 0 + or - flags both appear, the 0 flag is ignored. For b, d, i, o, + u, x or X conversions, if a precision is specified, the 0 flag + is ignored. For other conversions, the behaviour is undefined." + */ +// if( specs->_prec != -1 ) specs->_pad_char = ' '; /* 30-jul-95 *//*removed by JBS*/ + } + switch( *ctl ) { + case 'l': +#if defined( __LONG_LONG_SUPPORT__ ) + if( ctl[1] == 'l' ) { + specs->_flags |= SPF_LONG_LONG; + ctl += 2; + break; + } +#endif + /* fall through */ + ZSPEC_CASE_LONG + TSPEC_CASE_LONG + case 'w': + specs->_flags |= SPF_LONG; + ctl++; + break; + case 'h': + if( ctl[1] == 'h' ) { + specs->_flags |= SPF_CHAR; + ctl += 2; + break; + } + specs->_flags |= SPF_SHORT; + ctl++; + break; +#if defined( __LONG_LONG_SUPPORT__ ) + case 'I': + if(( ctl[1] == '6' ) && ( ctl[2] == '4' )) { + specs->_flags |= SPF_LONG_LONG; + ctl += 3; + } + break; + JSPEC_CASE_LLONG + /* fall through */ +#endif + case 'L': + specs->_flags |= SPF_LONG_DOUBLE | SPF_LONG_LONG; + ctl++; + break; +#if defined( __FAR_SUPPORT__ ) + case 'F': /* conflicts with ISO-defined 'F' conversion */ + /* fall through */ +#endif + case 'W': /* 8086 specific flag for FAR pointer */ + specs->_flags |= SPF_FAR; + ctl++; + break; + case 'N': /* 8086 specific flag for NEAR pointer */ + specs->_flags |= SPF_NEAR; + ctl++; + break; +#if defined( TSPEC_IS_INT ) || defined( ZSPEC_IS_INT ) + TSPEC_CASE_INT /* If either 't' or 'z' spec corresponds to 'int', */ + ZSPEC_CASE_INT /* we need to parse and ignore the spec. */ + ctl++; + break; +#endif + } + return( ctl ); +} + + +static const CHAR_TYPE *evalflags( const CHAR_TYPE *ctl, SPECS __SLIB *specs ) +{ + specs->_flags = 0; + for( ; ; ctl++ ) { + if( *ctl == '-' ) { + specs->_flags |= SPF_LEFT_ADJUST; + } else if( *ctl == '#' ) { + specs->_flags |= SPF_ALT; + } else if( *ctl == '+' ) { + specs->_flags |= SPF_FORCE_SIGN; + specs->_flags &= ~SPF_BLANK; + } else if( *ctl == ' ' ) { + if( ( specs->_flags & SPF_FORCE_SIGN ) == 0 ) { + specs->_flags |= SPF_BLANK; + } + } else if( *ctl == '0' ) { + specs->_pad_char = '0'; +#ifdef __QNX__ + specs->_flags |= SPF_ZERO_PAD; +#endif + } else { + break; + } + } + return( ctl ); +} + + +static int far_strlen( FAR_STRING s, int precision ) +{ + int len; + + len = 0; + while(( len != precision ) && ( *s++ != NULLCHAR )) + ++len; + + return( len ); +} + +/* + * far_other_strlen - calculates the length of an ascii string + * for the unicode version + * - calculates the length of a unicode string for + * the standard version + */ + +static int far_other_strlen( FAR_STRING s, int precision ) +{ + int len = 0; + _FAR_OTHER_STRING ptr = (_FAR_OTHER_STRING)s; + +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + char mbBuf[MB_CUR_MAX]; + int chBytes; + + if( precision == -1 ) { + while( *ptr ) { + chBytes = wctomb( mbBuf, *ptr++ ); + if( chBytes != -1 ) { + len += chBytes; + } + } + return( len ); + } + while( *ptr && ( len <= precision )) { + chBytes = wctomb( mbBuf, *ptr++ ); + if( chBytes != -1 ) { + len += chBytes; + } + } + return(( len <= precision ) ? len : precision ); + +#else + + while( *ptr++ && ( len != precision )) + ++len; + + return( len ); +#endif +} + +static void fmt4hex( unsigned value, CHAR_TYPE *buf, int maxlen ) +{ + int i, len; + + __F_NAME(itoa,_itow)( value, buf, 16 ); + len = __F_NAME(strlen,wcslen)( buf ); + for( i = maxlen - 1; len; --i ) { + --len; + buf[i] = buf[len]; + } + while( i >= 0 ) { + buf[i] = '0'; + --i; + } + buf[maxlen] = NULLCHAR; +} + + +static void FixedPoint_Format( CHAR_TYPE *buf, long value, SPECS __SLIB *specs ) +{ + T32 at; + int i; + CHAR_TYPE *bufp; + + at.sWhole = value; + if( at.sWhole < 0 ) { + at.sWhole = - at.sWhole; + *buf++ = '-'; + } + if( specs->_prec == -1 ) + specs->_prec = 4; + + __F_NAME(itoa,_itow)( at.wd.hi, buf, 10 ); + bufp = buf; /* remember start address of buffer */ + while( *buf ) + ++buf; + + if( specs->_prec != 0 ) { + *buf++ = '.'; + for( i = 0; i < specs->_prec; i++ ) { + at.wd.hi = 0; + at.uWhole *= 10; + *buf++ = at.bite.b3 + '0'; + } + *buf = NULLCHAR; + } + if( at.wd.lo & 0x8000 ) { /* fraction >= .5, need to round */ + for(;;) { /* 22-dec-91 */ + if( buf == bufp ) { + *buf++ = '1'; + while( *buf == '0' ) + ++buf; + + if( *buf == '.' ) { + *buf++ = '0'; + *buf++ = '.'; + while( *buf == '0' ) { + ++buf; + } + } + *buf++ = '0'; + *buf = NULLCHAR; + break; + } + --buf; + if( *buf == '.' ) + --buf; + + if( *buf != '9' ) { + ++ *buf; + break; + } + *buf = '0'; + } + } +} + +static void float_format( CHAR_TYPE *buffer, my_va_list *pargs, SPECS __SLIB *specs ) +{ +#ifdef __WIDECHAR__ + char mbBuffer[BUF_SIZE*MB_CUR_MAX]; + _mbcs_SPECS mbSpecs; + int count; + size_t rc; + char *p; +#endif // __WIDECHAR__ + +#ifdef __WIDECHAR__ + /* + * EFG_PRINTF can only handle MBCS buffers and the MBCS version of the + * SPECS structure. So, make a _mbcs_SPECS structure equivalent to the + * _wide_SPECS one, and use that instead. Note that we can't use + * memcpy() because some field sizes are different. + */ + mbSpecs._dest = NULL; /* this field isn't actually used */ + mbSpecs._flags = specs->_flags; + mbSpecs._version = specs->_version; + mbSpecs._fld_width = specs->_fld_width; + mbSpecs._prec = specs->_prec; + mbSpecs._output_count = specs->_output_count; + mbSpecs._n0 = specs->_n0; + mbSpecs._nz0 = specs->_nz0; + mbSpecs._n1 = specs->_n1; + mbSpecs._nz1 = specs->_nz1; + mbSpecs._n2 = specs->_n2; + mbSpecs._nz2 = specs->_nz2; + mbSpecs._character = (char)specs->_character; + mbSpecs._pad_char = (char)specs->_pad_char; +#endif + +#ifdef __WIDECHAR__ + EFG_PRINTF( mbBuffer, pargs, &mbSpecs ); +#else + EFG_PRINTF( buffer, pargs, specs ); +#endif + +#ifdef __WIDECHAR__ + /* + * Now convert the returned information back into our _wide_SPECS + * structure. We can't just use mbstowcs because it's an array of + * characters, not a string. + */ + p = mbBuffer; + for( count = 0; count < BUF_SIZE; count++ ) { + rc = mbtowc( &(buffer[count]), p, MB_CUR_MAX ); + if( rc == -1 ) { + buffer[count] = L'?'; + } + p = _mbsinc( p ); + } + specs->_flags = mbSpecs._flags; + specs->_version = mbSpecs._version; + specs->_fld_width = mbSpecs._fld_width; + specs->_prec = mbSpecs._prec; + specs->_output_count = mbSpecs._output_count; + specs->_n0 = mbSpecs._n0; + specs->_nz0 = mbSpecs._nz0; + specs->_n1 = mbSpecs._n1; + specs->_nz1 = mbSpecs._nz1; + specs->_n2 = mbSpecs._n2; + specs->_nz2 = mbSpecs._nz2; + specs->_character = (wchar_t) mbSpecs._character; + specs->_pad_char = (wchar_t) mbSpecs._pad_char; +#endif +} + +static void SetZeroPad( SPECS __SLIB *specs ) +{ + int n; + + if( !(specs->_flags & SPF_LEFT_ADJUST) ) { + if( specs->_pad_char == '0' ) { + n = specs->_fld_width - specs->_n0 - specs->_nz0 - + specs->_n1 - specs->_nz1 - specs->_n2 - specs->_nz2; + if( n > 0 ) { + specs->_nz0 += n; + } + } + } +} + + +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) +static void write_wide_string( FAR_UNI_STRING str, SPECS *specs, + slib_callback_t *out_putc ) +{ + int bytes; + char mbBuf[MB_CUR_MAX]; + char *mbBufPtr; + + while( specs->_n1 > 0 ) { + bytes = wctomb( mbBuf, *str++ ); + if( bytes != -1 ) { + if( bytes <= specs->_n1 ) { + mbBufPtr = mbBuf; + while( bytes-- ) { + (out_putc)( specs, *mbBufPtr++ ); + --specs->_n1; + } + } else { + specs->_n1 = 0; + } + } + } +} +#endif + + +#if defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) +static void write_skinny_string( FAR_ASCII_STRING str, SPECS *specs, + slib_callback_t *out_putc ) +{ + int bytes; + wchar_t wc; + FAR_ASCII_STRING mbPtr = str; + char mbBuf[MB_CUR_MAX]; + + while( specs->_n1 > 0 ) { + mbBuf[0] = *mbPtr++; + if( _ismbblead( mbBuf[0] ) ) + mbBuf[1] = *mbPtr++; + + bytes = mbtowc( &wc, mbBuf, MB_CUR_MAX ); + if( bytes <= specs->_n1 ) { + if( bytes != -1 ) { + (out_putc)( specs, wc ); + specs->_n1 -= bytes; + } + } else { + specs->_n1 = 0; + } + } +} +#endif + + +static FAR_STRING formstring( CHAR_TYPE *buffer, my_va_list *pargs, + SPECS __SLIB *specs, CHAR_TYPE *null_string ) +{ + FAR_STRING arg; + int length; + int radix; +#if defined( __LONG_LONG_SUPPORT__ ) + unsigned long long long_long_value; +#endif + unsigned long long_value; + unsigned int int_value; +#if defined( __FAR_SUPPORT__ ) + unsigned int seg_value; +#endif +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + int bytes; +#endif + + arg = buffer; + + specs->_n0 = specs->_nz0 = + specs->_n1 = specs->_nz1 = + specs->_n2 = specs->_nz2 = 0; + + if( ( specs->_character == 'b' ) || + ( specs->_character == 'o' ) || + ( specs->_character == 'u' ) || + ( specs->_character == 'x' ) || + ( specs->_character == 'X' ) ) { +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + long_long_value = va_arg( pargs->v, unsigned long long ); + } else +#endif + if( specs->_flags & SPF_LONG ) { + long_value = va_arg( pargs->v, unsigned long ); + } else { + long_value = va_arg( pargs->v, unsigned ); + if( specs->_flags & SPF_SHORT ) { /* JBS 92/02/12 */ + long_value = (unsigned short) long_value; + } else if( specs->_flags & SPF_CHAR ) { + long_value = (unsigned char)long_value; + } + } + } else + + if( ( specs->_character == 'd' ) || + ( specs->_character == 'i' ) ) { + +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + long_long_value = va_arg( pargs->v, long long ); + } else +#endif + if( specs->_flags & SPF_LONG ) { + long_value = va_arg( pargs->v, long ); + } else { + long_value = va_arg( pargs->v, int ); + if( specs->_flags & SPF_SHORT ) { /* JBS 92/02/12 */ + long_value = (short) long_value; + } else if( specs->_flags & SPF_CHAR ) { + long_value = (signed char)long_value; + } + } + { + int negative = FALSE; + +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + if( (long long)long_long_value < 0 ) { + negative = TRUE; + } + } else +#endif + if( (long)long_value < 0 ) { + negative = TRUE; + } + if( negative ) { + buffer[specs->_n0++] = '-'; + +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + long_long_value = -long_long_value; + } else +#endif + long_value = - long_value; + } else if( specs->_flags & SPF_FORCE_SIGN ) { + buffer[specs->_n0++] = '+'; + } else if( specs->_flags & SPF_BLANK ) { + buffer[specs->_n0++] = ' '; + } + } + } + + radix = 10; /* base 10 for 'd', 'i' and 'u' */ + switch( specs->_character ) { + case 'f': + case 'F': + if( specs->_flags & SPF_SHORT ) { /* "%hf" 13-jun-91 */ + long_value = va_arg( pargs->v, long ); + FixedPoint_Format( buffer, long_value, specs ); + specs->_n1 = far_strlen( buffer, -1 ); + break; + } + /* types f & F fall through */ + + case 'g': + case 'G': + case 'e': + case 'E': + float_format( buffer, pargs, specs ); + SetZeroPad( specs ); + arg++; // = &buffer[1]; + break; + + case 's': +#ifndef __NETWARE__ + case WIDE_CHAR_STRING: +#else + case PASCAL_STRING: +#endif + // arg has been initialized to point to buffer + // set buffer[0] to a null character assuming pointer will be NULL + // If pointer is not null, then arg will be assigned the pointer + buffer[0] = '\0'; // assume null pointer +#if defined( __FAR_SUPPORT__ ) + if( specs->_flags & SPF_FAR ) { + CHAR_TYPE _WCFAR *temp = va_arg( pargs->v, CHAR_TYPE _WCFAR * ); + + if( temp ) { + arg = temp; + } + } else if( specs->_flags & SPF_NEAR ) { + CHAR_TYPE _WCNEAR *temp = va_arg( pargs->v, CHAR_TYPE _WCNEAR * ); + + if( temp ) { + arg = (void *)temp; + } + } else { + CHAR_TYPE *temp = va_arg( pargs->v, CHAR_TYPE * ); + + if( temp ) { + arg = temp; + } + } +#else + { + CHAR_TYPE *temp = va_arg( pargs->v, CHAR_TYPE * ); + + if( temp ) { + arg = temp; + } + } +#endif + +#ifdef __NETWARE__ + if( specs->_character == PASCAL_STRING ) { +#ifdef __WIDECHAR__ + if( specs->_flags & SPF_SHORT ) +#else + if( specs->_flags & SPF_LONG ) +#endif + { + length = *( (_FAR_OTHER_STRING)arg ); + arg = (FAR_STRING)( (_FAR_OTHER_STRING)arg + 1 ); + } else { + length = *arg++; + } + } else +#elif !defined( __NETWARE__ ) && !defined( __WIDECHAR__ ) + if( specs->_character == WIDE_CHAR_STRING ) { + if( specs->_flags & SPF_SHORT ) { + length = far_strlen( arg, specs->_prec ); + } else { + length = far_other_strlen( arg, specs->_prec ); + } + } else +#endif + +#ifdef __WIDECHAR__ + if( specs->_flags & SPF_SHORT ) { +#else + if( specs->_flags & SPF_LONG ) { +#endif + length = far_other_strlen( arg, specs->_prec ); + } else { + length = far_strlen( arg, specs->_prec ); + } + + specs->_n1 = length; + if(( specs->_prec >= 0 ) && ( specs->_prec < length )) { + specs->_n1 = specs->_prec; + } + break; + + case 'x': + case 'X': + if( specs->_flags & SPF_ALT ) { +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + if( long_long_value != 0 ) { + buffer[specs->_n0++] = '0'; + buffer[specs->_n0++] = specs->_character; + } + } else +#endif + if( long_value != 0 ) { + buffer[specs->_n0++] = '0'; + buffer[specs->_n0++] = specs->_character; + } + } + radix = 16; /* base 16 */ + goto processNumericTypes; + + case 'b': /* CDH 2003 Apr 23 *//* Add binary mode */ + radix = 2; /* base 2 */ + goto processNumericTypes; + + case 'o': + radix = 8; /* base 8 */ + /* 'o' conversion falls through */ + + case 'd': + case 'i': + case 'u': +// 'x' and 'X' jumps here + +processNumericTypes: + if( specs->_prec != -1 ) + specs->_pad_char = ' '; /* 30-jul-95, 11-may-99 */ + + /* radix contains the base; 8 for 'o', 10 for 'd' and 'i' and 'u', + 16 for 'x' and 'X', and 2 for 'b' */ + + arg = &buffer[ specs->_n0 ]; + +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->_flags & SPF_LONG_LONG ) { + if(( specs->_prec == 0 ) && ( long_long_value == 0 )) { + *arg = '\0'; + length = 0; + } else { + __F_NAME(ulltoa,_ulltow)( long_long_value, &buffer[specs->_n0], radix ); + if( specs->_character == 'X' ) { + __F_NAME(strupr,_wcsupr)( buffer ); + } + length = far_strlen( arg, -1 ); + } + } else +#endif + if(( specs->_prec == 0 ) && ( long_value == 0 )) { + *arg = '\0'; + length = 0; + } else { + __F_NAME(ultoa,_ultow)( long_value, &buffer[specs->_n0], radix ); + if( specs->_character == 'X' ) { + __F_NAME(strupr,_wcsupr)( buffer ); + } + length = far_strlen( arg, -1 ); + } + specs->_n1 = length; + if( specs->_n1 < specs->_prec ) { + specs->_nz0 = specs->_prec - specs->_n1; + } else if( specs->_flags & SPF_ALT && radix < 10 + && (!length || (arg[0] != '0')) ) { + /* For 'b' and 'o' conversions, alternate format forces the number to + * start with a zero (effectively increases precision by one), but + * only if it doesn't start with a zero already. + */ + ++specs->_nz0; + } + if( specs->_prec == -1 ) { + SetZeroPad( specs ); + } + break; + + case 'p': + case 'P': +#if defined( __FAR_SUPPORT__ ) + #if defined( __BIG_DATA__ ) + if( !( specs->_flags & (SPF_NEAR|SPF_FAR) ) ) { + specs->_flags |= SPF_FAR; + } + if( specs->_fld_width == 0 ) { + if( specs->_flags & SPF_NEAR ) { + specs->_fld_width = sizeof( unsigned ) * 2; + } else { + specs->_fld_width = sizeof( CHAR_TYPE _WCFAR * ) * 2 + 1; + } + } + #else + if( specs->_fld_width == 0 ) { + if( specs->_flags & SPF_FAR ) { + specs->_fld_width = sizeof( CHAR_TYPE _WCFAR * ) * 2 + 1; + } else { + specs->_fld_width = sizeof( unsigned ) * 2; + } + } + #endif +#else + if( specs->_fld_width == 0 ) { + specs->_fld_width = sizeof( unsigned ) * 2; + } +#endif + specs->_flags &= ~( SPF_BLANK | SPF_FORCE_SIGN ); + int_value = va_arg( pargs->v, unsigned ); /* offset */ +#if defined( __FAR_SUPPORT__ ) + if( specs->_flags & SPF_FAR ) { + seg_value = va_arg( pargs->v, unsigned ) & 0xFFFF; /* segment */ + /* use "unsigned short" for 386 instead of "unsigned" 21-jul-89 */ + fmt4hex( seg_value, buffer, sizeof( unsigned short ) * 2 ); + buffer[sizeof(unsigned short)*2] = ':'; + fmt4hex( int_value, buffer + sizeof( unsigned short ) * 2 + 1, + sizeof( unsigned ) * 2 ); + } else { + fmt4hex( int_value, buffer, sizeof( unsigned ) * 2 ); + } +#else + fmt4hex( int_value, buffer, sizeof( unsigned ) * 2 ); +#endif + if( specs->_character == 'P' ) { + __F_NAME(strupr,_wcsupr)( buffer ); + } + specs->_n0 = far_strlen( arg, -1 ); + break; + + case 'c': +#if defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + if( specs->_flags & SPF_SHORT ) { + char * mbPtr; + char mbBuf[MB_CUR_MAX]; + wchar_t wc; + + mbPtr = va_arg( pargs->v, char* ); + mbBuf[0] = mbPtr[0]; + if( _ismbblead( mbBuf[0] ) ) + mbBuf[1] = mbPtr[1]; + + if( mbtowc( &wc, mbBuf, MB_CUR_MAX ) != -1 ) { + buffer[0] = wc; + } + } else { + buffer[0] = va_arg( pargs->v, int ); + } + specs->_n0 = 1; +#elif !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + specs->_n0 = 1; + if( specs->_flags & SPF_LONG ) { + char mbBuf[MB_CUR_MAX]; + wchar_t wc; + + wc = va_arg( pargs->v, int ); + if( wctomb( mbBuf, wc ) != -1 ) { + buffer[0] = mbBuf[0]; + if( _ismbblead( mbBuf[0] ) ) { + buffer[1] = mbBuf[1]; + specs->_n0++; + } + } + } else { + buffer[0] = va_arg( pargs->v, int ); + } +#else + specs->_n0 = 1; + buffer[0] = va_arg( pargs->v, int ); +#endif + break; + +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + case 'C': + bytes = wctomb( buffer, va_arg( pargs->v, int ) ); +// if( bytes != -1 && bytes <= specs->_prec ) { + if( bytes != -1 ) { /* Normative Addendum 4.5.3.3.1: no precision */ + specs->_n0 = bytes; + } else { + specs->_n0 = 0; + } + break; +#endif + + default: + specs->_fld_width = 0; + buffer[ 0 ] = specs->_character; + specs->_n0 = 1; + break; + } + return( arg ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/prtf_s.c b/programs/develop/open watcom/trunk/clib/streamio/prtf_s.c new file mode 100644 index 0000000000..19b05a077d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/prtf_s.c @@ -0,0 +1,35 @@ +/**************************************************************************** +* +* 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: Safe version of __prtf_s() worker routine. +* +****************************************************************************/ + + +// this file should remain an indirected file +// it is done this way to support the reuse of the source file +#define SAFE_PRINTF +#include "prtf.c" diff --git a/programs/develop/open watcom/trunk/clib/streamio/putc.c b/programs/develop/open watcom/trunk/clib/streamio/putc.c new file mode 100644 index 0000000000..b840357be1 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/putc.c @@ -0,0 +1,45 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of putc(). +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK INTCHAR_TYPE __F_NAME((putc),putwc)( INTCHAR_TYPE c, FILE *fp ) +{ + __stream_check( fp, 2 ); +#if !defined( __WIDECHAR__ ) && defined( putc ) + return( putc( c, fp ) ); +#else + return( __F_NAME(fputc,fputwc)( c, fp ) ); +#endif +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/putchar.c b/programs/develop/open watcom/trunk/clib/streamio/putchar.c new file mode 100644 index 0000000000..40bf754585 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/putchar.c @@ -0,0 +1,45 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of putchar(). +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + +#undef putchar + +_WCRTLINK INTCHAR_TYPE __F_NAME(putchar,putwchar)( INTCHAR_TYPE c ) +{ +#ifdef putc + return( __F_NAME(putc,putwc)( c, stdout ) ); +#else + return( __F_NAME(fputc,fputwc)( c, stdout ) ); +#endif +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/puts.c b/programs/develop/open watcom/trunk/clib/streamio/puts.c new file mode 100644 index 0000000000..9fcd78b223 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/puts.c @@ -0,0 +1,53 @@ +/**************************************************************************** +* +* 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: Implementation of puts() - put string to stdout. +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include + + +_WCRTLINK int __F_NAME(puts,putws)( const CHAR_TYPE *s ) +{ + INTCHAR_TYPE rc; + wint_t result; + + rc = __F_NAME(fputs,fputws)( s, stdout ); + if( rc != __F_NAME(EOF,WEOF) ) { + /* don't use macro version of putc: multi-threading issues */ + result = __F_NAME(fputc,fputwc)( __F_NAME('\n',L'\n'), stdout ); + if( result == __F_NAME('\n',L'\n') ) { + rc++; + } else { + rc = result; + } + } + return( rc ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/putw.c b/programs/develop/open watcom/trunk/clib/streamio/putw.c new file mode 100644 index 0000000000..e49aa4ac7f --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/putw.c @@ -0,0 +1,46 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of _putw(). +* +****************************************************************************/ + + +#include "variety.h" +#include + + +_WCRTLINK int _putw( int binint, FILE *fp ) +{ + size_t rc; + + rc = fwrite( &binint, sizeof( int ), 1, fp ); + if( rc > 0 ) { + return( binint ); + } else { + return( EOF ); + } +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/qread.h b/programs/develop/open watcom/trunk/clib/streamio/qread.h new file mode 100644 index 0000000000..ff1b3e2392 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/qread.h @@ -0,0 +1,36 @@ +/**************************************************************************** +* +* 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: Prototype for __qread() internal helper. +* +****************************************************************************/ + + +#ifdef __NETWARE__ + #define __qread( h, b, l ) read( h, b, l ) +#else + extern int __qread( int handle, void *buffer, unsigned len ); +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/qwrite.h b/programs/develop/open watcom/trunk/clib/streamio/qwrite.h new file mode 100644 index 0000000000..c9eb3b8672 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/qwrite.h @@ -0,0 +1,36 @@ +/**************************************************************************** +* +* 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: Prototype for __qwrite() internal helper. +* +****************************************************************************/ + + +#ifdef __NETWARE__ + #define __qwrite( h, b, l ) write( h, (void *)b, l ) +#else + extern int __qwrite( int, const void *, unsigned ); +#endif diff --git a/programs/develop/open watcom/trunk/clib/streamio/rewind.c b/programs/develop/open watcom/trunk/clib/streamio/rewind.c new file mode 100644 index 0000000000..23749ef765 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/rewind.c @@ -0,0 +1,41 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of rewind(). +* +****************************************************************************/ + + +#include "variety.h" +#include + + +_WCRTLINK void rewind( FILE *fp ) +{ + __stream_check( fp, 0 ); + clearerr( fp ); + fseek( fp, 0, SEEK_SET ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/scanf.c b/programs/develop/open watcom/trunk/clib/streamio/scanf.c new file mode 100644 index 0000000000..20e9bd4c66 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/scanf.c @@ -0,0 +1,72 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of scanf() and vscanf(). +* +****************************************************************************/ + + +#include "variety.h" +#include "widechar.h" +#include +#include +#include "scanf.h" + + +static int cget_stdin( PTR_SCNF_SPECS specs ) +{ + int c; + + if( (c = __F_NAME(getc,getwc)( stdin )) == __F_NAME(EOF,WEOF) ) { + specs->eoinp = 1; + } + return( c ); +} + + +static void uncget_stdin( int c, PTR_SCNF_SPECS specs ) +{ + __F_NAME(ungetc,ungetwc)( c, stdin ); +} + + +_WCRTLINK int __F_NAME(vscanf,vwscanf)( const CHAR_TYPE *format, va_list args ) +{ + SCNF_SPECS specs; + + specs.cget_rtn = cget_stdin; + specs.uncget_rtn = uncget_stdin; + return( __F_NAME(__scnf,__wscnf)( (PTR_SCNF_SPECS)&specs, format, args ) ); +} + + +_WCRTLINK int __F_NAME(scanf,wscanf)( const CHAR_TYPE *format, ... ) +{ + va_list args; + + va_start( args, format ); + return( __F_NAME(vscanf,vwscanf)( format, args ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/scanf_s.c b/programs/develop/open watcom/trunk/clib/streamio/scanf_s.c new file mode 100644 index 0000000000..7a90d44dab --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/scanf_s.c @@ -0,0 +1,81 @@ +/**************************************************************************** +* +* 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: Implementation of scanf_s() - safe formatted stream input. +* +****************************************************************************/ + + +#include "variety.h" +#include "saferlib.h" +#include "widechar.h" +#include +#include +#include +#include "scanf.h" + + +static int cget_stdin( PTR_SCNF_SPECS specs ) +{ + int c; + + if( (c = __F_NAME(getc,getwc)( stdin )) == __F_NAME(EOF,WEOF) ) { + specs->eoinp = 1; + } + return( c ); +} + + +static void uncget_stdin( int c, PTR_SCNF_SPECS specs ) +{ + __F_NAME(ungetc,ungetwc)( c, stdin ); +} + + +_WCRTLINK int __F_NAME(scanf_s,wscanf_s)( const CHAR_TYPE * __restrict format, ... ) +{ + va_list arg; + SCNF_SPECS specs; + const char *msg; + int rc; + + /* Basic check for null pointers to see if we can continue */ + if( __check_constraint_nullptr_msg( msg, format ) ) { + + specs.cget_rtn = cget_stdin; + specs.uncget_rtn = uncget_stdin; + msg = NULL; + va_start( arg, format ); + rc = __F_NAME(__scnf_s,__wscnf_s)( (PTR_SCNF_SPECS)&specs, format, &msg, arg ); + va_end( arg ); + if( msg == NULL ) { + /* no rt-constraint violation inside worker routine */ + return( rc ); + } + } + __rtct_fail( __func__, msg, NULL ); + return( __F_NAME(EOF,WEOF) ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/scnf.c b/programs/develop/open watcom/trunk/clib/streamio/scnf.c new file mode 100644 index 0000000000..378debae22 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/scnf.c @@ -0,0 +1,1116 @@ +/**************************************************************************** +* +* 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: Platform independent worker routines for scanf(). +* +****************************************************************************/ + + +#define __LONG_LONG_SUPPORT__ + +#if !defined( __NETWARE__ ) && !defined( __UNIX__ ) + #define USE_MBCS_TRANSLATION +#endif + +#include "variety.h" +#ifdef SAFE_SCANF + #include "saferlib.h" +#endif +#include "widechar.h" +#include +#include +#include +#ifdef __WIDECHAR__ + #include +#else + #include +#endif +#include +#include "scanf.h" +#include "prtscncf.h" +#include "fixpoint.h" +#include "ftos.h" +#include "farsupp.h" +#include "myvalist.h" +#if defined( __WIDECHAR__ ) || defined( USE_MBCS_TRANSLATION ) + #include +#endif + +#define TRUE 1 +#define FALSE 0 + +#define STOP_CHR 0xFFFFFFFF + +#define EFG_SCANF (*__EFG_scanf) + +/* internal file/string get, unget routines */ +#ifdef __WINDOWS_386__ + #ifdef __SW_3S + #pragma aux cget modify [eax edx ecx fs gs] + #pragma aux uncget modify [eax edx ecx fs gs] + #else + #pragma aux cget modify [fs gs] + #pragma aux uncget modify [fs gs] + #endif +#endif + +#if defined(__HUGE__) + #define SCNF_FAR _WCFAR +#else + #define SCNF_FAR +#endif + + +/* Macros to reduce the already large number of ifdefs in the code */ +#ifdef SAFE_SCANF + + #define GET_MAXELEM(x) x = va_arg( arg->v, size_t ) + #define DEFINE_VARS(x,y) size_t x, y = 0 + #define CHECK_ELEMS(x,y,z) if( x < ++y ) return( z ) +#else + + #define GET_MAXELEM(x) + #define DEFINE_VARS(x,y) + #define CHECK_ELEMS(x,y,z) + +#endif + +static int cget( PTR_SCNF_SPECS specs ) +{ + return( (*((specs)->cget_rtn))( specs ) ); +} + + +static void uncget( int c, PTR_SCNF_SPECS specs ) +{ + ((*((specs)->uncget_rtn))( c, specs )); +} + + +/* + * get_opt -- get option string for current conversion directive + * and fills in the SCNF_SPECS structure. + * returns advanced format string pointer. + */ +static const CHAR_TYPE *get_opt( const CHAR_TYPE *opt_str, PTR_SCNF_SPECS specs ) +{ + int c, width; + + specs->assign = TRUE; + specs->far_ptr = 0; + specs->near_ptr = 0; + specs->char_var = 0; + specs->short_var = 0; + specs->long_var = 0; + specs->long_long_var = 0; + specs->long_double_var = 0; + specs->p_format = 0; /* 21-nov-89 */ + specs->width = -1; + if( *opt_str == '*' ) { + specs->assign = FALSE; + ++opt_str; + } + c = *opt_str; + if( __F_NAME(isdigit,iswdigit)( c ) ) { + width = 0; + do { + width *= 10; + width += ( c - '0' ); + c = *++opt_str; + } while( __F_NAME(isdigit,iswdigit)( c ) ); + specs->width = width; + } + switch( *opt_str ) { + case 'N': + specs->near_ptr = 1; + ++opt_str; + break; +#if defined( __FAR_SUPPORT__ ) + case 'F': /* conflicts with ISO-defined 'F' conversion */ + /* fall through */ +#endif + case 'W': + specs->far_ptr = 1; + ++opt_str; + break; + } + switch( *opt_str ) { + case 'h': + if( opt_str[1] == 'h' ) { + specs->char_var = 1; + opt_str += 2; + break; + } + specs->short_var = 1; + ++opt_str; + break; + case 'l': +#if defined( __LONG_LONG_SUPPORT__ ) + if( opt_str[1] == 'l' ) { + specs->long_long_var = 1; + opt_str += 2; + break; + } +#endif + /* fall through */ + ZSPEC_CASE_LONG + TSPEC_CASE_LONG + case 'w': + specs->long_var = 1; + ++opt_str; + break; +#if defined( __LONG_LONG_SUPPORT__ ) + JSPEC_CASE_LLONG + /* fall through */ +#endif + case 'L': + specs->long_double_var = 1; + specs->long_long_var = 1; + ++opt_str; + break; +#if defined( __LONG_LONG_SUPPORT__ ) + case 'I': + if( opt_str[1] == '6' && opt_str[2] == '4' ) { + specs->long_long_var = 1; + opt_str += 3; + } + break; +#endif +#if defined( TSPEC_IS_INT ) || defined( ZSPEC_IS_INT ) + TSPEC_CASE_INT /* If either 't' or 'z' spec corresponds to 'int', */ + ZSPEC_CASE_INT /* we need to parse and ignore the spec. */ + ++opt_str; + break; +#endif + } + return( opt_str ); +} + + +/* + * scan_white -- scan white space from input stream + */ +static int scan_white( PTR_SCNF_SPECS specs ) +{ + int c, len; + + len = 0; + for( ;; ) { + c = cget( specs ); + if( !__F_NAME(isspace,iswspace)( c ) ) + break; + ++len; + } + if( !specs->eoinp ) + uncget( c, specs ); + return( len ); +} + +/* + * scan_char -- handles %c and %C + */ +static int scan_char( PTR_SCNF_SPECS specs, my_va_list *arg ) +{ + int len; + int width; + FAR_STRING str; + int c; + DEFINE_VARS( maxelem, nelem ); + + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + str = va_arg( arg->v, CHAR_TYPE _WCFAR * ); + } else if( specs->near_ptr ) { + str = va_arg( arg->v, CHAR_TYPE _WCNEAR * ); + } else { + str = va_arg( arg->v, CHAR_TYPE * ); + } +#else + str = va_arg( arg->v, CHAR_TYPE * ); +#endif + GET_MAXELEM( maxelem ); + } + len = 0; + if( (width = specs->width) == -1 ) + width = 1; + while( width > 0 ) { + c = cget( specs ); + if( specs->eoinp ) + break; + ++len; + --width; + if( specs->assign ) { + CHECK_ELEMS( maxelem, nelem, -1 ); +#if defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + if( specs->short_var ) { + char mbBuf[MB_CUR_MAX]; + + if( wctomb( mbBuf, c ) != -1 ) { + *(FAR_ASCII_STRING)str = mbBuf[0]; + str = (FAR_STRING) ( (FAR_ASCII_STRING)str + 1 ); + if( _ismbblead( mbBuf[0] ) ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + *(FAR_ASCII_STRING)str = mbBuf[1]; + str = (FAR_STRING) ( (FAR_ASCII_STRING)str + 1 ); + } + } else { + return( 0 ); + } + } else { + *str++ = c; + } +#elif defined( USE_MBCS_TRANSLATION ) + if( specs->long_var ) { + wchar_t wc; + char mbBuf[MB_CUR_MAX]; + + mbBuf[0] = c; + if( _ismbblead( mbBuf[0] ) ) + mbBuf[1] = cget( specs ); + if( mbtowc( &wc, mbBuf, MB_CUR_MAX ) != -1 ) { + *(FAR_UNI_STRING)str = wc; + str = (FAR_STRING) ( (FAR_UNI_STRING)str + 1 ); + } else { + return( 0 ); + } + } else { + *str++ = c; + } +#else + *str++ = c; +#endif + } + } + return( len ); +} + + +/* + * cgetw -- cget which keeps track of field width. + * returns STOP_CHR on end of field or end of file. + */ +static long cgetw( PTR_SCNF_SPECS specs ) +{ + int c; + + if( specs->width-- == 0 ) + return( STOP_CHR ); + c = cget( specs ); + return( !( specs->eoinp ) ? c : STOP_CHR ); +} + + +/* + * scan_string -- handles %s and %S + */ +static int scan_string( PTR_SCNF_SPECS specs, my_va_list *arg ) +{ + int c; + int len; + FAR_ASCII_STRING str; + char chsize; + DEFINE_VARS( maxelem, nelem ); + + if( specs->long_var ) { /* %ls or %ws */ + chsize = sizeof( wchar_t ); + } else if( specs->short_var ) { /* %hs */ + chsize = 1; + } else { /* %s */ + chsize = CHARSIZE; + } + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + str = va_arg( arg->v, char _WCFAR * ); + } else if( specs->near_ptr ) { + str = va_arg( arg->v, char _WCNEAR * ); + } else { + str = va_arg( arg->v, char * ); + } +#else + str = va_arg( arg->v, char * ); +#endif + GET_MAXELEM( maxelem ); + } + len = 0; + for( ;; ) { + c = cget( specs ); + if( !__F_NAME(isspace,iswspace)( c ) ) + break; + ++len; + } + if( specs->eoinp ) { + len = 0; /* since this is eof, no input done */ + goto done; + } + if( specs->width-- == 0 ) + goto ugdone; + do { + ++len; + if( specs->assign ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + if( chsize == 1 ) { +#if defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + char mbBuf[MB_CUR_MAX]; + + if( wctomb( mbBuf, c ) != -1 ) { + *(FAR_ASCII_STRING)str = mbBuf[0]; + if( _ismbblead( mbBuf[0] ) ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + str++; + *(FAR_ASCII_STRING)str = mbBuf[1]; + } + } else { + return( 0 ); + } +#else + *str = c; +#endif + } else { +#if !defined( __WIDECHAR__ ) && defined( USE_MBCS_TRANSLATION ) + wchar_t wc; + char mbBuf[MB_CUR_MAX]; + + mbBuf[0] = c; + if( _ismbblead( mbBuf[0] ) ) + mbBuf[1] = cget( specs ); + if( mbtowc( &wc, mbBuf, MB_CUR_MAX ) != -1 ) { + *(FAR_UNI_STRING)str = wc; + } else { + return( 0 ); + } +#else + *(FAR_UNI_STRING)str = c; +#endif + } + str += chsize; + } + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } while( !__F_NAME(isspace,iswspace)( c ) ); +ugdone: + uncget( c, specs ); +done: + if( specs->assign && len > 0 ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + if( chsize == 1 ) { + *str = '\0'; + } else { + *(FAR_UNI_STRING)str = 0; + } + } + return( len ); +} + + +/* + * report_scan -- handles %n + */ +static void report_scan( PTR_SCNF_SPECS specs, my_va_list *arg, int match ) +{ + FAR_INT iptr; + + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + iptr = va_arg( arg->v, int _WCFAR * ); + } else if( specs->near_ptr ) { + iptr = va_arg( arg->v, int _WCNEAR * ); + } else { + iptr = va_arg( arg->v, int * ); + } +#else + iptr = va_arg( arg->v, int * ); +#endif + if( specs->char_var ) { + *((FAR_CHAR)iptr) = match; + } else if( specs->short_var ) { + *((FAR_SHORT)iptr) = match; + } else if( specs->long_var ) { + *((FAR_LONG)iptr) = match; +#if defined( __LONG_LONG_SUPPORT__ ) + } else if( specs->long_long_var ) { + *((FAR_INT64)iptr) = match; +#endif + } else { + *iptr = match; + } + } +} + +#if !defined( __WIDECHAR__ ) +#define SCANSET_LENGTH (256 / 8) + +static const char lst_mask[8] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 +}; + +/* + * makelist -- create scanset for %[ directive. + * scanset is stored as 256 bit flags in a 32 byte array. + */ +static const char *makelist( const char *format, char *scanset ) +{ + int lst_chr; + + memset( scanset, 0, SCANSET_LENGTH ); + if( (lst_chr = *format++) == '\0' ) + return( format ); + do { + scanset[lst_chr >> 3] |= lst_mask[lst_chr & 0x07]; + if( (lst_chr = *format) == '\0' ) + break; + ++format; + } while( lst_chr != ']' ); + return( format ); +} +#endif + + +/* + * scan_arb -- handles %[ + */ +static int scan_arb( PTR_SCNF_SPECS specs, my_va_list *arg, const CHAR_TYPE **format ) +{ + unsigned width; + FAR_STRING str; + int len, c, not_flag; +#if defined( __WIDECHAR__ ) + const CHAR_TYPE *list; + CHAR_TYPE ch; + char in_list; +#else + char scanset[SCANSET_LENGTH]; +#endif + DEFINE_VARS( maxelem, nelem ); + + if( not_flag = (**format == '^') ) { + ++(*format); + } +#if !defined( __WIDECHAR__ ) + *format = makelist( *format, scanset ); +#endif + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + str = va_arg( arg->v, CHAR_TYPE _WCFAR * ); + } else if( specs->near_ptr ) { + str = va_arg( arg->v, CHAR_TYPE _WCNEAR * ); + } else { + str = va_arg( arg->v, CHAR_TYPE * ); + } +#else + str = va_arg( arg->v, CHAR_TYPE * ); +#endif + GET_MAXELEM( maxelem ); + } + len = 0; + width = specs->width; + while( width > 0 ) { + c = cget( specs ); + if( specs->eoinp ) + break; +#if defined( __WIDECHAR__ ) + list = *format; + ch = *list; + in_list = TRUE; + while( c != ch ) { + list++; + ch = *list; + if( ch == ']' ) { + in_list = FALSE; + break; + } + } + if( in_list == not_flag ) { + uncget( c, specs ); + break; + } +#else + if( ((scanset[c >> 3] & lst_mask[c & 0x07]) == 0) != not_flag ) { + uncget( c, specs ); + break; + } +#endif + ++len; + --width; + if( specs->assign ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + *str++ = c; + } + } + if( specs->assign && len > 0 ) { + CHECK_ELEMS( maxelem, nelem, -1 ); + *str = '\0'; + } +#if defined( __WIDECHAR__ ) + while( *(*format)++ != ']' ) /* skip past format specifier */ + ; +#endif + return( len ); +} + + +/* + * scan_float -- handles floating point numerical conversion + * *** should implement buffer overflow protection *** + */ +static int scan_float( PTR_SCNF_SPECS specs, my_va_list *arg ) +{ + double value; + char *num_str, buf[80]; + int len; + int pref_len; + long c; + int digit_found; + FAR_FLOAT fptr; + char *p; + T32 at; + T32 ft; + + num_str = buf; + pref_len = len = 0; + for( ;; ) { + c = cget( specs ); + if( !__F_NAME(isspace,iswspace)( c ) ) + break; + ++pref_len; + } + if( specs->eoinp ) + goto done; + if( specs->width-- == 0 ) + goto ugdone; + if( c == '+' || c == '-' ) { + *num_str++ = c; + ++pref_len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + if( !__F_NAME(isdigit,iswdigit)( c ) && c != '.' ) + goto ugdone; + at.uWhole = 0; + digit_found = FALSE; + if( __F_NAME(isdigit,iswdigit)( c ) ) { + digit_found = TRUE; + do { + *num_str++ = c; + if( specs->short_var ) + at.wd.hi = at.wd.hi * 10 + c - '0'; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } while( __F_NAME(isdigit,iswdigit)( c ) ); + } + if( c == '.' ) { + *num_str++ = c; + ++len; /* account for the '.' */ + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + if( !digit_found && !__F_NAME(isdigit,iswdigit)(c) ) + goto ugdone; + while( __F_NAME(isdigit,iswdigit)( c ) ) { + *num_str++ = c; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + break; + } + } + if( specs->short_var ) { /* %hf fixed-point format 05-feb-92 */ + ft.uWhole = 0; + p = num_str; + for( ;; ) { + --p; + if( *p == '.' ) + break; + ft.bite.b3 = *p - '0'; + ft.uWhole = ft.uWhole / 10; + } + at.wd.lo = ft.wd.lo; + } + if( c == STOP_CHR ) { + goto done; + } + } + if( specs->short_var == 0 && (c == 'e' || c == 'E') ) { + *num_str++ = c; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + if( c == '+' || c == '-' ) { + *num_str++ = c; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + if( !__F_NAME(isdigit,iswdigit)( c ) ) { + len = 0; /* fast way to flag error */ + } else { + do { + *num_str++ = c; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } while( __F_NAME(isdigit,iswdigit)( c ) ); + } + } +ugdone: + uncget( (int)c, specs ); +done: + if( len > 0 ) { + len += pref_len; + if( specs->assign ) { + *num_str = NULLCHAR; + if( specs->short_var ) { + if( buf[0] == '-' ) { + at.sWhole = - at.sWhole; + } + } else { + EFG_SCANF( buf, (void *)&value ); /* 27-mar-90 */ + } +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + fptr = va_arg( arg->v, float _WCFAR * ); + } else if( specs->near_ptr ) { + fptr = va_arg( arg->v, float _WCNEAR * ); + } else { + fptr = va_arg( arg->v, float * ); + } +#else + fptr = va_arg( arg->v, float * ); +#endif + if( specs->short_var ) { /* 05-feb-92 */ + *((FAR_LONG) fptr) = at.uWhole; + } else if( specs->long_var || specs->long_double_var ) { + *((FAR_DOUBLE) fptr) = value; + } else { + *fptr = value; + } + } + } + return( len ); +} + + +static int radix_value( int c ) +{ + if( c >= '0' && c <= '9' ) + return( c - '0' ); + c = __F_NAME(tolower,towlower)( c ); + if( c >= 'a' && c <= 'f' ) + return( c - 'a' + 10 ); + return( 16 ); +} + + +/* + * scan_int -- handles integer numeric conversion + */ +static int scan_int( PTR_SCNF_SPECS specs, my_va_list *arg, int base, int sign_flag ) +{ + long value; + int len; + int pref_len; + int c; + int sign; + int digit; + FAR_INT iptr; +#if defined( __LONG_LONG_SUPPORT__ ) + unsigned long long long_value; + FAR_INT64 llptr; + + long_value = 0; +#endif + + value = 0; + pref_len = len = 0; + for( ;; ) { + c = cget( specs ); + if( !__F_NAME(isspace,iswspace)( c ) ) + break; + ++pref_len; + } + if( specs->eoinp ) + goto done; + if( specs->width-- == 0 ) + goto ugdone; + sign = '+'; + if( sign_flag && (c == '+' || c == '-') ) { + sign = c; + ++pref_len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + if( base == 0 ) { + if( c == '0' ) { + len = 1; + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + if( c == 'x' || c == 'X' ) { + len = 0; + ++pref_len; /* for the '0' */ + ++pref_len; /* for the 'x' */ + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + base = 16; + } else { + base = 8; + } + } else { + base = 10; + } + } else if( base == 16 ) { + if( c == '0' ) { + len = 1; + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + if( c == 'x' || c == 'X' ) { + len = 0; + ++pref_len; /* for the '0' */ + ++pref_len; /* for the 'x' */ + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + } + } +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->long_long_var ) { + for( ;; ) { + digit = radix_value( c ); + if( digit >= base ) + break; + long_value = long_value * base + digit; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + if( c == ':' && specs->p_format ) { + for( ;; ) { + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + digit = radix_value( c ); + if( digit >= base ) + break; + long_value = long_value * base + digit; + } + } + } else +#endif + { + for( ;; ) { + digit = radix_value( c ); + if( digit >= base ) + break; + value = value * base + digit; + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) { + goto done; + } + } + if( c == ':' && specs->p_format ) { + for( ;; ) { + ++len; + if( (c = cgetw( specs )) == STOP_CHR ) + goto done; + digit = radix_value( c ); + if( digit >= base ) + break; + value = value * base + digit; + } + } + } +ugdone: + uncget( c, specs ); +done: +#if defined( __LONG_LONG_SUPPORT__ ) + if( specs->long_long_var ) { + if( sign == '-' ) { + long_value =- long_value; + } + if( len > 0 ) { + len += pref_len; + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + llptr = va_arg( arg->v, unsigned long long _WCFAR * ); + } else if( specs->near_ptr ) { + llptr = va_arg( arg->v, unsigned long long _WCNEAR * ); + } else { + llptr = va_arg( arg->v, unsigned long long * ); + } +#else + llptr = va_arg( arg->v, unsigned long long * ); +#endif + *llptr = long_value; + } + } + } else +#endif + { + if( sign == '-' ) { + value = -value; + } + if( len > 0 ) { + len += pref_len; + if( specs->assign ) { +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + iptr = va_arg( arg->v, int _WCFAR * ); + } else if( specs->near_ptr ) { + iptr = va_arg( arg->v, int _WCNEAR * ); + } else { + iptr = va_arg( arg->v, int * ); + } +#else + iptr = va_arg( arg->v, int * ); +#endif + if( specs->char_var ) { + *((FAR_CHAR)iptr) = value; + } else if( specs->short_var ) { + *((FAR_SHORT)iptr) = value; + } else if( specs->long_var ) { + *((FAR_LONG)iptr) = value; + } else { + *iptr = value; + } + } + } + } + return( len ); +} + + +#ifdef SAFE_SCANF + +/* + * null_arg -- check for a null pointer passed in arguments + */ +static int null_arg( PTR_SCNF_SPECS specs, my_va_list *arg ) +{ + FAR_STRING str = NULL; + va_list args_copy; + + va_copy( args_copy, arg->v ); +#if defined( __FAR_SUPPORT__ ) + if( specs->far_ptr ) { + str = va_arg( args_copy, CHAR_TYPE _WCFAR * ); + } else if( specs->near_ptr ) { + CHAR_TYPE _WCNEAR *ptr; + + ptr = va_arg( args_copy, CHAR_TYPE _WCNEAR * ); + /* The following should work: + * + * str = (ptr == NULL) ? (void _WCFAR *)NULL : ptr; + * + * but doesn't due to a bug in C compiler introduced in + * 11.0 and fixe in OW 1.4. Ternary operator may be used + * when building with OW 1.5. See also similar code in prtf.c. + */ + if( ptr == NULL ) { + str = NULL; + } else { + str = ptr; + } + } else { + CHAR_TYPE *ptr; + + ptr = va_arg( args_copy, CHAR_TYPE * ); + if( ptr == NULL ) { + str = NULL; + } else { + str = ptr; + } + } +#else + str = va_arg( args_copy, CHAR_TYPE * ); +#endif + va_end( args_copy ); + return( str == NULL ? 1 : 0 ); +} + +#endif + + +#ifdef SAFE_SCANF +int __F_NAME(__scnf_s,__wscnf_s)( PTR_SCNF_SPECS specs, const CHAR_TYPE *format, const char **msg, va_list args ) +#else +int __F_NAME(__scnf,__wscnf)( PTR_SCNF_SPECS specs, const CHAR_TYPE *format, va_list args ) +#endif +{ + int char_match; + int items_converted; + int items_assigned; + int match_len; + int c; + int fmt_chr; + my_va_list margs; + + margs = MY_VA_LIST( args ); + + char_match = items_assigned = items_converted = 0; + specs->eoinp = 0; + + while( (fmt_chr = *format++) != NULLCHAR ) { + if( __F_NAME(isspace,iswspace)( fmt_chr ) ) { + char_match += scan_white( specs ); + } else if( fmt_chr != '%' ) { + if( (c = cget( specs )) != fmt_chr ) { + if( !specs->eoinp ) + uncget( c, specs ); + break; + } + ++char_match; /* 27-oct-88 */ + } else { /* fmt_chr == '%' */ + format = get_opt( format, specs ); + if( (fmt_chr = *format) != NULLCHAR ) + ++format; +#ifdef SAFE_SCANF + if( fmt_chr != '%' ) { + /* The '%' specifier is the only one not expecting pointer arg */ + if( specs->assign && null_arg( specs, &margs ) ) { + *msg = "%ptr -> NULL"; + return( __F_NAME(EOF,WEOF) ); + } + } +#endif + switch( fmt_chr ) { + case 'd': + match_len = scan_int( specs, &margs, 10, TRUE ); + goto check_match; + case 'i': + match_len = scan_int( specs, &margs, 0, TRUE ); + goto check_match; + case 'o': + match_len = scan_int( specs, &margs, 8, TRUE ); + goto check_match; + case 'u': + match_len = scan_int( specs, &margs, 10, TRUE ); + goto check_match; + case 'p': +#if defined( __BIG_DATA__ ) + specs->long_var = 1; /* indicate far pointer */ + specs->p_format = 1; /* ... */ +#endif + // fall through + case 'x': + case 'X': + match_len = scan_int( specs, &margs, 16, TRUE ); + goto check_match; + case 'e': + case 'E': + case 'f': + case 'F': + case 'g': + case 'G': + match_len = scan_float( specs, &margs ); + goto check_match; +#if !defined(__WIDECHAR__) && !defined(__NETWARE__) + case 'S': + specs->long_var = 1; + /* fall through to %s handler */ +#endif + case 's': + match_len = scan_string( specs, &margs ); + goto check_match; + case '[': + match_len = scan_arb( specs, &margs, &format ); + goto check_match; +#if !defined(__WIDECHAR__) && !defined(__NETWARE__) + case 'C': + specs->long_var = 1; + /* fall through to %c handler */ +#endif + case 'c': + match_len = scan_char( specs, &margs ); +check_match: + if( match_len > 0 ) { + char_match += match_len; + ++items_converted; + if( specs->assign ) { + ++items_assigned; + } + } else { +#ifdef SAFE_SCANF + if( match_len < 0 ) { + /* Matching failure caused by insufficient space in output + * is not input failure, hence we won't return EOF regardless + * of specs->eoinp state. + */ + ++items_converted; + } +#endif + goto fail; + } + break; + case 'n': + report_scan( specs, &margs, char_match ); + break; + case '%': + if( (c = cget( specs )) != '%' ) { + if( !specs->eoinp ) + uncget( c, specs ); + goto fail; + } else { + char_match += 1; + } + break; + } + } + if( specs->eoinp ) { + while( *format == '%' ) { + ++format; + format = get_opt( format, specs ); + if( *format == 'n' ) { + ++format; +#ifdef SAFE_SCANF + if( specs->assign && null_arg( specs, &margs ) ) { + *msg = "%ptr -> NULL"; + return( __F_NAME(EOF,WEOF) ); + } +#endif + report_scan( specs, &margs, char_match ); + } else { + break; + } + } + break; + } + } + +fail: + if( items_converted == 0 && specs->eoinp ) + return( __F_NAME(EOF,WEOF) ); + return( items_assigned ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/scnf_s.c b/programs/develop/open watcom/trunk/clib/streamio/scnf_s.c new file mode 100644 index 0000000000..3948330d42 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/scnf_s.c @@ -0,0 +1,36 @@ +/**************************************************************************** +* +* 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: Safe version of scanf() worker routine. +* +****************************************************************************/ + + +// this file should remain an indirected file +// it is done this way to support the reuse of the source file +#define SAFE_SCANF +#undef __INLINE_FUNCTIONS__ +#include "scnf.c" diff --git a/programs/develop/open watcom/trunk/clib/streamio/setbuf.c b/programs/develop/open watcom/trunk/clib/streamio/setbuf.c new file mode 100644 index 0000000000..77a5db2904 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/setbuf.c @@ -0,0 +1,47 @@ +/**************************************************************************** +* +* 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: Platform independent implementation of setbuf(). +* +****************************************************************************/ + + +#include "variety.h" +#include + + +_WCRTLINK void setbuf( FILE *fp, char *buf ) +{ + int mode; + + __stream_check( fp, 1 ); + __null_check( buf, 2 ); + mode = _IOFBF; + if( buf == NULL ) { + mode = _IONBF; + } + setvbuf( fp, buf, mode, BUFSIZ ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/setefg.c b/programs/develop/open watcom/trunk/clib/streamio/setefg.c new file mode 100644 index 0000000000..3788fffcba --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/setefg.c @@ -0,0 +1,48 @@ +/**************************************************************************** +* +* 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: Routine to bring in real floating-point formatting code. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include "ftos.h" +#include "farsupp.h" + +/* This routine will be called by cstart if "_fltused" is referenced. */ + +void __setEFGfmt( void ) +{ +#ifdef __SW_BR + __EFG_printf = __get_EFG_Format(); + __EFG_scanf = __get__cnvs2d(); /* 27-mar-90 */ +#else + __EFG_printf = _EFG_Format; + __EFG_scanf = __cnvs2d; /* 27-mar-90 */ +#endif +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/setvbuf.c b/programs/develop/open watcom/trunk/clib/streamio/setvbuf.c new file mode 100644 index 0000000000..56359d991d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/setvbuf.c @@ -0,0 +1,79 @@ +/**************************************************************************** +* +* 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: Platform independent setvbuf() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include "fileacc.h" +#include "rtdata.h" +#include "streamio.h" + + +extern void __full_io_exit(); + + +_WCRTLINK int setvbuf( FILE *fp, char *buf, int mode, size_t size ) +{ + __stream_check( fp, 1 ); + __null_check( buf, 2 ); + if( size > INT_MAX ) { + /* 16-bit buffer filling code can't handle >32k (-ve size) */ + return( -1 ); + } + /* check for allowable values for mode */ + switch( mode ) { + case _IOFBF: + case _IOLBF: + case _IONBF: + break; + default: + return( -1 ); + } + if( buf != NULL && size == 0 ) { /* JBS 27-aug-90 */ + return( -1 ); + } + _ValidFile( fp, -1 ); + _AccessFile( fp ); + __chktty( fp ); /* JBS 28-aug-90 */ + if( size != 0 ) { + fp->_bufsize = size; /* JBS 27-aug-90 */ + } + _FP_BASE(fp) = buf; + fp->_ptr = buf; + fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF); /* FWC 14-jul-87 */ + fp->_flag |= mode; + if( buf == NULL ) { /* FWC 16-mar-93 */ + __ioalloc( fp ); + } + _ReleaseFile( fp ); + return( 0 ); +} diff --git a/programs/develop/open watcom/trunk/clib/streamio/tmpfl.c b/programs/develop/open watcom/trunk/clib/streamio/tmpfl.c new file mode 100644 index 0000000000..395a688b5d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/tmpfl.c @@ -0,0 +1,171 @@ +/**************************************************************************** +* +* 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: Platform independent tmpfile() implementation. +* +****************************************************************************/ + + +#include "variety.h" +#include "rtinit.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "rtdata.h" +#include "tmpfname.h" +#include "seterrno.h" +#include "openmode.h" + +#define OPEN_MODE (O_RDWR | O_CREAT | O_BINARY) +#define PMODE (S_IREAD | S_IWRITE) + +/* Netware doesn't define these */ +/* Symbolic constants for the access() function */ + +#if !defined( F_OK ) +#define R_OK 4 /* Test for read permission */ +#define W_OK 2 /* Test for write permission */ +#define X_OK 1 /* Test for execute permission */ +#define F_OK 0 /* Test for existence of file */ +#endif + +extern void __MkTmpFile( char *buf, int num ); +extern void __RmTmpFile( FILE *fp ); +extern void (*__RmTmpFileFn)( FILE *fp ); + +char __tmpfnext = _TMP_INIT_CHAR; + +_WCRTLINK FILE *tmpfile( void ) /* create a temporary file */ +{ + int hdl; + int old_errno; + int our_errno; + char suffix1; + char suffix2; + FILE *fp; + char name1[PATH_MAX + _TMPFNAME_LENGTH + 1]; + char name2[PATH_MAX + _TMPFNAME_LENGTH + 1]; + + old_errno = _RWD_errno; + suffix1 = 0; + for( ;; ) { + // Part I + for( ;; ) { + __MkTmpFile( name1, suffix1 ); + // if a file by this name does not exist + if( access( name1, F_OK ) != 0 ) { + + // then let's try to create it + hdl = sopen( name1, OPEN_MODE, OPENMODE_DENY_COMPAT, PMODE ); + + // if we created it then continue with part II + if( hdl != -1 ) break; + __set_errno( EAGAIN ); + } + suffix1++; + // give up after _TMP_INIT_CHAR tries JBS 99/10/26 + if( suffix1 >= _TMP_INIT_CHAR ) return NULL; + } + close( hdl ); + + // Part II + /* we now have a empty file. Let's try to rename it + rename should be an atomic operation in the operating system + so if it succeeds we can be sure no one else has this file. + Consider the following sequence: + + task1: access x.y => file does not exist + task2: access x.y => file does not exist + task1: fopen x.y => succeeds + task2: fopen x.y => succeeds (now have both tasks with x.y open) + task1: rename x.y to y.y => succeeds, can use this file + task2: rename x.y to y.y => fails (because x.y no longer exists) + task2: start over again to get a new file name + task2: succeeds second time around since no more race condition + with task1. + */ + suffix2 = _RWD_tmpfnext; // only one of these per process + for( ;; ) { + if( suffix2 == suffix1 ) { + suffix2++; + } + __MkTmpFile( name2, suffix2 ); + + if( rename( name1, name2 ) == 0 ) { // if rename worked + + // The file is now ours. Let's try to open it. + fp = fopen( name2, "wb+" ); + if( fp != NULL ) { + fp->_flag |= _TMPFIL; + _FP_TMPFCHAR(fp) = suffix2; + __set_errno( old_errno ); + return( fp ); + } + // We couldn't open it, probably because we have run out of handles. + // Remove the renamed file. + our_errno = errno; + remove( name2 ); + __set_errno( our_errno ); + return( NULL ); + } + // The rename didn't work or we couldn't open the renamed file. + // One of two possibilities: + // (1) The "to" name already exists. + // (2) Another process renamed it away from us. + + // Check for case (2). + // Quit if "from" file is gone and start over. + if( access( name1, F_OK ) != 0 ) break; + + // Must be case (1). Try another "to" name. + ++suffix2; + if( suffix2 == 0 ) { + suffix2 = _TMP_INIT_CHAR; + } + _RWD_tmpfnext = suffix2; // update for all processes + } + } +} + +/* tmpfil() pulls in a lot of overhead that many programs do not need. But */ +/* since temp files are removed on program shutdown, the code to remove */ +/* them would always get linked in even if the program never heard of temp */ +/* files. Since we know that temporary files can _only_ be created through */ +/* tmpfile(), we can have a dummy __RmTmpFile() by default and use the */ +/* real thing only if tmpfil() was called. */ +void __Init_Tmpfl( void ) +{ + // Just assign the function address + __RmTmpFileFn = __RmTmpFile; +} + +AXI( __Init_Tmpfl, INIT_PRIORITY_RUNTIME ) diff --git a/programs/develop/open watcom/trunk/clib/streamio/tmputil.c b/programs/develop/open watcom/trunk/clib/streamio/tmputil.c new file mode 100644 index 0000000000..62b6339312 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/streamio/tmputil.c @@ -0,0 +1,138 @@ +/**************************************************************************** +* +* 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: Internal helper routines __MkTmpFile() and __RmTmpFile(). +* +****************************************************************************/ + + +#include "variety.h" +#include +#include +#include +#include +#include +#include +#include "rtdata.h" +#include "tmpfname.h" + + +static unsigned __GetTmpPath( char *buf ) +{ +#ifndef __NETWARE__ + static char *evars[] = { "TMP", "TEMP", "TMPDIR", "TEMPDIR", "" }; + char **evar; + char *tmp; +#endif + unsigned i; + + buf[0] = '\0'; // initialize path +#ifdef __NETWARE__ + getcwd( buf, PATH_MAX ); // No environment vars on Netware +#else + for( evar = evars; **evar; ++evar ) { + tmp = getenv( *evar ); + if( (tmp != NULL) && (strlen( tmp ) <= PATH_MAX) ) { + tmp = _fullpath( buf, tmp, PATH_MAX ); + break; + } + } + /* + * If we didn't match on any environment vars, get current working dir + */ + if( buf[0] == '\0' ) { + getcwd( buf, PATH_MAX ); + } +#endif + + // if last char is not a path delimiter then append one + i = strlen( buf ); + if( i > 0 ) i--; + if( (buf[i] != '\\') && (buf[i] != '/') ) { + // if buf[i] is a null char then the following has no effect as planned + i++; + buf[i] = '\\'; + i++; + buf[i] = '\0'; + } + return( i ); +} + +static char __hex( int num ) +{ + num += '0'; + if( num > '9' ) { + num += 'a' - '0' - 10; + } + return( num ); +} + +#if defined( __NETWARE__ ) + extern int GetThreadID( void ); + #define getuniqueid() GetThreadID() +#elif defined( __NT__ ) + #define getuniqueid() ((getpid() << 12) + *_threadid) +#elif defined( __OS2__ ) + #define getuniqueid() ((getpid() << 12) + *_threadid) +#else + #define getuniqueid() (getpid()) +#endif + +void __MkTmpFile( char *buf, int num ) +{ + unsigned pid; + unsigned i; + char *ptr; + + pid = getuniqueid(); +// JBS on Win32 pid's range from 0 to n where n is not very large for +// most systems (e.g. 500 would indicate many, many processes are active). +// #if defined(__386__) || defined(__AXP__) || defined(__PPC__) +// // try to use more of the 32bit pid bits +// pid |= pid >> 16; +// #endif + + i = __GetTmpPath( buf ); + ptr = &buf[ i ]; + ptr[0] = 't'; + for( i = 7; i != 0; i-- ) { // JBS use 7 hex digits instead of 4 + ptr[i] = __hex( pid & 0x000F ); + pid = pid >> 4; + } + ptr[8] = '.'; + ptr[9] = 't'; + ptr[10] = __hex( (num >> 4) & 0x000F ); + ptr[11] = __hex( num & 0x000F ); + ptr[12] = '\0'; +} + +void __RmTmpFile( FILE *fp ) +{ + char name[PATH_MAX + _TMPFNAME_LENGTH + 1]; /* 02-aug-90 */ + + __MkTmpFile( name, _FP_TMPFCHAR(fp) ); + remove( name ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/_time.h b/programs/develop/open watcom/trunk/clib/time/_time.h new file mode 100644 index 0000000000..2297309c21 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/_time.h @@ -0,0 +1,34 @@ +/**************************************************************************** +* +* 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: prototypes for internal time related functions +* +****************************************************************************/ + + +extern int __DontCacheOSTZ( void ); +extern int __CacheOSTZ( void ); +extern void __parse_tz( char * tz ); diff --git a/programs/develop/open watcom/trunk/clib/time/gmtime.c b/programs/develop/open watcom/trunk/clib/time/gmtime.c new file mode 100644 index 0000000000..5d5d8c36ca --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/gmtime.c @@ -0,0 +1,141 @@ +/**************************************************************************** +* +* 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: __brktime() is an internal function to convert time to struct tm +* +****************************************************************************/ + +#include "variety.h" +#include +#include "thetime.h" +#include "timedata.h" + +// #define DAYS_IN_4_YRS ( 365 + 365 + 365 + 366 ) +// #define DAYS_IN_400_YRS ( ( 100 * DAYS_IN_4_YRS ) - 3 ) + +// #define SECONDS_PER_DAY ( 24 * 60 * 60 ) +// extern short __diyr[], __dilyr[]; + +/* + The number of leap years from year 1 to year 1900 is 460. + The number of leap years from year 1 to current year is + expressed by "years/4 - years/100 + years/400". To determine + the number of leap years from current year to 1900, we subtract + 460 from the formula result. We do this since "days" is the + number of days since 1900. +*/ + +static unsigned long __DaysToJan1( unsigned year ) +{ + unsigned years = 1900 + year - 1; + unsigned leap_days = years / 4 - years / 100 + years / 400 - 460; + + return( year * 365UL + leap_days ); +} + +/* __brktime breaks down a calendar time (clock) into a struct tm t */ + +struct tm *__brktime( unsigned long days, + time_t wallclock, + long gmtdelta, // localtime - gmtime + struct tm *t ) +{ + unsigned long secs; + unsigned year; + int day_of_year; + int month; + short const *month_start; + + /* + If date is Jan 1, 1970 0:00 to 12:00 UTC and we are west of UTC + then add a day to wallclock, subtract the gmtdelta value, and + decrement the calculated days. This prevents local times + such as "Wed Dec 31 19:00:00 1969 (EST)" from being + erroneously reported as "Sun Feb 6 01:28:16 2106 (EST)" + since (wallclock - gmtdelta) wraps (i.e., wallclock < gmtdelta). + */ + if( wallclock < 12 * 60 * 60UL && gmtdelta > 0 ) + wallclock += SECONDS_PER_DAY, days--; /* days compensated for wallclock one day ahead */ + wallclock -= ( time_t ) gmtdelta; + days += wallclock / SECONDS_PER_DAY; + secs = wallclock % SECONDS_PER_DAY; + t->tm_hour = ( int ) ( secs / 3600 ) ; + secs = secs % 3600; + t->tm_min = ( int ) ( secs / 60 ); + t->tm_sec = secs % 60; + + // The following two lines are not needed in the current implementation + // because the range of values for days does not exceed DAYS_IN_400_YRS. + // Even if it did, the algorithm still computes the correct values. + // + // unsigned year400s; + // + // year400s = (days / DAYS_IN_400_YRS) * 400; + // days %= DAYS_IN_400_YRS; + // + // It is OK to reduce days to a value less than DAYS_IN_400_YRS, because + // DAYS_IN_400_YRS is exactly divisible by 7. If it wasn't divisible by 7, + // then the following line which appears at the bottom, should be computed + // before the value of days is range reduced. + // t->tm_wday = (days + 1) % 7; /* 24-sep-92 */ + // + year = days / 365; + day_of_year = ( int ) ( days - __DaysToJan1( year ) ); + while( day_of_year < 0 ) { + --year; + day_of_year += __leapyear( year + 1900 ) + 365; + } + // year += year400s; + + t->tm_yday = day_of_year; + t->tm_year = ( int ) year; + month_start = __diyr; + if( __leapyear( year + 1900 ) ) + month_start = __dilyr; + month = day_of_year / 31; /* approximate month */ + if( day_of_year >= month_start[month + 1] ) + ++month; + t->tm_mon = month; + t->tm_mday = day_of_year - month_start[month] + 1; + + /* Calculate the day of the week */ + /* Jan 1,1900 is a Monday */ + + t->tm_wday = ( days + 1 ) % 7; /* 24-sep-92 */ + return( t ); +} + +_WCRTLINK struct tm *_gmtime( const time_t *timer, struct tm *tm ) +{ + tm->tm_isdst = 0; /* assume not */ + return __brktime( DAYS_FROM_1900_TO_1970, *timer, 0L, tm ); +} + +_WCRTLINK struct tm *gmtime( const time_t *timer ) +{ + _INITTHETIME; + return( _gmtime( timer, &_THE_TIME ) ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/leapyear.c b/programs/develop/open watcom/trunk/clib/time/leapyear.c new file mode 100644 index 0000000000..ffa2031b3a --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/leapyear.c @@ -0,0 +1,77 @@ +/**************************************************************************** +* +* 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 if a year is a leap year + associated arrays +* +****************************************************************************/ + +#include "variety.h" +#include +#include "rtdata.h" +#include "timedata.h" + +short const __based(__segname("_CONST")) __diyr[] = { /* days in normal year array */ + 0, /* Jan */ + 31, /* Feb */ + 31 + 28, /* Mar */ + 31 + 28 + 31, /* Apr */ + 31 + 28 + 31 + 30, /* May */ + 31 + 28 + 31 + 30 + 31, /* Jun */ + 31 + 28 + 31 + 30 + 31 + 30, /* Jul */ + 31 + 28 + 31 + 30 + 31 + 30 + 31, /* Aug */ + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, /* Sep */ + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, /* Oct */ + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, /* Nov */ + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, /* Dec */ + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 /* Jan, next year */ +}; + +short const __based(__segname("_CONST")) __dilyr[] = { /* days in leap year array */ + 0, /* Jan */ + 31, /* Feb */ + 31 + 29, /* Mar */ + 31 + 29 + 31, /* Apr */ + 31 + 29 + 31 + 30, /* May */ + 31 + 29 + 31 + 30 + 31, /* Jun */ + 31 + 29 + 31 + 30 + 31 + 30, /* Jul */ + 31 + 29 + 31 + 30 + 31 + 30 + 31, /* Aug */ + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, /* Sep */ + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, /* Oct */ + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, /* Nov */ + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, /* Dec */ + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 /* Jan, next year */ +}; + +int __leapyear( unsigned year ) +{ + if( year & 3 ) + return( 0 ); + if( ( year % 100 ) != 0 ) + return( 1 ); + if( ( year % 400 ) == 0 ) + return( 1 ); + return( 0 ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/localtim.c b/programs/develop/open watcom/trunk/clib/time/localtim.c new file mode 100644 index 0000000000..11e7baed4d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/localtim.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: _localtime() converts time_t to struct tm +* +****************************************************************************/ + +#include "variety.h" +#include +#include "rtdata.h" +#include "thetime.h" +#include "timedata.h" + +_WCRTLINK struct tm *_localtime( const time_t *timer, struct tm *t ) +{ + time_t tod; + + tzset(); + tod = *timer; + t->tm_isdst = -1; +#ifdef __LINUX__ + __check_tzfile( tod, t ); +#endif + __brktime( DAYS_FROM_1900_TO_1970, tod, _RWD_timezone, t ); + + if( __isindst( t ) ) + __brktime( DAYS_FROM_1900_TO_1970, tod, + _RWD_timezone - _RWD_dst_adjust, t ); + return( t ); +} + +_WCRTLINK struct tm *localtime( const time_t *timer ) +{ + _INITTHETIME; + return _localtime( timer, &_THE_TIME ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/locmktim.c b/programs/develop/open watcom/trunk/clib/time/locmktim.c new file mode 100644 index 0000000000..da6b63520d --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/locmktim.c @@ -0,0 +1,103 @@ +/**************************************************************************** +* +* 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: mktime() without timezone and struct tm fields adjustment. +* used by mktime() and DOS clock() +* +****************************************************************************/ + +#include "variety.h" +#include +#include +#include +#include +#include "rtdata.h" +#include "timedata.h" + +#define MONTH_YR ( 12 ) +#define DAY_YR ( 365 ) +#define HOUR_YR ( DAY_YR * 24 ) +#define MINUTE_YR ( HOUR_YR * 60 ) +#define SECOND_YR ( MINUTE_YR * 60 ) +#define __MONTHS ( INT_MIN / MONTH_YR ) +#define __DAYS ( INT_MIN / DAY_YR ) + +// these ones can underflow in 16bit environments, +// so check the relative values first +#if ( HOUR_YR ) < ( INT_MAX / 60 ) + #define __MINUTES ( INT_MIN / MINUTE_YR ) + #if ( MINUTE_YR ) < ( INT_MAX / 60 ) + #define __SECONDS ( INT_MIN / SECOND_YR ) + #else + #define __SECONDS ( 0 ) + #endif +#else + #define __MINUTES ( 0 ) + #define __SECONDS ( 0 ) +#endif + +#define SMALLEST_YEAR_VALUE ( __MONTHS + __DAYS + __MINUTES + __SECONDS ) + +time_t __local_mktime( const struct tm *t, long *pdays, long *pseconds ) +{ + int month; + int year; + long days; + long seconds; + short const *month_start; + + month_start = __diyr; + month = t->tm_mon % 12; /* put tm_mon into range */ + year = t->tm_year; + if( year < SMALLEST_YEAR_VALUE ) + return( ( time_t ) -1 ); + year += t->tm_mon / 12; + while( month < 0 ) + --year, month += 12; + if( year < 0 ) + return( ( time_t ) -1 ); + if( __leapyear( ( unsigned ) ( year + 1900 ) ) ) + month_start = __dilyr; + days = year * 365L /* # of days in the years */ + + ( ( year + 3 ) / 4 ) /* add # of leap years before year */ + - ( ( year + 99 ) / 100 ) /* sub # of leap centuries */ + + ( ( year + 399 - 100 ) / 400 ) /* add # of leap 4 centuries */ + /* adjust for 1900 offset */ + /* note: -100 == 300 (mod 400) */ + + month_start[month] /* # of days to 1st of month*/ + + t->tm_mday - 1; /* day of the month */ + seconds = ( ( ( long ) ( t->tm_hour ) ) *60L + ( long ) ( t->tm_min ) ) *60L + t->tm_sec; + /* seconds needs to be positive for __brktime */ + while( seconds < 0 ) + days -= 1, seconds += ( long ) SECONDS_PER_DAY; + while( seconds >= ( long ) SECONDS_PER_DAY ) + days += 1, seconds -= ( long ) SECONDS_PER_DAY; + if( days < ( DAYS_FROM_1900_TO_1970 - 1 ) ) + return( ( time_t ) -1 ); + if ( pdays ) *pdays = days; + if ( pseconds ) *pseconds = seconds; + return( seconds + ( days - DAYS_FROM_1900_TO_1970 ) * ( long ) SECONDS_PER_DAY ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/mktime.c b/programs/develop/open watcom/trunk/clib/time/mktime.c new file mode 100644 index 0000000000..e0a7409933 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/mktime.c @@ -0,0 +1,69 @@ +/**************************************************************************** +* +* 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: Platform independent mktime() implementation. +* +****************************************************************************/ + +#include "variety.h" +#include +#include +#include +#include +#include "rtdata.h" +#include "timedata.h" + +_WCRTLINK time_t mktime( struct tm *t ) +{ + long days; + long seconds; + long day_seconds; + + seconds = __local_mktime( t, &days, &day_seconds ); + __brktime( (unsigned long) days, (time_t) day_seconds, 0L, t ); + tzset(); + seconds += _RWD_timezone; /* add in seconds from GMT */ +#ifdef __LINUX__ + if( t->tm_isdst < 0 ) + __check_tzfile( seconds, t ); +#endif + /* if we are in d.s.t. then subtract __dst_adjust from seconds */ + if( __isindst( t ) ) /* - determine if we are in d.s.t. */ + seconds -= _RWD_dst_adjust; +#ifdef __UNIX__ /* time_t is signed */ + if( seconds < 0 ) + return( ( time_t ) -1 ); +#else /* time_t is unsigned, special day check needed for 31 dec 1969 */ + /* check for overflow; days == 75277 && seconds == 23296 returns 0, but + adjusted for dst may still be fine */ + if( days >= 75279 || + ( seconds >= 0 && days >= 75276 ) || + ( seconds < 0 && days <= DAYS_FROM_1900_TO_1970 ) ) + return( (time_t) -1 ); + /* 0 is year = 206, mon = 1, mday = 7, hour = 6, min = 28, sec = 16 */ +#endif + return( ( time_t ) seconds ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/thetime.h b/programs/develop/open watcom/trunk/clib/time/thetime.h new file mode 100644 index 0000000000..6c51746251 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/thetime.h @@ -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: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE +* DESCRIBE IT HERE! +* +****************************************************************************/ + + +#include "variety.h" +#if defined(__OS2__) || defined(__NT__) + // the OS/2 and NT files are identical + #if defined(__SW_BM) + + #include "thread.h" + + #define _INITTHETIME + #define _THE_TIME (__THREADDATAPTR->__The_timeP) + + #else + + static struct tm The_time; + #define _INITTHETIME + #define _THE_TIME The_time + + #endif +#else + #ifdef __NETWARE__ + #define _INITTHETIME + #define _THE_TIME (__THREADDATAPTR->__The_timeP) + #else + #define _INITTHETIME + static struct tm The_time; + #define _THE_TIME The_time + #endif +#endif diff --git a/programs/develop/open watcom/trunk/clib/time/time.c b/programs/develop/open watcom/trunk/clib/time/time.c new file mode 100644 index 0000000000..249dd0bd04 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/time.c @@ -0,0 +1,49 @@ +/**************************************************************************** +* +* 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: time() returns current time and date +* +****************************************************************************/ + +#include "variety.h" +#include +#include +#include "timedata.h" + +_WCRTLINK time_t time( time_t *timer ) +{ + auto struct tm t; + time_t seconds; + int milliseconds; + + milliseconds = __getctime( &t ); /* get raw time and date */ + if( milliseconds >= 500 ) + t.tm_sec++; + seconds = mktime( &t ); + if( timer != NULL ) + *timer = seconds; + return( seconds ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/timedata.h b/programs/develop/open watcom/trunk/clib/time/timedata.h new file mode 100644 index 0000000000..1f56f16083 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/timedata.h @@ -0,0 +1,56 @@ +/**************************************************************************** +* +* 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: Specification of internal interfaces. +* +****************************************************************************/ + +#ifndef TIMEDATA_H_INCLUDED +#define TIMEDATA_H_INCLUDED + +#include "variety.h" +#include + +extern int __dst_adjust; +extern struct tm __start_dst; /* start of daylight savings */ +extern struct tm __end_dst; /* end of daylight savings */ + +#define SECONDS_FROM_1900_TO_1970 2208988800UL +#define SECONDS_PER_DAY ( 24 * 60 * 60UL ) +#define DAYS_FROM_1900_TO_1970 ( ( long ) ( SECONDS_FROM_1900_TO_1970 / SECONDS_PER_DAY ) ) + +extern struct tm *__brktime( unsigned long, time_t, long, struct tm * ); +extern time_t __local_mktime( const struct tm *, long *, long * ); +extern int __leapyear( unsigned ); +extern int __isindst( struct tm * ); +extern int __getctime( struct tm * ); +extern int __read_tzfile( const char *tz ); +extern void __check_tzfile( time_t t, struct tm *timep ); + +extern short const __based(__segname("_CONST")) __diyr[]; /* days in normal year array */ +extern short const __based(__segname("_CONST")) __dilyr[]; /* days in leap year array */ + +#endif diff --git a/programs/develop/open watcom/trunk/clib/time/timeutil.c b/programs/develop/open watcom/trunk/clib/time/timeutil.c new file mode 100644 index 0000000000..dca6a066ae --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/timeutil.c @@ -0,0 +1,250 @@ +/**************************************************************************** +* +* 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: time utility functions +* +****************************************************************************/ + +#include "variety.h" +#include +#include "rtdata.h" +#include "timedata.h" + +static int time_less( const struct tm *t1, const struct tm *t2 ); + +static int calc_yday( const struct tm *timetm, int year ) +{ + struct tm tmptm; + int month_days; + int first_wday; + int nth_week; + short const *diyr; + + if( timetm->tm_isdst == 0 ) { // M.m.n.d form + diyr = ( __leapyear( ( unsigned ) year + 1900 ) ) ? __dilyr : __diyr; + month_days = diyr[timetm->tm_mon + 1] - diyr[timetm->tm_mon]; + tmptm.tm_sec = 0; + tmptm.tm_min = 0; + tmptm.tm_hour = 0; + tmptm.tm_mday = 1; + tmptm.tm_mon = timetm->tm_mon; + tmptm.tm_year = year; + tmptm.tm_isdst = 0; + ( void ) mktime( &tmptm ); + first_wday = ( timetm->tm_wday - tmptm.tm_wday + 7 ) % 7; + if( timetm->tm_mday == 5 ) { + if( ( 1 + first_wday + ( timetm->tm_mday - 1 ) * 7 ) > month_days ) + nth_week = timetm->tm_mday - 2; // fifth req. weekday does not exist + else + nth_week = timetm->tm_mday - 1; + } else + nth_week = timetm->tm_mday - 1; + return( tmptm.tm_yday + first_wday + nth_week * 7 ); + } + if( timetm->tm_isdst == 1 ) /* if Jn form */ + return( timetm->tm_yday - 1 ); + return( timetm->tm_yday ); +} + +/* determine if in souther hemisphere -> start is after end */ +static int check_order( const struct tm *start, const struct tm *end, int year ) +{ + int start_day; + int end_day; + + /* these quick checks should always be enough */ + if( ( start->tm_isdst == 0 ) && ( end->tm_isdst == 0 ) ) { // M.m.n.d form + if( start->tm_mon > end->tm_mon ) + return( 1 ); + if( start->tm_mon < end->tm_mon ) + return( 0 ); + } + /* start/end of daylight savings time is in the same month (rare case) */ + /* these are *expensive* calculations under NT since 2 TZ checks must be done */ + start_day = calc_yday( start, year ); + end_day = calc_yday( end, year ); + if( start_day > end_day ) + return( 1 ); + return( 0 ); +} + +/* determine if daylight savings time */ +int __isindst( struct tm *t ) +{ + int month; + int dst; + int n1; + int n2; + int month_days; + int time_check; + int south; + struct tm const *start; + struct tm const *end; + short const *diyr; + + // already determined -- if we are sure + if( t->tm_isdst >= 0 ) + return( t->tm_isdst ); + dst = 0; + // if zone doesn't have a daylight savings period + if( _RWD_daylight == 0 ) + return( t->tm_isdst = dst ); + // // check for no daylight savings time rule + // if( tzname[1][0] == '\0' ) { // doesn't work since Win32 says + // return( t->tm_isdst = dst );// daylight zone name = standard zone name + // } + + south = check_order( &_RWD_start_dst, &_RWD_end_dst, t->tm_year ); + if( south ) { + // if southern hemisphere + // invert start and end dates and then invert return value + start = &_RWD_end_dst; + end = &_RWD_start_dst; + } else { + start = &_RWD_start_dst; + end = &_RWD_end_dst; + } + month = t->tm_mon; + diyr = ( __leapyear( ( unsigned ) t->tm_year + 1900 ) ) ? __dilyr : __diyr; + month_days = diyr[month + 1] - diyr[month]; + time_check = 0; + /* + * M.m.n.d form + * m = start->tm_mon (month 0-11) + * n = start->tm_mday (n'th week day 1-5) + * d = start->tm_wday (week day 0-6) + */ + if( start->tm_isdst == 0 ) { /* if Mm.n.d form */ + if( month > start->tm_mon ) + dst = 1; /* assume dst for now */ + else if( month == start->tm_mon ) { + /* calculate for current day */ + n1 = t->tm_mday - ( t->tm_wday + 7 - start->tm_wday ) % 7; + /* calculate for previous day */ + n2 = t->tm_mday - 1 - ( t->tm_wday - 1 + 7 - start->tm_wday ) % 7; + // n_ stands for the day of the month that is past && + // is closest to today && is the required weekday + if( start->tm_mday == 5 ) { + if( n1 > month_days - 7 ) { + dst = 1; /* assume dst for now */ + if( n2 <= month_days - 7 ) + time_check = 1; + } + } else { + if( n1 >= 7 * ( start->tm_mday - 1 ) + 1 ) { + dst = 1; /* assume dst for now */ + if( n2 < 7 * ( start->tm_mday - 1 ) + 1 ) + time_check = 1; + } + } + } + } else { + n1 = start->tm_yday; + if( start->tm_isdst == 1 ) { /* if Jn form */ + if( __leapyear( ( unsigned ) t->tm_year + 1900 ) ) { + if( n1 > __diyr[2] ) + n1++; /* past Feb 28 */ + } + n1--; + } + if( t->tm_yday >= n1 ) { + dst = 1; /* assume dst for now */ + if( t->tm_yday == n1 ) + time_check = 1; + } + } + /* if it is the day for a switch-over then check the time too */ + if( time_check ) + dst = !time_less( t, start ); + + /* if we are certain that it is before daylight saving then return */ + if( dst == 0 ) { + if( south ) + dst = south - dst; /* invert value of dst */ + return( t->tm_isdst = dst ); + } + + /* now see if it is after daylight saving */ + time_check = 0; + if( end->tm_isdst == 0 ) { /* if Mm.n.d form */ + if( month > end->tm_mon ) + dst = 0; /* not dst */ + else if( month == end->tm_mon ) { + dst = 0; + /* calculate for current day */ + n1 = t->tm_mday - ( t->tm_wday + 7 - end->tm_wday ) % 7; + /* calculate for previous day */ + n2 = t->tm_mday - 1 - + ( t->tm_wday - 1 + 7 - end->tm_wday ) % 7; + if( end->tm_mday == 5 ) { + if( n1 <= month_days - 7 ) + dst = 1; + else if( n2 <= month_days - 7 ) + time_check = 1; + } else { + if( n1 < 7 * ( end->tm_mday - 1 ) + 1 ) + dst = 1; + else if( n2 < 7 * ( end->tm_mday - 1 ) + 1 ) + time_check = 1; + } + } + } else { + n1 = end->tm_yday; + if( end->tm_isdst == 1 ) { /* if Jn form */ + if( __leapyear( ( unsigned ) t->tm_year + 1900 ) ) { + if( n1 > __diyr[2] ) + n1++; /* past Feb 28 */ + } + n1--; + } + if( t->tm_yday >= n1 ) { + dst = 0; + if( t->tm_yday == n1 ) + time_check = 1; + } + } + /* if it is the day for a switch-over then check the time too */ + if( time_check ) + dst = time_less( t, end ); + if( south ) + dst = south - dst; /* invert value of dst */ + return( t->tm_isdst = dst ); +} + +static int time_less( const struct tm *t1, const struct tm *t2 ) +{ + int before; + + before = 0; + if( t1->tm_hour < t2->tm_hour ) + before = 1; + else if( t1->tm_hour == t2->tm_hour ) { + if( t1->tm_min < t2->tm_min + || t1->tm_min == t2->tm_min && t1->tm_sec < t2->tm_sec ) + before = 1; + } + return( before ); +} diff --git a/programs/develop/open watcom/trunk/clib/time/tzset.c b/programs/develop/open watcom/trunk/clib/time/tzset.c new file mode 100644 index 0000000000..148229ae74 --- /dev/null +++ b/programs/develop/open watcom/trunk/clib/time/tzset.c @@ -0,0 +1,285 @@ +/**************************************************************************** +* +* 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: Platform independent tzset() implementation. +* +****************************************************************************/ + +#include "variety.h" +#include +#include +#include +#include +#include "rtdata.h" +#include "timedata.h" +#include "exitwmsg.h" +#include "_time.h" + +/* + * TZSET - sets the values of the variables 'timezone', 'daylight' + * and 'tzname' according to the setting of the environment + * variable "TZ". The "TZ" variable has the format + * :characters + * or + * [[]][,[/