Просмотр исходного кода

[libc] implement extension standard C functions (#6044)

* implement extension standard C functions
Man, Jianting (Meco) 2 лет назад
Родитель
Сommit
8bee7a0c23
28 измененных файлов с 836 добавлено и 73 удалено
  1. 1 1
      components/libc/compilers/common/SConscript
  2. 25 0
      components/libc/compilers/common/cctype.c
  3. 85 0
      components/libc/compilers/common/cstdio.c
  4. 144 0
      components/libc/compilers/common/cstdlib.c
  5. 223 0
      components/libc/compilers/common/cstring.c
  6. 40 9
      components/libc/compilers/common/ctime.c
  7. 140 0
      components/libc/compilers/common/cwchar.c
  8. 4 4
      components/libc/compilers/common/extension/fcntl/msvc/fcntl.h
  9. 1 1
      components/libc/compilers/common/extension/fcntl/octal/fcntl.h
  10. 1 1
      components/libc/compilers/common/extension/sys/errno.h
  11. 1 1
      components/libc/compilers/common/extension/sys/stat.h
  12. 1 1
      components/libc/compilers/common/extension/sys/types.h
  13. 1 1
      components/libc/compilers/common/include/compiler_private.h
  14. 1 1
      components/libc/compilers/common/include/dirent.h
  15. 31 0
      components/libc/compilers/common/include/posix/ctype.h
  16. 28 0
      components/libc/compilers/common/include/posix/stdio.h
  17. 32 0
      components/libc/compilers/common/include/posix/stdlib.h
  18. 43 0
      components/libc/compilers/common/include/posix/string.h
  19. 27 0
      components/libc/compilers/common/include/posix/wchar.h
  20. 1 1
      components/libc/compilers/common/include/sys/ioctl.h
  21. 1 1
      components/libc/compilers/common/include/sys/select.h
  22. 1 1
      components/libc/compilers/common/include/sys/signal.h
  23. 1 1
      components/libc/compilers/common/include/sys/statfs.h
  24. 0 0
      components/libc/compilers/common/include/sys/time.h
  25. 1 1
      components/libc/compilers/common/include/sys/unistd.h
  26. 1 1
      components/libc/compilers/common/include/sys/vfs.h
  27. 1 1
      components/libc/compilers/common/include/unistd.h
  28. 0 46
      components/libc/compilers/common/stdlib.c

+ 1 - 1
components/libc/compilers/common/SConscript

@@ -4,7 +4,7 @@ Import('rtconfig')
 src   = []
 cwd   = GetCurrentDir()
 group = []
-CPPPATH = [cwd]
+CPPPATH = [cwd + '/include']
 CPPDEFINES = []
 
 if rtconfig.PLATFORM in ['armcc', 'armclang']:

+ 25 - 0
components/libc/compilers/common/cctype.c

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-06-07     Meco Man     The first version.
+ */
+
+#include "posix/ctype.h"
+
+#ifndef isascii /* some toolchain use macro to define it */
+int isascii(int c)
+{
+    return c >= 0x00 && c <= 0x7f;
+}
+#endif
+
+#ifndef toascii
+int toascii(int c)
+{
+    return (c)&0177;
+}
+#endif

+ 85 - 0
components/libc/compilers/common/cstdio.c

@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2014-05-22     ivanrad      implement getline
+ */
+
+#include "posix/stdio.h"
+#include <stdlib.h>
+#include <limits.h>
+#include <sys/errno.h>
+
+ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
+{
+    char *cur_pos, *new_lineptr;
+    size_t new_lineptr_len;
+    int c;
+
+    if (lineptr == NULL || n == NULL || stream == NULL)
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    if (*lineptr == NULL)
+    {
+        *n = 128; /* init len */
+        if ((*lineptr = (char *)malloc(*n)) == NULL)
+        {
+            errno = ENOMEM;
+            return -1;
+        }
+    }
+
+    cur_pos = *lineptr;
+    for (;;)
+    {
+        c = getc(stream);
+
+        if (ferror(stream) || (c == EOF && cur_pos == *lineptr))
+            return -1;
+
+        if (c == EOF)
+            break;
+
+        if ((*lineptr + *n - cur_pos) < 2)
+        {
+            if (LONG_MAX / 2 < *n)
+            {
+#ifdef EOVERFLOW
+                errno = EOVERFLOW;
+#else
+                errno = ERANGE; /* no EOVERFLOW defined */
+#endif
+                return -1;
+            }
+            new_lineptr_len = *n * 2;
+
+            if ((new_lineptr = (char *)realloc(*lineptr, new_lineptr_len)) == NULL)
+            {
+                errno = ENOMEM;
+                return -1;
+            }
+            cur_pos = new_lineptr + (cur_pos - *lineptr);
+            *lineptr = new_lineptr;
+            *n = new_lineptr_len;
+        }
+
+        *cur_pos++ = (char)c;
+
+        if (c == delim)
+            break;
+    }
+
+    *cur_pos = '\0';
+    return (ssize_t)(cur_pos - *lineptr);
+}
+
+ssize_t getline(char **lineptr, size_t *n, FILE *stream)
+{
+    return getdelim(lineptr, n, '\n', stream);
+}

+ 144 - 0
components/libc/compilers/common/cstdlib.c

@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-02-15     Meco Man     first version
+ */
+
+#include <rtthread.h>
+
+#define DBG_TAG    "stdlib"
+#define DBG_LVL    DBG_INFO
+#include <rtdbg.h>
+
+void __rt_libc_exit(int status)
+{
+    rt_thread_t self = rt_thread_self();
+
+    if (self != RT_NULL)
+    {
+#ifdef RT_USING_PTHREADS
+        extern void pthread_exit(void *value);
+        pthread_exit((void *)status);
+#else
+        LOG_E("thread:%s exit:%d!", self->name, status);
+        rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
+#endif
+    }
+}
+
+#ifdef RT_USING_MSH
+int system(const char *command)
+{
+    extern int msh_exec(char *cmd, rt_size_t length);
+
+    if (command)
+    {
+        msh_exec((char *)command, rt_strlen(command));
+    }
+
+    return 0;
+}
+RTM_EXPORT(system);
+#endif /* RT_USING_MSH */
+
+char *ltoa(long value, char *string, int radix)
+{
+    char tmp[33];
+    char *tp = tmp;
+    long i;
+    unsigned long v;
+    int sign;
+    char *sp;
+
+    if (string == NULL)
+    {
+        return 0 ;
+    }
+
+    if (radix > 36 || radix <= 1)
+    {
+        return 0 ;
+    }
+
+    sign = (radix == 10 && value < 0);
+    if (sign)
+    {
+        v = -value;
+    }
+    else
+    {
+        v = (unsigned long)value;
+    }
+
+    while (v || tp == tmp)
+    {
+        i = v % radix;
+        v = v / radix;
+        if (i < 10)
+            *tp++ = i+'0';
+        else
+            *tp++ = i + 'a' - 10;
+    }
+
+    sp = string;
+
+    if (sign)
+        *sp++ = '-';
+    while (tp > tmp)
+        *sp++ = *--tp;
+    *sp = 0;
+
+    return string;
+}
+
+char *itoa(int value, char *string, int radix)
+{
+    return ltoa(value, string, radix) ;
+}
+
+
+char *ultoa(unsigned long value, char *string, int radix)
+{
+    char tmp[33];
+    char *tp = tmp;
+    long i;
+    unsigned long v = value;
+    char *sp;
+
+    if (string == NULL)
+    {
+        return 0;
+    }
+
+    if (radix > 36 || radix <= 1)
+    {
+        return 0;
+    }
+
+    while (v || tp == tmp)
+    {
+        i = v % radix;
+        v = v / radix;
+        if (i < 10)
+            *tp++ = i+'0';
+        else
+            *tp++ = i + 'a' - 10;
+    }
+
+    sp = string;
+
+    while (tp > tmp)
+        *sp++ = *--tp;
+    *sp = 0;
+
+    return string;
+}
+
+char *utoa(unsigned value, char *string, int radix)
+{
+    return ultoa(value, string, radix) ;
+}

+ 223 - 0
components/libc/compilers/common/cstring.c

@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-01-12     Meco Man     The first version.
+ */
+
+#include "posix/string.h"
+#include <ctype.h>
+#include <rtthread.h>
+#include <stdlib.h>
+
+/**
+ * @brief erases the data in the n bytes of the memory starting at the
+ *        location pointed to by s, by writing zeros (bytes containing '\0') to that area.
+ *
+ * @note  The bzero() function is deprecated (marked as LEGACY in POSIX. 1-2001).
+ */
+void bzero(void* s, size_t n)
+{
+    rt_memset(s, 0, n);
+}
+
+void bcopy(const void* src, void* dest, size_t n)
+{
+    rt_memcpy(dest, src, n);
+}
+
+int bcmp(const void* s1, const void* s2, size_t n)
+{
+    return rt_memcmp(s1, s2, n);
+}
+
+void explicit_bzero(void* s, size_t n)
+{
+    volatile char* vs = (volatile char*)s;
+    while (n)
+    {
+        *vs++ = 0;
+        n--;
+    }
+}
+
+char* index(const char* s, int c)
+{
+    return strchr(s, c);
+}
+
+char* rindex(const char* s, int c)
+{
+    return strrchr(s, c);
+}
+
+int ffs(int i)
+{
+    int bit;
+
+    if (0 == i)
+        return 0;
+
+    for (bit = 1; !(i & 1); ++bit)
+        i >>= 1;
+    return bit;
+}
+
+int ffsl(long i)
+{
+    int bit;
+
+    if (0 == i)
+        return 0;
+
+    for (bit = 1; !(i & 1); ++bit)
+        i >>= 1;
+    return bit;
+}
+
+int ffsll(long long i)
+{
+    int bit;
+
+    if (0 == i)
+        return 0;
+
+    for (bit = 1; !(i & 1); ++bit)
+        i >>= 1;
+    return bit;
+}
+
+/**
+ * @brief The memchr() function scans the initial n bytes of the memory area pointed to
+ *        by s for the first instance of c. Both c and the bytes of the memory area
+ *        pointed to by s are interpreted as unsigned char.
+ *
+ * @note  This function is GNU extension, available since glibc 2.1.91.
+ */
+void* memrchr(const void* ptr, int ch, size_t pos)
+{
+    char* end = (char*)ptr + pos - 1;
+    while (end != ptr)
+    {
+        if (*end == ch)
+            return end;
+        end--;
+    }
+    return (*end == ch) ? (end) : (NULL);
+}
+
+size_t strnlen(const char *s, size_t maxlen)
+{
+    const char *sc;
+    for (sc = s; maxlen != 0 && *sc != '\0'; maxlen--, ++sc);
+    return sc - s;
+}
+
+char* strchrnul(const char* s, int c)
+{
+    while (*s != '\0' && *s != c)
+        s++;
+    return (char*)s;
+}
+
+int strcasecmp(const char* s1, const char* s2)
+{
+    const unsigned char* u1 = (const unsigned char*)s1;
+    const unsigned char* u2 = (const unsigned char*)s2;
+    int result;
+
+    while ((result = tolower(*u1) - tolower(*u2)) == 0 && *u1 != 0)
+    {
+        u1++;
+        u2++;
+    }
+
+    return result;
+}
+
+int strncasecmp(const char* s1, const char* s2, size_t n)
+{
+    const unsigned char* u1 = (const unsigned char*)s1;
+    const unsigned char* u2 = (const unsigned char*)s2;
+    int result;
+
+    for (; n != 0; n--)
+    {
+        result = tolower(*u1) - tolower(*u2);
+        if (result)
+            return result;
+        if (*u1 == 0)
+            return 0;
+        u1++;
+        u2++;
+    }
+    return 0;
+}
+
+char *strdup(const char *s)
+{
+    char *news = (char *)malloc(strlen(s) + 1);
+
+    if (news)
+    {
+        strcpy(news, s);
+    }
+
+    return news;
+}
+
+char *strndup(const char *s, size_t size)
+{
+    size_t nsize = strnlen(s, size);
+    char *news = (char *)malloc(nsize + 1);
+    if (news)
+    {
+        rt_memcpy(news, s, nsize);
+        news[nsize] = '\0';
+    }
+
+    return news;
+}
+
+char *strtok_r(char *str, const char *delim, char **saveptr)
+{
+    char *pbegin;
+    char *pend = NULL;
+
+    if (str)
+    {
+        pbegin = str;
+    }
+    else if (saveptr && *saveptr)
+    {
+        pbegin = *saveptr;
+    }
+    else
+    {
+        return NULL;
+    }
+
+    for (;*pbegin && strchr(delim, *pbegin) != NULL; pbegin++);
+
+    if (!*pbegin)
+    {
+        return NULL;
+    }
+
+    for (pend = pbegin + 1; *pend && strchr(delim, *pend) == NULL; pend++);
+
+    if (*pend)
+    {
+        *pend++ = '\0';
+    }
+
+    if (saveptr)
+    {
+        *saveptr = pend;
+    }
+
+    return pbegin;
+}

+ 40 - 9
components/libc/compilers/common/time.c → components/libc/compilers/common/ctime.c

@@ -173,6 +173,15 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r)
 {
     time_t i;
     time_t work = *timep % (SPD);
+
+    if(timep == RT_NULL || r == RT_NULL)
+    {
+        rt_set_errno(EFAULT);
+        return RT_NULL;
+    }
+
+    rt_memset(r, RT_NULL, sizeof(struct tm));
+
     r->tm_sec = work % 60;
     work /= 60;
     r->tm_min = work % 60;
@@ -198,8 +207,8 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r)
         work -= 1;
     }
 
