diff options
Diffstat (limited to 'components/libc')
69 files changed, 1889 insertions, 805 deletions
diff --git a/components/libc/Kconfig b/components/libc/Kconfig index b9600ef389..b2af15e841 100644 --- a/components/libc/Kconfig +++ b/components/libc/Kconfig @@ -4,19 +4,52 @@ config RT_USING_LIBC bool "Enable libc APIs from toolchain" default y +if RT_USING_LIBC + config RT_LIBC_USING_TIME + default y + + config RT_LIBC_USING_FILEIO + bool "Enable libc with file operation, eg.fopen/fwrite/fread/getchar" + select RT_USING_DFS + select RT_USING_POSIX + default n + + config RT_USING_MODULE + bool "Enable dynamic module with dlopen/dlsym/dlclose feature" + default n + + if RT_USING_MODULE + config RT_USING_CUSTOM_DLMODULE + bool "Enable load dynamic module by custom" + default n + endif +endif + +if RT_USING_LIBC != y + config RT_LIBC_USING_TIME + bool "Enable time functions without compiler's libc" + default y +endif + +config RT_LIBC_DEFAULT_TIMEZONE + depends on (RT_LIBC_USING_TIME || RT_USING_LIBC) + int "Set the default time zone (UTC+)" + range -12 12 + default 8 + config RT_USING_PTHREADS bool "Enable pthreads APIs" default n if RT_USING_PTHREADS config PTHREAD_NUM_MAX - int "Maximum number of pthreads" - default 8 + int "Maximum number of pthreads" + default 8 endif -if RT_USING_LIBC && RT_USING_DFS +if RT_USING_DFS config RT_USING_POSIX - bool "Enable POSIX layer for compatibility with UNIX APIs, poll/select etc" + bool "Enable POSIX layer, open/read/write/select etc" select RT_USING_DFS_DEVFS default y @@ -40,32 +73,4 @@ if RT_USING_LIBC && RT_USING_DFS endif -if RT_USING_LIBC - config RT_LIBC_USING_TIME - default y - - config RT_USING_MODULE - bool "Enable dynamic module with dlopen/dlsym/dlclose feature" - default n - - if RT_USING_MODULE - config RT_USING_CUSTOM_DLMODULE - bool "Enable load dynamic module by custom" - default n - endif - -endif - -if RT_USING_LIBC != y - config RT_LIBC_USING_TIME - bool "Enable time functions without compiler's libc" - default y -endif - -config RT_LIBC_DEFAULT_TIMEZONE - depends on (RT_LIBC_USING_TIME || RT_USING_LIBC) - int "Set the default time zone (UTC+)" - range -12 12 - default 8 - endmenu diff --git a/components/libc/aio/SConscript b/components/libc/aio/SConscript deleted file mode 100644 index f10462e9c4..0000000000 --- a/components/libc/aio/SConscript +++ /dev/null @@ -1,11 +0,0 @@ -# RT-Thread building script for component - -from building import * - -cwd = GetCurrentDir() -src = Glob('*.c') + Glob('*.cpp') -CPPPATH = [cwd] - -group = DefineGroup('libc', src, depend = ['RT_USING_POSIX', 'RT_USING_POSIX_AIO'], CPPPATH = CPPPATH) - -Return('group') diff --git a/components/libc/compilers/armlibc/SConscript b/components/libc/compilers/armlibc/SConscript index 52f7a6d93a..bf1a299304 100644 --- a/components/libc/compilers/armlibc/SConscript +++ b/components/libc/compilers/armlibc/SConscript @@ -1,21 +1,12 @@ from building import * Import('rtconfig') -src = Glob('*.c') + Glob('*.cpp') -cwd = GetCurrentDir() +src = Glob('*.c') group = [] -CPPPATH = [cwd] CPPDEFINES = ['RT_USING_ARM_LIBC'] -if GetDepend('RT_USING_DFS') == False: - SrcRemove(src, ['stdio.c']) - -if GetDepend('RT_USING_MODULE') == False: - SrcRemove(src, ['libc_syms.c']) - if rtconfig.PLATFORM == 'armcc' or rtconfig.PLATFORM == 'armclang': - group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], - CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], CPPDEFINES = CPPDEFINES) Return('group') diff --git a/components/libc/compilers/armlibc/libc.c b/components/libc/compilers/armlibc/libc.c deleted file mode 100644 index 889c687309..0000000000 --- a/components/libc/compilers/armlibc/libc.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } -#endif - -#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); diff --git a/components/libc/compilers/armlibc/libc.h b/components/libc/compilers/armlibc/libc.h deleted file mode 100644 index 5c1613728e..0000000000 --- a/components/libc/compilers/armlibc/libc.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#ifndef __RTT_LIBC_H__ -#define __RTT_LIBC_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -int libc_system_init(void); -int libc_stdio_get_console(void); -int libc_stdio_set_console(const char* device_name, int mode); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/libc/compilers/armlibc/stdio.c b/components/libc/compilers/armlibc/stdio.c deleted file mode 100644 index 7dd9dbfe3f..0000000000 --- a/components/libc/compilers/armlibc/stdio.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard implement stdio for armcc. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <rtthread.h> -#include "libc.h" - -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) -#include <dfs_posix.h> - -#define STDIO_DEVICE_NAME_MAX 32 - -static int std_fd = -1; - -int libc_stdio_set_console(const char* device_name, int mode) -{ - int fd; - char name[STDIO_DEVICE_NAME_MAX]; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - fd = open(name, mode, 0); - if (fd >= 0) - { - if (std_fd >= 0) - { - close(std_fd); - } - std_fd = fd; - } - - return std_fd; -} - -int libc_stdio_get_console(void) -{ - return std_fd; -} - -#endif diff --git a/components/libc/compilers/armlibc/syscalls.c b/components/libc/compilers/armlibc/syscalls.c index ccdcfc86ea..755ffef1a6 100644 --- a/components/libc/compilers/armlibc/syscalls.c +++ b/components/libc/compilers/armlibc/syscalls.c @@ -15,14 +15,14 @@ * 2020-02-14 Meco Man implement _sys_tmpnam() */ -#include <string.h> #include <rt_sys.h> - #include <rtthread.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#ifdef RT_USING_POSIX #include "libc.h" - -#ifdef RT_USING_DFS -#include <dfs_posix.h> #endif #define DBG_TAG "armlibc.syscalls" @@ -55,7 +55,7 @@ const char __stderr_name[] = "STDERR"; */ FILEHANDLE _sys_open(const char *name, int openmode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int fd; int mode = O_RDONLY; #endif @@ -68,8 +68,8 @@ FILEHANDLE _sys_open(const char *name, int openmode) if (strcmp(name, __stderr_name) == 0) return (STDERR); -#ifndef RT_USING_DFS - return -1; +#ifndef RT_USING_POSIX + return 0; /* error */ #else /* Correct openmode from fopen to open */ if (openmode & OPEN_PLUS) @@ -99,21 +99,22 @@ FILEHANDLE _sys_open(const char *name, int openmode) fd = open(name, mode, 0); if (fd < 0) - return -1; + return 0; /* error */ else return fd; -#endif +#endif /* RT_USING_POSIX */ } int _sys_close(FILEHANDLE fh) { -#ifndef RT_USING_DFS - return 0; -#else - if (fh <= STDERR) return 0; +#ifdef RT_USING_POSIX + if (fh <= STDERR) + return 0; /* error */ return close(fh); -#endif +#else + return 0; +#endif /* RT_USING_POSIX */ } /* @@ -143,13 +144,11 @@ int _sys_close(FILEHANDLE fh) */ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif if (fh == STDIN) { -#ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard output before initializing libc"); @@ -157,25 +156,20 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) } size = read(STDIN_FILENO, buf, len); return len - size; -#else - /* no stdin */ - return -1; -#endif } else if ((fh == STDOUT) || (fh == STDERR)) { - return -1; + return 0; /* error */ } -#ifndef RT_USING_DFS - return 0; -#else size = read(fh, buf, len); if (size >= 0) return len - size; else - return -1; -#endif + return 0; /* error */ +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } /* @@ -185,15 +179,12 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) */ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif +#endif /* RT_USING_POSIX */ if ((fh == STDOUT) || (fh == STDERR)) { -#if !defined(RT_USING_CONSOLE) || !defined(RT_USING_DEVICE) - return 0; -#else #ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { @@ -202,31 +193,29 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) } size = write(STDOUT_FILENO, buf, len); return len - size; -#else +#elif defined(RT_USING_CONSOLE) if (rt_console_get_device()) { rt_device_write(rt_console_get_device(), -1, buf, len); - return 0; } - return -1; -#endif -#endif + return 0; /* error */ +#endif /* RT_USING_POSIX */ } else if (fh == STDIN) { - return -1; + return 0; /* error */ } -#ifndef RT_USING_DFS - return 0; -#else +#ifdef RT_USING_POSIX size = write(fh, buf, len); if (size >= 0) return len - size; else - return -1; -#endif + return 0; /* error */ +#else + return 0; +#endif /* RT_USING_POSIX */ } /* @@ -235,16 +224,15 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) */ int _sys_seek(FILEHANDLE fh, long pos) { +#ifdef RT_USING_POSIX if (fh < STDERR) - return -1; - -#ifndef RT_USING_DFS - return -1; -#else + return 0; /* error */ /* position is relative to the start of file fh */ return lseek(fh, pos, 0); -#endif +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } /** @@ -270,7 +258,7 @@ void _ttywrch(int ch) c = (char)ch; rt_kprintf(&c); -#endif +#endif /* RT_USING_CONSOLE */ } /* for exit() and abort() */ @@ -289,17 +277,17 @@ RT_WEAK void _sys_exit(int return_code) */ long _sys_flen(FILEHANDLE fh) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX struct stat stat; if (fh < STDERR) - return -1; + return 0; /* error */ fstat(fh, &stat); return stat.st_size; #else - return -1; -#endif + return 0; +#endif /* RT_USING_POSIX */ } int _sys_istty(FILEHANDLE fh) @@ -312,11 +300,11 @@ int _sys_istty(FILEHANDLE fh) int remove(const char *filename) { -#ifndef RT_USING_DFS - return -1; -#else +#ifdef RT_USING_POSIX return unlink(filename); -#endif +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } #ifdef __MICROLIB @@ -324,11 +312,15 @@ int remove(const char *filename) int fputc(int c, FILE *f) { +#ifdef RT_USING_CONSOLE char ch[2] = {0}; ch[0] = c; rt_kprintf(&ch[0]); return 1; +#else + return 0; /* error */ +#endif /* RT_USING_CONSOLE */ } int fgetc(FILE *f) @@ -339,13 +331,13 @@ int fgetc(FILE *f) if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard output before initializing libc"); - return -1; + return 0; } if(read(STDIN_FILENO, &ch, 1) == 1) return ch; -#endif - - return -1; +#endif /* RT_USING_POSIX */ + return 0; /* error */ } -#endif + +#endif /* __MICROLIB */ diff --git a/components/libc/compilers/common/SConscript b/components/libc/compilers/common/SConscript index b1982aa329..504bf84e56 100644 --- a/components/libc/compilers/common/SConscript +++ b/components/libc/compilers/common/SConscript @@ -1,30 +1,25 @@ from building import * - Import('rtconfig') src = [] cwd = GetCurrentDir() group = [] CPPPATH = [cwd] +CPPDEFINES = [] + +if rtconfig.CROSS_TOOL == 'keil': + CPPDEFINES += ['__CLK_TCK=RT_TICK_PER_SECOND'] if GetDepend('RT_USING_LIBC'): src += Glob('*.c') - if GetDepend('RT_USING_POSIX') == False: - SrcRemove(src, ['unistd.c', 'delay.c']) elif GetDepend('RT_LIBC_USING_TIME'): src += ['time.c'] -if rtconfig.CROSS_TOOL == 'keil': - CPPDEFINES = ['__CLK_TCK=RT_TICK_PER_SECOND'] -else: - CPPDEFINES = [] - group = DefineGroup('libc', src, depend = [], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) list = os.listdir(cwd) -for d in list: - path = os.path.join(cwd, d) - if os.path.isfile(os.path.join(path, 'SConscript')): - group = group + SConscript(os.path.join(d, 'SConscript')) +for item in list: + if os.path.isfile(os.path.join(cwd, item, 'SConscript')): + group = group + SConscript(os.path.join(item, 'SConscript')) Return('group') diff --git a/components/libc/compilers/common/nogcc/sys/select.h b/components/libc/compilers/common/nogcc/sys/select.h index 575ad3a1b5..02fb77dd89 100644 --- a/components/libc/compilers/common/nogcc/sys/select.h +++ b/components/libc/compilers/common/nogcc/sys/select.h @@ -15,6 +15,10 @@ #include <sys/types.h> #include <sys/time.h> +#ifdef _WIN32 +#include <winsock.h> +#endif + #ifndef FD_SETSIZE #define FD_SETSIZE 32 #endif @@ -26,16 +30,17 @@ #define FD_SETSIZE DFS_FD_MAX #endif /* SAL_USING_POSIX */ -#define NBBY 8 /* number of bits in a byte */ - typedef long fd_mask; +#ifndef _WIN32 +#ifndef _SYS_TYPES_FD_SET /* MIPS */ + +#define NBBY 8 /* number of bits in a byte */ #define NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */ #ifndef howmany #define howmany(x,y) (((x)+((y)-1))/(y)) #endif -#ifndef _SYS_TYPES_FD_SET /* MIPS */ typedef struct _types_fd_set { fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)]; } _types_fd_set; @@ -48,5 +53,6 @@ typedef struct _types_fd_set { #endif /* _SYS_TYPES_FD_SET */ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); +#endif /* _WIN32 */ #endif /* __SYS_SELECT_H__ */ diff --git a/components/libc/compilers/common/readme.md b/components/libc/compilers/common/readme.md index 856ddeb045..82e23f8970 100644 --- a/components/libc/compilers/common/readme.md +++ b/components/libc/compilers/common/readme.md @@ -1,10 +1 @@ -## Attentions - -1. This folder is "common" for all toolchains. - -2. If you want to add new `.c` files, please do not forget to fix SConscript file too. eg: - -```python -if GetDepend('RT_USING_POSIX') == False: - SrcRemove(src, ['unistd.c']) -```
\ No newline at end of file +This folder is "common" for all toolchains.
\ No newline at end of file diff --git a/components/libc/compilers/common/sys/ioctl.h b/components/libc/compilers/common/sys/ioctl.h index b926a71875..04f73837bc 100644 --- a/components/libc/compilers/common/sys/ioctl.h +++ b/components/libc/compilers/common/sys/ioctl.h @@ -15,6 +15,10 @@ #include <dfs_posix.h> #endif +#ifdef _WIN32 +#include <winsock.h> +#endif + struct winsize { unsigned short ws_row; unsigned short ws_col; @@ -35,8 +39,8 @@ struct winsize { #define FIONREAD _IOR('f', 127, int) /* get # bytes to read */ #define FIONBIO _IOW('f', 126, int) /* set/clear non-blocking i/o */ -#define FIONWRITE _IOR('f', 121, int) /* get # bytes outstanding in send queue */ #endif +#define FIONWRITE _IOR('f', 121, int) /* get # bytes outstanding in send queue */ #define TCGETS 0x5401 #define TCSETS 0x5402 diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 24289088fb..5bfff15914 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -240,7 +240,7 @@ RTM_EXPORT(mktime); char* asctime_r(const struct tm *t, char *buf) { /* Checking input validity */ - if (rt_strlen(days) <= (t->tm_wday << 2) || rt_strlen(months) <= (t->tm_mon << 2)) + if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2)) { LOG_W("asctime_r: the input parameters exceeded the limit, please check it."); *(int*) buf = *(int*) days; diff --git a/components/libc/compilers/common/unistd.c b/components/libc/compilers/common/unistd.c deleted file mode 100644 index 9a457c4200..0000000000 --- a/components/libc/compilers/common/unistd.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2020-09-01 Meco Man first Version - * 2021-02-12 Meco Man move all functions located in <pthread_sleep.c> to this file - */ - -#include <unistd.h> -#include <rtthread.h> -#include <rthw.h> - -#ifdef RT_USING_POSIX_TERMIOS -#include "termios.h" -int isatty(int fd) -{ - struct termios ts; - return(tcgetattr(fd, &ts) != -1); /*true if no error (is a tty)*/ -} -#else -int isatty(int fd) -{ - if (fd >=0 && fd < 3) - { - return 1; - } - else - { - return 0; - } -} -#endif -RTM_EXPORT(isatty); - -char *ttyname(int fd) -{ - return "/dev/tty"; /* TODO: need to add more specific */ -} -RTM_EXPORT(ttyname); - -unsigned int sleep(unsigned int seconds) -{ - if (rt_thread_self() != RT_NULL) - { - rt_thread_delay(seconds * RT_TICK_PER_SECOND); - } - else /* scheduler has not run yet */ - { - while(seconds > 0) - { - rt_hw_us_delay(1000000u); - seconds --; - } - } - - return 0; -} -RTM_EXPORT(sleep); - -int usleep(useconds_t usec) -{ - if (rt_thread_self() != RT_NULL) - { - rt_thread_mdelay(usec / 1000u); - } - else /* scheduler has not run yet */ - { - rt_hw_us_delay(usec / 1000u); - } - rt_hw_us_delay(usec % 1000u); - - return 0; -} -RTM_EXPORT(usleep); - -pid_t gettid(void) -{ - /*TODO*/ - return 0; -} - -pid_t getpid(void) -{ - return gettid(); -} -RTM_EXPORT(getpid); - -pid_t getppid(void) -{ - return 0; -} -RTM_EXPORT(getppid); - -uid_t getuid(void) -{ - return 0; /*ROOT*/ -} -RTM_EXPORT(getuid); - -uid_t geteuid(void) -{ - return 0; /*ROOT*/ -} -RTM_EXPORT(geteuid); - -gid_t getgid(void) -{ - return 0; /*ROOT*/ -} -RTM_EXPORT(getgid); - -gid_t getegid(void) -{ - return 0; /*ROOT*/ -} -RTM_EXPORT(getegid); diff --git a/components/libc/compilers/dlib/SConscript b/components/libc/compilers/dlib/SConscript index 75a5812fab..39707abc61 100644 --- a/components/libc/compilers/dlib/SConscript +++ b/components/libc/compilers/dlib/SConscript @@ -1,26 +1,20 @@ from building import * - Import('rtconfig') src = Glob('*.c') -cwd = GetCurrentDir() group = [] -CPPPATH = [cwd] CPPDEFINES = ['RT_USING_DLIBC'] if rtconfig.PLATFORM == 'iar': - - if GetDepend('RT_USING_DFS'): + if GetDepend('RT_USING_POSIX'): from distutils.version import LooseVersion from iar import IARVersion CPPDEFINES = CPPDEFINES + ['_DLIB_FILE_DESCRIPTOR'] - if LooseVersion(IARVersion()) < LooseVersion("8.20.1"): CPPDEFINES = CPPDEFINES + ['_DLIB_THREAD_SUPPORT'] - group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], - CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], CPPDEFINES = CPPDEFINES) Return('group') diff --git a/components/libc/compilers/dlib/libc.c b/components/libc/compilers/dlib/libc.c deleted file mode 100644 index f540ac955f..0000000000 --- a/components/libc/compilers/dlib/libc.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } -#endif - -#if defined (RT_USING_PTHREADS) && !defined (RT_USING_COMPONENTS_INIT) - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); - diff --git a/components/libc/compilers/dlib/stdio.c b/components/libc/compilers/dlib/stdio.c deleted file mode 100644 index ed3f5dfef9..0000000000 --- a/components/libc/compilers/dlib/stdio.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard implement stdio for IAR dlib. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <rtthread.h> -#include "libc.h" - -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) -#include <dfs_posix.h> - -#define STDIO_DEVICE_NAME_MAX 32 - -static int std_fd = -1; -int libc_stdio_set_console(const char* device_name, int mode) -{ - int fd; - char name[STDIO_DEVICE_NAME_MAX]; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - fd = open(name, mode, 0); - if (fd >= 0) - { - if (std_fd >= 0) - { - close(std_fd); - } - std_fd = fd; - } - - return std_fd; -} - -int libc_stdio_get_console(void) { - return std_fd; -} - -#endif diff --git a/components/libc/compilers/dlib/syscall_close.c b/components/libc/compilers/dlib/syscall_close.c index 7ed1cc2f2d..2897c2418a 100644 --- a/components/libc/compilers/dlib/syscall_close.c +++ b/components/libc/compilers/dlib/syscall_close.c @@ -8,10 +8,8 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?__close" int __close(int handle) @@ -20,10 +18,9 @@ int __close(int handle) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; - -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX return close(handle); #else - return 0; -#endif + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_lseek.c b/components/libc/compilers/dlib/syscall_lseek.c index 47a069bf0a..4ea55ef1c3 100644 --- a/components/libc/compilers/dlib/syscall_lseek.c +++ b/components/libc/compilers/dlib/syscall_lseek.c @@ -8,10 +8,8 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?__lseek" long __lseek(int handle, long offset, int whence) @@ -20,10 +18,9 @@ long __lseek(int handle, long offset, int whence) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; - -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX return lseek(handle, offset, whence); #else return _LLIO_ERROR; -#endif +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_open.c b/components/libc/compilers/dlib/syscall_open.c index 37bfcecb47..8fb3c3a11f 100644 --- a/components/libc/compilers/dlib/syscall_open.c +++ b/components/libc/compilers/dlib/syscall_open.c @@ -10,17 +10,13 @@ #include <rtthread.h> #include <yfuns.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif +#include <fcntl.h> #pragma module_name = "?__open" int __open(const char *filename, int mode) { -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else +#ifdef RT_USING_POSIX int handle; int open_mode = O_RDONLY; @@ -70,5 +66,7 @@ int __open(const char *filename, int mode) return _LLIO_ERROR; return handle; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c index ae980be6ca..74cedb966a 100644 --- a/components/libc/compilers/dlib/syscall_read.c +++ b/components/libc/compilers/dlib/syscall_read.c @@ -9,11 +9,11 @@ */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> +#ifdef RT_USING_POSIX #include "libc.h" +#endif #define DBG_TAG "dlib.syscall_read" #define DBG_LVL DBG_INFO @@ -22,32 +22,26 @@ #pragma module_name = "?__read" size_t __read(int handle, unsigned char *buf, size_t len) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif if (handle == _LLIO_STDIN) { -#ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard input before initializing libc"); return 0; } return read(STDIN_FILENO, buf, len); -#else - return _LLIO_ERROR; -#endif } else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) { return _LLIO_ERROR; } -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else size = read(handle, buf, len); return size; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_remove.c b/components/libc/compilers/dlib/syscall_remove.c index 7d74236f4c..05bfbb67ae 100644 --- a/components/libc/compilers/dlib/syscall_remove.c +++ b/components/libc/compilers/dlib/syscall_remove.c @@ -8,17 +8,15 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?remove" int remove(const char *val) { -#ifndef RT_USING_DFS - return -1; -#else +#ifdef RT_USING_POSIX return unlink(val); -#endif +#else + return -1; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c index ffee51882d..ab011c1096 100644 --- a/components/libc/compilers/dlib/syscall_write.c +++ b/components/libc/compilers/dlib/syscall_write.c @@ -9,11 +9,11 @@ */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> +#ifdef RT_USING_POSIX #include "libc.h" +#endif #define DBG_TAG "dlib.syscall_write" #define DBG_LVL DBG_INFO @@ -23,16 +23,12 @@ size_t __write(int handle, const unsigned char *buf, size_t len) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif +#endif /* RT_USING_POSIX */ if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) { -#ifndef RT_USING_CONSOLE - return _LLIO_ERROR; -#else - #ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { @@ -40,7 +36,7 @@ size_t __write(int handle, const unsigned char *buf, size_t len) return 0; } return write(STDOUT_FILENO, (void*)buf, len); -#else +#elif defined(RT_USING_CONSOLE) rt_device_t console_device; console_device = rt_console_get_device(); @@ -50,19 +46,19 @@ size_t __write(int handle, const unsigned char *buf, size_t len) } return len; -#endif -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } else if (handle == _LLIO_STDIN) { return _LLIO_ERROR; } -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else +#ifdef RT_USING_POSIX size = write(handle, buf, len); return size; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } - diff --git a/components/libc/compilers/gcc/newlib/SConscript b/components/libc/compilers/gcc/newlib/SConscript index e0ee482747..ae50098dec 100644 --- a/components/libc/compilers/gcc/newlib/SConscript +++ b/components/libc/compilers/gcc/newlib/SConscript @@ -4,21 +4,14 @@ Import('rtconfig') src = [] cwd = GetCurrentDir() group = [] -LIBS = ['m'] +LIBS = ['m'] # link libm CPPDEFINES = ['RT_USING_NEWLIB'] CPPPATH = [cwd] if rtconfig.PLATFORM == 'gcc': if GetDepend('RT_USING_LIBC'): - # link with libc and libm: - # libm is a frequently used lib. Newlib is compiled with -ffunction-sections in - # recent GCC tool chains. The linker would just link in the functions that have - # been referenced. So setting this won't result in bigger text size. - LIBS += ['c'] - + LIBS += ['c'] # link libc src += Glob('*.c') - if GetDepend('RT_USING_MODULE') == False: - SrcRemove(src, ['libc_syms.c']) else: src += ['syscalls.c'] diff --git a/components/libc/compilers/gcc/newlib/libc.c b/components/libc/compilers/gcc/newlib/libc.c deleted file mode 100644 index 9834a9a015..0000000000 --- a/components/libc/compilers/gcc/newlib/libc.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <rtthread.h> -#include <stdio.h> -#include <stdlib.h> -#include <fcntl.h> -#include <sys/time.h> - -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int _EXFUN(putenv,(char *__string)); - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS) & defined(RT_USING_CONSOLE) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } - - /* set PATH and HOME */ - putenv("PATH=/bin"); - putenv("HOME=/home"); -#endif - -#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); diff --git a/components/libc/compilers/gcc/newlib/libc.h b/components/libc/compilers/gcc/newlib/libc.h deleted file mode 100644 index c470698aa6..0000000000 --- a/components/libc/compilers/gcc/newlib/libc.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#ifndef __RTT_LIBC_H__ -#define __RTT_LIBC_H__ - -#ifndef _EXFUN -#define _EXFUN(name, proto) name proto -#endif - -#ifdef __cplusplus -extern "C" { -#endif -int libc_system_init(void); -int libc_stdio_get_console(void); -int libc_stdio_set_console(const char* device_name, int mode); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/libc/compilers/gcc/newlib/stdio.c b/components/libc/compilers/gcc/newlib/stdio.c deleted file mode 100644 index baccafad87..0000000000 --- a/components/libc/compilers/gcc/newlib/stdio.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <stdio.h> -#include <stdlib.h> -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#define STDIO_DEVICE_NAME_MAX 32 - -int _EXFUN(fileno, (FILE *)); - -static FILE* std_console = NULL; - -int libc_stdio_set_console(const char* device_name, int mode) -{ - FILE *fp; - char name[STDIO_DEVICE_NAME_MAX]; - char *file_mode; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - if (mode == O_RDWR) file_mode = "r+"; - else if (mode == O_WRONLY) file_mode = "wb"; - else file_mode = "rb"; - - fp = fopen(name, file_mode); - if (fp) - { - setvbuf(fp, NULL, _IONBF, 0); - - if (std_console) - { - fclose(std_console); - std_console = NULL; - } - std_console = fp; - - if (mode == O_RDWR) - { - _GLOBAL_REENT->_stdin = std_console; - } - else - { - _GLOBAL_REENT->_stdin = NULL; - } - - if (mode == O_RDONLY) - { - _GLOBAL_REENT->_stdout = NULL; - _GLOBAL_REENT->_stderr = NULL; - } - else - { - _GLOBAL_REENT->_stdout = std_console; - _GLOBAL_REENT->_stderr = std_console; - } - - _GLOBAL_REENT->__sdidinit = 1; - } - - if (std_console) return fileno(std_console); - - return -1; -} - -int libc_stdio_get_console(void) { - if (std_console) - return fileno(std_console); - else - return -1; -} diff --git a/components/libc/compilers/gcc/newlib/syscalls.c b/components/libc/compilers/gcc/newlib/syscalls.c index f5a2c16ecc..f95e695a85 100644 --- a/components/libc/compilers/gcc/newlib/syscalls.c +++ b/components/libc/compilers/gcc/newlib/syscalls.c @@ -13,9 +13,19 @@ #include <reent.h> #include <rtthread.h> +#include <stdio.h> +#include <string.h> #include <stddef.h> +#include <fcntl.h> #include <unistd.h> #include <sys/errno.h> +#include <sys/stat.h> +#ifdef RT_USING_POSIX +#include "libc.h" +#endif +#ifdef RT_USING_MODULE +#include <dlmodule.h> +#endif #define DBG_TAG "newlib.syscalls" #define DBG_LVL DBG_INFO @@ -83,18 +93,8 @@ void __libc_init_array(void) } #ifdef RT_USING_LIBC -#include <reent.h> -#include <stdio.h> -#include "libc.h" -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif -#ifdef RT_USING_MODULE -#include <dlmodule.h> -#endif /* Reentrant versions of system calls. */ - #ifndef _REENT_ONLY int *__errno () { @@ -109,39 +109,29 @@ int _getpid_r(struct _reent *ptr) int _close_r(struct _reent *ptr, int fd) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else return close(fd); -#endif } int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fork_r(struct _reent *ptr) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } @@ -160,67 +150,73 @@ int _isatty_r(struct _reent *ptr, int fd) int _kill_r(struct _reent *ptr, int pid, int sig) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _link_r(struct _reent *ptr, const char *old, const char *new) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } -_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) +int _wait_r(struct _reent *ptr, int *status) { -#ifndef RT_USING_DFS - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; -#else +} + +mode_t umask(mode_t mask) +{ + return 022; +} + +int flock(int fd, int operation) +{ + return 0; +} + +_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) +{ +#ifdef RT_USING_POSIX _off_t rc; rc = lseek(fd, pos, whence); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _mkdir_r(struct _reent *ptr, const char *name, int mode) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = mkdir(name, mode); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _open_r(struct _reent *ptr, const char *file, int flags, int mode) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; - rc = open(file, flags, mode); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO) { @@ -229,74 +225,51 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) } rc = read(fd, buf, nbytes); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _rename_r(struct _reent *ptr, const char *old, const char *new) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = rename(old, new); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = stat(file, pstat); return rc; -#endif -} - -int _unlink_r(struct _reent *ptr, const char *file) -{ -#ifndef RT_USING_DFS - /* return "not supported" */ +#else ptr->_errno = ENOTSUP; return -1; -#else - return unlink(file); -#endif +#endif /* RT_USING_POSIX */ } -int _wait_r(struct _reent *ptr, int *status) +int _unlink_r(struct _reent *ptr, const char *file) { - /* return "not supported" */ +#ifdef RT_USING_POSIX + return unlink(file); +#else ptr->_errno = ENOTSUP; return -1; +#endif /* RT_USING_POSIX */ } _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { -#ifndef RT_USING_DFS -#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) - if (STDOUT_FILENO == fd) - { - rt_device_t console; - - console = rt_console_get_device(); - if (console) return rt_device_write(console, -1, buf, nbytes); - } - - return 0; -#else - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#endif /*RT_USING_DEVICE*/ -#else +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO) { @@ -305,7 +278,18 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) } rc = write(fd, buf, nbytes); return rc; -#endif +#elif defined(RT_USING_CONSOLE) + if (STDOUT_FILENO == fd) + { + rt_device_t console; + + console = rt_console_get_device(); + if (console) + return rt_device_write(console, -1, buf, nbytes); + } +#endif /* RT_USING_POSIX */ + ptr->_errno = ENOTSUP; + return -1; } /* for exit() and abort() */ @@ -316,16 +300,6 @@ __attribute__ ((noreturn)) void _exit (int status) while(1); } -mode_t umask(mode_t mask) -{ - return 022; -} - -int flock(int fd, int operation) -{ - return 0; -} - /* These functions are implemented and replaced by the 'common/time.c' file int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp); diff --git a/components/libc/posix/SConscript b/components/libc/posix/SConscript new file mode 100644 index 0000000000..4c815c49b8 --- /dev/null +++ b/components/libc/posix/SConscript @@ -0,0 +1,15 @@ +# RT-Thread building script for bridge + +import os +from building import * + +cwd = GetCurrentDir() +objs = [] +list = os.listdir(cwd) + +for d in list: + path = os.path.join(cwd, d) + if os.path.isfile(os.path.join(path, 'SConscript')): + objs = objs + SConscript(os.path.join(d, 'SConscript')) + +Return('objs') diff --git a/components/libc/posix/aio/SConscript b/components/libc/posix/aio/SConscript new file mode 100644 index 0000000000..445efaade5 --- /dev/null +++ b/components/libc/posix/aio/SConscript @@ -0,0 +1,11 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] + +group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX', 'RT_USING_POSIX_AIO'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/aio/posix_aio.c b/components/libc/posix/aio/posix_aio.c index 66cced8687..66cced8687 100644 --- a/components/libc/aio/posix_aio.c +++ b/components/libc/posix/aio/posix_aio.c diff --git a/components/libc/aio/posix_aio.h b/components/libc/posix/aio/posix_aio.h index 4e25e1d15b..4e25e1d15b 100644 --- a/components/libc/aio/posix_aio.h +++ b/components/libc/posix/aio/posix_aio.h diff --git a/components/libc/getline/README.md b/components/libc/posix/getline/README.md index ef3c19deae..ef3c19deae 100644 --- a/components/libc/getline/README.md +++ b/components/libc/posix/getline/README.md diff --git a/components/libc/getline/SConscript b/components/libc/posix/getline/SConscript index fa4c8618c1..bb18dc04c5 100644 --- a/components/libc/getline/SConscript +++ b/components/libc/posix/getline/SConscript @@ -6,7 +6,7 @@ cwd = GetCurrentDir() src = Glob('*.c') + Glob('*.cpp') CPPPATH = [cwd] -group = DefineGroup('libc', src, +group = DefineGroup('POSIX', src, depend = ['RT_USING_LIBC', 'RT_USING_POSIX','RT_USING_POSIX_GETLINE'], CPPPATH = CPPPATH) diff --git a/components/libc/getline/UNLICENSE b/components/libc/posix/getline/UNLICENSE index 68a49daad8..68a49daad8 100644 --- a/components/libc/getline/UNLICENSE +++ b/components/libc/posix/getline/UNLICENSE diff --git a/components/libc/getline/posix_getline.c b/components/libc/posix/getline/posix_getline.c index da80242ecc..da80242ecc 100644 --- a/components/libc/getline/posix_getline.c +++ b/components/libc/posix/getline/posix_getline.c diff --git a/components/libc/getline/posix_getline.h b/components/libc/posix/getline/posix_getline.h index 13a4487d7f..13a4487d7f 100644 --- a/components/libc/getline/posix_getline.h +++ b/components/libc/posix/getline/posix_getline.h diff --git a/components/libc/mmap/SConscript b/components/libc/posix/mmap/SConscript index 1a36a15710..fd5b2b953e 100644 --- a/components/libc/mmap/SConscript +++ b/components/libc/posix/mmap/SConscript @@ -6,7 +6,7 @@ cwd = GetCurrentDir() src = Glob('*.c') + Glob('*.cpp') CPPPATH = [cwd] -group = DefineGroup('libc', src, +group = DefineGroup('POSIX', src, depend = ['RT_USING_DFS', 'RT_USING_POSIX_MMAP'], CPPPATH = CPPPATH) diff --git a/components/libc/mmap/posix_mmap.c b/components/libc/posix/mmap/posix_mmap.c index e68b82e640..e68b82e640 100644 --- a/components/libc/mmap/posix_mmap.c +++ b/components/libc/posix/mmap/posix_mmap.c diff --git a/components/libc/pthreads/SConscript b/components/libc/posix/pthreads/SConscript index 767b406d4c..7cd78300b2 100644 --- a/components/libc/pthreads/SConscript +++ b/components/libc/posix/pthreads/SConscript @@ -16,7 +16,7 @@ try: except : pass -group = DefineGroup('libc', src, +group = DefineGroup('POSIX', src, depend = ['RT_USING_PTHREADS', 'RT_USING_LIBC'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) Return('group') diff --git a/components/libc/pthreads/mqueue.c b/components/libc/posix/pthreads/mqueue.c index 6840164a68..6840164a68 100644 --- a/components/libc/pthreads/mqueue.c +++ b/components/libc/posix/pthreads/mqueue.c diff --git a/components/libc/pthreads/mqueue.h b/components/libc/posix/pthreads/mqueue.h index 5152bfad29..5152bfad29 100644 --- a/components/libc/pthreads/mqueue.h +++ b/components/libc/posix/pthreads/mqueue.h diff --git a/components/libc/pthreads/posix_types.h b/components/libc/posix/pthreads/posix_types.h index 5838e8b370..5838e8b370 100644 --- a/components/libc/pthreads/posix_types.h +++ b/components/libc/posix/pthreads/posix_types.h diff --git a/components/libc/pthreads/pthread.c b/components/libc/posix/pthreads/pthread.c index b0bf39dff6..b0bf39dff6 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/posix/pthreads/pthread.c diff --git a/components/libc/pthreads/pthread.h b/components/libc/posix/pthreads/pthread.h index 177b2539c5..177b2539c5 100644 --- a/components/libc/pthreads/pthread.h +++ b/components/libc/posix/pthreads/pthread.h diff --git a/components/libc/pthreads/pthread_attr.c b/components/libc/posix/pthreads/pthread_attr.c index 4aedf5a90d..4aedf5a90d 100644 --- a/components/libc/pthreads/pthread_attr.c +++ b/components/libc/posix/pthreads/pthread_attr.c diff --git a/components/libc/pthreads/pthread_barrier.c b/components/libc/posix/pthreads/pthread_barrier.c index 1a5b7fa3f9..1a5b7fa3f9 100644 --- a/components/libc/pthreads/pthread_barrier.c +++ b/components/libc/posix/pthreads/pthread_barrier.c diff --git a/components/libc/pthreads/pthread_cond.c b/components/libc/posix/pthreads/pthread_cond.c index 9bf49704ed..9bf49704ed 100644 --- a/components/libc/pthreads/pthread_cond.c +++ b/components/libc/posix/pthreads/pthread_cond.c diff --git a/components/libc/pthreads/pthread_internal.h b/components/libc/posix/pthreads/pthread_internal.h index 1fce29f636..1fce29f636 100644 --- a/components/libc/pthreads/pthread_internal.h +++ b/components/libc/posix/pthreads/pthread_internal.h diff --git a/components/libc/pthreads/pthread_mutex.c b/components/libc/posix/pthreads/pthread_mutex.c index a681380b11..a681380b11 100644 --- a/components/libc/pthreads/pthread_mutex.c +++ b/components/libc/posix/pthreads/pthread_mutex.c diff --git a/components/libc/pthreads/pthread_rwlock.c b/components/libc/posix/pthreads/pthread_rwlock.c index 1c2fe19f5f..1c2fe19f5f 100644 --- a/components/libc/pthreads/pthread_rwlock.c +++ b/components/libc/posix/pthreads/pthread_rwlock.c diff --git a/components/libc/pthreads/pthread_spin.c b/components/libc/posix/pthreads/pthread_spin.c index 3f3dc1593c..3f3dc1593c 100644 --- a/components/libc/pthreads/pthread_spin.c +++ b/components/libc/posix/pthreads/pthread_spin.c diff --git a/components/libc/pthreads/pthread_tls.c b/components/libc/posix/pthreads/pthread_tls.c index d48df7f2dd..d48df7f2dd 100644 --- a/components/libc/pthreads/pthread_tls.c +++ b/components/libc/posix/pthreads/pthread_tls.c diff --git a/components/libc/pthreads/sched.c b/components/libc/posix/pthreads/sched.c index 5f2a53c01f..5f2a53c01f 100644 --- a/components/libc/pthreads/sched.c +++ b/components/libc/posix/pthreads/sched.c diff --git a/components/libc/pthreads/sched.h b/components/libc/posix/pthreads/sched.h index 07e2c0c6a8..07e2c0c6a8 100644 --- a/components/libc/pthreads/sched.h +++ b/components/libc/posix/pthreads/sched.h diff --git a/components/libc/pthreads/semaphore.c b/components/libc/posix/pthreads/semaphore.c index 5e59ae850f..5e59ae850f 100644 --- a/components/libc/pthreads/semaphore.c +++ b/components/libc/posix/pthreads/semaphore.c diff --git a/components/libc/pthreads/semaphore.h b/components/libc/posix/pthreads/semaphore.h index 872226e385..872226e385 100644 --- a/components/libc/pthreads/semaphore.h +++ b/components/libc/posix/pthreads/semaphore.h diff --git a/components/libc/signal/SConscript b/components/libc/posix/signal/SConscript index f07ff58f6e..07cd98cbe0 100644 --- a/components/libc/signal/SConscript +++ b/components/libc/posix/signal/SConscript @@ -6,7 +6,7 @@ cwd = GetCurrentDir() src = Glob('*.c') + Glob('*.cpp') CPPPATH = [cwd] -group = DefineGroup('libc', src, +group = DefineGroup('POSIX', src, depend = ['RT_USING_SIGNALS', 'RT_USING_PTHREADS', 'RT_USING_LIBC'], CPPPATH = CPPPATH) diff --git a/components/libc/signal/posix_signal.c b/components/libc/posix/signal/posix_signal.c index ac8d2b1eef..ac8d2b1eef 100644 --- a/components/libc/signal/posix_signal.c +++ b/components/libc/posix/signal/posix_signal.c diff --git a/components/libc/signal/posix_signal.h b/components/libc/posix/signal/posix_signal.h index f04b54c5f8..f04b54c5f8 100644 --- a/components/libc/signal/posix_signal.h +++ b/components/libc/posix/signal/posix_signal.h diff --git a/components/libc/posix/src/SConscript b/components/libc/posix/src/SConscript new file mode 100644 index 0000000000..f338b52cec --- /dev/null +++ b/components/libc/posix/src/SConscript @@ -0,0 +1,13 @@ +from building import * +Import('rtconfig') + +src = Glob('*.c') +cwd = GetCurrentDir() +CPPPATH = [cwd] + +if rtconfig.ARCH == 'sim': # the vs simulator does not support select and poll + SrcRemove(src, ['select.c', 'poll.c']) + +group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/compilers/common/delay.c b/components/libc/posix/src/delay.c index 4666258d3b..1638caafbf 100644 --- a/components/libc/compilers/common/delay.c +++ b/components/libc/posix/src/delay.c @@ -9,6 +9,15 @@ */ #include <rtthread.h> #include <rthw.h> +#define DBG_TAG "POSIX.delay" +#define DBG_LVL DBG_INFO +#include <rtdbg.h> + +RT_WEAK void rt_hw_us_delay(rt_uint32_t us) +{ + (void) us; + LOG_W("Please consider implementing rt_hw_us_delay() in another file."); +} void msleep(unsigned int msecs) { diff --git a/components/libc/posix/src/libc.c b/components/libc/posix/src/libc.c new file mode 100644 index 0000000000..068719d5d6 --- /dev/null +++ b/components/libc/posix/src/libc.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2017/10/15 bernard the first version + */ +#include <rtthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/time.h> +#include "libc.h" +#include <stdio.h> +#include <stdlib.h> +#ifdef RT_USING_PTHREADS +#include <pthread.h> +#endif + +int libc_system_init(void) +{ +#ifdef RT_USING_POSIX + rt_device_t dev_console; + + dev_console = rt_console_get_device(); + if (dev_console) + { + libc_stdio_set_console(dev_console->parent.name, O_RDWR); + } +#endif /* RT_USING_POSIX */ + +#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT + pthread_system_init(); +#endif + + return 0; +} +INIT_COMPONENT_EXPORT(libc_system_init); + +#ifdef RT_USING_POSIX + +#if defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) +#define STDIO_DEVICE_NAME_MAX 32 +static FILE* std_console = NULL; +int libc_stdio_set_console(const char* device_name, int mode) +{ + FILE *fp; + char name[STDIO_DEVICE_NAME_MAX]; + char *file_mode; + + snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); + name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; + + if (mode == O_RDWR) + { + file_mode = "r+"; + } + else if (mode == O_WRONLY) + { + file_mode = "wb"; + } + else + { + file_mode = "rb"; + } + + fp = fopen(name, file_mode); + if (fp) + { + setvbuf(fp, NULL, _IONBF, 0); + + if (std_console) + { + fclose(std_console); + std_console = NULL; + } + std_console = fp; + + if (mode == O_RDWR) + { + _GLOBAL_REENT->_stdin = std_console; + } + else + { + _GLOBAL_REENT->_stdin = NULL; + } + + if (mode == O_RDONLY) + { + _GLOBAL_REENT->_stdout = NULL; + _GLOBAL_REENT->_stderr = NULL; + } + else + { + _GLOBAL_REENT->_stdout = std_console; + _GLOBAL_REENT->_stderr = std_console; + } + + _GLOBAL_REENT->__sdidinit = 1; + } + + if (std_console) + return fileno(std_console); + + return -1; +} + +int libc_stdio_get_console(void) +{ + if (std_console) + return fileno(std_console); + else + return -1; +} + +#else +#define STDIO_DEVICE_NAME_MAX 32 +static int std_fd = -1; +int libc_stdio_set_console(const char* device_name, int mode) +{ + int fd; + char name[STDIO_DEVICE_NAME_MAX]; + + snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); + name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; + + fd = open(name, mode, 0); + if (fd >= 0) + { + if (std_fd >= 0) + { + close(std_fd); + } + std_fd = fd; + } + + return std_fd; +} + +int libc_stdio_get_console(void) { + return std_fd; +} +#endif /* defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) */ +#endif /* RT_USING_POSIX */ diff --git a/components/libc/compilers/dlib/libc.h b/components/libc/posix/src/libc.h index 9643c1161f..f81380bf78 100644 --- a/components/libc/compilers/dlib/libc.h +++ b/components/libc/posix/src/libc.h @@ -7,17 +7,20 @@ * Date Author Notes * 2017/10/15 bernard the first version */ - #ifndef __RTT_LIBC_H__ #define __RTT_LIBC_H__ +#include <rtconfig.h> + #ifdef __cplusplus extern "C" { #endif int libc_system_init(void); +#ifdef RT_USING_POSIX int libc_stdio_get_console(void); int libc_stdio_set_console(const char* device_name, int mode); +#endif /* RT_USING_POSIX */ #ifdef __cplusplus } diff --git a/components/libc/posix/src/poll.c b/components/libc/posix/src/poll.c new file mode 100644 index 0000000000..2109fb310b --- /dev/null +++ b/components/libc/posix/src/poll.c @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016-12-28 Bernard first version + * 2018-03-09 Bernard Add protection for pt->triggered. + */ +#include <stdint.h> +#include <rthw.h> +#include <rtthread.h> +#include <dfs_file.h> +#include <poll.h> + +#ifdef RT_USING_POSIX + +struct rt_poll_node; + +struct rt_poll_table +{ + rt_pollreq_t req; + rt_uint32_t triggered; /* the waited thread whether triggered */ + rt_thread_t polling_thread; + struct rt_poll_node *nodes; +}; + +struct rt_poll_node +{ + struct rt_wqueue_node wqn; + struct rt_poll_table *pt; + struct rt_poll_node *next; +}; + +static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key) +{ + struct rt_poll_node *pn; + + if (key && !((rt_ubase_t)key & wait->key)) + return -1; + + pn = rt_container_of(wait, struct rt_poll_node, wqn); + pn->pt->triggered = 1; + + return __wqueue_default_wake(wait, key); +} + +static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req) +{ + struct rt_poll_table *pt; + struct rt_poll_node *node; + + node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node)); + if (node == RT_NULL) + return; + + pt = rt_container_of(req, struct rt_poll_table, req); + + node->wqn.key = req->_key; + rt_list_init(&(node->wqn.list)); + node->wqn.polling_thread = pt->polling_thread; + node->wqn.wakeup = __wqueue_pollwake; + node->next = pt->nodes; + node->pt = pt; + pt->nodes = node; + rt_wqueue_add(wq, &node->wqn); +} + +static void poll_table_init(struct rt_poll_table *pt) +{ + pt->req._proc = _poll_add; + pt->triggered = 0; + pt->nodes = RT_NULL; + pt->polling_thread = rt_thread_self(); +} + +static int poll_wait_timeout(struct rt_poll_table *pt, int msec) +{ + rt_int32_t timeout; + int ret = 0; + struct rt_thread *thread; + rt_base_t level; + + thread = pt->polling_thread; + + timeout = rt_tick_from_millisecond(msec); + + level = rt_hw_interrupt_disable(); + + if (timeout != 0 && !pt->triggered) + { + rt_thread_suspend(thread); + if (timeout > 0) + { + rt_timer_control(&(thread->thread_timer), + RT_TIMER_CTRL_SET_TIME, + &timeout); + rt_timer_start(&(thread->thread_timer)); + } + + rt_hw_interrupt_enable(level); + + rt_schedule(); + + level = rt_hw_interrupt_disable(); + } + + ret = !pt->triggered; + rt_hw_interrupt_enable(level); + + return ret; +} + +static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) +{ + int mask = 0; + int fd; + + fd = pollfd->fd; + + if (fd >= 0) + { + struct dfs_fd *f = fd_get(fd); + mask = POLLNVAL; + + if (f) + { + mask = POLLMASK_DEFAULT; + if (f->fops->poll) + { + req->_key = pollfd->events | POLLERR | POLLHUP; + + mask = f->fops->poll(f, req); + + /* dealwith the device return error -1*/ + if (mask < 0) + { + fd_put(f); + pollfd->revents = 0; + return mask; + } + } + /* Mask out unneeded events. */ + mask &= pollfd->events | POLLERR | POLLHUP; + fd_put(f); + } + } + pollfd->revents = mask; + + return mask; +} + +static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec) +{ + int num; + int istimeout = 0; + int n; + struct pollfd *pf; + int ret = 0; + + if (msec == 0) + { + pt->req._proc = RT_NULL; + istimeout = 1; + } + + while (1) + { + pf = fds; + num = 0; + + for (n = 0; n < nfds; n ++) + { + ret = do_pollfd(pf, &pt->req); + if(ret < 0) + { + /*dealwith the device return error -1 */ + pt->req._proc = RT_NULL; + return ret; + } + else if(ret > 0) + { + num ++; + pt->req._proc = RT_NULL; + } + pf ++; + } + + pt->req._proc = RT_NULL; + + if (num || istimeout) + break; + + if (poll_wait_timeout(pt, msec)) + istimeout = 1; + } + + return num; +} + +static void poll_teardown(struct rt_poll_table *pt) +{ + struct rt_poll_node *node, *next; + + next = pt->nodes; + while (next) + { + node = next; + rt_wqueue_remove(&node->wqn); + next = node->next; + rt_free(node); + } +} + +int poll(struct pollfd *fds, nfds_t nfds, int timeout) +{ + int num; + struct rt_poll_table table; + + poll_table_init(&table); + + num = poll_do(fds, nfds, &table, timeout); + + poll_teardown(&table); + + return num; +} + +#endif diff --git a/components/libc/posix/src/select.c b/components/libc/posix/src/select.c new file mode 100644 index 0000000000..25cda0f0d1 --- /dev/null +++ b/components/libc/posix/src/select.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016-12-28 Bernard first version + */ +#include <rtthread.h> +#include <poll.h> +#include <sys/select.h> + +#ifdef RT_USING_POSIX + +static void fdszero(fd_set *set, int nfds) +{ + fd_mask *m; + int n; + + /* + The 'sizeof(fd_set)' of the system space may differ from user space, + so the actual size of the 'fd_set' is determined here with the parameter 'nfds' + */ + m = (fd_mask *)set; + for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8)) + { + rt_memset(m, 0, sizeof(fd_mask)); + m ++; + } +} + +int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + int fd; + int npfds; + int msec; + int ndx; + int ret; + struct pollfd *pollset = RT_NULL; + + /* How many pollfd structures do we need to allocate? */ + for (fd = 0, npfds = 0; fd < nfds; fd++) + { + /* Check if any monitor operation is requested on this fd */ + if ((readfds && FD_ISSET(fd, readfds)) || + (writefds && FD_ISSET(fd, writefds)) || + (exceptfds && FD_ISSET(fd, exceptfds))) + { + npfds++; + } + } + + /* Allocate the descriptor list for poll() */ + if (npfds > 0) + { + pollset = (struct pollfd *)rt_calloc(npfds, sizeof(struct pollfd)); + if (!pollset) + { + return -1; + } + } + + /* Initialize the descriptor list for poll() */ + for (fd = 0, ndx = 0; fd < nfds; fd++) + { + int incr = 0; + + /* The readfs set holds the set of FDs that the caller can be assured + * of reading from without blocking. Note that POLLHUP is included as + * a read-able condition. POLLHUP will be reported at the end-of-file + * or when a connection is lost. In either case, the read() can then + * be performed without blocking. + */ + + if (readfds && FD_ISSET(fd, readfds)) + { + pollset[ndx].fd = fd; + pollset[ndx].events |= POLLIN; + incr = 1; + } + + if (writefds && FD_ISSET(fd, writefds)) + { + pollset[ndx].fd = fd; + pollset[ndx].events |= POLLOUT; + incr = 1; + } + + if (exceptfds && FD_ISSET(fd, exceptfds)) + { + pollset[ndx].fd = fd; + incr = 1; + } + + ndx += incr; + } + + RT_ASSERT(ndx == npfds); + + /* Convert the timeout to milliseconds */ + if (timeout) + { + msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + } + else + { + msec = -1; + } + + /* Then let poll do all of the real work. */ + + ret = poll(pollset, npfds, msec); + + /* Now set up the return values */ + if (readfds) + { + fdszero(readfds, nfds); + } + + if (writefds) + { + fdszero(writefds, nfds); + } + + if (exceptfds) + { + fdszero(exceptfds, nfds); + } + + /* Convert the poll descriptor list back into selects 3 bitsets */ + + if (ret > 0) + { + ret = 0; + for (ndx = 0; ndx < npfds; ndx++) + { + /* Check for read conditions. Note that POLLHUP is included as a + * read condition. POLLHUP will be reported when no more data will + * be available (such as when a connection is lost). In either + * case, the read() can then be performed without blocking. + */ + + if (readfds) + { + if (pollset[ndx].revents & (POLLIN | POLLHUP)) + { + FD_SET(pollset[ndx].fd, readfds); + ret++; + } + } + + /* Check for write conditions */ + if (writefds) + { + if (pollset[ndx].revents & POLLOUT) + { + FD_SET(pollset[ndx].fd, writefds); + ret++; + } + } + + /* Check for exceptions */ + if (exceptfds) + { + if (pollset[ndx].revents & POLLERR) + { + FD_SET(pollset[ndx].fd, exceptfds); + ret++; + } + } + } + } + + if (pollset) rt_free(pollset); + + return ret; +} + +#endif diff --git a/components/libc/posix/src/unistd.c b/components/libc/posix/src/unistd.c new file mode 100644 index 0000000000..ef2df2c2fe --- /dev/null +++ b/components/libc/posix/src/unistd.c @@ -0,0 +1,1044 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2020-09-01 Meco Man first Version + * 2021-02-12 Meco Man move all functions located in <pthread_sleep.c> to this file + */ +#include <rtthread.h> +#include <dfs_file.h> +#include <dfs_private.h> +#include <sys/errno.h> +#include <unistd.h> +#include <delay.h> + +/** + * this function is a POSIX compliant version, which will open a file and + * return a file descriptor according specified flags. + * + * @param file the path name of file. + * @param flags the file open flags. + * + * @return the non-negative integer on successful open, others for failed. + */ +int open(const char *file, int flags, ...) +{ + int fd, result; + struct dfs_fd *d; + + /* allocate a fd */ + fd = fd_new(); + if (fd < 0) + { + rt_set_errno(-ENOMEM); + + return -1; + } + d = fd_get(fd); + + result = dfs_file_open(d, file, flags); + if (result < 0) + { + /* release the ref-count of fd */ + fd_put(d); + fd_put(d); + + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return fd; +} +RTM_EXPORT(open); + +/** + * this function is a POSIX compliant version, which will close the open + * file descriptor. + * + * @param fd the file descriptor. + * + * @return 0 on successful, -1 on failed. + */ +int close(int fd) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_close(d); + fd_put(d); + + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + fd_put(d); + + return 0; +} +RTM_EXPORT(close); + +/** + * this function is a POSIX compliant version, which will read specified data + * buffer length for an open file descriptor. + * + * @param fd the file descriptor. + * @param buf the buffer to save the read data. + * @param len the maximal length of data buffer + * + * @return the actual read data buffer length. If the returned value is 0, it + * may be reach the end of file, please check errno. + */ +#ifdef _READ_WRITE_RETURN_TYPE +_READ_WRITE_RETURN_TYPE read(int fd, void *buf, size_t len) /* some gcc tool chains will use different data structure */ +#else +ssize_t read(int fd, void *buf, size_t len) +#endif +{ + int result; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_read(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return result; +} +RTM_EXPORT(read); + +/** + * this function is a POSIX compliant version, which will write specified data + * buffer length for an open file descriptor. + * + * @param fd the file descriptor + * @param buf the data buffer to be written. + * @param len the data buffer length. + * + * @return the actual written data buffer length. + */ +#ifdef _READ_WRITE_RETURN_TYPE +_READ_WRITE_RETURN_TYPE write(int fd, const void *buf, size_t len) /* some gcc tool chains will use different data structure */ +#else +ssize_t write(int fd, const void *buf, size_t len) +#endif +{ + int result; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_write(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return result; +} +RTM_EXPORT(write); + +/** + * this function is a POSIX compliant version, which will seek the offset for + * an open file descriptor. + * + * @param fd the file descriptor. + * @param offset the offset to be seeked. + * @param whence the directory of seek. + * + * @return the current read/write position in the file, or -1 on failed. + */ +off_t lseek(int fd, off_t offset, int whence) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + switch (whence) + { + case SEEK_SET: + break; + + case SEEK_CUR: + offset += d->pos; + break; + + case SEEK_END: + offset += d->size; + break; + + default: + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + + if (offset < 0) + { + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + result = dfs_file_lseek(d, offset); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return offset; +} +RTM_EXPORT(lseek); + +#ifndef _WIN32 /* we can not implement these functions */ +/** + * this function is a POSIX compliant version, which will rename old file name + * to new file name. + * + * @param old the old file name. + * @param new the new file name. + * + * @return 0 on successful, -1 on failed. + * + * note: the old and new file name must be belong to a same file system. + */ +int rename(const char *old_file, const char *new_file) +{ + int result; + + result = dfs_file_rename(old_file, new_file); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(rename); +#endif + +/** + * this function is a POSIX compliant version, which will unlink (remove) a + * specified path file from file system. + * + * @param pathname the specified path name to be unlinked. + * + * @return 0 on successful, -1 on failed. + */ +int unlink(const char *pathname) +{ + int result; + + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(unlink); + +/** + * this function is a POSIX compliant version, which will get file information. + * + * @param file the file name + * @param buf the data buffer to save stat description. + * + * @return 0 on successful, -1 on failed. + */ +int stat(const char *file, struct stat *buf) +{ + int result; + + result = dfs_file_stat(file, buf); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return result; +} +RTM_EXPORT(stat); + +/** + * this function is a POSIX compliant version, which will get file status. + * + * @param fildes the file description + * @param buf the data buffer to save stat description. + * + * @return 0 on successful, -1 on failed. + */ +int fstat(int fildes, struct stat *buf) +{ + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + /* it's the root directory */ + buf->st_dev = 0; + + buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | + S_IWUSR | S_IWGRP | S_IWOTH; + if (d->type == FT_DIRECTORY) + { + buf->st_mode &= ~S_IFREG; + buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; + } + + buf->st_size = d->size; + buf->st_mtime = 0; + + fd_put(d); + + return RT_EOK; +} +RTM_EXPORT(fstat); + +/** + * this function is a POSIX compliant version, which shall request that all data + * for the open file descriptor named by fildes is to be transferred to the storage + * device associated with the file described by fildes. + * + * @param fildes the file description + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int fsync(int fildes) +{ + int ret; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d == NULL) + { + rt_set_errno(-EBADF); + return -1; + } + + ret = dfs_file_flush(d); + + fd_put(d); + return ret; +} +RTM_EXPORT(fsync); + +/** + * this function is a POSIX compliant version, which shall perform a variety of + * control functions on devices. + * + * @param fildes the file description + * @param cmd the specified command + * @param data represents the additional information that is needed by this + * specific device to perform the requested function. + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int fcntl(int fildes, int cmd, ...) +{ + int ret = -1; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fildes); + if (d) + { + void *arg; + va_list ap; + + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + + ret = dfs_file_ioctl(d, cmd, arg); + fd_put(d); + } + else ret = -EBADF; + + if (ret < 0) + { + rt_set_errno(ret); + ret = -1; + } + + return ret; +} +RTM_EXPORT(fcntl); + +/** + * this function is a POSIX compliant version, which shall perform a variety of + * control functions on devices. + * + * @param fildes the file description + * @param cmd the specified command + * @param data represents the additional information that is needed by this + * specific device to perform the requested function. + * + * @return 0 on successful completion. Otherwise, -1 shall be returned and errno + * set to indicate the error. + */ +int ioctl(int fildes, int cmd, ...) +{ + void *arg; + va_list ap; + + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + + /* we use fcntl for this API. */ + return fcntl(fildes, cmd, arg); +} +RTM_EXPORT(ioctl); + +/** + * + * this function is a POSIX compliant version, which cause the regular file + * referenced by fd to be truncated to a size of precisely length bytes. + * @param fd the file descriptor. + * @param length the length to be truncated. + * + * @return Upon successful completion, ftruncate() shall return 0; + * otherwise, -1 shall be returned and errno set to indicate the error. + */ +int ftruncate(int fd, off_t length) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + if (length < 0) + { + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + result = dfs_file_ftruncate(d, length); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return 0; +} +RTM_EXPORT(ftruncate); + +/** + * this function is a POSIX compliant version, which will return the + * information about a mounted file system. + * + * @param path the path which mounted file system. + * @param buf the buffer to save the returned information. + * + * @return 0 on successful, others on failed. + */ +int statfs(const char *path, struct statfs *buf) +{ + int result; + + result = dfs_statfs(path, buf); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return result; +} +RTM_EXPORT(statfs); + +/** + * this function is a POSIX compliant version, which will make a directory + * + * @param path the directory path to be made. + * @param mode + * + * @return 0 on successful, others on failed. + */ +int mkdir(const char *path, mode_t mode) +{ + int fd; + struct dfs_fd *d; + int result; + + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-ENOMEM); + + return -1; + } + + d = fd_get(fd); + + result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT); + + if (result < 0) + { + fd_put(d); + fd_put(d); + rt_set_errno(result); + + return -1; + } + + dfs_file_close(d); + fd_put(d); + fd_put(d); + + return 0; +} +RTM_EXPORT(mkdir); + +/** + * this function is a POSIX compliant version, which will remove a directory. + * + * @param pathname the path name to be removed. + * + * @return 0 on successful, others on failed. + */ +int rmdir(const char *pathname) +{ + int result; + + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + + return 0; +} +RTM_EXPORT(rmdir); + +/** + * this function is a POSIX compliant version, which will open a directory. + * + * @param name the path name to be open. + * + * @return the DIR pointer of directory, NULL on open directory failed. + */ +DIR *opendir(const char *name) +{ + struct dfs_fd *d; + int fd, result; + DIR *t; + + t = NULL; + + /* allocate a fd */ + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-ENOMEM); + + return NULL; + } + d = fd_get(fd); + + result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY); + if (result >= 0) + { + /* open successfully */ + t = (DIR *) rt_malloc(sizeof(DIR)); + if (t == NULL) + { + dfs_file_close(d); + fd_put(d); + } + else + { + memset(t, 0, sizeof(DIR)); + + t->fd = fd; + } + fd_put(d); + + return t; + } + + /* open failed */ + fd_put(d); + fd_put(d); + rt_set_errno(result); + + return NULL; +} +RTM_EXPORT(opendir); + +/** + * this function is a POSIX compliant version, which will return a pointer + * to a dirent structure representing the next directory entry in the + * directory stream. + * + * @param d the directory stream pointer. + * + * @return the next directory entry, NULL on the end of directory or failed. + */ +struct dirent *readdir(DIR *d) +{ + int result; + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + return NULL; + } + + if (d->num) + { + struct dirent *dirent_ptr; + dirent_ptr = (struct dirent *)&d->buf[d->cur]; + d->cur += dirent_ptr->d_reclen; + } + + if (!d->num || d->cur >= d->num) + { + /* get a new entry */ + result = dfs_file_getdents(fd, + (struct dirent *)d->buf, + sizeof(d->buf) - 1); + if (result <= 0) + { + fd_put(fd); + rt_set_errno(result); + + return NULL; + } + + d->num = result; + d->cur = 0; /* current entry index */ + } + + fd_put(fd); + + return (struct dirent *)(d->buf + d->cur); +} +RTM_EXPORT(readdir); + +/** + * this function is a POSIX compliant version, which will return current + * location in directory stream. + * + * @param d the directory stream pointer. + * + * @return the current location in directory stream. + */ +long telldir(DIR *d) +{ + struct dfs_fd *fd; + long result; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return 0; + } + + result = fd->pos - d->num + d->cur; + fd_put(fd); + + return result; +} +RTM_EXPORT(telldir); + +/** + * this function is a POSIX compliant version, which will set position of + * next directory structure in the directory stream. + * + * @param d the directory stream. + * @param offset the offset in directory stream. + */ +void seekdir(DIR *d, off_t offset) +{ + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return ; + } + + /* seek to the offset position of directory */ + if (dfs_file_lseek(fd, offset) >= 0) + d->num = d->cur = 0; + fd_put(fd); +} +RTM_EXPORT(seekdir); + +/** + * this function is a POSIX compliant version, which will reset directory + * stream. + * + * @param d the directory stream. + */ +void rewinddir(DIR *d) +{ + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return ; + } + + /* seek to the beginning of directory */ + if (dfs_file_lseek(fd, 0) >= 0) + d->num = d->cur = 0; + fd_put(fd); +} +RTM_EXPORT(rewinddir); + +/** + * this function is a POSIX compliant version, which will close a directory + * stream. + * + * @param d the directory stream. + * + * @return 0 on successful, -1 on failed. + */ +int closedir(DIR *d) +{ + int result; + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + result = dfs_file_close(fd); + fd_put(fd); + + fd_put(fd); + rt_free(d); + + if (result < 0) + { + rt_set_errno(result); + + return -1; + } + else + return 0; +} +RTM_EXPORT(closedir); + +#ifdef DFS_USING_WORKDIR +/** + * this function is a POSIX compliant version, which will change working + * directory. + * + * @param path the path name to be changed to. + * + * @return 0 on successful, -1 on failed. + */ +int chdir(const char *path) +{ + char *fullpath; + DIR *d; + + if (path == NULL) + { + dfs_lock(); + rt_kprintf("%s\n", working_directory); + dfs_unlock(); + + return 0; + } + + if (strlen(path) > DFS_PATH_MAX) + { + rt_set_errno(-ENOTDIR); + + return -1; + } + + fullpath = dfs_normalize_path(NULL, path); + if (fullpath == NULL) + { + rt_set_errno(-ENOTDIR); + + return -1; /* build path failed */ + } + + dfs_lock(); + d = opendir(fullpath); + if (d == NULL) + { + rt_free(fullpath); + /* this is a not exist directory */ + dfs_unlock(); + + return -1; + } + + /* close directory stream */ + closedir(d); + + /* copy full path to working directory */ + strncpy(working_directory, fullpath, DFS_PATH_MAX); + /* release normalize directory path name */ + rt_free(fullpath); + + dfs_unlock(); + + return 0; +} +RTM_EXPORT(chdir); + +#ifdef RT_USING_FINSH +FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory); +#endif +#endif + +/** + * this function is a POSIX compliant version, which shall check the file named + * by the pathname pointed to by the path argument for accessibility according + * to the bit pattern contained in amode. + * + * @param path the specified file/dir path. + * @param amode the value is either the bitwise-inclusive OR of the access + * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK). + */ +int access(const char *path, int amode) +{ + struct stat sb; + if (stat(path, &sb) < 0) + return -1; /* already sets errno */ + + /* ignore R_OK,W_OK,X_OK condition */ + return 0; +} + +/** + * this function is a POSIX compliant version, which will return current + * working directory. + * + * @param buf the returned current directory. + * @param size the buffer size. + * + * @return the returned current directory. + */ +char *getcwd(char *buf, size_t size) +{ +#ifdef DFS_USING_WORKDIR + dfs_lock(); + strncpy(buf, working_directory, size); + dfs_unlock(); +#else + rt_kprintf(NO_WORKING_DIR); +#endif + + return buf; +} +RTM_EXPORT(getcwd); + +#ifdef RT_USING_POSIX_TERMIOS +#include "termios.h" +int isatty(int fd) +{ + struct termios ts; + return(tcgetattr(fd, &ts) != -1); /*true if no error (is a tty)*/ +} +#else +int isatty(int fd) +{ + if (fd >=0 && fd < 3) + { + return 1; + } + else + { + return 0; + } +} +#endif +RTM_EXPORT(isatty); + +char *ttyname(int fd) +{ + return "/dev/tty"; /* TODO: need to add more specific */ +} +RTM_EXPORT(ttyname); + +unsigned int sleep(unsigned int seconds) +{ + if (rt_thread_self() != RT_NULL) + { + ssleep(seconds); + } + else /* scheduler has not run yet */ + { + while(seconds > 0) + { + udelay(1000000u); + seconds --; + } + } + + return 0; +} +RTM_EXPORT(sleep); + +int usleep(useconds_t usec) +{ + if (rt_thread_self() != RT_NULL) + { + msleep(usec / 1000u); + } + else /* scheduler has not run yet */ + { + udelay(usec / 1000u); + } + udelay(usec % 1000u); + + return 0; +} +RTM_EXPORT(usleep); + +pid_t gettid(void) +{ + /*TODO*/ + return 0; +} + +pid_t getpid(void) +{ + return gettid(); +} +RTM_EXPORT(getpid); + +pid_t getppid(void) +{ + return 0; +} +RTM_EXPORT(getppid); + +uid_t getuid(void) +{ + return 0; /*ROOT*/ +} +RTM_EXPORT(getuid); + +uid_t geteuid(void) +{ + return 0; /*ROOT*/ +} +RTM_EXPORT(geteuid); + +gid_t getgid(void) +{ + return 0; /*ROOT*/ +} +RTM_EXPORT(getgid); + +gid_t getegid(void) +{ + return 0; /*ROOT*/ +} +RTM_EXPORT(getegid); diff --git a/components/libc/termios/SConscript b/components/libc/posix/termios/SConscript index 381eb5ad8b..bd9a528461 100644 --- a/components/libc/termios/SConscript +++ b/components/libc/posix/termios/SConscript @@ -6,7 +6,7 @@ cwd = GetCurrentDir() src = Glob('*.c') + Glob('*.cpp') CPPPATH = [cwd] -group = DefineGroup('libc', src, +group = DefineGroup('POSIX', src, depend = ['RT_USING_LIBC', 'RT_USING_POSIX', 'RT_USING_POSIX_TERMIOS'], CPPPATH = CPPPATH) diff --git a/components/libc/termios/posix_termios.c b/components/libc/posix/termios/posix_termios.c index 9f33c857e6..9f33c857e6 100644 --- a/components/libc/termios/posix_termios.c +++ b/components/libc/posix/termios/posix_termios.c diff --git a/components/libc/termios/posix_termios.h b/components/libc/posix/termios/posix_termios.h index 6dc5531955..6dc5531955 100644 --- a/components/libc/termios/posix_termios.h +++ b/components/libc/posix/termios/posix_termios.h |
