diff --git a/base/usr/include/assert.h b/base/usr/include/assert.h index 6f12cbd2..49a556f4 100644 --- a/base/usr/include/assert.h +++ b/base/usr/include/assert.h @@ -1,4 +1,5 @@ #ifndef NDEBUG +extern void __assert_func(const char * file, int line, const char * func, const char * failedexpr); #define assert(statement) ((statement) ? (void)0 : __assert_func(__FILE__, __LINE__, __FUNCTION__, #statement)) #else #define assert(statement) ((void)0) diff --git a/base/usr/include/ctype.h b/base/usr/include/ctype.h index 78a0bc04..c7a797e9 100644 --- a/base/usr/include/ctype.h +++ b/base/usr/include/ctype.h @@ -17,3 +17,16 @@ extern int isascii(int c); extern int tolower(int c); extern int toupper(int c); + +/* Derived from newlib */ +#define _U 01 +#define _L 02 +#define _N 04 +#define _S 010 +#define _P 020 +#define _C 040 +#define _X 0100 +#define _B 0200 + +extern char _ctype_[256]; + diff --git a/base/usr/include/iconv.h b/base/usr/include/iconv.h new file mode 100644 index 00000000..bd64d9aa --- /dev/null +++ b/base/usr/include/iconv.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +typedef void * iconv_t; + +extern iconv_t iconv_open(const char *tocode, const char *fromcode); +extern int iconv_close(iconv_t cd); +extern size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft); + diff --git a/base/usr/include/math.h b/base/usr/include/math.h index 782715df..5ab4af1d 100644 --- a/base/usr/include/math.h +++ b/base/usr/include/math.h @@ -6,7 +6,9 @@ extern double pow(double x, double y); extern double exp(double x); extern double fmod(double x, double y); extern double sqrt(double x); +extern float sqrtf(float x); extern double fabs(double x); +extern float fabsf(float x); extern double sin(double x); extern double cos(double x); diff --git a/base/usr/include/stdio.h b/base/usr/include/stdio.h index a1561f16..02075a9b 100644 --- a/base/usr/include/stdio.h +++ b/base/usr/include/stdio.h @@ -83,3 +83,10 @@ extern int vsscanf(const char *str, const char *format, va_list ap); extern int sscanf(const char *str, const char *format, ...); extern int vfscanf(FILE * stream, const char *format, va_list ap); extern int fscanf(FILE *stream, const char *format, ...); +extern int scanf(const char *format, ...); + +typedef long fpos_t; + +extern int fgetpos(FILE *stream, fpos_t *pos); +extern int fsetpos(FILE *stream, const fpos_t *pos); + diff --git a/base/usr/include/stdlib.h b/base/usr/include/stdlib.h index 366dac27..9c0cc54e 100644 --- a/base/usr/include/stdlib.h +++ b/base/usr/include/stdlib.h @@ -21,6 +21,7 @@ extern int setenv(const char *name, const char *value, int overwrite); extern int unsetenv(const char * str); extern double strtod(const char *nptr, char **endptr); +extern float strtof(const char *nptr, char **endptr); extern double atof(const char * nptr); extern int atoi(const char * nptr); extern long atol(const char * nptr); @@ -49,7 +50,14 @@ extern void abort(void); extern void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); -extern char * mktemp(char * template); +extern char * mktemp(char *); extern size_t mbstowcs(wchar_t *dest, const char *src, size_t n); extern size_t wcstombs(char * dest, const wchar_t *src, size_t n); + +typedef struct { int quot; int rem; } div_t; +typedef struct { long int quot; long int rem; } ldiv_t; + +extern div_t div(int numerator, int denominator); +extern ldiv_t ldiv(long numerator, long denominator); + diff --git a/base/usr/include/string.h b/base/usr/include/string.h index c35535e2..58178c8d 100644 --- a/base/usr/include/string.h +++ b/base/usr/include/string.h @@ -45,5 +45,6 @@ extern char * strtok_r(char * str, const char * delim, char ** saveptr); extern char * strncpy(char *dest, const char *src, size_t n); extern char * strerror(int errnum); +extern size_t strxfrm(char *dest, const char *src, size_t n); #include diff --git a/libc/ctype/_ctype.c b/libc/ctype/_ctype.c new file mode 100644 index 00000000..5e44c1b7 --- /dev/null +++ b/libc/ctype/_ctype.c @@ -0,0 +1,20 @@ +#include + +char _ctype_[256]= { + _C, _C, _C, _C, _C, _C, _C, _C, + _C, _C|_S, _C|_S, _C|_S, _C|_S, _C|_S, _C, _C, + _C, _C, _C, _C, _C, _C, _C, _C, + _C, _C, _C, _C, _C, _C, _C, _C, + _S|_B, _P, _P, _P, _P, _P, _P, _P, + _P, _P, _P, _P, _P, _P, _P, _P, + _N, _N, _N, _N, _N, _N, _N, _N, + _N, _N, _P, _P, _P, _P, _P, _P, + _P, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U, + _U, _U, _U, _U, _U, _U, _U, _U, + _U, _U, _U, _U, _U, _U, _U, _U, + _U, _U, _U, _P, _P, _P, _P, _P, + _P, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L, + _L, _L, _L, _L, _L, _L, _L, _L, + _L, _L, _L, _L, _L, _L, _L, _L, + _L, _L, _L, _P, _P, _P, _P, _C +}; diff --git a/libc/iconv/iconv.c b/libc/iconv/iconv.c new file mode 100644 index 00000000..db6855e3 --- /dev/null +++ b/libc/iconv/iconv.c @@ -0,0 +1,37 @@ +#include +#include +#include + +struct _iconv_state { + char *tocode; + char *fromcode; +}; + +iconv_t iconv_open(const char *tocode, const char *fromcode) { + errno = EINVAL; + return (iconv_t)-1; + +#if 0 + struct _iconv_state * state = malloc(sizeof(struct _iconv_state)); + + state->tocode = strdup(tocode); + state->fromcode = strdup(fromcode); + + return (iconv_t)state; +#endif +} + +int iconv_close(iconv_t cd) { + struct _iconv_state * state = (struct _iconv_state*)cd; + + free(state->tocode); + free(state->fromcode); + + free(cd); + + return 0; +} + +size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { + return -1; +} diff --git a/libc/math/math.c b/libc/math/math.c index 4be9ffa7..5b6c567e 100644 --- a/libc/math/math.c +++ b/libc/math/math.c @@ -68,6 +68,10 @@ double fabs(double x) { return __builtin_fabs(x); } +float fabsf(float x) { + return fabs(x); +} + double fmod(double x, double y) { MATH; if (x >= 0.0) { @@ -85,6 +89,10 @@ double sqrt(double x) { return __builtin_sqrt(x); } +float sqrtf(float x) { + return sqrt(x); +} + static double bad_sine_table[] = { 0, 0.01745240644, diff --git a/libc/stdio/scanf.c b/libc/stdio/scanf.c index e88d74d1..e4baeddb 100644 --- a/libc/stdio/scanf.c +++ b/libc/stdio/scanf.c @@ -52,3 +52,11 @@ int fscanf(FILE *stream, const char *format, ...) { va_end(args); return out; } + +int scanf(const char *format, ...) { + va_list args; + va_start(args, format); + int out = vfscanf(stdin, format, args); + va_end(args); + return out; +} diff --git a/libc/stdio/stdio.c b/libc/stdio/stdio.c index a21c59e5..84be750d 100644 --- a/libc/stdio/stdio.c +++ b/libc/stdio/stdio.c @@ -289,6 +289,18 @@ long ftell(FILE * stream) { return resp; } +int fgetpos(FILE *stream, fpos_t *pos) { + long ret = ftell(stream); + if (ret == -1) return -1; + + *pos = ret; + return 0; +} + +int fsetpos(FILE *stream, const fpos_t *pos) { + return fseek(stream, *pos, SEEK_SET); +} + size_t fread(void *ptr, size_t size, size_t nmemb, FILE * stream) { char * tracking = (char*)ptr; for (size_t i = 0; i < nmemb; ++i) { diff --git a/libc/stdlib/div.c b/libc/stdlib/div.c new file mode 100644 index 00000000..93071ddd --- /dev/null +++ b/libc/stdlib/div.c @@ -0,0 +1,33 @@ +#include + +div_t div(int numerator, int denominator) { + div_t out; + out.quot = numerator / denominator; + out.rem = numerator % denominator; + + if (numerator >= 0 && out.rem < 0) { + out.quot++; + out.rem -= denominator; + } else if (numerator < 0 && out.rem > 0) { + out.quot--; + out.rem += denominator; + } + + return out; +} + +ldiv_t ldiv(long numerator, long denominator) { + ldiv_t out; + out.quot = numerator / denominator; + out.rem = numerator % denominator; + + if (numerator >= 0 && out.rem < 0) { + out.quot++; + out.rem -= denominator; + } else if (numerator < 0 && out.rem > 0) { + out.quot--; + out.rem += denominator; + } + + return out; +} diff --git a/libc/stdlib/strtod.c b/libc/stdlib/strtod.c index a405f889..5a360f5b 100644 --- a/libc/stdlib/strtod.c +++ b/libc/stdlib/strtod.c @@ -72,3 +72,6 @@ double strtod(const char *nptr, char **endptr) { return result; } +float strtof(const char *nptr, char **endptr) { + return strtod(nptr,endptr); +} diff --git a/libc/string/strxfrm.c b/libc/string/strxfrm.c new file mode 100644 index 00000000..0a68d037 --- /dev/null +++ b/libc/string/strxfrm.c @@ -0,0 +1,19 @@ +#include + +/** + * We only really support the "C" locale, so this is always + * just a dumb memcpy. + */ +size_t strxfrm(char *dest, const char *src, size_t n) { + size_t i = 0; + while (*src && i < n) { + *dest = *src; + dest++; + src++; + i++; + } + if (i < n) { + *dest = '\0'; + } + return i; +}