//------------------------------------------------------------------- // dynaview.cpp // // This program produces a dynamic view of the volatile text in // a Linux pseudo-file whose filename is supplied as a command- // line argument (provided this file's contents does not exceed // the bounds of the current terminal window). It continuously // rereads and redraws this file's text, hiding cursor-movement // in order to eliminate the distraction of its screen-flashes, // and 'non-canonical' keyboard-input is employed so keypresses // do not get echoed. The user quits by just hitting . // // to compile: $ g++ dynaview.cpp -o dynaview // to execute: $ ./dynaview // // programmer: ALLAN CRUSE // written on: 24 AUG 2009 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read() #include // for strncpy() #include // for tcgetattr(), tcsetattr() char filename[ 64 ] = "/proc/interrupts"; int main( int argc, char **argv ) { if ( argc > 1 ) strncpy( filename, argv[1], 63 ); int fd = open( filename, O_RDONLY ); if ( fd < 0 ) { perror( filename ); exit(1); } struct termios orig_tty; tcgetattr( STDIN_FILENO, &orig_tty ); struct termios work_tty = orig_tty; work_tty.c_lflag &= ~( ECHO | ICANON | ISIG ); work_tty.c_cc[ VMIN ] = 0; tcsetattr( STDIN_FILENO, TCSAFLUSH, &work_tty ); printf( "\e[H\e[J" ); // clear the screen printf( "\e[?25l" ); // hide the cursor int done = 0; while ( !done ) { char buf[ 4096 ] = { 0 }; lseek( fd, 0, SEEK_SET ); int nbytes = read( fd, buf, sizeof( buf ) ); if ( nbytes < 0 ) { perror( "read" ); break; } printf( "\e[1;1H\e[7m FILENAME: \'%s\' \e[0m\n%s\e[J", filename, buf ); int inch = 0; read( STDIN_FILENO, &inch, sizeof( inch ) ); if ( inch == 0x1B ) done = 1; usleep( 100000 ); } printf( "\e[?25h" ); // show the cursor tcsetattr( STDIN_FILENO, TCSAFLUSH, &orig_tty ); }