2000-02-18 03:39:52 +08:00
|
|
|
/* Support files for GNU libc. Files in the system namespace go here.
|
|
|
|
Files in the C namespace (ie those that do not start with an
|
|
|
|
underscore) go in .c. */
|
|
|
|
|
|
|
|
#include <_ansi.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <reent.h>
|
2006-06-06 03:45:08 +08:00
|
|
|
#include <signal.h>
|
2004-01-07 03:27:21 +08:00
|
|
|
#include <unistd.h>
|
2006-06-14 04:50:24 +08:00
|
|
|
#include <sys/wait.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include "swi.h"
|
|
|
|
|
|
|
|
/* Forward prototypes. */
|
2018-03-15 17:55:11 +08:00
|
|
|
int _system (const char *);
|
|
|
|
int _rename (const char *, const char *);
|
|
|
|
int _isatty (int);
|
2017-12-04 10:41:16 +08:00
|
|
|
clock_t _times (struct tms *);
|
2018-03-15 17:55:11 +08:00
|
|
|
int _gettimeofday (struct timeval *, void *);
|
|
|
|
void _raise (void);
|
|
|
|
int _unlink (const char *);
|
|
|
|
int _link (const char *, const char *);
|
|
|
|
int _stat (const char *, struct stat *);
|
|
|
|
int _fstat (int, struct stat *);
|
|
|
|
void * _sbrk (ptrdiff_t);
|
|
|
|
pid_t _getpid (void);
|
|
|
|
int _kill (int, int);
|
|
|
|
void _exit (int);
|
|
|
|
int _close (int);
|
|
|
|
int _swiclose (int);
|
|
|
|
int _open (const char *, int, ...);
|
|
|
|
int _swiopen (const char *, int);
|
|
|
|
int _write (int, const void *, size_t);
|
|
|
|
int _swiwrite (int, const void *, size_t);
|
|
|
|
_off_t _lseek (int, _off_t, int);
|
|
|
|
_off_t _swilseek (int, _off_t, int);
|
|
|
|
int _read (int, void *, size_t);
|
|
|
|
int _swiread (int, void *, size_t);
|
|
|
|
void initialise_monitor_handles (void);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2017-12-04 10:41:16 +08:00
|
|
|
static int wrap (int);
|
|
|
|
static int error (int);
|
|
|
|
static int get_errno (void);
|
|
|
|
static int remap_handle (int);
|
2018-03-15 17:55:11 +08:00
|
|
|
static int findslot (int);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
/* Register name faking - works in collusion with the linker. */
|
|
|
|
register char * stack_ptr asm ("sp");
|
|
|
|
|
|
|
|
|
|
|
|
/* following is copied from libc/stdio/local.h to check std streams */
|
2017-12-04 13:54:48 +08:00
|
|
|
extern void __sinit (struct _reent *);
|
2005-02-08 09:33:19 +08:00
|
|
|
#define CHECK_INIT(ptr) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if ((ptr) && !(ptr)->__sdidinit) \
|
|
|
|
__sinit (ptr); \
|
|
|
|
} \
|
2000-02-18 03:39:52 +08:00
|
|
|
while (0)
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Adjust our internal handles to stay away from std* handles. */
|
2000-02-18 03:39:52 +08:00
|
|
|
#define FILE_HANDLE_OFFSET (0x20)
|
|
|
|
|
|
|
|
static int monitor_stdin;
|
|
|
|
static int monitor_stdout;
|
|
|
|
static int monitor_stderr;
|
|
|
|
|
|
|
|
/* Struct used to keep track of the file position, just so we
|
|
|
|
can implement fseek(fh,x,SEEK_CUR). */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int handle;
|
2018-08-31 19:26:02 +08:00
|
|
|
off_t pos;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
poslog;
|
|
|
|
|
|
|
|
#define MAX_OPEN_FILES 20
|
|
|
|
static poslog openfiles [MAX_OPEN_FILES];
|
|
|
|
|
|
|
|
static int
|
|
|
|
findslot (int fh)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_OPEN_FILES; i ++)
|
|
|
|
if (openfiles[i].handle == fh)
|
|
|
|
break;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Function to convert std(in|out|err) handles to internal versions. */
|
2000-02-18 03:39:52 +08:00
|
|
|
static int
|
|
|
|
remap_handle (int fh)
|
|
|
|
{
|
2005-02-08 09:33:19 +08:00
|
|
|
CHECK_INIT(_REENT);
|
|
|
|
|
2004-01-07 03:27:21 +08:00
|
|
|
if (fh == STDIN_FILENO)
|
2000-02-18 03:39:52 +08:00
|
|
|
return monitor_stdin;
|
2004-01-07 03:27:21 +08:00
|
|
|
if (fh == STDOUT_FILENO)
|
2000-02-18 03:39:52 +08:00
|
|
|
return monitor_stdout;
|
2004-01-07 03:27:21 +08:00
|
|
|
if (fh == STDERR_FILENO)
|
2000-02-18 03:39:52 +08:00
|
|
|
return monitor_stderr;
|
|
|
|
|
|
|
|
return fh - FILE_HANDLE_OFFSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initialise_monitor_handles (void)
|
|
|
|
{
|
|
|
|
int i;
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int volatile block[3];
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
block[0] = (int) ":tt";
|
|
|
|
block[2] = 3; /* length of filename */
|
|
|
|
block[1] = 0; /* mode "r" */
|
|
|
|
monitor_stdin = do_AngelSWI (AngelSWI_Reason_Open, (void *) block);
|
|
|
|
|
|
|
|
block[0] = (int) ":tt";
|
|
|
|
block[2] = 3; /* length of filename */
|
|
|
|
block[1] = 4; /* mode "w" */
|
2018-03-15 17:55:11 +08:00
|
|
|
monitor_stdout = monitor_stderr
|
|
|
|
= do_AngelSWI (AngelSWI_Reason_Open, (void *) block);
|
2000-02-18 03:39:52 +08:00
|
|
|
#else
|
|
|
|
int fh;
|
|
|
|
const char * name;
|
|
|
|
|
|
|
|
name = ":tt";
|
|
|
|
asm ("mov r0,%2; mov r1, #0; swi %a1; mov %0, r0"
|
|
|
|
: "=r"(fh)
|
|
|
|
: "i" (SWI_Open),"r"(name)
|
|
|
|
: "r0","r1");
|
|
|
|
monitor_stdin = fh;
|
|
|
|
|
|
|
|
name = ":tt";
|
|
|
|
asm ("mov r0,%2; mov r1, #4; swi %a1; mov %0, r0"
|
|
|
|
: "=r"(fh)
|
|
|
|
: "i" (SWI_Open),"r"(name)
|
|
|
|
: "r0","r1");
|
|
|
|
monitor_stdout = monitor_stderr = fh;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_OPEN_FILES; i ++)
|
|
|
|
openfiles[i].handle = -1;
|
|
|
|
|
|
|
|
openfiles[0].handle = monitor_stdin;
|
|
|
|
openfiles[0].pos = 0;
|
|
|
|
openfiles[1].handle = monitor_stdout;
|
|
|
|
openfiles[1].pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_errno (void)
|
|
|
|
{
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
return do_AngelSWI (AngelSWI_Reason_Errno, NULL);
|
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0");
|
|
|
|
asm ("swi %a1" : "=r"(r0): "i" (SWI_GetErrno));
|
|
|
|
return r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
error (int result)
|
|
|
|
{
|
|
|
|
errno = get_errno ();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
wrap (int result)
|
|
|
|
{
|
|
|
|
if (result == -1)
|
|
|
|
return error (-1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Returns # chars not! written. */
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_swiread (int file, void * ptr, size_t len)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int fh = remap_handle (file);
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[3];
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
block[0] = fh;
|
|
|
|
block[1] = (int) ptr;
|
2018-03-15 17:55:11 +08:00
|
|
|
block[2] = (int) len;
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
return do_AngelSWI (AngelSWI_Reason_Read, block);
|
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0") = fh;
|
|
|
|
register int r1 asm("r1") = (int) ptr;
|
|
|
|
register int r2 asm("r2") = (int) len;
|
|
|
|
asm ("swi %a4"
|
|
|
|
: "=r" (r0)
|
|
|
|
: "0"(r0), "r"(r1), "r"(r2), "i"(SWI_Read));
|
|
|
|
return r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-12 03:54:30 +08:00
|
|
|
int __attribute__((weak))
|
2018-03-15 17:55:11 +08:00
|
|
|
_read (int file, void * ptr, size_t len)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int slot = findslot (remap_handle (file));
|
|
|
|
int x = _swiread (file, ptr, len);
|
|
|
|
|
|
|
|
if (x < 0)
|
|
|
|
return error (-1);
|
|
|
|
|
|
|
|
if (slot != MAX_OPEN_FILES)
|
|
|
|
openfiles [slot].pos += len - x;
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* x == len is not an error, at least if we want feof() to work. */
|
2000-02-18 03:39:52 +08:00
|
|
|
return len - x;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:11 +08:00
|
|
|
off_t
|
|
|
|
_swilseek (int file, off_t ptr, int dir)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2018-03-15 17:55:11 +08:00
|
|
|
_off_t res;
|
2000-02-18 03:39:52 +08:00
|
|
|
int fh = remap_handle (file);
|
|
|
|
int slot = findslot (fh);
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[2];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (dir == SEEK_CUR)
|
|
|
|
{
|
2018-08-31 19:26:02 +08:00
|
|
|
off_t pos;
|
2000-02-18 03:39:52 +08:00
|
|
|
if (slot == MAX_OPEN_FILES)
|
|
|
|
return -1;
|
2018-08-31 19:26:02 +08:00
|
|
|
pos = openfiles[slot].pos;
|
|
|
|
|
|
|
|
/* Avoid SWI SEEK command when just querying file position. */
|
|
|
|
if (ptr == 0)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
ptr += pos;
|
2000-02-18 03:39:52 +08:00
|
|
|
dir = SEEK_SET;
|
|
|
|
}
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
if (dir == SEEK_END)
|
|
|
|
{
|
|
|
|
block[0] = fh;
|
|
|
|
ptr += do_AngelSWI (AngelSWI_Reason_FLen, block);
|
|
|
|
}
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* This code only does absolute seeks. */
|
2000-02-18 03:39:52 +08:00
|
|
|
block[0] = remap_handle (file);
|
|
|
|
block[1] = ptr;
|
|
|
|
res = do_AngelSWI (AngelSWI_Reason_Seek, block);
|
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0");
|
|
|
|
register int r1 asm("r1");
|
2000-02-18 03:39:52 +08:00
|
|
|
if (dir == SEEK_END)
|
|
|
|
{
|
2018-03-15 17:55:11 +08:00
|
|
|
r0 = (int) fh;
|
|
|
|
asm ("swi %a2"
|
|
|
|
: "=r" (r0)
|
|
|
|
: "0"(r0), "i" (SWI_Flen));
|
|
|
|
res = r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
ptr += res;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* This code only does absolute seeks. */
|
2018-03-15 17:55:11 +08:00
|
|
|
r0 = (int) fh;
|
|
|
|
r1 = (int) ptr;
|
|
|
|
asm ("swi %a3"
|
|
|
|
: "=r" (r0)
|
|
|
|
: "0"(r0), "r"(r1), "i" (SWI_Seek));
|
|
|
|
res = r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (slot != MAX_OPEN_FILES && res == 0)
|
|
|
|
openfiles[slot].pos = ptr;
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* This is expected to return the position in the file. */
|
2000-02-18 03:39:52 +08:00
|
|
|
return res == 0 ? ptr : -1;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:11 +08:00
|
|
|
off_t
|
|
|
|
_lseek (int file, off_t ptr, int dir)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
return wrap (_swilseek (file, ptr, dir));
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Returns #chars not! written. */
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_swiwrite (int file, const void * ptr, size_t len)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int fh = remap_handle (file);
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[3];
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
block[0] = fh;
|
|
|
|
block[1] = (int) ptr;
|
2018-03-15 17:55:11 +08:00
|
|
|
block[2] = (int) len;
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
return do_AngelSWI (AngelSWI_Reason_Write, block);
|
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0") = fh;
|
|
|
|
register int r1 asm("r1") = (int) ptr;
|
|
|
|
register int r2 asm("r2") = (int) len;
|
|
|
|
|
|
|
|
asm ("swi %a4"
|
|
|
|
: "=r" (r0)
|
|
|
|
: "0"(fh), "r"(r1), "r"(r2), "i"(SWI_Write));
|
|
|
|
return r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-12 03:54:30 +08:00
|
|
|
int __attribute__((weak))
|
2018-03-15 17:55:11 +08:00
|
|
|
_write (int file, const void * ptr, size_t len)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int slot = findslot (remap_handle (file));
|
2018-03-15 17:55:11 +08:00
|
|
|
int x = _swiwrite (file, ptr, len);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
if (x == -1 || x == len)
|
|
|
|
return error (-1);
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (slot != MAX_OPEN_FILES)
|
|
|
|
openfiles[slot].pos += len - x;
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
return len - x;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
extern int strlen (const char *);
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_swiopen (const char * path, int flags)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int aflags = 0, fh;
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[3];
|
|
|
|
#endif
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
int i = findslot (-1);
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (i == MAX_OPEN_FILES)
|
|
|
|
return -1;
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* The flags are Unix-style, so we need to convert them. */
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef O_BINARY
|
|
|
|
if (flags & O_BINARY)
|
|
|
|
aflags |= 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (flags & O_RDWR)
|
|
|
|
aflags |= 2;
|
|
|
|
|
|
|
|
if (flags & O_CREAT)
|
|
|
|
aflags |= 4;
|
|
|
|
|
|
|
|
if (flags & O_TRUNC)
|
|
|
|
aflags |= 4;
|
|
|
|
|
|
|
|
if (flags & O_APPEND)
|
|
|
|
{
|
2000-03-25 02:17:17 +08:00
|
|
|
aflags &= ~4; /* Can't ask for w AND a; means just 'a'. */
|
2000-02-18 03:39:52 +08:00
|
|
|
aflags |= 8;
|
|
|
|
}
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
block[0] = (int) path;
|
|
|
|
block[2] = strlen (path);
|
|
|
|
block[1] = aflags;
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
fh = do_AngelSWI (AngelSWI_Reason_Open, block);
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0") = (int) path;
|
|
|
|
register int r1 asm("r1") = (int) aflags;;
|
|
|
|
asm ("swi %a3"
|
|
|
|
: "=r"(r0)
|
|
|
|
: "0"(r0), "r"(r1), "i" (SWI_Open));
|
|
|
|
fh = r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (fh >= 0)
|
|
|
|
{
|
|
|
|
openfiles[i].handle = fh;
|
|
|
|
openfiles[i].pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fh >= 0 ? fh + FILE_HANDLE_OFFSET : error (fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_open (const char * path, int flags, ...)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
return wrap (_swiopen (path, flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_swiclose (int file)
|
|
|
|
{
|
|
|
|
int myhan = remap_handle (file);
|
|
|
|
int slot = findslot (myhan);
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (slot != MAX_OPEN_FILES)
|
|
|
|
openfiles[slot].handle = -1;
|
|
|
|
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
return do_AngelSWI (AngelSWI_Reason_Close, & myhan);
|
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0") = myhan;
|
|
|
|
asm ("swi %a2" : "=r"(r0): "0"(r0), "i" (SWI_Close));
|
|
|
|
return r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_close (int file)
|
|
|
|
{
|
|
|
|
return wrap (_swiclose (file));
|
|
|
|
}
|
|
|
|
|
2006-06-06 03:45:08 +08:00
|
|
|
int
|
|
|
|
_kill (int pid, int sig)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2006-06-06 03:45:08 +08:00
|
|
|
(void)pid; (void)sig;
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
2006-06-06 03:45:08 +08:00
|
|
|
/* Note: The pid argument is thrown away. */
|
|
|
|
switch (sig) {
|
|
|
|
case SIGABRT:
|
|
|
|
return do_AngelSWI (AngelSWI_Reason_ReportException,
|
|
|
|
(void *) ADP_Stopped_RunTimeError);
|
|
|
|
default:
|
|
|
|
return do_AngelSWI (AngelSWI_Reason_ReportException,
|
|
|
|
(void *) ADP_Stopped_ApplicationExit);
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
#else
|
|
|
|
asm ("swi %a0" :: "i" (SWI_Exit));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-06 03:45:08 +08:00
|
|
|
void
|
|
|
|
_exit (int status)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2006-06-06 03:45:08 +08:00
|
|
|
/* There is only one SWI for both _exit and _kill. For _exit, call
|
|
|
|
the SWI with the second argument set to -1, an invalid value for
|
|
|
|
signum, so that the SWI handler can distinguish the two calls.
|
|
|
|
Note: The RDI implementation of _kill throws away both its
|
|
|
|
arguments. */
|
|
|
|
_kill(status, -1);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:11 +08:00
|
|
|
pid_t
|
|
|
|
_getpid (void)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2018-03-15 17:55:11 +08:00
|
|
|
return (pid_t)1;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:11 +08:00
|
|
|
void * __attribute__((weak))
|
|
|
|
_sbrk (ptrdiff_t incr)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2000-03-25 02:17:17 +08:00
|
|
|
extern char end asm ("end"); /* Defined by the linker. */
|
2000-02-18 03:39:52 +08:00
|
|
|
static char * heap_end;
|
|
|
|
char * prev_heap_end;
|
|
|
|
|
|
|
|
if (heap_end == NULL)
|
|
|
|
heap_end = & end;
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
prev_heap_end = heap_end;
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
if (heap_end + incr > stack_ptr)
|
|
|
|
{
|
2002-01-18 00:39:53 +08:00
|
|
|
/* Some of the libstdc++-v3 tests rely upon detecting
|
|
|
|
out of memory errors, so do not abort here. */
|
|
|
|
#if 0
|
|
|
|
extern void abort (void);
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
_write (1, "_sbrk: Heap and stack collision\n", 32);
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
abort ();
|
2002-01-18 00:39:53 +08:00
|
|
|
#else
|
|
|
|
errno = ENOMEM;
|
2018-03-15 17:55:11 +08:00
|
|
|
return (void *) -1;
|
2002-01-18 00:39:53 +08:00
|
|
|
#endif
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
heap_end += incr;
|
|
|
|
|
2018-03-15 17:55:11 +08:00
|
|
|
return (void *) prev_heap_end;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
extern void memset (struct stat *, int, unsigned int);
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
|
|
|
_fstat (int file, struct stat * st)
|
|
|
|
{
|
|
|
|
memset (st, 0, sizeof (* st));
|
|
|
|
st->st_mode = S_IFCHR;
|
|
|
|
st->st_blksize = 1024;
|
|
|
|
return 0;
|
2000-03-25 02:17:17 +08:00
|
|
|
file = file;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
2002-03-13 05:25:12 +08:00
|
|
|
int _stat (const char *fname, struct stat *st)
|
|
|
|
{
|
|
|
|
int file;
|
|
|
|
|
|
|
|
/* The best we can do is try to open the file readonly. If it exists,
|
|
|
|
then we can guess a few things about it. */
|
|
|
|
if ((file = _open (fname, O_RDONLY)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset (st, 0, sizeof (* st));
|
|
|
|
st->st_mode = S_IFREG | S_IREAD;
|
|
|
|
st->st_blksize = 1024;
|
|
|
|
_swiclose (file); /* Not interested in the error. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_link (const char *__path1 __attribute__ ((unused)),
|
|
|
|
const char *__path2 __attribute__ ((unused)))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2018-03-15 17:55:11 +08:00
|
|
|
errno = ENOSYS;
|
2000-02-18 03:39:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-03-15 17:55:11 +08:00
|
|
|
_unlink (const char *path __attribute__ ((unused)))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2006-06-14 04:50:24 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[2];
|
2018-10-02 01:22:43 +08:00
|
|
|
block[0] = (int) path;
|
2006-06-14 04:50:24 +08:00
|
|
|
block[1] = strlen(path);
|
|
|
|
return wrap (do_AngelSWI (AngelSWI_Reason_Remove, block)) ? -1 : 0;
|
2018-03-15 17:55:11 +08:00
|
|
|
#else
|
|
|
|
errno = ENOSYS;
|
2000-02-18 03:39:52 +08:00
|
|
|
return -1;
|
2006-06-14 04:50:24 +08:00
|
|
|
#endif
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_raise (void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-07-07 00:56:30 +08:00
|
|
|
_gettimeofday (struct timeval * tp, void * tzvp)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2007-07-07 00:56:30 +08:00
|
|
|
struct timezone *tzp = tzvp;
|
2000-02-18 03:39:52 +08:00
|
|
|
if (tp)
|
|
|
|
{
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Ask the host for the seconds since the Unix epoch. */
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
tp->tv_sec = do_AngelSWI (AngelSWI_Reason_Time,NULL);
|
|
|
|
#else
|
|
|
|
{
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0");
|
|
|
|
asm ("swi %a1" : "=r" (r0): "i" (SWI_Time));
|
|
|
|
tp->tv_sec = r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
tp->tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Return fixed data for the timezone. */
|
2000-02-18 03:39:52 +08:00
|
|
|
if (tzp)
|
|
|
|
{
|
|
|
|
tzp->tz_minuteswest = 0;
|
|
|
|
tzp->tz_dsttime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-03-25 02:17:17 +08:00
|
|
|
/* Return a clock that ticks at 100Hz. */
|
2018-03-15 17:55:11 +08:00
|
|
|
clock_t
|
2000-02-18 03:39:52 +08:00
|
|
|
_times (struct tms * tp)
|
|
|
|
{
|
|
|
|
clock_t timeval;
|
|
|
|
|
|
|
|
#ifdef ARM_RDI_MONITOR
|
2018-03-15 17:55:11 +08:00
|
|
|
timeval = do_AngelSWI (AngelSWI_Reason_Clock, NULL);
|
2000-02-18 03:39:52 +08:00
|
|
|
#else
|
2018-03-15 17:55:11 +08:00
|
|
|
register int r0 asm("r0");
|
|
|
|
asm ("swi %a1" : "=r" (r0): "i" (SWI_Clock));
|
|
|
|
timeval = (clock_t) r0;
|
2000-02-18 03:39:52 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (tp)
|
|
|
|
{
|
|
|
|
tp->tms_utime = timeval; /* user time */
|
|
|
|
tp->tms_stime = 0; /* system time */
|
|
|
|
tp->tms_cutime = 0; /* user time, children */
|
|
|
|
tp->tms_cstime = 0; /* system time, children */
|
|
|
|
}
|
2018-03-15 17:55:11 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
return timeval;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2008-01-04 06:33:37 +08:00
|
|
|
_isatty (int fd)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2006-06-14 04:50:24 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int fh = remap_handle (fd);
|
|
|
|
return wrap (do_AngelSWI (AngelSWI_Reason_IsTTY, &fh));
|
|
|
|
#else
|
|
|
|
return (fd <= 2) ? 1 : 0; /* one of stdin, stdout, stderr */
|
|
|
|
#endif
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
2002-04-30 05:49:56 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
_system (const char *s)
|
|
|
|
{
|
2006-06-14 04:50:24 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[2];
|
|
|
|
int e;
|
|
|
|
|
|
|
|
/* Hmmm. The ARM debug interface specification doesn't say whether
|
|
|
|
SYS_SYSTEM does the right thing with a null argument, or assign any
|
|
|
|
meaning to its return value. Try to do something reasonable.... */
|
|
|
|
if (!s)
|
|
|
|
return 1; /* maybe there is a shell available? we can hope. :-P */
|
2018-10-02 01:22:43 +08:00
|
|
|
block[0] = (int) s;
|
2006-06-14 04:50:24 +08:00
|
|
|
block[1] = strlen (s);
|
|
|
|
e = wrap (do_AngelSWI (AngelSWI_Reason_System, block));
|
|
|
|
if ((e >= 0) && (e < 256))
|
|
|
|
{
|
|
|
|
/* We have to convert e, an exit status to the encoded status of
|
2018-03-15 17:55:11 +08:00
|
|
|
the command. To avoid hard coding the exit status, we simply
|
2006-06-14 04:50:24 +08:00
|
|
|
loop until we find the right position. */
|
|
|
|
int exit_code;
|
|
|
|
|
|
|
|
for (exit_code = e; e && WEXITSTATUS (e) != exit_code; e <<= 1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
#else
|
2002-04-30 05:49:56 +08:00
|
|
|
if (s == NULL)
|
|
|
|
return 0;
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2006-06-14 04:50:24 +08:00
|
|
|
#endif
|
2002-04-30 05:49:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-05-07 04:46:37 +08:00
|
|
|
_rename (const char * oldpath, const char * newpath)
|
2002-04-30 05:49:56 +08:00
|
|
|
{
|
2006-06-14 04:50:24 +08:00
|
|
|
#ifdef ARM_RDI_MONITOR
|
|
|
|
int block[4];
|
2018-10-02 01:22:43 +08:00
|
|
|
block[0] = (int) oldpath;
|
2006-06-14 04:50:24 +08:00
|
|
|
block[1] = strlen(oldpath);
|
2018-10-02 01:22:43 +08:00
|
|
|
block[2] = (int) newpath;
|
2006-06-14 04:50:24 +08:00
|
|
|
block[3] = strlen(newpath);
|
|
|
|
return wrap (do_AngelSWI (AngelSWI_Reason_Rename, block)) ? -1 : 0;
|
2018-03-15 17:55:11 +08:00
|
|
|
#else
|
2002-04-30 05:49:56 +08:00
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
2006-06-14 04:50:24 +08:00
|
|
|
#endif
|
2002-04-30 05:49:56 +08:00
|
|
|
}
|