Skip to content

Commit 7273233

Browse files
committed
added perror and strerror along with autotests for them
1 parent f92e4d0 commit 7273233

File tree

9 files changed

+186
-0
lines changed

9 files changed

+186
-0
lines changed

src/libc/errno_str.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <assert.h>
2+
#include <errno.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
int _ti_sprintf(
7+
char *__restrict buffer, const char *__restrict format, ...
8+
) __attribute__ ((format (__printf__, 2, 3)));
9+
10+
static char const * const errno_strings[] = {
11+
"no error",
12+
"permission error",
13+
"invalid argument",
14+
"io error",
15+
"math domain error",
16+
"math range error",
17+
};
18+
19+
static char * const unknown_errno_string = "unknown error -8388608";
20+
21+
#define unknown_errno_number_offset 14
22+
#if 0
23+
/* static_assert on a string is a Clang 16.0.0 extension */
24+
static_assert(
25+
unknown_errno_string[unknown_errno_number_offset + 0] == '-' &&
26+
unknown_errno_string[unknown_errno_number_offset + 8] == '\0',
27+
"the string for unknown errno numbers has been changed"
28+
);
29+
#endif
30+
31+
#define errno_strings_count (sizeof(errno_strings) / sizeof(errno_strings[0]))
32+
33+
char* strerror(int errnum) {
34+
if ((unsigned int)errnum >= errno_strings_count) {
35+
_ti_sprintf(&(unknown_errno_string[unknown_errno_number_offset]), "%d", errnum);
36+
return (char*)unknown_errno_string;
37+
}
38+
return (char*)errno_strings[errnum];
39+
}
40+
41+
#if 0
42+
/** disabled until the prototypes for this function are defined */
43+
size_t strerrorlen_s(errno_t errnum) {
44+
return strlen(strerror(errnum));
45+
}
46+
#endif
47+
48+
void perror(const char *str) {
49+
if (str != NULL && *str != '\0') {
50+
fputs(str, stderr);
51+
fputc(':', stderr);
52+
fputc(' ', stderr);
53+
}
54+
fputs(strerror(errno), stderr);
55+
fputc('\n', stderr);
56+
}

src/libc/include/stdio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ int sprintf(char *__restrict buffer,
104104
const char *__restrict format, ...)
105105
__attribute__ ((format (__printf__, 2, 3)));
106106

107+
void perror(const char* str);
108+
107109
__END_DECLS
108110

109111
#endif /* _STDIO_H */

src/libc/include/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int strcasecmp(const char *s1, const char *s2)
7272
int strncasecmp(const char *s1, const char *s2, size_t n)
7373
__attribute__((nonnull(1, 2)));
7474

75+
char* strerror(int errnum);
7576

7677
__END_DECLS
7778

src/libc/ti_routines.src

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
; to reduce binary size (or performance in the case of sprintf), ti's routines
6+
; can be linked instead of the toolchain's routines
7+
8+
public __ti_sprintf
9+
__ti_sprintf := 0000BCh

src/libcxx/include/cstdio

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using ::vsprintf;
3535
using ::snprintf;
3636
using ::vsnprintf;
3737
using ::sprintf;
38+
using ::perror;
3839
} // namespace std
3940

4041
#endif // _EZCXX_CSTDINT

src/libcxx/include/cstring

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using ::strcmp;
3333
using ::strncmp;
3434
using ::strcasecmp;
3535
using ::strncasecmp;
36+
using ::strerror;
3637
} // namespace std
3738

3839
#endif // _EZCXX_CSTRING
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|600",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|600",
15+
"hashWait|2",
16+
"key|enter",
17+
"delay|300",
18+
"hashWait|3"
19+
],
20+
"hashes": {
21+
"1": {
22+
"description": "Valid errno screen",
23+
"start": "vram_start",
24+
"size": "vram_16_size",
25+
"expected_CRCs": [
26+
"B0BE2F4E"
27+
]
28+
},
29+
"2": {
30+
"description": "Invalid input screen",
31+
"start": "vram_start",
32+
"size": "vram_16_size",
33+
"expected_CRCs": [
34+
"29401BEE"
35+
]
36+
},
37+
"3": {
38+
"description": "Exit",
39+
"start": "vram_start",
40+
"size": "vram_16_size",
41+
"expected_CRCs": [
42+
"FFAF89BA",
43+
"101734A5",
44+
"9DA19F44",
45+
"A32840C8",
46+
"349F4775"
47+
]
48+
}
49+
}
50+
}

test/standalone/errno/makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
11+
CFLAGS = -Wall -Wextra -Oz
12+
CXXFLAGS = -Wall -Wextra -Oz
13+
14+
PREFER_OS_LIBC = NO
15+
16+
# ----------------------------
17+
18+
include $(shell cedev-config --makefile)

test/standalone/errno/src/main.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <ti/screen.h>
2+
#include <ti/getcsc.h>
3+
#include <sys/util.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <errno.h>
7+
#include <limits.h>
8+
9+
/** @note assumes that the following strings are used: */
10+
#if 0
11+
static char const * const errno_str[] = {
12+
"no error",
13+
"permission error",
14+
"invalid argument",
15+
"io error",
16+
"math domain error",
17+
"math range error",
18+
};
19+
20+
static char * const unknown_errno_string = "unknown error -8388608";
21+
#endif
22+
23+
int main(void)
24+
{
25+
os_ClrHome();
26+
27+
errno = 0; perror(NULL);
28+
errno = 1; perror("");
29+
errno = 2; perror("\0");
30+
errno = 3; perror(" ");
31+
errno = 4; perror("%d");
32+
errno = 5; perror("perror");
33+
34+
while (!os_GetCSC());
35+
36+
os_ClrHome();
37+
38+
errno = 6; perror(NULL);
39+
errno = -1; perror("");
40+
errno = -123456; perror("%d");
41+
errno = 123456; perror("???");
42+
errno = INT_MIN; perror(" ");
43+
errno = INT_MAX; perror("#");
44+
45+
while (!os_GetCSC());
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)