1 | /*
|
---|
2 | * Test name: test10.c
|
---|
3 | *
|
---|
4 | * Objetive: The purpose of this test is to make sure that select works
|
---|
5 | * when working with the terminal with timeouts
|
---|
6 | *
|
---|
7 | * Description: This tests wait entry from stdin using select and displays
|
---|
8 | * it again in stdout when it is ready to write (which is always). It has
|
---|
9 | * a timeout value as well.
|
---|
10 | *
|
---|
11 | * Jose M. Gomez
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include <time.h>
|
---|
15 | #include <sys/types.h>
|
---|
16 | #include <sys/asynchio.h>
|
---|
17 | #include <fcntl.h>
|
---|
18 | #include <unistd.h>
|
---|
19 | #include <sys/select.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <limits.h>
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 | void main(void) {
|
---|
26 | fd_set fds_read, fds_write;
|
---|
27 | int retval;
|
---|
28 | char data[1024];
|
---|
29 | struct timeval timeout;
|
---|
30 |
|
---|
31 | while(1) {
|
---|
32 | timeout.tv_sec = 3;
|
---|
33 | timeout.tv_usec = 0;
|
---|
34 | FD_ZERO(&fds_read);
|
---|
35 | FD_ZERO(&fds_write);
|
---|
36 | FD_SET(0, &fds_read);
|
---|
37 | FD_SET(1, &fds_write);
|
---|
38 | printf("Input some data: ");
|
---|
39 | fflush(stdout);
|
---|
40 | retval=select(3, &fds_read, NULL, NULL, &timeout);
|
---|
41 | if (retval < 0) {
|
---|
42 | fprintf(stderr, "Error while executing select\n");
|
---|
43 | exit(-1);
|
---|
44 | }
|
---|
45 | if (retval == 0) {
|
---|
46 | printf("\n Hey! Feed me some data!\n");
|
---|
47 | fflush(stdout);
|
---|
48 | continue;
|
---|
49 | }
|
---|
50 | if (!FD_ISSET(0, &fds_read)) {
|
---|
51 | fprintf(stderr, "Error: stdin not ready (?)\n");
|
---|
52 | exit(-1);
|
---|
53 | }
|
---|
54 | gets(data);
|
---|
55 | if (!strcmp(data, "exit"))
|
---|
56 | exit(0);
|
---|
57 | printf("Try to write it back\n");
|
---|
58 | retval=select(3, NULL, &fds_write, NULL, NULL);
|
---|
59 | if (retval < 0) {
|
---|
60 | fprintf(stderr, "Error while executing select\n");
|
---|
61 | exit(-1);
|
---|
62 | }
|
---|
63 | if (!FD_ISSET(1, &fds_write)) {
|
---|
64 | fprintf(stderr, "Error: stdout not ready (?)\n");
|
---|
65 | exit(-1);
|
---|
66 | }
|
---|
67 | printf("Data: %s\n", data);
|
---|
68 | }
|
---|
69 | }
|
---|