#include /** * Set a file descriptor to blocking or non-blocking mode. * * @param fd The file descriptor * @param blocking 0:non-blocking mode, 1:blocking mode * * @return 1:success, 0:failure. **/ int fd_set_blocking(int fd, int blocking) { /* Save the current flags */ int flags = fcntl(fd, F_GETFL, 0); if (flags == -1) return 0; if (blocking) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; return fcntl(fd, F_SETFL, flags) != -1; }