Add support for getenv as defined by the Open Group's "System Interface & Header" in https://pubs.opengroup.org/onlinepubs/7908799/xsh/getenv.html getenv requires a standard way of accessing the environment, so a pointer to the environment is added to the startup in crt1. Consquently, this function is not usable on top of other libcs. Added starts_with method to StringView. getenv function uses it. Co-authored-by: Jeff Bailey <jeffbailey@google.com> Reviewed By: sivachandra, rtenneti Differential Revision: https://reviews.llvm.org/D119403
46 lines
1.7 KiB
C++
46 lines
1.7 KiB
C++
//===-- Unittests for getenv ----------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "loader_test.h"
|
|
#include "src/stdlib/getenv.h"
|
|
|
|
static bool my_streq(const char *lhs, const char *rhs) {
|
|
if (lhs == rhs)
|
|
return true;
|
|
if (((lhs == static_cast<char *>(nullptr)) &&
|
|
(rhs != static_cast<char *>(nullptr))) ||
|
|
((lhs != static_cast<char *>(nullptr)) &&
|
|
(rhs == static_cast<char *>(nullptr)))) {
|
|
return false;
|
|
}
|
|
const char *l, *r;
|
|
for (l = lhs, r = rhs; *l != '\0' && *r != '\0'; ++l, ++r)
|
|
if (*l != *r)
|
|
return false;
|
|
|
|
return *l == '\0' && *r == '\0';
|
|
}
|
|
|
|
int main(int argc, char **argv, char **envp) {
|
|
ASSERT_TRUE(my_streq(__llvm_libc::getenv(""), static_cast<char *>(nullptr)));
|
|
ASSERT_TRUE(my_streq(__llvm_libc::getenv("="), static_cast<char *>(nullptr)));
|
|
ASSERT_TRUE(my_streq(__llvm_libc::getenv("MISSING ENV VARIABLE"),
|
|
static_cast<char *>(nullptr)));
|
|
ASSERT_FALSE(
|
|
my_streq(__llvm_libc::getenv("PATH"), static_cast<char *>(nullptr)));
|
|
ASSERT_TRUE(my_streq(__llvm_libc::getenv("FRANCE"), "Paris"));
|
|
ASSERT_FALSE(my_streq(__llvm_libc::getenv("FRANCE"), "Berlin"));
|
|
ASSERT_TRUE(my_streq(__llvm_libc::getenv("GERMANY"), "Berlin"));
|
|
ASSERT_TRUE(
|
|
my_streq(__llvm_libc::getenv("FRANC"), static_cast<char *>(nullptr)));
|
|
ASSERT_TRUE(
|
|
my_streq(__llvm_libc::getenv("FRANCE1"), static_cast<char *>(nullptr)));
|
|
|
|
return 0;
|
|
}
|