From 0797e54f3f5b58e54012570d9a6ff9443d7610d8 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Thu, 30 Oct 2014 03:53:22 +0000 Subject: [PATCH] [LIBC] fix the fopen/open issue in armlibc --- components/libc/armlibc/stubs.c | 34 ++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/components/libc/armlibc/stubs.c b/components/libc/armlibc/stubs.c index 1f9e01ba3c..ea42777a08 100644 --- a/components/libc/armlibc/stubs.c +++ b/components/libc/armlibc/stubs.c @@ -50,6 +50,7 @@ FILEHANDLE _sys_open(const char *name, int openmode) { #ifdef RT_USING_DFS int fd; + int mode = O_RDONLY; #endif /* Register standard Input Output devices. */ @@ -63,8 +64,33 @@ FILEHANDLE _sys_open(const char *name, int openmode) #ifndef RT_USING_DFS return -1; #else - /* TODO: adjust open file mode */ - fd = open(name, openmode, 0); + /* Correct openmode from fopen to open */ + if (openmode & OPEN_PLUS) + { + if (openmode & OPEN_W) + { + mode |= (O_RDWR | O_TRUNC | O_CREAT); + } + else if (openmode & OPEN_A) + { + mode |= (O_RDWR | O_APPEND | O_CREAT); + } + else + mode |= O_RDWR; + } + else + { + if (openmode & OPEN_W) + { + mode |= (O_WRONLY | O_TRUNC | O_CREAT); + } + else if (openmode & OPEN_A) + { + mode |= (O_WRONLY | O_APPEND | O_CREAT); + } + } + + fd = open(name, mode, 0); if(fd < 0) return -1; else @@ -140,7 +166,6 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) return 0; #else rt_device_t console_device; - extern rt_device_t rt_console_get_device(void); console_device = rt_console_get_device(); if (console_device != 0) rt_device_write(console_device, 0, buf, len); @@ -227,7 +252,6 @@ int _sys_istty(FILEHANDLE fh) return 0; } - int remove(const char *filename) { #ifndef RT_USING_DFS @@ -238,7 +262,7 @@ int remove(const char *filename) } #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS) -/* use system implementation in the msh */ +/* use system(const char *string) implementation in the msh */ #else int system(const char *string) {