Need Non-Blocking Terminal Input Function For Berkeley 4.2

mo at wgivax.UUCP mo at wgivax.UUCP
Fri Jan 24 23:29:11 AEST 1986


If you are willing to write a simple input manager, you could use getch()
with alarm() and longjmp/setjmp() to manage your problem.

For instance:

#include <stdio.h>
#include <setjmp.h>
#include <signal.h>

jmp_buf xyz;

main()
{
    int trapalarm();
    char string[80];

    do {
        fprintf(stderr,"Please enter string: ");
        signal(SIGALRM, trapalarm);
        alarm(5);

        if(setjmp(xyz) == 0)
        {
            get_input(string);
            fprintf(stderr,"%s",string);
        }
        else
        {
            fprintf(stderr,"no input\n");
            continue;
        }

    } while(strcmp(string,"end"));
}

get_input(str)
char *str;
{
    register char *c;

    c = str;

    do {
        *c = getchar();
        alarm(0);
    } while(*c++ != '\n');

    *c = 0;
}


trapalarm()
{
    alarm(0);
    longjmp(xyz,1);
}

===============================================================================

              Mike O'Shea   (decvax!mcnc!unccvax!wgivax!mo)



More information about the Comp.unix mailing list