#include <termios.h>
struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
};
#include <termios.h>
int tcgetattr(int fd, struct termios *termios_p);
#include <termios.h>
int tcsetattr(int fd, int actions, const struct termios *termios_p);
[root@localhost ~]# stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;
swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
stty sane
stty -g > save_stty
..
<修改终端设置>
..
stty $(cat save_stty)
save_stty = "$(stty -g)"
<alter stty setting>
stty $save_stty
stty -icanon min 1 time 0
[root@localhost ~]# stty -icanon min 1 time 0
[root@localhost ~]# cd C
[root@localhost C]# ./menu-1.exe
Choice: Please select an cation
a - add new record
d - delete record
q - quit
aYou have chosen: a #这里,在敲入 a 时,程序马上显示出 You have chosen: a 而不需要输入回车
Choice: Please select an cation
a - add new record
d - delete record
q - quit
dYou have chosen: d
Choice: Please select an cation
a - add new record
d - delete record
q - quit
[root@localhost C]# stty $(cat ../save_stty)
[root@localhost C]# ./menu-1.exe
Choice: Please select an cation
a - add new record
d - delete record
q - quit
a #这里,输入 a 后需要再输入回车才可以继续运行程序
You have chosen: a
Choice: Please select an cation
a - add new record
d - delete record
q - quit
stty -echo
stty echo #可见,有个 - 号就是关闭,没有 - 号就恢复
#include <termios.h>
speed_t cfgetispeed(const struct termios *);
speed_t cfgetospeed(const struct termios *);
int cfsetispeed(struct termios *, speed_t speed);
int cfsetospeed(struct termios *, speed_t speed);
#include <termios.h>
int tcdrain(int fd);
int tcflow(int fd, int flowtype);
int tcflush(int fd, int in_out_selector);
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#define PASSWORD_LEN 8
int main()
{
struct termios initialrsettings, newrsettings; /*声明两个 termios 型数组*/
char password[PASSWORD_LEN + 1]; /*密码存储数组*/
tcgetattr(fileno(stdin), &initialrsettings); /*得到相应终端控制参数*/
newrsettings = initialrsettings; /*initialrsettings为备份,newrsetting为需更改的参数*/
newrsettings.c_lflag &= ~ECHO; /*屏蔽回显*/
printf("Enter password: ");
if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) {
fprintf(stderr, "Could not set attributes\\n");
}
else {
fgets(password, PASSWORD_LEN, stdin);
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings); /*恢复*/
fprintf(stdout, "\\nYou entered %s\\n", password);
}
exit(0);
}
[root@localhost C]# ./password.exe
Enter password: #这里输入密码不回显
You entered love
/*程序功能:读取每一个字符*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
char *menu[] = {
"a - add new record",
"d - delete record",
"q - quit",
NULL,
};
int getchoice(char *greet, char *choices[], FILE *in, FILE *out);
int main()
{
int choice = 0;
FILE *input;
FILE *output;
struct termios initial_settings, new_settings;
if(!isatty(fileno(stdout))) {
fprintf(stderr, "You are not a terminal, OK.\\n");
}
input = fopen("/dev/tty", "r");
output = fopen("/dev/tty", "w");
if(!input || !output) {
fprintf(stderr, "Unable to open /dev/tty\\n");
exit(1);
}
/*进行终端各个参数的设置*/
/*获取与标准输入相关联的终端控制属性*/
tcgetattr(fileno(input), &initial_settings);
new_settings = initial_settings; /*转存*/
new_settings.c_lflag &= ~ICANON; /*非标准模式*/
new_settings.c_lflag &= ~ECHO; /*不回显*/
new_settings.c_cc[VMIN] = 1; /*读到1个字符时就返回*/
new_settings.c_cc[VTIME] = 0; /*不设置时间影响*/
new_settings.c_lflag &= ~ISIG; /*终端不响应信号*/
if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) {
fprintf(stderr, "could not set attributes\\n");
}
do {
choice = getchoice("Please select an action", menu, input, output);
printf("You have chosen: %c\\n", choice);
} while(choice != 'q');
tcsetattr(fileno(input), TCSANOW, &initial_settings);
exit(0);
}
int getchoice(char *greet, char *choices[], FILE *in, FILE *out)
{
int chosen = 0;
int selected;
char **option;
do {
fprintf(out, "choice: %s\\n", greet);
option = choices;
while(*option) { /*打印menu菜单*/
fprintf(stdout, "%s\\n", *option);
option++;
}
do {
selected = fgetc(in); /*读取一个字符,遇到回车或换行则继续读取*/
} while(selected == '\\n' || selected == '\\r');
option = choices;
while(*option) { /*查询输入的字符和menu中的首字符是否一样*/
if(selected == *option[0]) {
chosen = 1;
break;
}
option++;
}
if(!chosen) { /*错误选择*/
fprintf(out, "Incorrect choice, select again\\n");
}
} while(!chosen); /*错误选择则需重新输入*/
return selected; /*输入正确返回选择结果*/
}
[root@localhost C]# ./password-2.exe
choice: Please select an action
a - add new record
d - delete record
q - quit
You have chosen: a #每次输入都会理解响应
choice: Please select an action
a - add new record
d - delete record
q - quit
You have chosen: d
choice: Please select an action #这里按下 Ctrl+C 测试,结果程序并不相应信号,这是因为程序中的 ~ISIG 的缘故
a - add new record
d - delete record
q - quit
Incorrect choice, select again
choice: Please select an action
a - add new record
d - delete record
q - quit
You have chosen: q
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |