Merge pull request #204 from aozima/pulls

fixed _sys_read()/_sys_write() issues.
This commit is contained in:
Bernard Xiong 2013-11-24 07:22:41 -08:00
commit 35d83be62f
1 changed files with 44 additions and 15 deletions

View File

@ -12,6 +12,7 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2012-11-23 Yihui The first version * 2012-11-23 Yihui The first version
* 2013-11-24 aozima fixed _sys_read()/_sys_write() issues.
*/ */
#include <string.h> #include <string.h>
@ -45,6 +46,8 @@ const char __stderr_name[] = "STDERR";
*/ */
FILEHANDLE _sys_open(const char *name, int openmode) FILEHANDLE _sys_open(const char *name, int openmode)
{ {
int fd;
/* Register standard Input Output devices. */ /* Register standard Input Output devices. */
if (strcmp(name, __stdin_name) == 0) if (strcmp(name, __stdin_name) == 0)
return (STDIN); return (STDIN);
@ -57,7 +60,11 @@ FILEHANDLE _sys_open(const char *name, int openmode)
return -1; return -1;
#else #else
/* TODO: adjust open file mode */ /* TODO: adjust open file mode */
return open(name, openmode, 0); fd = open(name, openmode, 0);
if(fd < 0)
return -1;
else
return fd + STDERR + 1;
#endif #endif
} }
@ -66,10 +73,10 @@ int _sys_close(FILEHANDLE fh)
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
if (fh < 3) if (fh < STDERR)
return 0; return 0;
return close(fh); return close(fh - STDERR - 1);
#endif #endif
} }
@ -80,10 +87,12 @@ int _sys_close(FILEHANDLE fh)
* @param buf - buffer to save read data * @param buf - buffer to save read data
* @param len - max length of data buffer * @param len - max length of data buffer
* @param mode - useless, for historical reasons * @param mode - useless, for historical reasons
* @return actual read data length * @return The number of bytes not read.
*/ */
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{ {
int size;
if (fh == STDIN) if (fh == STDIN)
{ {
/* TODO */ /* TODO */
@ -91,10 +100,17 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
return 0; return 0;
} }
if ((fh == STDOUT) || (fh == STDERR))
return -1;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
return read(fh, buf, len); size = read(fh - STDERR - 1, buf, len);
if(size >= 0)
return len - size;
else
return -1;
#endif #endif
} }
@ -105,10 +121,12 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
* @param buf - data buffer * @param buf - data buffer
* @param len - buffer length * @param len - buffer length
* @param mode - useless, for historical reasons * @param mode - useless, for historical reasons
* @return actual written data length * @return a positive number representing the number of characters not written.
*/ */
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{ {
int size;
if ((fh == STDOUT) || (fh == STDERR)) if ((fh == STDOUT) || (fh == STDERR))
{ {
#ifndef RT_USING_CONSOLE #ifndef RT_USING_CONSOLE
@ -123,10 +141,17 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
#endif #endif
} }
if(fh == STDIN)
return -1;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return 0; return 0;
#else #else
return write(fh, buf, len); size = write(fh - STDERR - 1, buf, len);
if(size >= 0)
return len - size;
else
return -1;
#endif #endif
} }
@ -138,11 +163,15 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
*/ */
int _sys_seek(FILEHANDLE fh, long pos) int _sys_seek(FILEHANDLE fh, long pos)
{ {
if (fh < STDERR)
return -1;
#ifndef RT_USING_DFS #ifndef RT_USING_DFS
return -1; return -1;
#else #else
/* position is relative to the start of file fh */ /* position is relative to the start of file fh */
return lseek(fh, pos, 0); return lseek(fh - STDERR - 1, pos, 0);
#endif #endif
} }