]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: add helper for disabling terminal echo in termios struct
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Dec 2023 11:01:32 +0000 (12:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 21 Dec 2023 18:15:01 +0000 (19:15 +0100)
src/basic/terminal-util.c
src/basic/terminal-util.h
src/shared/ask-password-api.c

index 3355b749cc01c8af2b2bf3f557140570dc778a25..63aca3684f25b916c44093633896311742133e02 100644 (file)
@@ -1551,3 +1551,11 @@ int set_terminal_cursor_position(int fd, unsigned int row, unsigned int column)
 
         return 0;
 }
+
+void termios_disable_echo(struct termios *termios) {
+        assert(termios);
+
+        termios->c_lflag &= ~(ICANON|ECHO);
+        termios->c_cc[VMIN] = 1;
+        termios->c_cc[VTIME] = 0;
+}
index cae42887c44a89659bde1d92c435f7cbe08813b0..d7537c23bff800278d3173f1458520b447badb3b 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <syslog.h>
 #include <sys/types.h>
+#include <termios.h>
 
 #include "macro.h"
 #include "time-util.h"
@@ -168,7 +169,6 @@ bool underline_enabled(void);
 bool dev_console_colors_enabled(void);
 
 static inline bool colors_enabled(void) {
-
         /* Returns true if colors are considered supported on our stdout. */
         return get_color_mode() != COLOR_OFF;
 }
@@ -286,3 +286,5 @@ static inline const char* ansi_highlight_green_red(bool b) {
 
 /* This assumes there is a 'tty' group */
 #define TTY_MODE 0620
+
+void termios_disable_echo(struct termios *termios);
index 0e323f4644e576274a1840896264147b5a81df23..6d71245549d66f3ee84969918d2fa2161061b7b2 100644 (file)
@@ -443,9 +443,7 @@ int ask_password_tty(
                         (void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX);
 
                 new_termios = old_termios;
-                new_termios.c_lflag &= ~(ICANON|ECHO);
-                new_termios.c_cc[VMIN] = 1;
-                new_termios.c_cc[VTIME] = 0;
+                termios_disable_echo(&new_termios);
 
                 r = RET_NERRNO(tcsetattr(ttyfd, TCSADRAIN, &new_termios));
                 if (r < 0)