2007-08-10 Carlos O'Donell <carlos@codesourcery.com>
2007-07-12 Daniel Jacobowitz <dan@codesourcery.com> * arm/syscalls.c (_stat): Use _close * arm/syscalls.c (struct poslog): Rename to... (struct fdent): ... this. (FILE_HANDLE_OFFSET): Remove. (findslot): Return a struct fdent* if fd is valid, otherwise return NULL. (newslot): New function. (remap_handle): Remove. (initialise_monitor_handles): Use stdout as fallback for stderr. (wrap): Rename to... (checkerror): ... this. (_swiread): Use checkerror() for the return of SWI calls. Correct the use of r0. (_read): Use findslot(). Return EBADF on bad handle. (_swilseek): Call findslot(). Check for valid whence. Check for negative offset when using SEEK_CUR, and check for overflow. Use checkerror(). Check *_Flen calls for errors. (_swiwrite): Correct the use of r0. (_write): Call findslot(). (_swiopen): Call findslot(). Check for valid flags. Use checkerror(). Call newslot(). Handle O_TRUNC, and O_WRONLY. Return index into openfiles[] for fd. (_swiclose): Correct the use of r0. (_close): Handle stderr == stdout case. Only reclaim handle if _swiclose succeeded. (_getpid): Use __attribute__((unused)). (_sbrk): Fix formatting. (_swistat): New function. (_stat): Call _swistat(). (_fstat): Call _swistat(). (_unlink): Correct the use of r0. (isatty): Call finslot(). Correct the use of r0. (_system): Call checkerror(). Correct the use of r0. (_rename): Correct the use of r0.
This commit is contained in:
parent
5ede1e7109
commit
847e89f8e1
|
@ -1,8 +1,43 @@
|
||||||
2007-07-13 Kevin Buettner <kevinb@redhat.com>
|
2007-08-10 Carlos O'Donell <carlos@codesourcery.com>
|
||||||
|
|
||||||
* mep/fmax.ld, mep/gmap_default.ld, mep/min.ld, mep/simple.ld
|
2007-07-12 Daniel Jacobowitz <dan@codesourcery.com>
|
||||||
(.gcc_except_table): Add pattern for .gcc_except_table.*.
|
|
||||||
|
|
||||||
|
* arm/syscalls.c (_stat): Use _close
|
||||||
|
|
||||||
|
* arm/syscalls.c (struct poslog): Rename to...
|
||||||
|
(struct fdent): ... this.
|
||||||
|
(FILE_HANDLE_OFFSET): Remove.
|
||||||
|
(findslot): Return a struct fdent* if fd is valid, otherwise return NULL.
|
||||||
|
(newslot): New function.
|
||||||
|
(remap_handle): Remove.
|
||||||
|
(initialise_monitor_handles): Use stdout as fallback for stderr.
|
||||||
|
(wrap): Rename to...
|
||||||
|
(checkerror): ... this.
|
||||||
|
(_swiread): Use checkerror() for the return of SWI calls. Correct the use of r0.
|
||||||
|
(_read): Use findslot(). Return EBADF on bad handle.
|
||||||
|
(_swilseek): Call findslot(). Check for valid whence. Check for negative offset when using
|
||||||
|
SEEK_CUR, and check for overflow. Use checkerror(). Check *_Flen calls for errors.
|
||||||
|
(_swiwrite): Correct the use of r0.
|
||||||
|
(_write): Call findslot().
|
||||||
|
(_swiopen): Call findslot(). Check for valid flags. Use checkerror(). Call newslot().
|
||||||
|
Handle O_TRUNC, and O_WRONLY. Return index into openfiles[] for fd.
|
||||||
|
(_swiclose): Correct the use of r0.
|
||||||
|
(_close): Handle stderr == stdout case. Only reclaim handle if _swiclose succeeded.
|
||||||
|
(_getpid): Use __attribute__((unused)).
|
||||||
|
(_sbrk): Fix formatting.
|
||||||
|
(_swistat): New function.
|
||||||
|
(_stat): Call _swistat().
|
||||||
|
(_fstat): Call _swistat().
|
||||||
|
(_unlink): Correct the use of r0.
|
||||||
|
(isatty): Call finslot(). Correct the use of r0.
|
||||||
|
(_system): Call checkerror(). Correct the use of r0.
|
||||||
|
(_rename): Correct the use of r0.
|
||||||
|
|
||||||
|
2007-07-13 Kevin Buettner <kevinb@redhat.com>
|
||||||
|
|
||||||
|
* mep/fmax.ld, mep/gmap_default.ld, mep/min.ld, mep/simple.ld (.gcc_except_table): Add pattern
|
||||||
|
for .gcc_except_table.*.
|
||||||
|
|
||||||
2007-07-06 Jeff Johnston <jjohnstn@redhat.com>
|
2007-07-06 Jeff Johnston <jjohnstn@redhat.com>
|
||||||
|
|
||||||
* arm/syscalls.c (gettimeofday): Change to POSIX signature
|
* arm/syscalls.c (gettimeofday): Change to POSIX signature
|
||||||
|
|
|
@ -27,6 +27,7 @@ int _unlink _PARAMS ((const char *));
|
||||||
int _link _PARAMS ((void));
|
int _link _PARAMS ((void));
|
||||||
int _stat _PARAMS ((const char *, struct stat *));
|
int _stat _PARAMS ((const char *, struct stat *));
|
||||||
int _fstat _PARAMS ((int, struct stat *));
|
int _fstat _PARAMS ((int, struct stat *));
|
||||||
|
int _swistat _PARAMS ((int fd, struct stat * st));
|
||||||
caddr_t _sbrk _PARAMS ((int));
|
caddr_t _sbrk _PARAMS ((int));
|
||||||
int _getpid _PARAMS ((int));
|
int _getpid _PARAMS ((int));
|
||||||
int _close _PARAMS ((int));
|
int _close _PARAMS ((int));
|
||||||
|
@ -42,11 +43,39 @@ int _read _PARAMS ((int, char *, int));
|
||||||
int _swiread _PARAMS ((int, char *, int));
|
int _swiread _PARAMS ((int, char *, int));
|
||||||
void initialise_monitor_handles _PARAMS ((void));
|
void initialise_monitor_handles _PARAMS ((void));
|
||||||
|
|
||||||
static int wrap _PARAMS ((int));
|
static int checkerror _PARAMS ((int));
|
||||||
static int error _PARAMS ((int));
|
static int error _PARAMS ((int));
|
||||||
static int get_errno _PARAMS ((void));
|
static int get_errno _PARAMS ((void));
|
||||||
static int remap_handle _PARAMS ((int));
|
|
||||||
static int findslot _PARAMS ((int));
|
/* Struct used to keep track of the file position, just so we
|
||||||
|
can implement fseek(fh,x,SEEK_CUR). */
|
||||||
|
struct fdent
|
||||||
|
{
|
||||||
|
int handle;
|
||||||
|
int pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define MAX_OPEN_FILES 20
|
||||||
|
|
||||||
|
/* User file descriptors (fd) are integer indexes into
|
||||||
|
the openfiles[] array. Error checking is done by using
|
||||||
|
findslot().
|
||||||
|
|
||||||
|
This openfiles array is manipulated directly by only
|
||||||
|
these 5 functions:
|
||||||
|
|
||||||
|
findslot() - Translate entry.
|
||||||
|
newslot() - Find empty entry.
|
||||||
|
initilise_monitor_handles() - Initialize entries.
|
||||||
|
_swiopen() - Initialize entry.
|
||||||
|
_close() - Handle stdout == stderr case.
|
||||||
|
|
||||||
|
Every other function must use findslot(). */
|
||||||
|
|
||||||
|
static struct fdent openfiles [MAX_OPEN_FILES];
|
||||||
|
|
||||||
|
static struct fdent* findslot _PARAMS ((int));
|
||||||
|
static int newslot _PARAMS ((void));
|
||||||
|
|
||||||
/* Register name faking - works in collusion with the linker. */
|
/* Register name faking - works in collusion with the linker. */
|
||||||
register char * stack_ptr asm ("sp");
|
register char * stack_ptr asm ("sp");
|
||||||
|
@ -62,49 +91,44 @@ extern void _EXFUN(__sinit,(struct _reent *));
|
||||||
} \
|
} \
|
||||||
while (0)
|
while (0)
|
||||||
|
|
||||||
/* Adjust our internal handles to stay away from std* handles. */
|
|
||||||
#define FILE_HANDLE_OFFSET (0x20)
|
|
||||||
|
|
||||||
static int monitor_stdin;
|
static int monitor_stdin;
|
||||||
static int monitor_stdout;
|
static int monitor_stdout;
|
||||||
static int monitor_stderr;
|
static int monitor_stderr;
|
||||||
|
|
||||||
/* Struct used to keep track of the file position, just so we
|
/* Return a pointer to the structure associated with
|
||||||
can implement fseek(fh,x,SEEK_CUR). */
|
the user file descriptor fd. */
|
||||||
typedef struct
|
static struct fdent*
|
||||||
{
|
findslot (int fd)
|
||||||
int handle;
|
|
||||||
int pos;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Function to convert std(in|out|err) handles to internal versions. */
|
|
||||||
static int
|
|
||||||
remap_handle (int fh)
|
|
||||||
{
|
{
|
||||||
CHECK_INIT(_REENT);
|
CHECK_INIT(_REENT);
|
||||||
|
|
||||||
if (fh == STDIN_FILENO)
|
/* User file descriptor is out of range. */
|
||||||
return monitor_stdin;
|
if ((unsigned int)fd >= MAX_OPEN_FILES)
|
||||||
if (fh == STDOUT_FILENO)
|
return NULL;
|
||||||
return monitor_stdout;
|
|
||||||
if (fh == STDERR_FILENO)
|
|
||||||
return monitor_stderr;
|
|
||||||
|
|
||||||
return fh - FILE_HANDLE_OFFSET;
|
/* User file descriptor is open? */
|
||||||
|
if (openfiles[fd].handle == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* Valid. */
|
||||||
|
return &openfiles[fd];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return the next lowest numbered free file
|
||||||
|
structure, or -1 if we can't find one. */
|
||||||
|
static int
|
||||||
|
newslot (void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_OPEN_FILES; i++)
|
||||||
|
if (openfiles[i].handle == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (i == MAX_OPEN_FILES)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -165,6 +189,10 @@ initialise_monitor_handles (void)
|
||||||
monitor_stderr = fh;
|
monitor_stderr = fh;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* If we failed to open stderr, redirect to stdout. */
|
||||||
|
if (monitor_stderr == -1)
|
||||||
|
monitor_stderr = monitor_stdout;
|
||||||
|
|
||||||
for (i = 0; i < MAX_OPEN_FILES; i ++)
|
for (i = 0; i < MAX_OPEN_FILES; i ++)
|
||||||
openfiles[i].handle = -1;
|
openfiles[i].handle = -1;
|
||||||
|
|
||||||
|
@ -188,6 +216,7 @@ get_errno (void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set errno and return result. */
|
||||||
static int
|
static int
|
||||||
error (int result)
|
error (int result)
|
||||||
{
|
{
|
||||||
|
@ -195,21 +224,24 @@ error (int result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check the return and set errno appropriately. */
|
||||||
static int
|
static int
|
||||||
wrap (int result)
|
checkerror (int result)
|
||||||
{
|
{
|
||||||
if (result == -1)
|
if (result == -1)
|
||||||
return error (-1);
|
return error (-1);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns # chars not! written. */
|
/* fh, is a valid internal file handle.
|
||||||
|
ptr, is a null terminated string.
|
||||||
|
len, is the length in bytes to read.
|
||||||
|
Returns the number of bytes *not* written. */
|
||||||
int
|
int
|
||||||
_swiread (int file,
|
_swiread (int fh,
|
||||||
char * ptr,
|
char * ptr,
|
||||||
int len)
|
int len)
|
||||||
{
|
{
|
||||||
int fh = remap_handle (file);
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
int block[3];
|
int block[3];
|
||||||
|
|
||||||
|
@ -217,104 +249,152 @@ _swiread (int file,
|
||||||
block[1] = (int) ptr;
|
block[1] = (int) ptr;
|
||||||
block[2] = len;
|
block[2] = len;
|
||||||
|
|
||||||
return do_AngelSWI (AngelSWI_Reason_Read, block);
|
return checkerror (do_AngelSWI (AngelSWI_Reason_Read, block));
|
||||||
#else
|
#else
|
||||||
asm ("mov r0, %1; mov r1, %2;mov r2, %3; swi %a0"
|
register r0 asm("r0");
|
||||||
: /* No outputs */
|
register r1 asm("r1");
|
||||||
: "i"(SWI_Read), "r"(fh), "r"(ptr), "r"(len)
|
register r2 asm("r2");
|
||||||
: "r0","r1","r2");
|
r0 = fh;
|
||||||
|
r1 = (int)ptr;
|
||||||
|
r2 = len;
|
||||||
|
asm ("swi %a4"
|
||||||
|
: "=r" (r0)
|
||||||
|
: "0"(r0), "r"(r1), "r"(r2), "i"(SWI_Read));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fd, is a valid user file handle.
|
||||||
|
Translates the return of _swiread into
|
||||||
|
bytes read. */
|
||||||
int
|
int
|
||||||
_read (int file,
|
_read (int fd,
|
||||||
char * ptr,
|
char * ptr,
|
||||||
int len)
|
int len)
|
||||||
{
|
{
|
||||||
int slot = findslot (remap_handle (file));
|
int res;
|
||||||
int x = _swiread (file, ptr, len);
|
struct fdent *pfd;
|
||||||
|
|
||||||
if (x < 0)
|
pfd = findslot (fd);
|
||||||
return error (-1);
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (slot != MAX_OPEN_FILES)
|
res = _swiread (pfd->handle, ptr, len);
|
||||||
openfiles [slot].pos += len - x;
|
|
||||||
|
|
||||||
/* x == len is not an error, at least if we want feof() to work. */
|
if (res == -1)
|
||||||
return len - x;
|
return res;
|
||||||
|
|
||||||
|
pfd->pos += len - res;
|
||||||
|
|
||||||
|
/* res == len is not an error,
|
||||||
|
at least if we want feof() to work. */
|
||||||
|
return len - res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fd, is a user file descriptor. */
|
||||||
int
|
int
|
||||||
_swilseek (int file,
|
_swilseek (int fd,
|
||||||
int ptr,
|
int ptr,
|
||||||
int dir)
|
int dir)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
int fh = remap_handle (file);
|
struct fdent *pfd;
|
||||||
int slot = findslot (fh);
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
|
||||||
int block[2];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
/* Valid file descriptor? */
|
||||||
|
pfd = findslot (fd);
|
||||||
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Valid whence? */
|
||||||
|
if ((dir != SEEK_CUR)
|
||||||
|
&& (dir != SEEK_SET)
|
||||||
|
&& (dir != SEEK_END))
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Convert SEEK_CUR to SEEK_SET */
|
||||||
if (dir == SEEK_CUR)
|
if (dir == SEEK_CUR)
|
||||||
{
|
{
|
||||||
if (slot == MAX_OPEN_FILES)
|
ptr = pfd->pos + ptr;
|
||||||
return -1;
|
/* The resulting file offset would be negative. */
|
||||||
ptr = openfiles[slot].pos + ptr;
|
if (ptr < 0)
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
if ((pfd->pos > 0) && (ptr > 0))
|
||||||
|
errno = EOVERFLOW;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
dir = SEEK_SET;
|
dir = SEEK_SET;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
|
int block[2];
|
||||||
if (dir == SEEK_END)
|
if (dir == SEEK_END)
|
||||||
{
|
{
|
||||||
block[0] = fh;
|
block[0] = pfd->handle;
|
||||||
ptr += do_AngelSWI (AngelSWI_Reason_FLen, block);
|
res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, block));
|
||||||
|
if (res == -1)
|
||||||
|
return -1;
|
||||||
|
ptr += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This code only does absolute seeks. */
|
/* This code only does absolute seeks. */
|
||||||
block[0] = remap_handle (file);
|
block[0] = pfd->handle;
|
||||||
block[1] = ptr;
|
block[1] = ptr;
|
||||||
res = do_AngelSWI (AngelSWI_Reason_Seek, block);
|
res = checkerror (do_AngelSWI (AngelSWI_Reason_Seek, block));
|
||||||
#else
|
#else
|
||||||
if (dir == SEEK_END)
|
if (dir == SEEK_END)
|
||||||
{
|
{
|
||||||
asm ("mov r0, %2; swi %a1; mov %0, r0"
|
asm ("mov r0, %2; swi %a1; mov %0, r0"
|
||||||
: "=r" (res)
|
: "=r" (res)
|
||||||
: "i" (SWI_Flen), "r" (fh)
|
: "i" (SWI_Flen), "r" (pfd->handle)
|
||||||
: "r0");
|
: "r0");
|
||||||
|
checkerror (res);
|
||||||
|
if (res == -1)
|
||||||
|
return -1;
|
||||||
ptr += res;
|
ptr += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This code only does absolute seeks. */
|
/* This code only does absolute seeks. */
|
||||||
asm ("mov r0, %2; mov r1, %3; swi %a1; mov %0, r0"
|
asm ("mov r0, %2; mov r1, %3; swi %a1; mov %0, r0"
|
||||||
: "=r" (res)
|
: "=r" (res)
|
||||||
: "i" (SWI_Seek), "r" (fh), "r" (ptr)
|
: "i" (SWI_Seek), "r" (pfd->handle), "r" (ptr)
|
||||||
: "r0", "r1");
|
: "r0", "r1");
|
||||||
|
checkerror (res);
|
||||||
#endif
|
#endif
|
||||||
|
/* At this point ptr is the current file position. */
|
||||||
if (slot != MAX_OPEN_FILES && res == 0)
|
if (res >= 0)
|
||||||
openfiles[slot].pos = ptr;
|
{
|
||||||
|
pfd->pos = ptr;
|
||||||
/* This is expected to return the position in the file. */
|
return ptr;
|
||||||
return res == 0 ? ptr : -1;
|
}
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
_lseek (int fd,
|
||||||
_lseek (int file,
|
|
||||||
int ptr,
|
int ptr,
|
||||||
int dir)
|
int dir)
|
||||||
{
|
{
|
||||||
return wrap (_swilseek (file, ptr, dir));
|
return _swilseek (fd, ptr, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns #chars not! written. */
|
/* fh, is a valid internal file handle.
|
||||||
|
Returns the number of bytes *not* written. */
|
||||||
int
|
int
|
||||||
_swiwrite (
|
_swiwrite (
|
||||||
int file,
|
int fh,
|
||||||
char * ptr,
|
char * ptr,
|
||||||
int len)
|
int len)
|
||||||
{
|
{
|
||||||
int fh = remap_handle (file);
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
int block[3];
|
int block[3];
|
||||||
|
|
||||||
|
@ -322,64 +402,103 @@ _swiwrite (
|
||||||
block[1] = (int) ptr;
|
block[1] = (int) ptr;
|
||||||
block[2] = len;
|
block[2] = len;
|
||||||
|
|
||||||
return do_AngelSWI (AngelSWI_Reason_Write, block);
|
return checkerror (do_AngelSWI (AngelSWI_Reason_Write, block));
|
||||||
#else
|
#else
|
||||||
asm ("mov r0, %1; mov r1, %2;mov r2, %3; swi %a0"
|
register r0 asm("r0");
|
||||||
: /* No outputs */
|
register r1 asm("r1");
|
||||||
: "i"(SWI_Write), "r"(fh), "r"(ptr), "r"(len)
|
register r2 asm("r2");
|
||||||
: "r0","r1","r2");
|
r0 = fh;
|
||||||
|
r1 = (int)ptr;
|
||||||
|
r2 = len;
|
||||||
|
asm ("swi %a4"
|
||||||
|
: "=r" (r0)
|
||||||
|
: "0"(r0), "r"(r1), "r"(r2), "i"(SWI_Write));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fd, is a user file descriptor. */
|
||||||
int
|
int
|
||||||
_write (int file,
|
_write (int fd,
|
||||||
char * ptr,
|
char * ptr,
|
||||||
int len)
|
int len)
|
||||||
{
|
{
|
||||||
int slot = findslot (remap_handle (file));
|
int res;
|
||||||
int x = _swiwrite (file, ptr,len);
|
struct fdent *pfd;
|
||||||
|
|
||||||
if (x == -1 || x == len)
|
pfd = findslot (fd);
|
||||||
return error (-1);
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = _swiwrite (pfd->handle, ptr,len);
|
||||||
|
|
||||||
|
/* Clearly an error. */
|
||||||
|
if (res < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
pfd->pos += len - res;
|
||||||
|
|
||||||
|
/* We wrote 0 bytes?
|
||||||
|
Retrieve errno just in case. */
|
||||||
|
if ((len - res) == 0)
|
||||||
|
return error (0);
|
||||||
|
|
||||||
if (slot != MAX_OPEN_FILES)
|
return (len - res);
|
||||||
openfiles[slot].pos += len - x;
|
|
||||||
|
|
||||||
return len - x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
_swiopen (const char * path,
|
_swiopen (const char * path, int flags)
|
||||||
int flags)
|
|
||||||
{
|
{
|
||||||
int aflags = 0, fh;
|
int aflags = 0, fh;
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
int block[3];
|
int block[3];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int i = findslot (-1);
|
int fd = newslot ();
|
||||||
|
|
||||||
if (i == MAX_OPEN_FILES)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
/* The flags are Unix-style, so we need to convert them. */
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
errno = EMFILE;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* It is an error to open a file that already exists. */
|
||||||
|
if ((flags & O_CREAT)
|
||||||
|
&& (flags & O_EXCL))
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
int res;
|
||||||
|
res = _stat (path, &st);
|
||||||
|
if (res != -1)
|
||||||
|
{
|
||||||
|
errno = EEXIST;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The flags are Unix-style, so we need to convert them. */
|
||||||
#ifdef O_BINARY
|
#ifdef O_BINARY
|
||||||
if (flags & O_BINARY)
|
if (flags & O_BINARY)
|
||||||
aflags |= 1;
|
aflags |= 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* In O_RDONLY we expect aflags == 0. */
|
||||||
|
|
||||||
if (flags & O_RDWR)
|
if (flags & O_RDWR)
|
||||||
aflags |= 2;
|
aflags |= 2;
|
||||||
|
|
||||||
if (flags & O_CREAT)
|
if ((flags & O_CREAT)
|
||||||
aflags |= 4;
|
|| (flags & O_TRUNC)
|
||||||
|
|| (flags & O_WRONLY))
|
||||||
if (flags & O_TRUNC)
|
|
||||||
aflags |= 4;
|
aflags |= 4;
|
||||||
|
|
||||||
if (flags & O_APPEND)
|
if (flags & O_APPEND)
|
||||||
{
|
{
|
||||||
aflags &= ~4; /* Can't ask for w AND a; means just 'a'. */
|
/* Can't ask for w AND a; means just 'a'. */
|
||||||
|
aflags &= ~4;
|
||||||
aflags |= 8;
|
aflags |= 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -397,58 +516,83 @@ _swiopen (const char * path,
|
||||||
: "r0","r1");
|
: "r0","r1");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Return a user file descriptor or an error. */
|
||||||
if (fh >= 0)
|
if (fh >= 0)
|
||||||
{
|
{
|
||||||
openfiles[i].handle = fh;
|
openfiles[fd].handle = fh;
|
||||||
openfiles[i].pos = 0;
|
openfiles[fd].pos = 0;
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
return fh >= 0 ? fh + FILE_HANDLE_OFFSET : error (fh);
|
return error (fh);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
_open (const char * path,
|
_open (const char * path, int flags, ...)
|
||||||
int flags,
|
|
||||||
...)
|
|
||||||
{
|
{
|
||||||
return wrap (_swiopen (path, flags));
|
return _swiopen (path, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fh, is a valid internal file handle. */
|
||||||
int
|
int
|
||||||
_swiclose (int file)
|
_swiclose (int fh)
|
||||||
{
|
{
|
||||||
int myhan = remap_handle (file);
|
|
||||||
int slot = findslot (myhan);
|
|
||||||
|
|
||||||
if (slot != MAX_OPEN_FILES)
|
|
||||||
openfiles[slot].handle = -1;
|
|
||||||
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
return do_AngelSWI (AngelSWI_Reason_Close, & myhan);
|
return checkerror (do_AngelSWI (AngelSWI_Reason_Close, &fh));
|
||||||
#else
|
#else
|
||||||
asm ("mov r0, %1; swi %a0" :: "i" (SWI_Close),"r"(myhan):"r0");
|
register r0 asm("r0");
|
||||||
|
r0 = fh;
|
||||||
|
asm ("swi %a2"
|
||||||
|
: "=r"(r0)
|
||||||
|
: "0"(r0), "i" (SWI_Close));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* fd, is a user file descriptor. */
|
||||||
int
|
int
|
||||||
_close (int file)
|
_close (int fd)
|
||||||
{
|
{
|
||||||
return wrap (_swiclose (file));
|
int res;
|
||||||
|
struct fdent *pfd;
|
||||||
|
|
||||||
|
pfd = findslot (fd);
|
||||||
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Handle stderr == stdout. */
|
||||||
|
if ((fd == 1 || fd == 2)
|
||||||
|
&& (openfiles[1].handle == openfiles[2].handle))
|
||||||
|
{
|
||||||
|
pfd->handle = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempt to close the handle. */
|
||||||
|
res = _swiclose (pfd->handle);
|
||||||
|
|
||||||
|
/* Reclaim handle? */
|
||||||
|
if (res == 0)
|
||||||
|
pfd->handle = -1;
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int __attribute__((weak))
|
int __attribute__((weak))
|
||||||
_getpid (int n)
|
_getpid (int n __attribute__ ((unused)))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
n = n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
caddr_t
|
caddr_t
|
||||||
_sbrk (int incr)
|
_sbrk (int incr)
|
||||||
{
|
{
|
||||||
extern char end asm ("end"); /* Defined by the linker. */
|
extern char end asm ("end"); /* Defined by the linker. */
|
||||||
static char * heap_end;
|
static char * heap_end;
|
||||||
char * prev_heap_end;
|
char * prev_heap_end;
|
||||||
|
|
||||||
if (heap_end == NULL)
|
if (heap_end == NULL)
|
||||||
heap_end = & end;
|
heap_end = & end;
|
||||||
|
@ -476,31 +620,60 @@ _sbrk (int incr)
|
||||||
return (caddr_t) prev_heap_end;
|
return (caddr_t) prev_heap_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
_swistat (int fd, struct stat * st)
|
||||||
|
{
|
||||||
|
struct fdent *pfd;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
pfd = findslot (fd);
|
||||||
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Always assume a character device,
|
||||||
|
with 1024 byte blocks. */
|
||||||
|
st->st_mode |= S_IFCHR;
|
||||||
|
st->st_blksize = 1024;
|
||||||
|
#ifdef ARM_RDI_MONITOR
|
||||||
|
res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
|
||||||
|
#else
|
||||||
|
asm ("mov r0, %2; swi %a1; mov %0, r0"
|
||||||
|
: "=r" (res)
|
||||||
|
: "i" (SWI_Flen), "r" (pfd->handle)
|
||||||
|
: "r0");
|
||||||
|
checkerror (res);
|
||||||
|
#endif
|
||||||
|
if (res == -1)
|
||||||
|
return -1;
|
||||||
|
/* Return the file size. */
|
||||||
|
st->st_size = res;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int __attribute__((weak))
|
int __attribute__((weak))
|
||||||
_fstat (int file, struct stat * st)
|
_fstat (int fd, struct stat * st)
|
||||||
{
|
{
|
||||||
memset (st, 0, sizeof (* st));
|
memset (st, 0, sizeof (* st));
|
||||||
st->st_mode = S_IFCHR;
|
return _swistat (fd, st);
|
||||||
st->st_blksize = 1024;
|
|
||||||
return 0;
|
|
||||||
file = file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int __attribute__((weak))
|
int __attribute__((weak))
|
||||||
_stat (const char *fname, struct stat *st)
|
_stat (const char *fname, struct stat *st)
|
||||||
{
|
{
|
||||||
int file;
|
int fd, res;
|
||||||
|
|
||||||
/* 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));
|
memset (st, 0, sizeof (* st));
|
||||||
st->st_mode = S_IFREG | S_IREAD;
|
/* The best we can do is try to open the file readonly.
|
||||||
st->st_blksize = 1024;
|
If it exists, then we can guess a few things about it. */
|
||||||
_swiclose (file); /* Not interested in the error. */
|
if ((fd = _open (fname, O_RDONLY)) == -1)
|
||||||
return 0;
|
return -1;
|
||||||
|
st->st_mode |= S_IFREG | S_IREAD;
|
||||||
|
res = _swistat (fd, st);
|
||||||
|
/* Not interested in the error. */
|
||||||
|
_close (fd);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int __attribute__((weak))
|
int __attribute__((weak))
|
||||||
|
@ -513,15 +686,23 @@ _link (void)
|
||||||
int
|
int
|
||||||
_unlink (const char *path)
|
_unlink (const char *path)
|
||||||
{
|
{
|
||||||
|
int res;
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
int block[2];
|
int block[2];
|
||||||
block[0] = (int)path;
|
block[0] = (int)path;
|
||||||
block[1] = strlen(path);
|
block[1] = strlen(path);
|
||||||
return wrap (do_AngelSWI (AngelSWI_Reason_Remove, block)) ? -1 : 0;
|
res = do_AngelSWI (AngelSWI_Reason_Remove, block);
|
||||||
#else
|
#else
|
||||||
(void)path;
|
register r0 asm("r0");
|
||||||
asm ("swi %a0" :: "i" (SWI_Remove));
|
r0 = (int)path;
|
||||||
|
asm ("swi %a2"
|
||||||
|
: "=r"(r0)
|
||||||
|
: "0"(r0), "i" (SWI_Remove));
|
||||||
|
res = r0;
|
||||||
#endif
|
#endif
|
||||||
|
if (res == -1)
|
||||||
|
return error (res);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -588,14 +769,24 @@ _times (struct tms * tp)
|
||||||
int
|
int
|
||||||
_isatty (int fd)
|
_isatty (int fd)
|
||||||
{
|
{
|
||||||
int fh = remap_handle (fd);
|
struct fdent *pfd;
|
||||||
|
|
||||||
|
pfd = findslot (fd);
|
||||||
|
if (pfd == NULL)
|
||||||
|
{
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef ARM_RDI_MONITOR
|
#ifdef ARM_RDI_MONITOR
|
||||||
return wrap (do_AngelSWI (AngelSWI_Reason_IsTTY, &fh));
|
return checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
|
||||||
#else
|
#else
|
||||||
asm ("mov r0, %1; swi %a0"
|
register r0 asm("r0");
|
||||||
: /* No outputs */
|
r0 = pfd->handle;
|
||||||
: "i" (SWI_IsTTY), "r"(fh)
|
asm ("swi %a2"
|
||||||
: "r0");
|
: "=r" (r0)
|
||||||
|
: "0"(r0), "i" (SWI_IsTTY));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -613,7 +804,7 @@ _system (const char *s)
|
||||||
return 1; /* maybe there is a shell available? we can hope. :-P */
|
return 1; /* maybe there is a shell available? we can hope. :-P */
|
||||||
block[0] = (int)s;
|
block[0] = (int)s;
|
||||||
block[1] = strlen (s);
|
block[1] = strlen (s);
|
||||||
e = wrap (do_AngelSWI (AngelSWI_Reason_System, block));
|
e = checkerror (do_AngelSWI (AngelSWI_Reason_System, block));
|
||||||
if ((e >= 0) && (e < 256))
|
if ((e >= 0) && (e < 256))
|
||||||
{
|
{
|
||||||
/* We have to convert e, an exit status to the encoded status of
|
/* We have to convert e, an exit status to the encoded status of
|
||||||
|
@ -626,8 +817,12 @@ _system (const char *s)
|
||||||
}
|
}
|
||||||
return e;
|
return e;
|
||||||
#else
|
#else
|
||||||
(void)s;
|
register r0 asm("r0");
|
||||||
asm ("swi %a0" :: "i" (SWI_CLI));
|
r0 = (int)s;
|
||||||
|
asm ("swi %a2"
|
||||||
|
: "=r" (r0)
|
||||||
|
: "0"(r0), "i" (SWI_CLI));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -640,9 +835,15 @@ _rename (const char * oldpath, const char * newpath)
|
||||||
block[1] = strlen(oldpath);
|
block[1] = strlen(oldpath);
|
||||||
block[2] = (int)newpath;
|
block[2] = (int)newpath;
|
||||||
block[3] = strlen(newpath);
|
block[3] = strlen(newpath);
|
||||||
return wrap (do_AngelSWI (AngelSWI_Reason_Rename, block)) ? -1 : 0;
|
return checkerror (do_AngelSWI (AngelSWI_Reason_Rename, block)) ? -1 : 0;
|
||||||
#else
|
#else
|
||||||
(void)oldpath; (void)newpath;
|
register r0 asm("r0");
|
||||||
asm ("swi %a0" :: "i" (SWI_Rename));
|
register r1 asm("r1");
|
||||||
|
r0 = (int)oldpath;
|
||||||
|
r1 = (int)newpath;
|
||||||
|
asm ("swi %a3"
|
||||||
|
: "=r" (r0)
|
||||||
|
: "0" (r0), "r" (r1), "i" (SWI_Rename));
|
||||||
|
return checkerror (r0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue