1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibCore: Use OSError in get_password() return type

This commit is contained in:
Andreas Kling 2021-01-10 13:48:05 +01:00
parent a8681da4bf
commit 70fce5c4c7
4 changed files with 15 additions and 15 deletions

View file

@ -32,31 +32,31 @@
namespace Core { namespace Core {
Result<String, int> get_password(const StringView& prompt) Result<String, OSError> get_password(const StringView& prompt)
{ {
fwrite(prompt.characters_without_null_termination(), sizeof(char), prompt.length(), stdout); if (write(STDOUT_FILENO, prompt.characters_without_null_termination(), prompt.length()) < 0)
fflush(stdout); return OSError(errno);
struct termios original; termios original {};
tcgetattr(STDIN_FILENO, &original); if (tcgetattr(STDIN_FILENO, &original) < 0)
return OSError(errno);
struct termios no_echo = original; termios no_echo = original;
no_echo.c_lflag &= ~ECHO; no_echo.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0)
return errno; return OSError(errno);
}
char* password = nullptr; char* password = nullptr;
size_t n = 0; size_t n = 0;
auto line_length = getline(&password, &n, stdin); auto line_length = getline(&password, &n, stdin);
int saved_errno = errno; auto saved_errno = errno;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original); tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
putchar('\n'); putchar('\n');
if (line_length < 0) if (line_length < 0)
return saved_errno; return OSError(saved_errno);
ASSERT(line_length != 0); ASSERT(line_length != 0);
@ -65,7 +65,6 @@ Result<String, int> get_password(const StringView& prompt)
String s(password); String s(password);
free(password); free(password);
return s; return s;
} }
} }

View file

@ -26,11 +26,12 @@
#pragma once #pragma once
#include <AK/OSError.h>
#include <AK/Result.h> #include <AK/Result.h>
#include <AK/String.h> #include <AK/String.h>
namespace Core { namespace Core {
Result<String, int> get_password(const StringView& prompt = "Password: "); Result<String, OSError> get_password(const StringView& prompt = "Password: ");
} }

View file

@ -124,7 +124,7 @@ int main(int argc, char** argv)
} else { } else {
auto new_password = Core::get_password("New password: "); auto new_password = Core::get_password("New password: ");
if (new_password.is_error()) { if (new_password.is_error()) {
warnln("{}", strerror(new_password.error())); warnln("{}", new_password.error());
return 1; return 1;
} }

View file

@ -75,7 +75,7 @@ int main(int argc, char** argv)
if (getuid() != 0 && account.has_password()) { if (getuid() != 0 && account.has_password()) {
auto password = Core::get_password(); auto password = Core::get_password();
if (password.is_error()) { if (password.is_error()) {
warnln("{}", strerror(password.error())); warnln("{}", password.error());
return 1; return 1;
} }