[9] | 1 | /*
|
---|
| 2 | * Test name: test09.c
|
---|
| 3 | *
|
---|
| 4 | * Objetive: The purpose of this test is to make sure that select works
|
---|
| 5 | * when working with the terminal.
|
---|
| 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)
|
---|
| 9 | *
|
---|
| 10 | * Jose M. Gomez
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include <sys/types.h>
|
---|
| 14 | #include <fcntl.h>
|
---|
| 15 | #include <unistd.h>
|
---|
| 16 | #include <sys/select.h>
|
---|
| 17 | #include <stdio.h>
|
---|
| 18 | #include <stdlib.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | #include <limits.h>
|
---|
| 21 |
|
---|
| 22 | void main(void) {
|
---|
| 23 | fd_set fds_read, fds_write;
|
---|
| 24 | int retval;
|
---|
| 25 | char data[1024];
|
---|
| 26 |
|
---|
| 27 | FD_ZERO(&fds_read);
|
---|
| 28 | FD_ZERO(&fds_write);
|
---|
| 29 | FD_SET(0, &fds_read); /* stdin */
|
---|
| 30 | FD_SET(1, &fds_write); /* stdout */
|
---|
| 31 |
|
---|
| 32 | while(1) {
|
---|
| 33 | printf("Input some data: ");
|
---|
| 34 | fflush(stdout);
|
---|
| 35 | retval=select(3, &fds_read, NULL, NULL, NULL);
|
---|
| 36 | if (retval < 0) {
|
---|
| 37 | fprintf(stderr, "Error while executing select\n");
|
---|
| 38 | exit(-1);
|
---|
| 39 | }
|
---|
| 40 | printf("select retval: %d\n", retval);
|
---|
| 41 | if (!FD_ISSET(0, &fds_read)) {
|
---|
| 42 | fprintf(stderr, "Error: stdin not ready (?)\n");
|
---|
| 43 | exit(-1);
|
---|
| 44 | }
|
---|
| 45 | printf("gets..\n");
|
---|
| 46 | gets(data);
|
---|
| 47 | printf("gets done..\n");
|
---|
| 48 | if (!strcmp(data, "exit"))
|
---|
| 49 | exit(0);
|
---|
| 50 | printf("Try to write it back\n");
|
---|
| 51 | retval=select(3, NULL, &fds_write, NULL, NULL);
|
---|
| 52 | if (retval < 0) {
|
---|
| 53 | fprintf(stderr, "Error while executing select\n");
|
---|
| 54 | exit(-1);
|
---|
| 55 | }
|
---|
| 56 | if (!FD_ISSET(1, &fds_write)) {
|
---|
| 57 | fprintf(stderr, "Error: stdout not ready (?)\n");
|
---|
| 58 | exit(-1);
|
---|
| 59 | }
|
---|
| 60 | printf("Data: %s\n", data);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|