Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/ce/include/ti_sprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef TI_SPRINTF_H
#define TI_SPRINTF_H

#include <stddef.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief C89 `sprintf`. `long` arguments and width specifiers are unsupported.
* @note `%s` will write up to 255 characters.
*/
int ti_sprintf(
char *__restrict buffer, const char *__restrict format, ...
) __attribute__((format (__printf__, 2, 3)));

/**
* @brief Returns an empty string if the output from sprintf does not fit.
* @warning `__VA_ARGS__` is evaluated twice.
* @note Undefined behaviour if the output is longer than ~258000 characters.
*/
#define ti_snprintf(buffer, count, ...)\
({\
char * const __buffer = buffer;\
const int __count = count;\
int __ret = -1;\
int __str_len = ti_sprintf((char*)0xFC1000, __VA_ARGS__);\
if (__buffer == NULL || __count == 0) {\
__ret = __str_len;\
} else if ((size_t)__str_len > __count) {\
*__buffer = '\0'; /* won't fit or invalid formatting */\
} else {\
__ret = ti_sprintf(__buffer, __VA_ARGS__);\
}\
__ret;\
})

/**
* @brief Allocates a null terminated string containing the output of sprintf.
* The returned pointer shall be deallocated with `free`.
* @warning `__VA_ARGS__` is evaluated twice.
* @note Undefined behaviour if the output is longer than ~258000 characters.
*/
#define ti_asprintf(p_buffer, ...)\
({\
char** const __p_buffer = p_buffer;\
int __ret = -1;\
int __str_len = ti_sprintf((char*)0xFC1000, __VA_ARGS__);\
if (__str_len >= 0) {\
size_t __buffer_size = (size_t)__str_len + 1;\
*__p_buffer = (char*)malloc(__buffer_size);\
if (*__p_buffer != NULL) {\
__ret = ti_sprintf(*__p_buffer, __VA_ARGS__);\
}\
}\
__ret;\
})

#ifdef __cplusplus
}
#endif

#endif /* TI_SPRINTF_H */
18 changes: 9 additions & 9 deletions src/libc/ti_routines.src → src/ce/ti_sprintf.src
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
assume adl=1
section .text
; to reduce binary size (or performance in the case of sprintf), ti's routines
; can be linked instead of the toolchain's routines
public __ti_sprintf
__ti_sprintf := 0000BCh
assume adl=1

section .text

; to reduce binary size (or performance in the case of sprintf), ti's routines
; can be linked instead of the toolchain's routines

public _ti_sprintf
_ti_sprintf := 0000BCh
10 changes: 3 additions & 7 deletions src/libc/errno_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>

int _ti_sprintf(
char *__restrict buffer, const char *__restrict format, ...
) __attribute__ ((format (__printf__, 2, 3)));
#include <ti_sprintf.h>

static char const * const errno_strings[] = {
"no error",
Expand All @@ -32,7 +29,7 @@ static_assert(

char* strerror(int errnum) {
if ((unsigned int)errnum >= errno_strings_count) {
_ti_sprintf(&(unknown_errno_string[unknown_errno_number_offset]), "%d", errnum);
ti_sprintf(&(unknown_errno_string[unknown_errno_number_offset]), "%d", errnum);
return (char*)unknown_errno_string;
}
return (char*)errno_strings[errnum];
Expand All @@ -48,8 +45,7 @@ size_t strerrorlen_s(errno_t errnum) {
void perror(const char *str) {
if (str != NULL && *str != '\0') {
fputs(str, stderr);
fputc(':', stderr);
fputc(' ', stderr);
fputs(": ", stderr);
}
fputs(strerror(errno), stderr);
fputc('\n', stderr);
Expand Down
25 changes: 17 additions & 8 deletions src/libc/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,29 @@ int printf(const char *__restrict format, ...)
int vprintf(const char *__restrict format, va_list va)
__attribute__((format(__printf__, 1, 0)));

int vsprintf(char *__restrict buffer, const char *__restrict format,
va_list va)
__attribute__((format(__printf__, 1, 0)));
int sprintf(char *__restrict buffer, const char *__restrict format, ...)
__attribute__((format (__printf__, 2, 3)));

int vsprintf(char *__restrict buffer, const char *__restrict format, va_list va)
__attribute__((format(__printf__, 2, 0)));

int snprintf(char* buffer, size_t count, const char *__restrict format, ...)
__attribute__((format(__printf__, 3, 4)));

int vsnprintf(char* buffer, size_t count, const char *__restrict format,
va_list va)
int vsnprintf(char* buffer, size_t count, const char *__restrict format, va_list va)
__attribute__((format(__printf__, 3, 0)));

int sprintf(char *__restrict buffer,
const char *__restrict format, ...)
__attribute__ ((format (__printf__, 2, 3)));
int fprintf(FILE* __restrict stream, const char* __restrict format, ...)
__attribute__((format (__printf__, 2, 3)));

int vfprintf(FILE* __restrict stream, const char* __restrict format, va_list va)
__attribute__((format(__printf__, 2, 0)));

int asprintf(char **__restrict p_buffer, const char *__restrict format, ...)
__attribute__((format (__printf__, 2, 3))) __attribute__((nonnull(1)));

int vasprintf(char **__restrict p_buffer, const char *__restrict format, va_list va)
__attribute__((format(__printf__, 2, 0))) __attribute__((nonnull(1)));

void perror(const char* str);

Expand Down
82 changes: 57 additions & 25 deletions src/libc/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,104 @@

__BEGIN_DECLS

extern void *memcpy(void *__restrict dest, const void *__restrict src,
size_t n) __attribute__((nonnull(1, 2)));
extern void *memcpy(void *__restrict dest, const void *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));

void *memmove(void *dest, const void *src, size_t n)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

void *memset(void *s, int c, size_t n) __attribute__((nonnull(1)));
void *memset(void *s, int c, size_t n)
__attribute__((nonnull(1)));

int memcmp(const void *s1, const void *s2, size_t n)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

void *memchr(const void *s, int c, size_t n) __attribute__((nonnull(1)));
void *memchr(const void *s, int c, size_t n)
__attribute__((nonnull(1)));

void *memrchr(const void *s, int c, size_t n)
__NOEXCEPT __attribute__((nonnull(1))) __attribute((__pure__));

void *memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len)
__NOEXCEPT __attribute__((nonnull(1, 3))) __attribute((__pure__));

void *memccpy(void *__restrict dest, const void *__restrict src, int c, size_t n)
__NOEXCEPT __attribute__((nonnull(1, 2)));

void *mempcpy(void *__restrict dest, const void *__restrict src, size_t n)
__NOEXCEPT __attribute__((nonnull(1, 2)));

char *strcpy(char *__restrict dest, const char *__restrict src)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

char *strncpy(char *__restrict dest, const char *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

char *stpcpy(char *__restrict dest, const char *__restrict src)
__NOEXCEPT __attribute__((nonnull(1, 2)));

char *stpncpy(char *__restrict dest, const char *__restrict src, size_t n)
__NOEXCEPT __attribute__((nonnull(1, 2)));

char *strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
__NOEXCEPT __attribute__((nonnull(1, 2)));

char *strcat(char *__restrict dest, const char *__restrict src)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

char *strncat(char *__restrict dest, const char *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

char *strchr(const char *s, int c) __attribute__((nonnull(1)));
char *strlcat(char *__restrict dest, const char *__restrict src, size_t n)
__NOEXCEPT __attribute__((nonnull(1, 2)));

char *strrchr(const char *s, int c) __attribute__((nonnull(1)));
char *strchr(const char *s, int c)
__attribute__((nonnull(1)));

char *strpbrk(const char *s, const char *accept) __attribute__((nonnull(1, 2)));
char *strrchr(const char *s, int c)
__attribute__((nonnull(1)));

char *strpbrk(const char *s, const char *accept)
__attribute__((nonnull(1, 2)));

char *strstr(const char *haystack, const char *needle)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

char *strcasestr(const char *haystack, const char *needle)
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));

char *strtok(char *__restrict s, const char *__restrict delim)
__attribute__((nonnull(2)));
__attribute__((nonnull(2)));

char *strdup(const char *s)
__attribute__ ((__malloc__)) __attribute__((nonnull(1)));
__attribute__((__malloc__)) __attribute__((nonnull(1)));

char *strndup(const char *s, size_t n)
__attribute__ ((__malloc__)) __attribute__((nonnull(1)));
__attribute__((__malloc__)) __attribute__((nonnull(1)));

size_t strcspn(const char *s, const char *reject)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

size_t strspn(const char *s, const char *accept)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

size_t strlen(const char *s)
__attribute__((nonnull(1)));
__attribute__((nonnull(1)));

size_t strnlen(const char *s, size_t maxlen)
__attribute__((nonnull(1)));
__attribute__((nonnull(1)));

int strcmp(const char *s1, const char *s2)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

int strncmp(const char *s1, const char *s2, size_t n)
__attribute__((nonnull(1, 2)));
__attribute__((nonnull(1, 2)));

int strcasecmp(const char *s1, const char *s2)
__attribute__((nonnull(1, 2)));
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));

int strncasecmp(const char *s1, const char *s2, size_t n)
__attribute__((nonnull(1, 2)));
__NOEXCEPT __attribute__((nonnull(1, 2))) __attribute__((__pure__));

char* strerror(int errnum);

Expand Down
3 changes: 2 additions & 1 deletion src/libc/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ char *asctime(const struct tm *tmp);

char *ctime(const time_t *timer);

size_t strftime(char* ptr, size_t maxsize, const char* format, const struct tm* timeptr);
size_t strftime(char* ptr, size_t maxsize, const char* format, const struct tm* timeptr)
__attribute__((format(__strftime__, 3, 0)));

__END_DECLS

Expand Down
66 changes: 66 additions & 0 deletions src/libc/include/wchar.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _WCHAR_H
#define _WCHAR_H

#include <cdefs.h>

#ifndef _WCHAR_T_DEFINED
#define _WCHAR_T_DEFINED
#ifndef __cplusplus
Expand All @@ -17,4 +19,68 @@ typedef __WCHAR_TYPE__ wchar_t;

#define WEOF -1

__BEGIN_DECLS

wchar_t *wmemcpy(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));

wchar_t *wmemmove(wchar_t *dest, const wchar_t *src, size_t n)
__attribute__((nonnull(1, 2)));

wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n)
__attribute__((nonnull(1)));

int wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n)
__attribute__((nonnull(1, 2)));

wchar_t *wmemchr(const wchar_t *s, int c, size_t n)
__attribute__((nonnull(1)));

wchar_t *wcscpy(wchar_t *__restrict dest, const wchar_t *__restrict src)
__attribute__((nonnull(1, 2)));

wchar_t *wcsncpy(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));

wchar_t *wcscat(wchar_t *__restrict dest, const wchar_t *__restrict src)
__attribute__((nonnull(1, 2)));

wchar_t *wcsncat(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
__attribute__((nonnull(1, 2)));

wchar_t *wcschr(const wchar_t *s, int c)
__attribute__((nonnull(1)));

wchar_t *wcsrchr(const wchar_t *s, int c)
__attribute__((nonnull(1)));

wchar_t *wcspbrk(const wchar_t *s, const wchar_t *accept)
__attribute__((nonnull(1, 2)));

wchar_t *wcsstr(const wchar_t *haystack, const wchar_t *needle)
__attribute__((nonnull(1, 2)));

wchar_t *wcstok(wchar_t *__restrict s, const wchar_t *__restrict delim)
__attribute__((nonnull(2)));

size_t wcscspn(const wchar_t *s, const wchar_t *reject)
__attribute__((nonnull(1, 2)));

size_t wcsspn(const wchar_t *s, const wchar_t *accept)
__attribute__((nonnull(1, 2)));

size_t wcslen(const wchar_t *s)
__attribute__((nonnull(1)));

size_t wcsnlen(const wchar_t *s, size_t maxlen)
__attribute__((nonnull(1)));

int wcscmp(const wchar_t *s1, const wchar_t *s2)
__attribute__((nonnull(1, 2)));

int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
__attribute__((nonnull(1, 2)));

__END_DECLS

#endif /* _WCHAR_H */
Loading
Loading