Skip to content

Commit d20d0b1

Browse files
committed
tools/nolibc: implement strerror()
strerror() is commonly used. For example in kselftest which currently needs to do an #ifdef NOLIBC to handle the lack of strerror(). Keep it simple and reuse the output format of perror() for strerror(). Acked-by: Shuah Khan <[email protected]> Signed-off-by: Thomas Weißschuh <[email protected]>
1 parent 582facf commit d20d0b1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

tools/include/nolibc/stdio.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ int setvbuf(FILE *stream __attribute__((unused)),
376376
return 0;
377377
}
378378

379+
static __attribute__((unused))
380+
const char *strerror(int errno)
381+
{
382+
static char buf[18] = "errno=";
383+
384+
i64toa_r(errno, &buf[6]);
385+
386+
return buf;
387+
}
388+
379389
/* make sure to include all global symbols */
380390
#include "nolibc.h"
381391

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,10 @@ int run_stdlib(int min, int max)
12101210
CASE_TEST(strtol_underflow); EXPECT_STRTOX(1, strtol, "-0x8000000000000001", 16, LONG_MIN, -1, ERANGE); break;
12111211
CASE_TEST(strtoul_negative); EXPECT_STRTOX(1, strtoul, "-0x1", 16, ULONG_MAX, 4, 0); break;
12121212
CASE_TEST(strtoul_overflow); EXPECT_STRTOX(1, strtoul, "0x10000000000000000", 16, ULONG_MAX, -1, ERANGE); break;
1213+
CASE_TEST(strerror_success); EXPECT_STREQ(is_nolibc, strerror(0), "errno=0"); break;
1214+
CASE_TEST(strerror_EINVAL); EXPECT_STREQ(is_nolibc, strerror(EINVAL), "errno=22"); break;
1215+
CASE_TEST(strerror_int_max); EXPECT_STREQ(is_nolibc, strerror(INT_MAX), "errno=2147483647"); break;
1216+
CASE_TEST(strerror_int_min); EXPECT_STREQ(is_nolibc, strerror(INT_MIN), "errno=-2147483648"); break;
12131217

12141218
case __LINE__:
12151219
return ret; /* must be last */

0 commit comments

Comments
 (0)