Reputation: 205
I compile and run the C program below on WSL2 Ubuntu, it's supposed to turn echo off on standard input, let the user type a line of text, show them what they typed, and turn echo back on. But the terminal echoes. What could be wrong?
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int main(int argc, void **argv) {
struct termios term;
char input[256];
if(tcgetattr(STDIN_FILENO, &term) < 0) {
perror("tcgetattr");
return 1;
}
printf("term.c_iflag: %x\n", term.c_iflag);
term.c_iflag &= ~ECHO;
if(tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
perror("tcsetattr");
return 1;
}
printf("Echo mode disabled.\n");
printf("term.c_iflag: %x\n", term.c_iflag);
fgets(input, sizeof(input), stdin);
printf("Entered: %s\n", input);
term.c_iflag |= ECHO;
if(tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
perror("tcsetattr");
return 1;
}
printf("term.c_iflag: %x\n", term.c_iflag);
return 0;
}
The output looks like this, and the echo is not turned off.
term.c_iflag: 50a
Echo mode disabled.
term.c_iflag: 502
this should not be visible
Entered: this should not be visible
term.c_iflag: 50a
Upvotes: 0
Views: 32