Skip to content

Commit

Permalink
getenv for Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Jun 28, 2024
1 parent 1de09bb commit 6c6d0b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 3 additions & 2 deletions libsrc/crt0/_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

extern char **environ;

#if defined(__linux__)

#include "stdio.h" // fflush
Expand All @@ -28,7 +26,10 @@ static void _atexit_proc(void) {

static void start2(int argc, char *argv[], char *env[]) {
extern int main(int, char**, char **);
#ifndef __APPLE__
extern char **environ;
environ = env;
#endif
atexit(_atexit_proc);
int ec = main(argc, argv, env);
exit(ec);
Expand Down
9 changes: 7 additions & 2 deletions libsrc/stdlib/getenv.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "stdlib.h"
#include "string.h"

extern char **environ;

char *getenv(const char *varname) {
#ifdef __APPLE__
extern char ***_NSGetEnviron(void);
char **environ = *_NSGetEnviron();
#else
extern char **environ;
#endif

if (environ != NULL) {
size_t len = strlen(varname);
for (char **pp = environ, *p; (p = *pp++) != NULL;)
Expand Down
9 changes: 7 additions & 2 deletions libsrc/unistd/execvp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "unistd.h"

extern char **environ;

int execvp(const char *path, char *const args[]) {
#ifdef __APPLE__
extern char ***_NSGetEnviron(void);
char **environ = *_NSGetEnviron();
#else
extern char **environ;
#endif

return execve(path, args, environ);
}

0 comments on commit 6c6d0b4

Please sign in to comment.