/contrib/sdk/sources/newlib/libc/reent/closer.c |
---|
0,0 → 1,87 |
/* Reentrant version of close system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <_syslist.h> |
#include <errno.h> |
/* Some targets provides their own versions of this functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
/* |
FUNCTION |
<<_close_r>>---Reentrant version of close |
INDEX |
_close_r |
ANSI_SYNOPSIS |
#include <reent.h> |
int _close_r(struct _reent *<[ptr]>, int <[fd]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
int _close_r(<[ptr]>, <[fd]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
DESCRIPTION |
This is a reentrant version of <<close>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
extern unsigned __NFiles; |
#define __handle_check( __h, __r ) \ |
if( (__h) < 0 || (__h) > __NFiles ) { \ |
ptr->_errno = EBADF ; \ |
return( __r ); \ |
} |
int |
_DEFUN(_close_r, (ptr, fd), |
struct _reent *ptr _AND |
int fd) |
{ |
int ret; |
int h; |
__file_handle *fh; |
__handle_check( fd, -1 ); |
fh = (__file_handle*) __getOSHandle( fd ); |
if( fd > STDERR_FILENO ) |
{ |
_free_r(ptr, fh->name); |
_free_r(ptr, fh); |
__freePOSIXHandle( fd ); |
__SetIOMode_nogrow( fd, 0 ); |
} |
return 0; |
} |
int |
_DEFUN( close,(fd), |
int fd) |
{ |
return _close_r(_REENT, fd); |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/fstatr.c |
---|
0,0 → 1,156 |
/* Reentrant versions of fstat system call. This implementation just |
calls the fstat system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <sys/stat.h> |
#include <_syslist.h> |
#include <errno.h> |
#include <string.h> |
/* Some targets provides their own versions of these functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifdef REENTRANT_SYSCALLS_PROVIDED |
int _dummy_fstat_syscalls = 1; |
#else |
/* We use the errno variable used by the system dependent layer. */ |
/* |
FUNCTION |
<<_fstat_r>>---Reentrant version of fstat |
INDEX |
_fstat_r |
ANSI_SYNOPSIS |
#include <reent.h> |
int _fstat_r(struct _reent *<[ptr]>, |
int <[fd]>, struct stat *<[pstat]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
int _fstat_r(<[ptr]>, <[fd]>, <[pstat]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
struct stat *<[pstat]>; |
DESCRIPTION |
This is a reentrant version of <<fstat>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
#pragma pack(push, 1) |
typedef struct |
{ |
char sec; |
char min; |
char hour; |
char rsv; |
}detime_t; |
typedef struct |
{ |
char day; |
char month; |
short year; |
}dedate_t; |
typedef struct |
{ |
unsigned attr; |
unsigned flags; |
union |
{ |
detime_t ctime; |
unsigned cr_time; |
}; |
union |
{ |
dedate_t cdate; |
unsigned cr_date; |
}; |
union |
{ |
detime_t atime; |
unsigned acc_time; |
}; |
union |
{ |
dedate_t adate; |
unsigned acc_date; |
}; |
union |
{ |
detime_t mtime; |
unsigned mod_time; |
}; |
union |
{ |
dedate_t mdate; |
unsigned mod_date; |
}; |
unsigned size; |
unsigned size_high; |
} FILEINFO; |
#pragma pack(pop) |
extern unsigned __NFiles; |
#define __handle_check( __h, __r ) \ |
if( (__h) < 0 || (__h) > __NFiles ) { \ |
ptr->_errno = EBADF ; \ |
return( __r ); \ |
} |
int |
_fstat_r (ptr, fd, pstat) |
struct _reent *ptr; |
int fd; |
struct stat *pstat; |
{ |
FILEINFO info; |
int ret; |
__file_handle *fh; |
__handle_check( fd, -1 ); |
if (fd < 3) |
{ |
pstat->st_mode = S_IFCHR; |
pstat->st_blksize = 0; |
return 0; |
} |
fh = (__file_handle*) __getOSHandle( fd ); |
get_fileinfo(fh->name, &info); |
memset (pstat, 0, sizeof (* pstat)); |
pstat->st_mode = S_IFREG; |
pstat->st_blksize = 4096; |
pstat->st_size = info.size; |
return 0; |
} |
int |
_DEFUN (fstat, (fd, pstat), |
int fd _AND |
struct stat *pstat) |
{ |
return _fstat_r (_REENT, fd, pstat); |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/getreent.c |
---|
0,0 → 1,25 |
/* default reentrant pointer when multithread enabled */ |
#include <_ansi.h> |
#include <string.h> |
#include <reent.h> |
#include <kos32sys.h> |
void init_reent() |
{ |
struct _reent *ent; |
ent = user_alloc(sizeof(struct _reent)); |
_REENT_INIT_PTR(ent); |
__asm__ __volatile__( |
"movl %0, %%fs:16" |
::"r"(ent)); |
__sinit(ent); |
} |
/contrib/sdk/sources/newlib/libc/reent/gettimeofdayr.c |
---|
0,0 → 1,113 |
/* Reentrant version of gettimeofday system call |
This implementation just calls the times/gettimeofday system calls. |
Gettimeofday may not be available on all targets. It's presence |
here is dubious. Consider it for internal use only. */ |
#include <reent.h> |
#include <time.h> |
#include <sys/time.h> |
#include <sys/times.h> |
#include <_syslist.h> |
#include <errno.h> |
/* Some targets provides their own versions of these functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifdef REENTRANT_SYSCALLS_PROVIDED |
int _dummy_gettimeofday_syscalls = 1; |
#else |
/* We use the errno variable used by the system dependent layer. */ |
#undef errno |
static int errno; |
/* |
FUNCTION |
<<_gettimeofday_r>>---Reentrant version of gettimeofday |
INDEX |
_gettimeofday_r |
ANSI_SYNOPSIS |
#include <reent.h> |
#include <time.h> |
int _gettimeofday_r(struct _reent *<[ptr]>, |
struct timeval *<[ptimeval]>, |
void *<[ptimezone]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
#include <time.h> |
int _gettimeofday_r(<[ptr]>, <[ptimeval]>, <[ptimezone]>) |
struct _reent *<[ptr]>; |
struct timeval *<[ptimeval]>; |
void *<[ptimezone]>; |
DESCRIPTION |
This is a reentrant version of <<gettimeofday>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
This function is only available for a few targets. |
Check libc.a to see if its available on yours. |
*/ |
int |
_DEFUN (_gettimeofday_r, (ptr, ptimeval, ptimezone), |
struct _reent *ptr _AND |
struct timeval *ptimeval _AND |
void *ptimezone) |
{ |
int ret; |
errno = 0; |
if ((ret = _gettimeofday (ptimeval, ptimezone)) == -1 && errno != 0) |
ptr->_errno = errno; |
return ret; |
} |
#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10) |
int |
_gettimeofday (struct timeval *tv, void *tz) |
{ |
unsigned int xtmp; |
struct tm tmblk; |
if( tv ) |
{ |
tv->tv_usec = 0; |
__asm__ __volatile__("int $0x40":"=a"(xtmp):"0"(3)); |
tmblk.tm_sec = (xtmp>>16)&0xff; |
tmblk.tm_min = (xtmp>>8)&0xff; |
tmblk.tm_hour = xtmp&0xff; |
BCD_TO_BIN(tmblk.tm_sec); |
BCD_TO_BIN(tmblk.tm_min); |
BCD_TO_BIN(tmblk.tm_hour); |
__asm__ __volatile__("int $0x40":"=a"(xtmp):"0"(29)); |
tmblk.tm_mday = (xtmp>>16)&0xff; |
tmblk.tm_mon = ((xtmp>>8)&0xff)-1; |
tmblk.tm_year = ((xtmp&0xff)+2000)-1900; |
tmblk.tm_wday = tmblk.tm_yday = 0; |
tmblk.tm_isdst = -1; |
tv->tv_sec = mktime(&tmblk); |
return 0; |
} |
else |
{ |
errno = EINVAL; |
return -1; |
}; |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/hdlman.c |
---|
0,0 → 1,381 |
/**************************************************************************** |
* |
* Open Watcom Project |
* |
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. |
* |
* ======================================================================== |
* |
* This file contains Original Code and/or Modifications of Original |
* Code as defined in and that are subject to the Sybase Open Watcom |
* Public License version 1.0 (the 'License'). You may not use this file |
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO |
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is |
* provided with the Original Code and Modifications, and is also |
* available at www.sybase.com/developer/opensource. |
* |
* The Original Code and all software distributed under the License are |
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM |
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR |
* NON-INFRINGEMENT. Please see the License for the specific language |
* governing rights and limitations under the License. |
* |
* ======================================================================== |
* |
* Description: Handle manager routines. |
* |
****************************************************************************/ |
#include <reent.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <errno.h> |
#include <fcntl.h> |
#define _NFILES 20 |
#define _DYNAMIC 0x4000 /* FILE is dynamically allocated */ |
#define _READ 0x0001 /* file opened for reading */ |
#define _WRITE 0x0002 /* file opened for writing */ |
#define NULL_HANDLE (int)-1 |
#define DUMMY_HANDLE (int)-2 |
#define INVALID_HANDLE_VALUE (int) -1 |
#define _AccessIOB() |
#define _ReleaseIOB() |
#undef __getOSHandle |
void __ChkTTYIOMode( int handle ); |
void __initPOSIXHandles( void ) __attribute__ ((constructor)); |
void __grow_iomode( int num ); |
int debugwrite(const char *path,const void *buff, |
size_t offset, size_t count, size_t *writes); |
int _fmode; |
#define NUM_STD_STREAMS 3 |
#define _ISTTY 0x2000 /* is console device */ |
unsigned __init_mode[_NFILES] = { /* file mode information (flags) */ |
_READ, /* stdin */ |
_WRITE, /* stdout */ |
_WRITE /* stderr */ |
}; |
unsigned *__io_mode = __init_mode; /* initially points to static array */ |
unsigned __NFiles = _NFILES; /* maximum # of files we can open */ |
unsigned __NHandles = 0; |
int *__OSHandles = NULL; |
static __file_handle |
stdin_handle = { |
NULL, |
0, |
NULL |
}; |
static __file_handle |
stdout_handle = |
{ |
NULL, |
0, |
debugwrite |
}; |
static __file_handle |
stderr_handle = |
{ |
NULL, |
0, |
debugwrite |
}; |
unsigned __growPOSIXHandles( unsigned num ) |
{ |
int *new2; |
unsigned i; |
if( num > __NHandles ) |
{ |
if( __OSHandles == NULL ) |
{ |
new2 = malloc( num * sizeof( int ) ); |
} |
else |
{ |
new2 = realloc( __OSHandles, num * sizeof( int ) ); |
} |
if( new2 == NULL ) |
{ |
// __set_errno( ENOMEM ); |
num = __NHandles; |
} |
else |
{ |
for( i = __NHandles; i < num; i++ ) |
{ |
new2[ i ] = NULL_HANDLE; |
} |
__OSHandles = new2; |
__NHandles = num; |
} |
} |
return( __NHandles ); |
} |
int __allocPOSIXHandle( int hdl ) |
{ |
int i; |
for( i = 0; i < __NHandles; i++ ) |
{ |
if( __OSHandles[i] == NULL_HANDLE ) break; |
} |
if( i >= __NHandles ) |
{ |
// 20 -> (20+10+1) -> 31 |
// 31 -> (31+15+1) -> 47 |
// 47 -> (47+23+1) -> 71 |
__growPOSIXHandles( i + (i >> 1) + 1 ); |
// keep iomode array in sync |
if( __NHandles > __NFiles ) __grow_iomode( __NHandles ); |
for( ; i < __NHandles; i++ ) |
{ |
if( __OSHandles[i] == NULL_HANDLE ) break; |
} |
} |
if( i >= __NHandles ) |
{ |
i = -1; |
} else { |
__OSHandles[i] = hdl; |
} |
return( i ); |
} |
void __freePOSIXHandle( int hid ) |
{ |
__OSHandles[ hid ] = NULL_HANDLE; |
} |
int __getOSHandle( int hid ) |
{ |
return( __OSHandles[ hid ] ); |
} |
int __setOSHandle( unsigned hid, int hdl ) |
{ |
// call the Win32 API for a standard file handle |
switch( hid ) { |
case STDIN_FILENO: |
// SetStdHandle( STD_INPUT_HANDLE, hdl ); |
break; |
case STDOUT_FILENO: |
// SetStdHandle( STD_OUTPUT_HANDLE, hdl ); |
break; |
case STDERR_FILENO: |
// SetStdHandle( STD_ERROR_HANDLE, hdl ); |
break; |
} |
if( hid < __NHandles ) |
{ |
__OSHandles[ hid ] = hdl; |
} |
else |
{ |
hid = (unsigned)-1; // this should never happen |
} |
return( hid ); |
} |
// called from library startup code |
void __initPOSIXHandles( void ) |
{ |
int h; |
_fmode = O_BINARY; |
__growPOSIXHandles( __NFiles ); |
h = (int)&stdin_handle; |
__allocPOSIXHandle( h ); // should return 0==STDIN_FILENO |
h = (int)&stdout_handle; |
__allocPOSIXHandle( h ); // should return 1==STDOUT_FILENO |
h = (int)&stderr_handle; |
__allocPOSIXHandle( h ); // should return 3==STDERR_FILENO |
} |
/* |
static void __finiPOSIXHandles( void ) |
{ |
if( __OSHandles != NULL ) { |
free( __OSHandles ); |
__OSHandles = NULL; |
} |
if( __FakeHandles != NULL ) |
{ |
int i; |
for( i = 0 ; i < __topFakeHandle ; i++ ) |
{ |
// CloseHandle( __FakeHandles[i] ); |
} |
free( __FakeHandles ); |
__FakeHandles = 0; |
} |
} |
*/ |
void __set_handles( int num ) |
{ |
__NHandles = num; |
} |
int _grow_handles( int num ) |
{ |
if( num > __NHandles ) |
{ |
num = __growPOSIXHandles( num ); |
if( num > __NFiles ) { |
__grow_iomode( num ); // sets new __NFiles if successful |
} |
__NHandles = num; |
} |
return( __NHandles ); |
} |
static unsigned _init_NFiles; // original __NFiles value; |
void __grow_iomode( int num ) |
{ |
unsigned *new; |
_AccessIOB(); |
if( __io_mode == __init_mode ) |
{ |
_init_NFiles = __NFiles; |
new = (unsigned *) malloc( num * sizeof( unsigned ) ); |
if( new != NULL ) { |
memcpy( new, __init_mode, __NFiles * sizeof(unsigned) ); |
} |
} |
else |
{ |
new = (unsigned *) realloc( __io_mode, num * sizeof( unsigned ) ); |
} |
if( new == NULL ) |
{ |
// __set_errno( ENOMEM ); |
} |
else |
{ |
memset( &new[__NFiles], 0, (num-__NFiles)*sizeof(unsigned) ); |
__io_mode = new; |
__NFiles = num; |
} |
_ReleaseIOB(); |
} |
void __shrink_iomode( void ) |
{ |
_AccessIOB(); |
// free any malloc'd iomode array |
if( __io_mode != __init_mode ) |
{ |
free( __io_mode ); |
__io_mode = __init_mode; |
__NFiles = _init_NFiles; |
} |
_ReleaseIOB(); |
} |
#define _INITIALIZED _DYNAMIC |
signed __SetIOMode( int handle, unsigned value ) |
{ |
int i; |
if( handle >= __NFiles ) |
{ |
i = __NFiles; // 20 -> (20+10+1) -> 31 |
// 31 -> (31+15+1) -> 47 |
// 47 -> (47+23+1) -> 71 |
__grow_iomode( i + (i > 1) + 1 ); |
} |
if( handle >= __NFiles ) |
{ |
// return an error indication (errno should be set to ENOMEM) |
return( -1 ); |
} |
else |
{ |
if( value != 0 ) |
{ |
__ChkTTYIOMode( handle ); |
__io_mode[handle] = value | _INITIALIZED; |
} |
else |
{ |
__io_mode[handle] = value; /* we're closing it; smite _INITIALIZED */ |
} |
return( handle ); |
} |
} |
int _isatty( int hid ) |
{ |
return( 0 ); |
} |
void __ChkTTYIOMode( int handle ) |
{ |
if( handle < NUM_STD_STREAMS && !(__io_mode[handle] & _INITIALIZED) ) |
{ |
__io_mode[handle] |= _INITIALIZED; |
if( _isatty( handle ) ) |
{ |
__io_mode[handle] |= _ISTTY; |
} |
} |
} |
unsigned __GetIOMode( int handle ) |
{ |
if( handle >= __NFiles ) |
{ |
return( 0 ); |
} |
return( __io_mode[handle] ); |
}; |
void __SetIOMode_nogrow( int handle, unsigned value ) |
{ |
if( handle < __NFiles ) |
{ |
__io_mode[handle] = value; /* we're closing it; smite _INITIALIZED */ |
} |
} |
/contrib/sdk/sources/newlib/libc/reent/impure.c |
---|
0,0 → 1,28 |
#include <reent.h> |
/* Note that there is a copy of this in sys/reent.h. */ |
#ifndef __ATTRIBUTE_IMPURE_PTR__ |
#define __ATTRIBUTE_IMPURE_PTR__ |
#endif |
#ifndef __ATTRIBUTE_IMPURE_DATA__ |
#define __ATTRIBUTE_IMPURE_DATA__ |
#endif |
/* Redeclare these symbols locally as weak so that the file containing |
their definitions (along with a lot of other stuff) isn't sucked in |
unless they are actually used by other compilation units. This is |
important to reduce image size for targets with very small amounts |
of memory. */ |
#ifdef _REENT_SMALL |
extern const struct __sFILE_fake __sf_fake_stdin _ATTRIBUTE ((weak)); |
extern const struct __sFILE_fake __sf_fake_stdout _ATTRIBUTE ((weak)); |
extern const struct __sFILE_fake __sf_fake_stderr _ATTRIBUTE ((weak)); |
#endif |
static struct _reent __ATTRIBUTE_IMPURE_DATA__ impure_data = _REENT_INIT (impure_data); |
#ifdef __CYGWIN__ |
extern struct _reent reent_data __attribute__ ((alias("impure_data"))); |
#endif |
struct _reent *__ATTRIBUTE_IMPURE_PTR__ _impure_ptr = &impure_data; |
struct _reent *_CONST __ATTRIBUTE_IMPURE_PTR__ _global_impure_ptr = &impure_data; |
/contrib/sdk/sources/newlib/libc/reent/init_reent.c |
---|
0,0 → 1,18 |
#include <_ansi.h> |
#include <string.h> |
#include <reent.h> |
void init_global_reent() |
{ |
struct _reent *ent; |
ent =_GLOBAL_REENT; |
_REENT_INIT_PTR(ent); |
__asm__ __volatile__( |
"movl %0, %%fs:16" |
::"r"(ent)); |
// __sinit(ent); |
} |
/contrib/sdk/sources/newlib/libc/reent/isattyr.c |
---|
0,0 → 1,62 |
/* Reentrant versions of isatty system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <_syslist.h> |
#include <errno.h> |
/* Some targets provides their own versions of these functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifdef REENTRANT_SYSCALLS_PROVIDED |
int _dummy_isatty_syscalls = 1; |
#else |
/* We use the errno variable used by the system dependent layer. */ |
#undef errno |
extern int errno; |
/* |
FUNCTION |
<<_isatty_r>>---Reentrant version of isatty |
INDEX |
_isatty_r |
ANSI_SYNOPSIS |
#include <reent.h> |
int _isatty_r(struct _reent *<[ptr]>, |
int <[fd]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
int _isatty_r(<[ptr]>, <[fd]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
DESCRIPTION |
This is a reentrant version of <<isatty>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
int |
_isatty_r (ptr, fd) |
struct _reent *ptr; |
int fd; |
{ |
int ret; |
ptr->_errno = ENOTTY ; |
return 0; |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/lseekr.c |
---|
0,0 → 1,159 |
/* Reentrant versions of lseek system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <_syslist.h> |
#include <errno.h> |
/* Some targets provides their own versions of this functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#pragma pack(push, 1) |
typedef struct |
{ |
char sec; |
char min; |
char hour; |
char rsv; |
}detime_t; |
typedef struct |
{ |
char day; |
char month; |
short year; |
}dedate_t; |
typedef struct |
{ |
unsigned attr; |
unsigned flags; |
union |
{ |
detime_t ctime; |
unsigned cr_time; |
}; |
union |
{ |
dedate_t cdate; |
unsigned cr_date; |
}; |
union |
{ |
detime_t atime; |
unsigned acc_time; |
}; |
union |
{ |
dedate_t adate; |
unsigned acc_date; |
}; |
union |
{ |
detime_t mtime; |
unsigned mod_time; |
}; |
union |
{ |
dedate_t mdate; |
unsigned mod_date; |
}; |
unsigned size; |
unsigned size_high; |
} FILEINFO; |
#pragma pack(pop) |
extern unsigned __NFiles; |
#define __handle_check( __h, __r ) \ |
if( (__h) < 0 || (__h) > __NFiles ) { \ |
ptr->_errno = EBADF ; \ |
return( __r ); \ |
} |
/* |
FUNCTION |
<<_lseek_r>>---Reentrant version of lseek |
INDEX |
_lseek_r |
ANSI_SYNOPSIS |
#include <reent.h> |
off_t _lseek_r(struct _reent *<[ptr]>, |
int <[fd]>, off_t <[pos]>, int <[whence]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
off_t _lseek_r(<[ptr]>, <[fd]>, <[pos]>, <[whence]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
off_t <[pos]>; |
int <[whence]>; |
DESCRIPTION |
This is a reentrant version of <<lseek>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
_off_t |
_DEFUN (_lseek_r, (ptr, fd, pos, whence), |
struct _reent *ptr _AND |
int fd _AND |
_off_t pos _AND |
int whence) |
{ |
_off_t ret; |
__file_handle *fh; |
__handle_check( fd, -1 ); |
fh = (__file_handle*) __getOSHandle( fd ); |
switch(whence) |
{ |
case SEEK_SET: |
ret = pos; |
break; |
case SEEK_CUR: |
ret = fh->offset + pos; |
break; |
case SEEK_END: |
{ |
FILEINFO info; |
get_fileinfo(fh->name, &info); |
ret = pos + info.size; |
break; |
} |
default: |
ptr->_errno = EINVAL; |
return -1; |
}; |
fh->offset = ret; |
return( ret ); |
} |
_off_t |
_DEFUN (lseek, (fd, pos, whence), |
int fd _AND |
_off_t pos _AND |
int whence) |
{ |
return _lseek_r(_REENT, fd, pos, whence); |
}; |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/mutex.c |
---|
0,0 → 1,23 |
void __mutex_lock(volatile int *val) |
{ |
int tmp; |
__asm__ __volatile__ ( |
"0:\n\t" |
"mov %0, %1\n\t" |
"testl %1, %1\n\t" |
"jz 1f\n\t" |
"movl $68, %%eax\n\t" |
"movl $1, %%ebx\n\t" |
"int $0x40\n\t" |
"jmp 0b\n\t" |
"1:\n\t" |
"incl %1\n\t" |
"xchgl %0, %1\n\t" |
"testl %1, %1\n\t" |
"jnz 0b\n" |
: "+m" (*val), "=&r"(tmp) |
::"eax","ebx" ); |
} |
/contrib/sdk/sources/newlib/libc/reent/openr.c |
---|
0,0 → 1,221 |
/* Reentrant versions of open system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <fcntl.h> |
#include <_syslist.h> |
#include <sys/errno.h> |
#include <stdlib.h> |
#include <string.h> |
#include <stdarg.h> |
#include <sys/kos_io.h> |
/* Some targets provides their own versions of this functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
/* We use the errno variable used by the system dependent layer. */ |
/* |
FUNCTION |
<<_open_r>>---Reentrant version of open |
INDEX |
_open_r |
ANSI_SYNOPSIS |
#include <reent.h> |
int _open_r(struct _reent *<[ptr]>, |
const char *<[file]>, int <[flags]>, int <[mode]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
int _open_r(<[ptr]>, <[file]>, <[flags]>, <[mode]>) |
struct _reent *<[ptr]>; |
char *<[file]>; |
int <[flags]>; |
int <[mode]>; |
DESCRIPTION |
This is a reentrant version of <<open>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
#define NULL_HANDLE (int)-1 |
#define DUMMY_HANDLE (int)-2 |
#define _READ 0x0001 /* file opened for reading */ |
#define _WRITE 0x0002 /* file opened for writing */ |
#define _UNGET 0x0004 /* ungetc has been done */ |
#define _BIGBUF 0x0008 /* big buffer allocated */ |
#define _EOF 0x0010 /* EOF has occurred */ |
#define _SFERR 0x0020 /* error has occurred on this file */ |
#define _APPEND 0x0080 /* file opened for append */ |
#define _BINARY 0x0040 /* file is binary, skip CRLF processing */ |
#define _TMPFIL 0x0800 /* this is a temporary file */ |
#define _DIRTY 0x1000 /* buffer has been modified */ |
#define _ISTTY 0x2000 /* is console device */ |
#define _DYNAMIC 0x4000 /* FILE is dynamically allocated */ |
#define _FILEEXT 0x8000 /* lseek with positive offset has been done */ |
#define _COMMIT 0x0001 /* extended flag: commit OS buffers on flush */ |
extern int _fmode; |
static inline void debug_out(const char val) |
{ |
__asm__ __volatile__( |
"int $0x40 \n\t" |
::"a"(63), "b"(1),"c"(val)); |
} |
int debugwrite(const char *path, const void *buff, |
size_t offset, size_t count, size_t *writes) |
{ |
int ret = count; |
const char *p = buff; |
while (count--) |
{ |
debug_out(*p++); |
}; |
*writes = ret; |
return ret; |
}; |
static int __openFileHandle(const char *path, int mode, int *err) |
{ |
fileinfo_t info; |
__file_handle *handle; |
// path = getfullpath(name); |
*err = get_fileinfo(path, &info); |
if( mode & O_EXCL && mode & O_CREAT ) |
{ |
if( ! *err) |
{ |
*err = EEXIST; |
return -1; |
}; |
} |
if( *err) |
{ |
if(mode & O_CREAT) |
*err=create_file(path); |
if( *err) |
{ |
return -1; |
}; |
}; |
if( mode & O_TRUNC ) |
set_file_size(path, 0); |
if ( !(handle=(__file_handle*)malloc(sizeof( __file_handle) ))) |
{ |
*err = ENOMEM; |
return -1; |
}; |
handle->name = strdup(path); |
handle->offset = 0; |
handle->write = write_file; |
*err = 0; |
return (int)handle; |
}; |
int |
_DEFUN (_open_r, (ptr, file, flags, dmode), |
struct _reent *ptr _AND |
_CONST char *file _AND |
int flags _AND |
int dmode) |
{ |
int hid; |
int handle; |
int err = 0; |
unsigned iomode_flags; |
int rwmode; |
/* |
if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC)) |
{ |
ptr->_errno = ENOSYS; |
return -1; |
} |
*/ |
// First try to get the required slot. |
// No point in creating a file only to not use it. JBS 99/10/26 |
hid = __allocPOSIXHandle( DUMMY_HANDLE ); |
if( hid == -1 ) |
{ |
ptr->_errno = EMFILE; |
return( -1 ); |
} |
handle = __openFileHandle( file, flags, &err); |
if( handle == -1 ) |
{ |
__freePOSIXHandle( hid ); |
ptr->_errno = err; |
return( -1 ); |
} |
__setOSHandle( hid, handle ); // JBS 99/11/01 |
rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR | O_NOINHERIT ); |
iomode_flags = 0; |
if( rwmode == O_RDWR ) iomode_flags |= _READ | _WRITE; |
else if( rwmode == O_RDONLY) iomode_flags |= _READ; |
else if( rwmode == O_WRONLY) iomode_flags |= _WRITE; |
if( flags & O_APPEND ) iomode_flags |= _APPEND; |
if( flags & (O_BINARY|O_TEXT) ) { |
if( flags & O_BINARY ) iomode_flags |= _BINARY; |
} else { |
if( _fmode == O_BINARY ) iomode_flags |= _BINARY; |
} |
__SetIOMode( hid, iomode_flags ); |
ptr->_errno = 0; |
return (hid); |
} |
int |
_DEFUN (open, (file, flags, ...), |
const char *file _AND |
int flags _DOTS) |
{ |
va_list ap; |
int ret; |
va_start (ap, flags); |
ret = _open_r (_REENT, file, flags, va_arg (ap, int)); |
va_end (ap); |
return ret; |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/readr.c |
---|
0,0 → 1,168 |
/* Reentrant versions of read system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <_syslist.h> |
#include <errno.h> |
/* Some targets provides their own versions of this functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
/* We use the errno variable used by the system dependent layer. */ |
/* |
FUNCTION |
<<_read_r>>---Reentrant version of read |
INDEX |
_read_r |
ANSI_SYNOPSIS |
#include <reent.h> |
_ssize_t _read_r(struct _reent *<[ptr]>, |
int <[fd]>, void *<[buf]>, size_t <[cnt]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
_ssize_t _read_r(<[ptr]>, <[fd]>, <[buf]>, <[cnt]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
char *<[buf]>; |
size_t <[cnt]>; |
DESCRIPTION |
This is a reentrant version of <<read>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
extern unsigned __NFiles; |
#define _READ 0x0001 /* file opened for reading */ |
#define _BINARY 0x0040 /* file is binary, skip CRLF processing */ |
#define _ISTTY 0x2000 /* is console device */ |
#define __handle_check( __h, __r ) \ |
if( (__h) < 0 || (__h) > __NFiles ) { \ |
ptr->_errno = EBADF; \ |
return( __r ); \ |
} |
_ssize_t |
_DEFUN (_read, (fd, buf, cnt), |
int fd _AND |
_PTR buf _AND |
size_t cnt) |
{ |
return _read_r( _REENT, fd, buf, cnt); |
} |
_ssize_t |
_DEFUN (_read_r, (ptr, fd, buf, cnt), |
struct _reent *ptr _AND |
int fd _AND |
_PTR buf _AND |
size_t cnt) |
{ |
_ssize_t ret; |
_ssize_t read_len, total_len; |
unsigned reduce_idx, finish_idx; |
unsigned iomode_flags; |
char *buffer = buf; |
int rc; |
int h; |
unsigned amount_read; |
int err; |
__file_handle *fh; |
__handle_check( fd, -1 ); |
__ChkTTYIOMode( fd ); |
iomode_flags = __GetIOMode( fd ); |
if( iomode_flags == 0 ) |
{ |
ptr->_errno = EBADF; |
return( -1 ); |
} |
if( !(iomode_flags & _READ) ) |
{ |
ptr->_errno = EACCES; /* changed from EBADF to EACCES 23-feb-89 */ |
return( -1 ); |
} |
fh = (__file_handle*) __getOSHandle( fd ); |
if( iomode_flags & _BINARY ) /* if binary mode */ |
{ |
err = read_file(fh->name, buffer, fh->offset, cnt, &amount_read); |
fh->offset+= amount_read; |
total_len = amount_read; |
if(err) |
if ( amount_read == 0) |
return (-1); |
} |
else |
{ |
total_len = 0; |
read_len = cnt; |
do |
{ |
err=read_file(fh->name,buffer, fh->offset, cnt, &amount_read); |
fh->offset+=amount_read; |
if( amount_read == 0 ) |
break; /* EOF */ |
reduce_idx = 0; |
finish_idx = reduce_idx; |
for( ; reduce_idx < amount_read; ++reduce_idx ) |
{ |
if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ |
{ |
_lseek_r(ptr, fd, ((long)reduce_idx - (long)amount_read)+1L, |
SEEK_CUR ); |
total_len += finish_idx; |
return( total_len ); |
} |
if( buffer[ reduce_idx ] != '\r' ) |
{ |
buffer[ finish_idx++ ] = buffer[ reduce_idx ]; |
}; |
} |
total_len += finish_idx; |
buffer += finish_idx; |
read_len -= finish_idx; |
if( iomode_flags & _ISTTY ) |
{ |
break; /* 04-feb-88, FWC */ |
} |
} while( read_len != 0 ); |
} |
return( total_len ); |
} |
_ssize_t |
_DEFUN (read, (fd, buf, cnt), |
int fd _AND |
_PTR buf _AND |
size_t cnt) |
{ |
return _read_r(_REENT, fd, buf, cnt); |
}; |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |
/contrib/sdk/sources/newlib/libc/reent/writer.c |
---|
0,0 → 1,257 |
/* Reentrant versions of write system call. */ |
#include <reent.h> |
#include <unistd.h> |
#include <_syslist.h> |
#include <alloca.h> |
#include <errno.h> |
#include <string.h> |
/* Some targets provides their own versions of this functions. Those |
targets should define REENTRANT_SYSCALLS_PROVIDED in TARGET_CFLAGS. */ |
#ifdef _REENT_ONLY |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
#define REENTRANT_SYSCALLS_PROVIDED |
#endif |
#endif |
#ifndef REENTRANT_SYSCALLS_PROVIDED |
/* We use the errno variable used by the system dependent layer. */ |
/* |
FUNCTION |
<<_write_r>>---Reentrant version of write |
INDEX |
_write_r |
ANSI_SYNOPSIS |
#include <reent.h> |
_ssize_t _write_r(struct _reent *<[ptr]>, |
int <[fd]>, const void *<[buf]>, size_t <[cnt]>); |
TRAD_SYNOPSIS |
#include <reent.h> |
_ssize_t _write_r(<[ptr]>, <[fd]>, <[buf]>, <[cnt]>) |
struct _reent *<[ptr]>; |
int <[fd]>; |
char *<[buf]>; |
size_t <[cnt]>; |
DESCRIPTION |
This is a reentrant version of <<write>>. It |
takes a pointer to the global data block, which holds |
<<errno>>. |
*/ |
#define _WRITE 0x0002 /* file opened for writing */ |
#define _APPEND 0x0080 /* file opened for append */ |
#define _BINARY 0x0040 /* file is binary, skip CRLF processing */ |
#define _ISTTY 0x2000 /* is console device */ |
#define _FILEEXT 0x8000 /* lseek with positive offset has been done */ |
#define __handle_check( __h, __r ) \ |
if( (__h) < 0 || (__h) > __NFiles ) { \ |
ptr->_errno = EBADF; \ |
return( __r ); \ |
} |
extern unsigned __NFiles; |
#define PAD_SIZE 512 |
static int zero_pad(struct _reent *ptr, int handle ) /* 09-jan-95 */ |
/*******************************/ |
{ |
int rc; |
long curPos, eodPos; |
long bytesToWrite; |
unsigned writeAmt; |
char zeroBuf[PAD_SIZE]; |
// Pad with zeros due to lseek() past EOF (POSIX) |
curPos = _lseek_r( ptr, handle, 0L, SEEK_CUR ); /* current offset */ |
if( curPos == -1 ) |
return( -1 ); |
eodPos = _lseek_r( ptr, handle, 0L, SEEK_END ); /* end of data offset */ |
if( eodPos == -1 ) |
return( -1 ); |
if( curPos > eodPos ) { |
bytesToWrite = curPos - eodPos; /* amount to pad by */ |
if( bytesToWrite > 0 ) { /* only write if needed */ |
memset( zeroBuf, 0x00, PAD_SIZE ); /* zero out a buffer */ |
do { /* loop until done */ |
if( bytesToWrite > PAD_SIZE ) |
writeAmt = 512; |
else |
writeAmt = (unsigned)bytesToWrite; |
rc = _write_r(ptr, handle, zeroBuf, writeAmt ); |
if( rc < 0 ) |
return( rc ); |
bytesToWrite -= writeAmt; /* more bytes written */ |
} while( bytesToWrite != 0 ); |
} |
} else { |
curPos = _lseek_r( ptr, handle, curPos, SEEK_SET ); |
if( curPos == -1 ) { |
return( -1 ); |
} |
} |
return( 0 ); /* return success code */ |
} |
static int os_write(struct _reent *ptr, int handle, |
const void *buffer, unsigned len, unsigned *amt ) |
/********************************************************************************/ |
{ |
__file_handle *fh; |
int rc; |
rc = 0; |
*amt = 0; |
fh = (__file_handle*) __getOSHandle( handle ); |
rc = fh->write(fh->name,buffer,fh->offset,len,amt); |
fh->offset+= *amt; |
if( *amt != len ) |
{ |
rc = ENOSPC; |
ptr->_errno = ENOSPC; |
} |
return( rc ); |
} |
_ssize_t |
_DEFUN (_write_r, (ptr, fd, buffer, cnt), |
struct _reent *ptr _AND |
int fd _AND |
_CONST _PTR buffer _AND |
size_t cnt) |
{ |
_ssize_t ret; |
unsigned int iomode_flags; |
unsigned len_written, i, j; |
int rc2; |
char *buf; |
__file_handle *fh; |
__handle_check( fd, -1 ); |
iomode_flags = __GetIOMode( fd ); |
if( iomode_flags == 0 ) |
{ |
ptr->_errno = EBADF; |
return( -1 ); |
} |
if( !(iomode_flags & _WRITE) ) { |
ptr->_errno = EACCES ; /* changed from EBADF to EACCES 23-feb-89 */ |
return( -1 ); |
} |
if( (iomode_flags & _APPEND) && !(iomode_flags & _ISTTY) ) |
{ |
fh->offset = _lseek_r(ptr, fd, 0L, SEEK_END ); /* end of data offset */ |
} |
len_written = 0; |
rc2 = 0; |
// Pad the file with zeros if necessary |
if( iomode_flags & _FILEEXT ) |
{ |
// turn off file extended flag |
__SetIOMode_nogrow( fd, iomode_flags&(~_FILEEXT) ); |
// It is not required to pad a file with zeroes on an NTFS file system; |
// unfortunately it is required on FAT (and probably FAT32). (JBS) |
rc2 = zero_pad( ptr, fd ); |
} |
if( rc2 == 0 ) |
{ |
if( iomode_flags & _BINARY ) { /* if binary mode */ |
rc2 = os_write(ptr, fd, buffer, cnt, &len_written ); |
/* end of binary mode part */ |
} else { /* text mode */ |
int buf_size = 512; |
buf = (char*)alloca( buf_size ); |
j = 0; |
for( i = 0; i < cnt; ) |
{ |
if( ((const char*)buffer)[i] == '\n' ) |
{ |
buf[j] = '\r'; |
++j; |
if( j == buf_size ) |
{ |
rc2 = os_write(ptr, fd, buf, buf_size, &j ); |
if( rc2 == -1 ) |
break; |
len_written += j; |
if( rc2 == ENOSPC ) |
break; |
len_written = i; |
j = 0; |
} |
} |
buf[j] = ((const char*)buffer)[i]; |
++i; |
++j; |
if( j == buf_size ) { |
rc2 = os_write(ptr, fd, buf, buf_size, &j ); |
if( rc2 == -1 ) |
break; |
len_written += j; |
if( rc2 == ENOSPC ) |
break; |
len_written = i; |
j = 0; |
} |
} |
if( j ) { |
rc2 = os_write(ptr, fd, buf, j, &i ); |
if( rc2 == ENOSPC ) { |
len_written += i; |
} else { |
len_written = cnt; |
} |
} |
/* end of text mode part */ |
} |
} |
if( rc2 == -1 ) { |
return( rc2 ); |
} else { |
return( len_written ); |
} |
} |
_ssize_t |
_DEFUN (write, ( fd, buffer, cnt), |
int fd _AND |
_CONST _PTR buffer _AND |
size_t cnt) |
{ |
return _write_r(_REENT, fd, buffer, cnt); |
} |
#endif /* ! defined (REENTRANT_SYSCALLS_PROVIDED) */ |