/*
Name: input.c
Purpose: Command-line input reader with timeout.
Author: M. J. Fromberger
Info: $Id: input.c,v 1.3 2005/05/16 18:38:45 sting Exp $
Copyright (C) 2003 Michael J. Fromberger, All Rights Reserved.
Reads one line from the standard input with a timeout. If the timer
expires before a line becomes available, the default output string
is written to standard output. If a line does become available in
time, it is echoed to the standard output.
Usage: input [ []]
*/
#include
#include
#include
#include
#include
#include
char g_buf[BUFSIZ];
const char *g_output = "nothing\n";
unsigned long g_timeout = 10;
void time_out(int v);
int main(int argc, char *argv[])
{
if(argc > 1)
g_timeout = strtoul(argv[1], NULL, 0);
if(argc > 2)
g_output = argv[2];
signal(SIGALRM, time_out);
alarm(g_timeout);
fgets(g_buf, sizeof(g_buf), stdin);
alarm(0);
printf("%s", g_buf);
return 0;
}
void time_out(int v)
{
printf("%s\n", g_output);
exit(0);
}
/* Here there be dragons */