I have a c program It uses tcgetattr and tcsetattr, which stop echo of user input.
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
int
main(int argc, char **argv)
{
struct termios oflags, nflags;
char password[64];
/* disabling echo */
tcgetattr(fileno(stdin), &oflags);
nflags = oflags;
nflags.c_lflag &= ~ECHO;
nflags.c_lflag |= ECHONL;
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
printf("password: ");
fgets(password, sizeof(password), stdin);
password[strlen(password) - 1] = 0;
printf("you typed '%s'\n", password);
/* restore terminal */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
perror("tcsetattr");
return EXIT_FAILURE;
}
return 0;
}
I want to execute this program using shell script and give some input to it. Following steps from here I tried
$ ./test <<EOF
> hello
> EOF
and
$ ./test <<<'hello'
and
$ ./test <input
and
$ cat input | ./test
but all above method gave me tcsetattr: Inappropriate ioctl for device
error
What is the appropriate way to run such program adding it to shell script? Or can we run it from python? If yes, how to pass inputs from python to c program?