[libc-commits] [libc] [libc] Implement getprotobyname_r (PR #243486)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 14:50:45 PST 2026
================
@@ -0,0 +1,387 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of getprotobyname_r.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/netdb/getprotobyname_r.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_protoent.h"
+#include "src/__support/CPP/optional.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/CPP/span.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/File/file.h"
+#include "src/__support/common.h"
+#include "src/__support/ctype_utils.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/str_to_integer.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static constexpr const char *DEFAULT_PROTOCOLS_PATH = "/etc/protocols";
+
+#ifndef LIBC_COPT_PUBLIC_PACKAGING
+static LIBC_THREAD_LOCAL const char *protocols_path = nullptr;
+
+void netdb::set_protocols_path(const char *path) { protocols_path = path; }
+#endif // LIBC_COPT_PUBLIC_PACKAGING
+
+namespace {
+
+enum class TokenKind : uint8_t {
+ END_OF_LINE,
+ TOKEN,
+ EMBEDDED_NUL,
+};
+
+struct TokenResult {
+ TokenKind kind;
+ size_t len;
+ bool matches;
+ bool overflow;
+};
+
+struct AliasScan {
+ size_t count = 0;
+ size_t used = 0;
+ bool matched = false;
+ bool buf_overflow = false;
+ bool valid = true;
+};
+
+enum class LineStatus : uint8_t {
+ END_OF_FILE,
+ SKIP,
+ MATCHED,
+};
+
+struct ParsedMatch {
+ LineStatus status;
+ int proto_num = 0;
+ size_t used = 0;
+ size_t alias_count = 0;
+ bool buf_overflow = false;
+};
+
+struct ChunkReader {
+ static constexpr size_t CHUNK_SIZE = 256;
+
+ File *file;
+ char chunk[CHUNK_SIZE];
+ size_t pos = 0;
+ size_t len = 0;
+ int error = 0;
+
+ explicit ChunkReader(File *f) : file(f) {}
+
+ bool next(char &ch) {
+ if (pos == len) {
+ FileIOResult res = file->read_unlocked(chunk, sizeof(chunk));
+ if (res.has_error()) {
+ error = res.error;
+ return false;
+ }
+ if (res.value == 0)
+ return false;
+
+ pos = 0;
+ len = res.value;
+ }
+
+ ch = chunk[pos++];
+ return true;
+ }
----------------
kaladron wrote:
Done. Since there are three outcomes (a byte including '\0', clean EOF, or an I/O error), next_char() now returns ErrorOr<cpp::optional<char>> (nullopt on EOF), which also drops the separate error member.
https://github.com/llvm/llvm-project/pull/243486
More information about the libc-commits
mailing list