Tomek Dobrzynski
Tomek Dobrzynski

Reputation: 205

Why does disabling echo on WSL2 not work?

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

Answers (1)

Tomek Dobrzynski
Tomek Dobrzynski

Reputation: 205

ECHO is a bit in c_lflag not c_iflag.

Upvotes: 0

Related Questions