blob: 35cb41081d7454932ca97fea94956bfc5c4ebdb0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// Functions for working with stdio streams (as opposed to RStream/WStream).
#include <stdio.h>
#include <stdbool.h>
#include <uv.h>
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/stream.c.generated.h"
#endif
/// Sets the stream associated with `fd` to "blocking" mode.
///
/// @return `0` on success, or `-errno` on failure.
int stream_set_blocking(int fd, bool blocking)
{
int flags = fcntl(fd, F_GETFL, 0);
int err = 0;
if (!blocking && !(flags & O_NONBLOCK)) {
err = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
} else if (blocking && (flags & O_NONBLOCK)) {
err = fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
}
return err == -1 ? -errno : 0;
}
|