mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-03 23:25:20 +00:00
LibCore: Use OSError in get_password() return type
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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: ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user