From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/05.3.c | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/05.3.c (limited to 'reference/C/CONTRIB/OR_USING_C/05.3.c') diff --git a/reference/C/CONTRIB/OR_USING_C/05.3.c b/reference/C/CONTRIB/OR_USING_C/05.3.c new file mode 100644 index 0000000..9457d07 --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/05.3.c @@ -0,0 +1,61 @@ +#include +#include +#include + +main() +{ + int n, nfds; + char buf[32]; + fd_set readfds; + struct timeval tv; + + /* + * We will be reading from standard input (file + * descriptor 0), so we want to know when the + * user has typed something. + */ + FD_ZERO(&readfds); + FD_SET(0, &readfds); + + /* + * Set the timeout for 15 seconds. + */ + tv.tv_sec = 15; + tv.tv_usec = 0; + + /* + * Prompt for input. + */ + printf("Type a word; if you don't in 15 "); + printf("seconds I'll use \"WORD\": "); + fflush(stdout); + + /* + * Now call select. We pass NULL for + * writefds and exceptfds, since we + * aren't interested in them. + */ + nfds = select(1, &readfds, NULL, NULL, &tv); + + /* + * Now we check the results. If nfds is zero, + * then we timed out, and should assume the + * default. Otherwise, if file descriptor 0 + * is set in readfds, that means that it is + * ready to be read, and we can read something + * from it. + */ + if (nfds == 0) { + strcpy(buf, "WORD"); + } + else { + if (FD_ISSET(0, &readfds)) { + n = read(0, buf, sizeof(buf)); + buf[n-1] = '\0'; + } + } + + printf("\nThe word is: %s\n", buf); + exit(0); +} + -- cgit v1.2.3-54-g00ecf