Browse Source

libc: support for building libstdc++?

K. Lange 5 years ago
parent
commit
b33815c632

+ 1 - 0
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)

+ 13 - 0
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];
+

+ 10 - 0
base/usr/include/iconv.h

@@ -0,0 +1,10 @@
+#pragma once
+
+#include <stddef.h>
+
+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);
+

+ 2 - 0
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);
 

+ 7 - 0
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);
+

+ 9 - 1
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);
+

+ 1 - 0
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 <strings.h>

+ 20 - 0
libc/ctype/_ctype.c

@@ -0,0 +1,20 @@
+#include <ctype.h>
+
+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
+};

+ 37 - 0
libc/iconv/iconv.c

@@ -0,0 +1,37 @@
+#include <iconv.h>
+#include <errno.h>
+#include <string.h>
+
+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;
+}

+ 8 - 0
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,

+ 8 - 0
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;
+}

+ 12 - 0
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) {

+ 33 - 0
libc/stdlib/div.c

@@ -0,0 +1,33 @@
+#include <stdlib.h>
+
+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;
+}

+ 3 - 0
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);
+}

+ 19 - 0
libc/string/strxfrm.c

@@ -0,0 +1,19 @@
+#include <string.h>
+
+/**
+ * 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;
+}