[libc-commits] [libc] [libc][NFC] Add FieldTokenizer and FlatFileDatabase (PR #214720)
Alexey Samsonov via libc-commits
libc-commits at lists.llvm.org
Fri Aug 7 11:53:07 PDT 2026
================
@@ -13,196 +13,55 @@
#include "src/pwd/pwd_utils.h"
#include "hdr/errno_macros.h"
-#include "hdr/stdio_macros.h"
+#include "hdr/types/gid_t.h"
#include "hdr/types/struct_passwd.h"
+#include "hdr/types/uid_t.h"
#include "src/__support/CPP/span.h"
-#include "src/__support/File/file.h"
-#include "src/__support/ctype_utils.h"
-#include "src/__support/str_to_integer.h"
+#include "src/pwd/flat_file_db.h"
#include "src/string/string_utils.h"
#ifndef LIBC_COPT_PWD_FILE_PATH
#define LIBC_COPT_PWD_FILE_PATH "/etc/passwd"
#endif
namespace LIBC_NAMESPACE_DECL {
-namespace internal {
+namespace pwd {
ErrorOr<struct passwd> parse_passwd_line(char *line) {
if (!line)
return Error(EINVAL);
struct passwd pwd;
- char *context = line;
-
- pwd.pw_name = string_token<false>(nullptr, ":", &context);
- if (!pwd.pw_name)
- return Error(EINVAL);
-
- pwd.pw_passwd = string_token<false>(nullptr, ":", &context);
- if (!pwd.pw_passwd)
- return Error(EINVAL);
-
- char *uid_str = string_token<false>(nullptr, ":", &context);
- if (!uid_str || !isdigit(uid_str[0]))
- return Error(EINVAL);
- auto uid_res = strtointeger<uid_t>(uid_str, 10);
- if (uid_res.has_error() || uid_res.parsed_len == 0 ||
- uid_str[uid_res.parsed_len] != '\0')
- return Error(EINVAL);
- pwd.pw_uid = uid_res.value;
-
- char *gid_str = string_token<false>(nullptr, ":", &context);
- if (!gid_str || !isdigit(gid_str[0]))
- return Error(EINVAL);
- auto gid_res = strtointeger<gid_t>(gid_str, 10);
- if (gid_res.has_error() || gid_res.parsed_len == 0 ||
- gid_str[gid_res.parsed_len] != '\0')
- return Error(EINVAL);
- pwd.pw_gid = gid_res.value;
-
- pwd.pw_gecos = string_token<false>(nullptr, ":", &context);
- if (!pwd.pw_gecos)
- return Error(EINVAL);
-
- pwd.pw_dir = string_token<false>(nullptr, ":", &context);
- if (!pwd.pw_dir)
- return Error(EINVAL);
-
- pwd.pw_shell = string_token<false>(nullptr, ":", &context);
- if (!pwd.pw_shell)
+ size_t len = internal::string_length(line);
+ if (!parse_line(cpp::span<char>(line, len + 1), &pwd))
return Error(EINVAL);
return pwd;
}
-} // namespace internal
+} // namespace pwd
namespace passwd {
-static File *pwd_file = nullptr;
-static const char *pwd_file_path = LIBC_COPT_PWD_FILE_PATH;
+static pwd::FlatFileDatabase<struct passwd> db(LIBC_COPT_PWD_FILE_PATH);
----------------
vonosmas wrote:
I'm wary of static constructors here, as I can imagine `pwd` logic being triggered from some other static constructor in the application logic. I wonder if we can do some form of lazy initialization.
https://github.com/llvm/llvm-project/pull/214720
More information about the libc-commits
mailing list