diff --git a/Libraries/LibCore/GetPassword.cpp b/Libraries/LibCore/GetPassword.cpp index 028e987c59..dfcff21583 100644 --- a/Libraries/LibCore/GetPassword.cpp +++ b/Libraries/LibCore/GetPassword.cpp @@ -32,31 +32,31 @@ namespace Core { -Result get_password(const StringView& prompt) +Result get_password(const StringView& prompt) { - fwrite(prompt.characters_without_null_termination(), sizeof(char), prompt.length(), stdout); - fflush(stdout); + if (write(STDOUT_FILENO, prompt.characters_without_null_termination(), prompt.length()) < 0) + return OSError(errno); - struct termios original; - tcgetattr(STDIN_FILENO, &original); + termios original {}; + if (tcgetattr(STDIN_FILENO, &original) < 0) + return OSError(errno); - struct termios no_echo = original; + termios no_echo = original; no_echo.c_lflag &= ~ECHO; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0) { - return errno; - } + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0) + return OSError(errno); char* password = nullptr; size_t n = 0; auto line_length = getline(&password, &n, stdin); - int saved_errno = errno; + auto saved_errno = errno; tcsetattr(STDIN_FILENO, TCSAFLUSH, &original); putchar('\n'); if (line_length < 0) - return saved_errno; + return OSError(saved_errno); ASSERT(line_length != 0); @@ -65,7 +65,6 @@ Result get_password(const StringView& prompt) String s(password); free(password); - return s; } } diff --git a/Libraries/LibCore/GetPassword.h b/Libraries/LibCore/GetPassword.h index b0993c56f3..c62a071935 100644 --- a/Libraries/LibCore/GetPassword.h +++ b/Libraries/LibCore/GetPassword.h @@ -26,11 +26,12 @@ #pragma once +#include #include #include namespace Core { -Result get_password(const StringView& prompt = "Password: "); +Result get_password(const StringView& prompt = "Password: "); } diff --git a/Userland/passwd.cpp b/Userland/passwd.cpp index 7735dfd887..b0a36e437a 100644 --- a/Userland/passwd.cpp +++ b/Userland/passwd.cpp @@ -124,7 +124,7 @@ int main(int argc, char** argv) } else { auto new_password = Core::get_password("New password: "); if (new_password.is_error()) { - warnln("{}", strerror(new_password.error())); + warnln("{}", new_password.error()); return 1; } diff --git a/Userland/su.cpp b/Userland/su.cpp index f69fe80ad5..3377615570 100644 --- a/Userland/su.cpp +++ b/Userland/su.cpp @@ -75,7 +75,7 @@ int main(int argc, char** argv) if (getuid() != 0 && account.has_password()) { auto password = Core::get_password(); if (password.is_error()) { - warnln("{}", strerror(password.error())); + warnln("{}", password.error()); return 1; }