From 70fce5c4c7279c045e593a8a3a6f79ebc28e2972 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 Jan 2021 13:48:05 +0100 Subject: LibCore: Use OSError in get_password() return type --- Libraries/LibCore/GetPassword.cpp | 23 +++++++++++------------ Libraries/LibCore/GetPassword.h | 3 ++- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'Libraries/LibCore') 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: "); } -- cgit v1.2.3