1, P168
A complete terminal output selection menu for the program.
#include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h># include<math.h> #include <termios.h> #include <term.h> #include <curses.h> #include < stdlib.h> int Getchoice (char*, const char**, file*,file*), Static file* output_stream= (file*) 0;char* opt[]={"A--add a New record "," D--delete a new record "," Q--Quit ", Null};int char_to_terminal (int char_to_write) {if (Output_stream) PUTC (c Har_to_write,output_stream); return 0;} int Getchoice (char* words,const char** opt,file* in,file* out) {int choice;char** tmp_opt=opt;int screenrow=4;int Screencol=10;char *cursor,*clear;output_stream=out;while (1) {//Change output to clear screen, then upper left corner output, screenrow=4;setupterm (NULL, Fileno (out), (int *) 0), CURSOR=TIGETSTR ("Cup"), Clear=tigetstr ("clear"), Tputs (clear,1, (int *) char_to_terminal); Tputs (Tparm (Cursor,screenrow,screencol), 1,char_to_terminal); fprintf (out, "choice:\n"); Screenrow+=2;while (*tmp_ opt) {tputs (Tparm (cursor,screenrow,screencol), 1,char_to_terminal); fprintf (out, "%s\n", *tmp_opt); tmp_opt++;screenrow++;} fprintf (out, "\ n");d O{fflush (out); choice=fgetc (in);} while (choice== ' \ n ' | | choice== ' \ r '); Tmp_opt=opt;while (*tmp_opt) {if (choice==*tmp_opt[0]) {return choice;} else{tmp_opt++;}} Tputs (Tparm (Cursor,screenrow,screencol), 1,char_to_terminal); fprintf (stderr, "incorrect Choice,select again\n");}} int main (int argc,char** argv) {int choice=0; file* In=fopen ("/dev/tty", "R"); file* Out=fopen ("/dev/tty", "w"); if (!in| |! Out) {fprintf (stderr, "%s\n", "unable to open/dev/tty \ n"); exit (1);} struct Termios tmp_tos,back_tos;tcgetattr (Fileno (in), &tmp_tos); Back_tos=tmp_tos;tmp_tos.c_lflag &= ~ICANON ; Tmp_tos.c_lflag &= ~echo;tmp_tos.c_lflag &= ~isig;tmp_tos.c_cc[vmin] = 1;tmp_tos.c_cc[vtime] = 0;if (tcsetattr (Fileno (in), Tcsanow,&tmp_tos)!=0) {fprintf (stderr, "%s\n", "unable to set/dev/tty \ n");} while (choice!= ' Q ') {choice=getchoice ("Please select a action", Opt,in,out) fprintf (out, "You have choose:%c \ n", choice ); sleep (1);} Tcsetattr (Fileno (in), Tcsanow,&back_tos); return 0;}
1, P171
A program that detects keystroke events.
#include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h># include<math.h> #include <termios.h> #include <term.h> #include <curses.h> #include < stdlib.h> static struct Termios initsetting,newsetting;void close_keybord (); void Init_keybord (); int readch (); int Kbhit (); int main (int argc,char** argv) {int Choice=0;init_keybord (); while (choice!= ' Q ') {printf ("looping\n"); Sleep (1) if (Choice=kbhit ()) {CHOICE=READCH ();p rintf ("You have key:%c \ n", choice);}} Close_keybord (); return 0;} void Close_keybord () {tcsetattr (0,tcsanow,&initsetting);} void Init_keybord () {tcgetattr (0,&initsetting); Newsetting=initsetting;newsetting.c_lflag &= ~ICANON; Newsetting.c_lflag &= ~echo;newsetting.c_lflag &= ~isig;newsetting.c_cc[vmin] = 1;newsetting.c_cc[VTIME] = 0; Tcsetattr (0,tcsanow,&newsetting);} int Readch () {}int kbhit () {char ch;int nread;newsetting.c_cc[vmin] = 0;tcsetattr (0,tcsanow,&newsetting); nread= Read (0,& ch,1); Newsetting.c_cc[vmin] = 1;tcsetattr (0,tcsanow,&newsetting); if (nread==1) {return ch;} return 0;}
"Fourth edition of Linux Programming", chapter fifth of the exercises