From 8a3fe6d1bf6502b31df2e1094ae06f72e15789cb Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 17 Nov 2013 19:00:51 +0100 Subject: [PATCH] Add private __getenv_reentrant() A reentrant variant of getenv(). --- headers/private/libroot/stdlib_private.h | 23 +++++++++++++++++++++++ src/system/libroot/posix/stdlib/env.cpp | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 headers/private/libroot/stdlib_private.h diff --git a/headers/private/libroot/stdlib_private.h b/headers/private/libroot/stdlib_private.h new file mode 100644 index 0000000000..b2b20578ff --- /dev/null +++ b/headers/private/libroot/stdlib_private.h @@ -0,0 +1,23 @@ +/* + * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef _LIBROOT_STDLIB_PRIVATE_H +#define _LIBROOT_STDLIB_PRIVATE_H + + +#include +#include + + +__BEGIN_DECLS + + +ssize_t __getenv_reentrant(const char* name, char* buffer, + size_t bufferSize); + + +__END_DECLS + + +#endif /* _LIBROOT_STDLIB_PRIVATE_H */ diff --git a/src/system/libroot/posix/stdlib/env.cpp b/src/system/libroot/posix/stdlib/env.cpp index d27a66fdf8..578a25506b 100644 --- a/src/system/libroot/posix/stdlib/env.cpp +++ b/src/system/libroot/posix/stdlib/env.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -315,3 +316,21 @@ putenv(const char *string) RETURN_AND_SET_ERRNO(status); } + +ssize_t +__getenv_reentrant(const char* name, char* buffer, size_t bufferSize) +{ + size_t nameLength = strlen(name); + + lock_variables(); + + char* value = find_variable(name, nameLength, NULL); + ssize_t result = value != NULL + ? strlcpy(buffer, value + nameLength + 1, bufferSize) + : B_NAME_NOT_FOUND; + + unlock_variables(); + + return result; +} +