diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/options/posix/generic/resolv_conf.cpp | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/options/posix/generic/resolv_conf.cpp')
-rw-r--r-- | lib/mlibc/options/posix/generic/resolv_conf.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/mlibc/options/posix/generic/resolv_conf.cpp b/lib/mlibc/options/posix/generic/resolv_conf.cpp new file mode 100644 index 0000000..a5c3aa7 --- /dev/null +++ b/lib/mlibc/options/posix/generic/resolv_conf.cpp @@ -0,0 +1,42 @@ +#include <mlibc/resolv_conf.hpp> +#include <mlibc/allocator.hpp> +#include <stdio.h> +#include <ctype.h> + +namespace mlibc { + +frg::optional<struct nameserver_data> get_nameserver() { + auto file = fopen("/etc/resolv.conf", "r"); + if (!file) + return frg::null_opt; + + char line[128]; + struct nameserver_data ret; + while (fgets(line, 128, file)) { + char *pos; + if (!strchr(line, '\n') && !feof(file)) { + // skip truncated lines + for (int c = getc(file); c != '\n' && c != EOF; c = getc(file)); + continue; + } + + // TODO(geert): resolv.conf can actually have multiple nameservers + // but we just pick the first one for now + if (!strncmp(line, "nameserver", 10) && isspace(line[10])) { + char *end; + for (pos = line + 11; isspace(*pos); pos++); + for (end = pos; *end && !isspace(*end); end++); + *end = '\0'; + ret.name = frg::string<MemoryAllocator>( + pos, end - pos, getAllocator()); + break; + } + } + + fclose(file); + if(ret.name.empty()) + return frg::null_opt; + return ret; +} + +} // namespace mlibc |