ref: 04f3da918d80958735fd99e1b807758b06ff7e7d
parent: 6eb087522f3a7aca2f1df8599d125daea263f2e7
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Sep 21 09:14:53 EDT 2018
[lib/c] Add getenv() for posix systems
--- /dev/null
+++ b/lib/c/target/posix/getenv.c
@@ -1,0 +1,22 @@
+#include <stdlib.h>
+#include <string.h>
+#undef getenv
+
+extern char **environ;
+
+char *
+getenv(const char *name)
+{
+ char **p;
+ size_t len = strlen(name);
+
+ for (p = environ; *p; ++p) {
+ if (!memcmp(name, *p, len) && (*p)[len] == '=')
+ break;
+ }
+
+ if (!*p)
+ return NULL;
+
+ return &(*p)[len];
+}
--- a/lib/crt/amd64-sysv-netbsd/crt.s
+++ b/lib/crt/amd64-sysv-netbsd/crt.s
@@ -8,10 +8,16 @@
.p2align 2
.long 200000000
+ .bss
+environ:
+ .quad 0
+
.text
.align 8
.global _start
_start:
+ movq 16(%rbp),%rbx
+ movq %rbx,environ
call main
- movl %eax, %edi
+ movl %eax,%edi
jmp exit