1
私はCプログラムを持っています ユーザ入力のエコーを停止するtcgetattrとtcsetattrを使用します。シェルスクリプトからCプログラムに入力を送信
#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;
}
このプログラムをシェルスクリプトを使用して実行し、それに何らかの入力をしたいとします。私は
$ ./test <<EOF
> hello
> EOF
と
$ ./test <<<'hello'
と
$ ./test <input
と
$ cat input | ./test
が、すべてのメソッドの上にしようとしたhereから次の手順では、私を与えたtcsetattr: Inappropriate ioctl for device
エラー
シェルスクリプトに追加するプログラムを実行する適切な方法は何ですか? または、Pythonから実行できますか?はいの場合は、どのようにPythonからCプログラムに入力を渡すのですか?
@andlrc重複する質問は、「標準が存在するかどうかを確認する方法」を尋ねます。私のプログラムではstdinは存在していますが、tcgetattrとtcsetattrを使ってエコーをオフにしました。 – hiabcwelcome
'#include int isatty(int fd); ' –
wildplasser
@wildplasser説明できますか?私はあなたが何を言っているのか理解していません – hiabcwelcome