[libc-commits] [libc] [libc] Implement getprotobyname_r (PR #243486)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 12:58:28 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;
+ }
+};
+
+struct TokenWriter {
+ cpp::string_view target;
+ cpp::span<char> dst;
+ size_t len = 0;
+ bool matches = true;
+
+ void push(char ch) {
+ if (len < target.size()) {
+ if (ch != target[len])
+ matches = false;
+ } else {
+ matches = false;
+ }
+
+ if (len < dst.size())
+ dst[len] = ch;
+ ++len;
+ }
+
+ TokenResult finish() {
+ const bool OVERFLOWED = len >= dst.size();
+ if (!OVERFLOWED)
+ dst[len] = '\0';
+
+ bool is_match = matches;
+ if (len != target.size())
+ is_match = false;
+
+ return {TokenKind::TOKEN, len, is_match, OVERFLOWED};
+ }
+};
+
+} // namespace
+
+static void drain_to_newline(ChunkReader &reader) {
+ char ch = '\0';
+ while (reader.next(ch)) {
+ if (ch == '\n')
+ return;
+ }
+}
+
+static bool find_token_start(ChunkReader &reader, bool &saw_bytes, bool &at_eol,
----------------
michaelrj-google wrote:
there are a bunch of functions passing around references to `saw_bytes` and `at_eol`, those seem like properties of the stream that should probably go inside `ChunkReader` if it stays.
https://github.com/llvm/llvm-project/pull/243486
More information about the libc-commits
mailing list