LibC: Don't use BAN inside pwd.cpp

This made pwd.cpp require libstdc++ which meant that getlogin() also
needed libstdc++. This made build process of libstdc++ require itself
which is of course not possible.
This commit is contained in:
Bananymous 2024-05-29 19:12:15 +03:00
parent 53e572f072
commit b23511edb1
2 changed files with 64 additions and 53 deletions

View File

@ -1,6 +1,4 @@
#include <BAN/String.h> #include <BAN/Assert.h>
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -12,8 +10,6 @@
static FILE* s_pwent_fp = nullptr; static FILE* s_pwent_fp = nullptr;
static passwd s_pwent_struct; static passwd s_pwent_struct;
#define TRY_LIBC(expr, ret) ({ auto&& e = expr; if (e.is_error()) { errno = e.error().get_error_code(); return ret; } e.release_value(); })
static bool open_pwent() static bool open_pwent()
{ {
if (s_pwent_fp) if (s_pwent_fp)
@ -51,51 +47,67 @@ struct passwd* getpwent(void)
return nullptr; return nullptr;
clear_pwent(s_pwent_struct); clear_pwent(s_pwent_struct);
BAN::String line; static char buffer[4096];
while (true)
{
char buffer[128];
if (!fgets(buffer, sizeof(buffer), s_pwent_fp)) if (!fgets(buffer, sizeof(buffer), s_pwent_fp))
return nullptr; return nullptr;
TRY_LIBC(line.append(buffer), nullptr);
if (line.back() == '\n') size_t buffer_len = strlen(buffer);
ASSERT(buffer[buffer_len - 1] == '\n');
buffer[buffer_len - 1] = '\0';
buffer_len--;
const char* ptr = buffer;
for (int i = 0; i < 7; i++)
{ {
line.pop_back(); char* end = strchr(ptr, ':');
break; ASSERT((i < 6) ? end != nullptr : end == nullptr);
} if (!end)
} end = buffer + buffer_len;
*end = '\0';
auto parts = TRY_LIBC(line.sv().split(':', true), nullptr); const size_t field_len = end - ptr;
ASSERT(parts.size() == 7);
ASSERT(1 <= parts[2].size() && parts[2].size() <= 9); switch (i)
for (char c : parts[2]) {
ASSERT(isdigit(c)); case 0:
ASSERT(1 <= parts[3].size() && parts[3].size() <= 9); s_pwent_struct.pw_name = (char*)malloc(field_len + 1);
for (char c : parts[3])
ASSERT(isdigit(c));
s_pwent_struct.pw_uid = atoi(parts[2].data());
s_pwent_struct.pw_gid = atoi(parts[3].data());
s_pwent_struct.pw_name = (char*)malloc(parts[0].size() + 1);
if (!s_pwent_struct.pw_name) if (!s_pwent_struct.pw_name)
return nullptr; return nullptr;
memcpy(s_pwent_struct.pw_name, parts[0].data(), parts[0].size()); strcpy(s_pwent_struct.pw_name, ptr);
s_pwent_struct.pw_name[parts[0].size()] = '\0'; break;
case 1:
s_pwent_struct.pw_dir = (char*)malloc(parts[5].size() + 1); break;
case 2:
ASSERT(1 <= field_len && field_len <= 9);
for (size_t j = 0; j < field_len; j++)
ASSERT(isdigit(ptr[j]));
s_pwent_struct.pw_uid = atoi(ptr);
break;
case 3:
ASSERT(1 <= field_len && field_len <= 9);
for (size_t j = 0; j < field_len; j++)
ASSERT(isdigit(ptr[j]));
s_pwent_struct.pw_uid = atoi(ptr);
break;
case 4:
break;
case 5:
s_pwent_struct.pw_dir = (char*)malloc(field_len + 1);
if (!s_pwent_struct.pw_dir) if (!s_pwent_struct.pw_dir)
return nullptr; return nullptr;
memcpy(s_pwent_struct.pw_dir, parts[5].data(), parts[5].size()); strcpy(s_pwent_struct.pw_dir, ptr);
s_pwent_struct.pw_dir[parts[5].size()] = '\0'; break;
case 6:
s_pwent_struct.pw_shell = (char*)malloc(parts[6].size() + 1); s_pwent_struct.pw_shell = (char*)malloc(field_len + 1);
if (!s_pwent_struct.pw_shell) if (!s_pwent_struct.pw_shell)
return nullptr; return nullptr;
memcpy(s_pwent_struct.pw_shell, parts[6].data(), parts[6].size()); strcpy(s_pwent_struct.pw_shell, ptr);
s_pwent_struct.pw_shell[parts[6].size()] = '\0'; break;
}
ptr = end + 1;
}
return &s_pwent_struct; return &s_pwent_struct;
} }

View File

@ -206,14 +206,13 @@ if [[ -f $BANAN_TOOLCHAIN_PREFIX/bin/$BANAN_TOOLCHAIN_TRIPLE_PREFIX-gcc ]]; then
fi fi
fi fi
BUILD_LIBSTDCPP=0 BUILD_LIBSTDCPP=$BUILD_GCC
#BUILD_LIBSTDCPP=$BUILD_GCC if ! (($BUILD_LIBSTDCPP)); then
#if ! (($BUILD_LIBSTDCPP)); then read -e -p "Do you want to rebuild libstdc++ [y/N]? " choice
# read -e -p "Do you want to rebuild libstdc++ [y/N]? " choice if [[ "$choice" == [Yy]* ]]; then
# if [[ "$choice" == [Yy]* ]]; then BUILD_LIBSTDCPP=1
# BUILD_LIBSTDCPP=1 fi
# fi fi
#fi
BUILD_GRUB=1 BUILD_GRUB=1
if [[ -f $BANAN_TOOLCHAIN_PREFIX/bin/grub-mkstandalone ]]; then if [[ -f $BANAN_TOOLCHAIN_PREFIX/bin/grub-mkstandalone ]]; then