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
Notes: sideshowbarker 2024-07-18 23:57:47 +09:00
4 changed files with 15 additions and 15 deletions

View file

@ -32,31 +32,31 @@
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);
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<String, int> get_password(const StringView& prompt)
String s(password);
free(password);
return s;
}
}

View file

@ -26,11 +26,12 @@
#pragma once
#include <AK/OSError.h>
#include <AK/Result.h>
#include <AK/String.h>
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 {
auto new_password = Core::get_password("New password: ");
if (new_password.is_error()) {
warnln("{}", strerror(new_password.error()));
warnln("{}", new_password.error());
return 1;
}

View file

@ -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;
}