aboutsummaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib.c
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2016-07-16 01:28:04 +0200
committerAlex Auvolat <alex@adnab.me>2016-07-16 01:28:04 +0200
commit59000174aa50ed6b2d24a71576d15e6a53c5be0c (patch)
tree38e0a7623f1b83c4dabb1fddfc49014e623f6456 /src/lib/libc/stdlib.c
parent32407e728971006ed3d0885e01c22fb66c8adc57 (diff)
downloadkogata-59000174aa50ed6b2d24a71576d15e6a53c5be0c.tar.gz
kogata-59000174aa50ed6b2d24a71576d15e6a53c5be0c.zip
Add stubs for many libc functions, and a few implemenations too
Diffstat (limited to 'src/lib/libc/stdlib.c')
-rw-r--r--src/lib/libc/stdlib.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib.c b/src/lib/libc/stdlib.c
new file mode 100644
index 0000000..d8598dd
--- /dev/null
+++ b/src/lib/libc/stdlib.c
@@ -0,0 +1,80 @@
+#include <stdlib.h>
+#include <ctype.h>
+
+#include <kogata/debug.h>
+
+int rand(void) {
+ return 0; // TODO
+}
+
+void srand(unsigned int seed) {
+ //TODO
+}
+
+void abort() {
+ PANIC("Aborted.");
+}
+
+float strtof(const char *nptr, const char **endptr) {
+ return (float)strtod(nptr, endptr);
+}
+double strtod(const char *nptr, const char **endptr) {
+ // TODO: better (inf, nan, ...)
+
+ const char* p = nptr;
+ while (isspace(*p)) p++;
+
+ double val = 0;
+ double sign = 1;
+ if (*p == '-') sign = -1;
+ if (*p == '-' || *p == '+') p++;
+ while (isdigit(*p)) {
+ val = val*10. + (double)((int)*p - '0');
+ p++;
+ }
+ if (*p == '.') {
+ p++;
+ double fac = 0.1;
+ while (isdigit(*p)) {
+ val += fac * (double)((int)*p - '0');
+ fac /= 10.;
+ p++;
+ }
+ }
+ if (*p == 'e' || *p == 'E') {
+ p++;
+ int exp = 0;
+ int sexp = 1;
+ if (*p == '-') sexp = -1;
+ if (*p == '-' || *p =='+') p++;
+ while (isdigit(*p)) {
+ exp = exp * 10 + (*p - '0');
+ p++;
+ }
+ if (sexp == 1) {
+ for (int i = 0; i < exp; i++) val *= 10;
+ } else {
+ for (int i = 0; i < exp; i++) val /= 10;
+ }
+ }
+ if (endptr != NULL) *endptr = p-1;
+
+ return val * sign;
+}
+
+char *getenv(const char *name) {
+ // TODO
+ return 0;
+}
+
+int system(const char *command) {
+ // TODO
+ return -1;
+}
+
+int abs(int j) {
+ if (j < 0) return -j;
+ return j;
+}
+
+/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/