Path: blob/main/system/lib/llvm-libc/src/stdlib/getenv.cpp
6175 views
//===-- Implementation of getenv ------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "src/stdlib/getenv.h"9#include "config/app.h"10#include "src/__support/CPP/string_view.h"11#include "src/__support/common.h"12#include "src/__support/macros/config.h"1314#include <stddef.h> // For size_t.1516namespace LIBC_NAMESPACE_DECL {1718LLVM_LIBC_FUNCTION(char *, getenv, (const char *name)) {19char **env_ptr = reinterpret_cast<char **>(LIBC_NAMESPACE::app.env_ptr);2021if (name == nullptr || env_ptr == nullptr)22return nullptr;2324LIBC_NAMESPACE::cpp::string_view env_var_name(name);25if (env_var_name.size() == 0)26return nullptr;27for (char **env = env_ptr; *env != nullptr; env++) {28LIBC_NAMESPACE::cpp::string_view cur(*env);29if (!cur.starts_with(env_var_name))30continue;3132if (cur[env_var_name.size()] != '=')33continue;3435// Remove the name and the equals sign.36cur.remove_prefix(env_var_name.size() + 1);37// We know that data is null terminated, so this is safe.38return const_cast<char *>(cur.data());39}4041return nullptr;42}4344} // namespace LIBC_NAMESPACE_DECL454647