Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 613 → Rev 614

/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 <stdio.h>
#include <string.h>
#include <errno.h>
#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 */
}
/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 <stdio.h>
#include <malloc.h>
#include <unistd.h>
#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;
}
}
}
}
/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 <stdio.h>
#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 );
}
/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 <stdio.h>
#include "commode.h"
 
 
int _commode = 0;
 
 
/*
* Before changing any of this, check startup/c/commode.c!
*/
 
_WCRTLINK void _WCI86FAR __set_commode( void )
{
_commode = _COMMIT;
}
/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"
/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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "liballoc.h"
#include "mf.h"
#include <string.h>
#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 ) );
}
/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 <stdio.h>
#include <ctype.h>
#ifdef __WIDECHAR__
#include <wctype.h>
#endif
#include <errno.h>
#include <fcntl.h>
#ifdef __NT__
#include <windows.h>
#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 );
}
/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 <stdio.h>
#include "fileacc.h"
 
 
_WCRTLINK int (feof)( FILE *fp )
{
_ValidFile( fp, 0 );
return( fp->_flag & _EOF );
}
/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 <stdio.h>
#include "fileacc.h"
 
 
_WCRTLINK int (ferror)( FILE *fp )
{
_ValidFile( fp, 0 );
return( fp->_flag & _SFERR );
}
/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 <stdio.h>
#include <errno.h>
#include <fcntl.h>
#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 ) );
}
/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 <stdio.h>
#include <unistd.h>
#ifndef __UNIX__
#include <conio.h>
#endif
#include "fileacc.h"
#include <errno.h>
#include "rtdata.h"
#include "seterrno.h"
#ifdef __WIDECHAR__
#include <mbstring.h>
#include <wchar.h>
#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
/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 <stdio.h>
 
 
_WCRTLINK INTCHAR_TYPE __F_NAME(fgetchar,fgetwchar)( void )
{
return( __F_NAME(fgetc,fgetwc)( stdin ) );
}
/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 <stdio.h>
#include "fileacc.h"
 
 
_WCRTLINK int fgetpos( FILE *fp, fpos_t *pos )
{
_ValidFile( fp, -1 );
*pos = ftell( fp );
return( *pos == -1L );
}
/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 <stdio.h>
#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 );
}
/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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#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
/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 <stdio.h>
#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 ) );
}
/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 */
/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 <stdio.h>
#include <ctype.h>
#ifdef __WIDECHAR__
#include <wctype.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#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 );
}
/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 <wchar.h>
#include <stdio.h>
#include <ctype.h>
#ifdef __WIDECHAR__
#include <wctype.h>
#endif
// #include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#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 );
}
/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 <stdio.h>
#include <stdarg.h>
#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 ) );
}
/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 <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#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 ) );
}
/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 <stdio.h>
#include <stdarg.h>
#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 );
}
/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 <stdio.h>
#include <stdarg.h>
#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 );
}
/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 );
/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 <stdio.h>
#include <errno.h>
#include "fileacc.h"
#include "rtdata.h"
#include "seterrno.h"
#include "orient.h"
#ifdef __WIDECHAR__
#include <mbstring.h>
#include <wchar.h>
#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
/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 <stdio.h>
 
 
_WCRTLINK INTCHAR_TYPE __F_NAME(fputchar,fputwchar)( INTCHAR_TYPE c )
{
return( __F_NAME(fputc,fputwc)( c, stdout ) );
}
/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 <stdio.h>
#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 );
}
/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 <stdio.h>
#include <unistd.h>
#include "fileacc.h"
#include <string.h>
#include <errno.h>
#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 );
}
/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 <stdio.h>
#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();
}
/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 <wchar.h>
#include <stdio.h>
#include <ctype.h>
#ifdef __WIDECHAR__
#include <wctype.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#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 );
}
/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 <stdio.h>
#include <stdarg.h>
#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 ) );
}
/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 <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#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) );
}
/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 <stdio.h>
#include <errno.h>
#include <unistd.h>
#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 */
}
/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 <stdio.h>
 
 
_WCRTLINK int fsetpos( FILE *fp, const fpos_t *pos )
{
return( fseek( fp, *pos, SEEK_SET ) );
}
/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 <stdio.h>
#include <string.h>
#include <unistd.h>
#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 );
}
/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 <stdio.h>
#include <wchar.h>
#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 );
}
/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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#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 );
}
/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 <stdio.h>
 
 
_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
}
/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 <stdio.h>
 
#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
}
/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 <stdio.h>
 
 
_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 );
}
/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 <stdio.h>
 
_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 );
}
/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 <stdio.h>
 
 
_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 );
}
}
/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 <stdio.h>
#include <stdlib.h>
#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;
}
/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 <stdio.h>
#include "liballoc.h"
#include <unistd.h>
#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;
}
/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 <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#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);
/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 <stdio.h>
#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 <io.h>
 
#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 <io.h>
 
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
/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 <stdio.h>
#include "rtdata.h"
 
 
_WCRTLINK FILE (*__get_iob_ptr( void ))[]
{
return( &__iob );
}
/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 <stdio.h>
#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;
/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 <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#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 );
}
/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 <stdio.h>
#include <stdarg.h>
#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++;
}
}
 
 
 
/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 <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#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 ) );
}
/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 <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if defined( __WIDECHAR__ ) || defined( USE_MBCS_TRANSLATION )
#include <mbstring.h>
#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 );
}
/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"
/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 <stdio.h>
 
 
_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
}
/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 <stdio.h>
 
#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
}
/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 <stdio.h>
 
 
_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 );
}
/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 <stdio.h>
 
 
_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 );
}
}
/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
/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
/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 <stdio.h>
 
 
_WCRTLINK void rewind( FILE *fp )
{
__stream_check( fp, 0 );
clearerr( fp );
fseek( fp, 0, SEEK_SET );
}
/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 <stdio.h>
#include <stdarg.h>
#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 ) );
}
/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 <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#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) );
}
/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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __WIDECHAR__
#include <wctype.h>
#else
#include <ctype.h>
#endif
#include <stdarg.h>
#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 <mbstring.h>
#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 );
}
/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"
/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 <stdio.h>
 
 
_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 );
}
/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 <stdio.h>
#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
}
/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 <stdio.h>
#include <stddef.h>
#include <limits.h>
#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 );
}
/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 <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <direct.h>
#include <string.h>
#include <process.h>
#include <unistd.h>
#include <errno.h>
#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 )
/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 <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <direct.h>
#include <string.h>
#include <process.h>
#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 );
}