-    for (i = 11; i && (__spm[i] > work); --i)
-        ;
+    for (i = 11; i && (__spm[i] > work); --i);
+
     r->tm_mon = i;
     r->tm_mday += work - __spm[i];
 
@@ -243,6 +252,14 @@ RTM_EXPORT(mktime);
 
 char* asctime_r(const struct tm *t, char *buf)
 {
+    if(t == RT_NULL || buf == RT_NULL)
+    {
+        rt_set_errno(EFAULT);
+        return RT_NULL;
+    }
+
+    rt_memset(buf, RT_NULL, 26);
+
     /* Checking input validity */
     if ((int)rt_strlen(days) <= (t->tm_wday << 2) || (int)rt_strlen(months) <= (t->tm_mon << 2))
     {
@@ -354,7 +371,7 @@ int stime(const time_t *t)
 {
     struct timeval tv;
 
-    if (!t)
+    if (t == RT_NULL)
     {
         rt_set_errno(EFAULT);
         return -1;
@@ -377,8 +394,15 @@ time_t timegm(struct tm * const t)
 {
     time_t day;
     time_t i;
-    time_t years = (time_t)t->tm_year - 70;
+    time_t years;
+
+    if(t == RT_NULL)
+    {
+        rt_set_errno(EFAULT);
+        return (time_t)-1;
+    }
 
+    years = (time_t)t->tm_year - 70;
     if (t->tm_sec > 60)         /* seconds after the minute - [0, 60] including leap second */
     {
         t->tm_min += t->tm_sec / 60;
@@ -415,7 +439,10 @@ time_t timegm(struct tm * const t)
     }
 
     if (t->tm_year < 70)
-        return (time_t) - 1;
+    {
+        rt_set_errno(EINVAL);
+        return (time_t) -1;
+    }
 
     /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
     day = years * 365 + (years + 1) / 4;
@@ -467,7 +494,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
     }
     else
     {
-        rt_set_errno(EFAULT);
+        rt_set_errno(EINVAL);
         return -1;
     }
 }
@@ -549,7 +576,7 @@ int clock_getres(clockid_t clockid, struct timespec *res)
 
     if (res == RT_NULL)
     {
-        rt_set_errno(EINVAL);
+        rt_set_errno(EFAULT);
         return -1;
     }
 
@@ -568,6 +595,8 @@ int clock_getres(clockid_t clockid, struct timespec *res)
 #endif
 
     default:
+        res->tv_sec  = 0;
+        res->tv_nsec = 0;
         ret = -1;
         rt_set_errno(EINVAL);
         break;
@@ -588,7 +617,7 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
 
     if (tp == RT_NULL)
     {
-        rt_set_errno(EINVAL);
+        rt_set_errno(EFAULT);
         return -1;
     }
 
@@ -622,6 +651,8 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
         break;
 #endif
     default:
+        tp->tv_sec  = 0;
+        tp->tv_nsec = 0;
         rt_set_errno(EINVAL);
         ret = -1;
     }
@@ -655,7 +686,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
 
     if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
     {
-        rt_set_errno(EINVAL);
+        rt_set_errno(EFAULT);
         return -1;
     }
 

+ 140 - 0
components/libc/compilers/common/cwchar.c

@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2014-01-01     mattn        implement wcwidth
+ */
+
+#include "posix/wchar.h"
+#include <string.h>
+
+struct interval
+{
+    long first;
+    long last;
+};
+
+static int bisearch(wchar_t ucs, const struct interval *table, int max)
+{
+    int min = 0;
+    int mid;
+
+    if (ucs < table[0].first || ucs > table[max].last)
+    {
+        return 0;
+    }
+
+    while (max >= min)
+    {
+        mid = (min + max) / 2;
+        if (ucs > table[mid].last)
+        {
+            min = mid + 1;
+        }
+        else if (ucs < table[mid].first)
+        {
+            max = mid - 1;
+        }
+        else
+        {
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
+int wcwidth(wchar_t ucs)
+{
+    /* sorted list of non-overlapping intervals of non-spacing characters */
+    static const struct interval combining[] = {
+        { 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
+        { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
+        { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
+        { 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
+        { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
+        { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
+        { 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
+        { 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
+        { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
+        { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
+        { 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
+        { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
+        { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
+        { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
+        { 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
+        { 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
+        { 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
+        { 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
+        { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
+        { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
+        { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
+        { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
+        { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
+        { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
+        { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
+        { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
+        { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
+        { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
+        { 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
+        { 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
+        { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
+        { 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
+        { 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
+        { 0xFFF9, 0xFFFB }
+    };
+
+    /* test for 8-bit control characters */
+    if (ucs == 0)
+    {
+        return 0;
+    }
+    if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
+    {
+        return -1;
+    }
+
+    /* binary search in table of non-spacing characters */
+    if (bisearch(ucs, combining, sizeof(combining) / sizeof(struct interval) - 1))
+    {
+        return 0;
+    }
+
+    return 1 +
+        (ucs >= 0x1100 &&
+        (ucs <= 0x115f ||                    /* Hangul Jamo init. consonants */
+        (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
+        ucs != 0x303f) ||                  /* CJK ... Yi */
+        (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
+        (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
+        (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
+        (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
+        (ucs >= 0xffe0 && ucs <= 0xffe6) // ||
+    //#ifndef _WIN32
+    //      (ucs >= 0x20000 && ucs <= 0x2ffff)
+    //#else
+    //    0
+    //#endif
+        ));
+}
+
+int wcswidth(const wchar_t *pwcs, size_t n)
+{
+    int w, width = 0;
+
+    for (;*pwcs && n-- > 0; pwcs++)
+    {
+        if ((w = wcwidth(*pwcs)) < 0)
+        {
+            return -1;
+        }
+        else
+        {
+            width += w;
+        }
+    }
+    return width;
+}

+ 4 - 4
components/libc/compilers/common/extension/fcntl/msvc/fcntl.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -39,9 +39,9 @@ extern "C" {
 #define O_RANDOM        0x0010  /* file access is primarily random */
 
 /* extension */
-#define	O_ACCMODE		0x0003	/* mask for above modes, from 4.4BSD https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/include/sys/fcntl.h */
-#define O_NONBLOCK		0x0004	/* non blocking I/O, from BSD apple https://opensource.apple.com/source/xnu/xnu-1228.0.2/bsd/sys/fcntl.h */
-#define O_DIRECTORY		0x200000 /* from Newlib */
+#define O_ACCMODE       0x0003  /* mask for above modes, from 4.4BSD https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/include/sys/fcntl.h */
+#define O_NONBLOCK      0x0004  /* non blocking I/O, from BSD apple https://opensource.apple.com/source/xnu/xnu-1228.0.2/bsd/sys/fcntl.h */
+#define O_DIRECTORY     0x200000 /* from Newlib */
 
 #define F_DUPFD  0
 #define F_GETFD  1

+ 1 - 1
components/libc/compilers/common/extension/fcntl/octal/fcntl.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/extension/sys/errno.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/extension/sys/stat.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/extension/sys/types.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/compiler_private.h → components/libc/compilers/common/include/compiler_private.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/dirent.h → components/libc/compilers/common/include/dirent.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 31 - 0
components/libc/compilers/common/include/posix/ctype.h

@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-06-07     Meco Man     The first version.
+ */
+
+#ifndef __POSIX_CTYPE_H__
+#define __POSIX_CTYPE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <ctype.h>
+
+#ifndef isascii /* some toolchain use macro to define it */
+int isascii(int c);
+#endif
+#ifndef toascii
+int toascii(int c);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __POSIX_CTYPE_H__ */

+ 28 - 0
components/libc/compilers/common/include/posix/stdio.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-06-07     Meco Man     first version
+ */
+
+#ifndef __POSIX_STDIO_H__
+#define __POSIX_STDIO_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+#include <sys/types.h>
+
+ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
+ssize_t getline(char **lineptr, size_t *n, FILE *stream);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __POSIX_STDIO_H__ */

+ 32 - 0
components/libc/compilers/common/include/posix/stdlib.h

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-06-07     Meco Man     The first version.
+ */
+
+#ifndef __POSIX_STDLIB_H__
+#define __POSIX_STDLIB_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stdlib.h>
+
+char *itoa(int n, char *buffer, int radix);
+char *lltoa(int64_t ll, char *buffer, int radix);
+char *ltoa(long l, char *buffer, int radix);
+char *ulltoa(uint64_t ll, char *buffer, int radix);
+char *ultoa(unsigned long l, char *buffer, int radix);
+char *utoa(unsigned int n, char *buffer, int radix);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __POSIX_STDLIB_H__ */

+ 43 - 0
components/libc/compilers/common/include/posix/string.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-01-12     Meco Man     The first version.
+ */
+
+#ifndef __POSIX_STRING_H__
+#define __POSIX_STRING_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+#include <string.h>
+
+void bzero(void * s, size_t n);
+void bcopy(const void * src, void * dest, size_t n);
+int bcmp(const void * s1, const void * s2, size_t n);
+void explicit_bzero(void * s, size_t n);
+char *index(const char * s, int c);
+char *rindex(const char * s, int c);
+int ffs(int i);
+int ffsl(long i);
+int ffsll(long long i);
+void *memrchr(const void* ptr, int ch, size_t pos);
+size_t strnlen(const char *s, size_t maxlen);
+char* strchrnul(const char *s, int c);
+int strcasecmp(const char * s1, const char * s2);
+int strncasecmp(const char * s1, const char * s2, size_t n);
+char *strdup(const char *s);
+char *strndup(const char *s, size_t size);
+char *strtok_r(char *str, const char *delim, char **saveptr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __POSIX_STRING_H__ */

+ 27 - 0
components/libc/compilers/common/include/posix/wchar.h

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-06-07     Meco Man     The first version
+ */
+
+#ifndef __POSIX_WCHAR_H__
+#define __POSIX_WCHAR_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <wchar.h>
+
+int wcwidth(wchar_t);
+int wcswidth(const wchar_t*, size_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __POSIX_WCHAR_H__ */

+ 1 - 1
components/libc/compilers/common/sys/ioctl.h → components/libc/compilers/common/include/sys/ioctl.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/sys/select.h → components/libc/compilers/common/include/sys/select.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/sys/signal.h → components/libc/compilers/common/include/sys/signal.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/sys/statfs.h → components/libc/compilers/common/include/sys/statfs.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 0 - 0
components/libc/compilers/common/sys/time.h → components/libc/compilers/common/include/sys/time.h


+ 1 - 1
components/libc/compilers/common/sys/unistd.h → components/libc/compilers/common/include/sys/unistd.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/sys/vfs.h → components/libc/compilers/common/include/sys/vfs.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 1 - 1
components/libc/compilers/common/unistd.h → components/libc/compilers/common/include/unistd.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *

+ 0 - 46
components/libc/compilers/common/stdlib.c

@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date           Author       Notes
- * 2021-02-15     Meco Man     first version
- */
-
-#include <rtthread.h>
-
-#define DBG_TAG    "stdlib"
-#define DBG_LVL    DBG_INFO
-#include <rtdbg.h>
-
-void __rt_libc_exit(int status)
-{
-    rt_thread_t self = rt_thread_self();
-
-    if (self != RT_NULL)
-    {
-#ifdef RT_USING_PTHREADS
-        extern void pthread_exit(void *value);
-        pthread_exit((void *)status);
-#else
-        LOG_E("thread:%s exit:%d!", self->name, status);
-        rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);
-#endif
-    }
-}
-
-#ifdef RT_USING_MSH
-int system(const char *command)
-{
-    extern int msh_exec(char *cmd, rt_size_t length);
-
-    if (command)
-    {
-        msh_exec((char *)command, rt_strlen(command));
-    }
-
-    return 0;
-}
-RTM_EXPORT(system);
-#endif /* RT_USING_MSH */