[ale] keystrokes

Jacob Langseth dragon at cc.gatech.edu
Wed Oct 30 22:53:37 EST 1996


ok, perhaps I was a bit hasty w/ my last response -- all _either_ of you
would see is *'s.  This should work correctly, however.  Invoke as
	PASS=`tread -p 30`
to have it read a passphrase and abort after thirty seconds of inactivity.

enjoy,
    -JwL

/* tread.c	Jacob Langseth <dragon at cc.gatech.edu>

   tread -- read a line, abort after a specified period of inactivity.

   USAGE:  tread  [-p]  <timeout>
	-p	passphrase mode -- disables echo to the terminal and reads
		directly from the tty.

  96.10.30 JwL	created
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <termios.h>

static FILE *f;
static struct termios saved_tio;

void usage( void )
{
    fprintf( stderr, "USAGE:  tread  [-p]  timeout\n" );
    fprintf( stderr, "    -p\tpassphrase mode -- do not echo to the " );
    fprintf( stderr, "terminal and read directly\n\tfrom the tty.\n" );
    exit( -1 );
}

void cleanup( void )
{
    if (f != stdin) {
	tcsetattr( fileno( f ), TCSANOW, &saved_tio );
	fclose( f );
    }
}

void sigcatch( int nSigType )
{
    cleanup();
    exit( 1 );
}

int main( int argc, char **argv )
{
    unsigned int uTimeout=0, c;
    struct termios tio;
    char *p;

    /* process command line */
    argc--;
    if (! argc)
	usage();
    argv++;

    /* check for -p, initialize input handle */
    if (strncmp( *argv, "-p", 2 )) {
	f = stdin;
    } else {
	/* passphrase mode */
	argc--;
	if (! argc)
	    usage();
	argv++;

	/* open the tty for reading */
	f = fopen( "/dev/tty", "r" );
	if (! f) {
	    perror( "unable to open tty" );
	    return( -1 );
	}

	/* disable echo to the terminal */
	tcgetattr( fileno( f ), &tio );
	saved_tio = tio;
	tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
	tcsetattr( fileno(f), TCSANOW, &tio );
    }

    /* convert timeout value */
    p = *argv;
    while( *p ) {
	if ((*p < '0') || (*p > '9')) {
	    cleanup();
	    fprintf( stderr, "ERROR:  timeout should be a number >= 0\n\n" );
	    usage();
	}
	uTimeout = 10 * uTimeout + *p - '0';
	p++;
    }

    /* read input, abort after uTimeout seconds of inactivity */
    signal( SIGALRM, sigcatch );
    alarm( uTimeout );
    while((c = getchar()) != '\n') {
	alarm( 0 );
	putchar( c );
	alarm( uTimeout );
    }
    alarm( 0 );

    cleanup();
    return( 0 );
}

-- 
      /          "Meddle not in the affairs of dragons, for             \
 *}=={*}>======-  thou art crunchy and go well with ketchup."  -======<{*}=={*
      \                    <dragon at cc.gatech.edu>                       /
    Musashi          - - -=- Finger for PGP key -=- - -             Musashi






More information about the Ale mailing list