Perform inconsequential code cleanup

This commit is contained in:
Justine Tunney 2023-08-07 20:22:49 -07:00
parent 929478c524
commit decf216655
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 326 additions and 442 deletions

View file

@ -19,13 +19,13 @@
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/dns/hoststxt.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/pushpop.internal.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/systeminfo.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -39,16 +39,11 @@ static struct HostsTxtInitialStaticMemory {
char strings[64];
} g_hoststxt_init;
static textwindows dontinline char *GetNtHostsTxtPath(char *pathbuf,
uint32_t size) {
const char *const kWinHostsPath = "\\drivers\\etc\\hosts";
uint32_t len = GetSystemDirectoryA(&pathbuf[0], size);
if (len && len + strlen(kWinHostsPath) + 1 < size) {
if (pathbuf[len] == '\\') pathbuf[len--] = '\0';
memcpy(&pathbuf[len], kWinHostsPath, strlen(kWinHostsPath) + 1);
return &pathbuf[0];
static const char *GetHostsTxtPath(char *path, size_t size) {
if (!IsWindows()) {
return "/etc/hosts";
} else {
return NULL;
return GetSystemDirectoryPath(path, size, "drivers\\etc\\hosts");
}
}
@ -60,8 +55,7 @@ static textwindows dontinline char *GetNtHostsTxtPath(char *pathbuf,
*/
const struct HostsTxt *GetHostsTxt(void) {
FILE *f;
const char *path;
char pathbuf[PATH_MAX];
char pathbuf[256];
struct HostsTxtInitialStaticMemory *init;
init = &g_hoststxt_init;
pthread_mutex_lock(&init->lock);
@ -72,11 +66,7 @@ const struct HostsTxt *GetHostsTxt(void) {
init->ht.strings.n = pushpop(ARRAYLEN(init->strings));
init->ht.strings.p = init->strings;
__cxa_atexit(FreeHostsTxt, &g_hoststxt, NULL);
path = "/etc/hosts";
if (IsWindows()) {
path = firstnonnull(GetNtHostsTxtPath(pathbuf, ARRAYLEN(pathbuf)), path);
}
if (fileexists(path) && (f = fopen(path, "r"))) {
if ((f = fopen(GetHostsTxtPath(pathbuf, sizeof(pathbuf)), "r"))) {
if (ParseHostsTxt(g_hoststxt, f) == -1) {
/* TODO(jart): Elevate robustness. */
}