⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 play_again2.c

📁 unix linux 编程实践源代码
💻 C
字号:
/* play_again2.c *	purpose: ask if user wants another transaction *	 method: set tty into char-by-char mode and no-echo mode *		 read char, return result *	returns: 0=>yes, 1=>no *	 better: timeout if user walks away *	 */#include	<stdio.h>#include	<termios.h>#define	QUESTION	"Do you want another transaction"main(){	int	response;	tty_mode(0);				/* save mode */	set_cr_noecho_mode();			/* set -icanon, -echo	*/	response = get_response(QUESTION);	/* get some answer	*/	tty_mode(1);				/* restore tty state	*/	return response;}int get_response(char *question)/* * purpose: ask a question and wait for a y/n answer *  method: use getchar and ignore non y/n answers * returns: 0=>yes, 1=>no */{	printf("%s (y/n)?", question);	while(1){		switch( getchar() ){			case 'y': 			case 'Y': return 0;			case 'n': 			case 'N': 			case EOF: return 1;		}	}}set_cr_noecho_mode()/*  * purpose: put file descriptor 0 into chr-by-chr mode and noecho mode *  method: use bits in termios */{	struct	termios	ttystate;	tcgetattr( 0, &ttystate);		/* read curr. setting	*/	ttystate.c_lflag    	&= ~ICANON;	/* no buffering		*/	ttystate.c_lflag    	&= ~ECHO;	/* no echo either	*/	ttystate.c_cc[VMIN]  	=  1;		/* get 1 char at a time	*/	tcsetattr( 0 , TCSANOW, &ttystate);	/* install settings	*/}/* how == 0 => save current mode,  how == 1 => restore mode */tty_mode(int how){	static struct termios original_mode;	if ( how == 0 )		tcgetattr(0, &original_mode);	else		return tcsetattr(0, TCSANOW, &original_mode); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -