> For the complete documentation index, see [llms.txt](https://applezu.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://applezu.netdpi.net/linux-prog/sample-code-of-read-stdin.md).

# 讀取 STDIN (Standard INPUT) 範例程式

```c
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/* return value:
 * END_OF_FILE: End of File
 * More than 0: read bytes from stdin
 * Less than 0: error
 * 0 : nodata
 */
int get_stdin(int block, char *buf, int buflen)
{
    /* man 2 select, for details */
    struct timeval tv;
    fd_set    rfds;
        int retval;
    char *ret;

    tv.tv_sec = TIME_OUT_S;
    tv.tv_usec = TIME_OUT_US; 

        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET( STDIN, &rfds);

    if(block) 
        retval = select( ( STDIN + 1), &rfds, NULL, NULL, &tv);
    else
            retval = select( ( STDIN + 1), &rfds, NULL, NULL, NULL);
        /* Don't rely on the value of tv now! */

    //} else if( FD_ISSET( STDIN, &rfds) ) {

    if( retval > 0 ) {

        bzero(buf, buflen);

        /* read() retval:0 endoffile, retval:-1 error*/

        /* gets() ignore the newline and ENDOFFILE */
        ret = gets( buf );

        if(ret == NULL)
            return END_OF_FILE;
    } 

    /* error: -1 , no data: 0, read bytes number */ 
    return retval;

}c
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://applezu.netdpi.net/linux-prog/sample-code-of-read-stdin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
