提交 e037d2e5 编写于 作者: wuyangyong's avatar wuyangyong

fixed _sys_read()/_sys_write() issues.

上级 6c58f80a
...@@ -12,6 +12,7 @@ ...@@ -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"; ...@@ -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) ...@@ -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) ...@@ -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,21 +87,30 @@ int _sys_close(FILEHANDLE fh) ...@@ -80,21 +87,30 @@ 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 */
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) ...@@ -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
...@@ -122,11 +140,18 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) ...@@ -122,11 +140,18 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
return len; return len;
#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) ...@@ -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
} }
...@@ -162,7 +191,7 @@ char *_sys_command_string(char *cmd, int len) ...@@ -162,7 +191,7 @@ char *_sys_command_string(char *cmd, int len)
void _ttywrch(int ch) void _ttywrch(int ch)
{ {
/* TODO */ /* TODO */
} }
void _sys_exit(int return_code) void _sys_exit(int return_code)
...@@ -172,7 +201,7 @@ void _sys_exit(int return_code) ...@@ -172,7 +201,7 @@ void _sys_exit(int return_code)
} }
/** /**
* return current length of file. * return current length of file.
* *
* @param fh - file handle * @param fh - file handle
* @return file length, or -1 on failed * @return file length, or -1 on failed
...@@ -208,7 +237,7 @@ int rename(const char *old, const char *new) ...@@ -208,7 +237,7 @@ int rename(const char *old, const char *new)
int system(const char *string) int system(const char *string)
{ {
RT_ASSERT(0); RT_ASSERT(0);
for(;;); for(;;);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册