/* rotate.c : map a->b, b->c, .. z->a * purpose: useful for showing tty modes */ #include #include #include #include #include int main() { int c; signal( SIGINT, SIG_IGN ); signal( SIGTSTP, SIG_IGN ); signal( SIGQUIT, SIG_IGN ); tty_mode(0); /* save current mode */ set_cr_noecho_mode(); /* set -icanon, -echo */ while ( ( c=getchar() ) != EOF ){ if ( c == 'Q') { tty_mode(1); exit(0); } else if ( c == 'z' ) c = 'a'; else if (islower(c)) c++; putchar(c); } } 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 */ } set_nodelay_mode() /* * purpose: put file descriptor 0 into no-delay mode * method: use fcntl to set bits * notes: tcsetattr() will do something similar, but it is complicated */ { int termflags; termflags = fcntl(0, F_GETFL); /* read curr. settings */ termflags |= O_NDELAY; /* flip on nodelay bit */ fcntl(0, F_SETFL, termflags); /* and install 'em */ } /* how == 0 => save current mode, how == 1 => restore mode */ /* this version handles termios and fcntl flags */ tty_mode(int how) { static struct termios original_mode; static int original_flags; if ( how == 0 ){ tcgetattr(0, &original_mode); original_flags = fcntl(0, F_GETFL); } else { tcsetattr(0, TCSANOW, &original_mode); fcntl( 0, F_SETFL, original_flags); } }