introduce mark_fd_nonblock()
[paraslash.git] / fd.c
1 #include "para.h"
2 #include "error.h"
3 /**
4 * check whether a file exists
5 *
6 * \param fn the file name
7 *
8 * \return Non-zero iff file exists.
9 */
10 int file_exists(const char *fn)
11 {
12 struct stat statbuf;
13
14 return !stat(fn, &statbuf);
15 }
16
17 int para_select(int n, fd_set *readfds, fd_set *writefds,
18 struct timeval *timeout)
19 {
20 int ret, err;
21 do {
22 ret = select(n, readfds, writefds, NULL, timeout);
23 err = errno;
24 } while (ret < 0 && errno == EINTR);
25 if (ret < 0)
26 PARA_CRIT_LOG("select error (%s)\n", strerror(err));
27 return ret;
28 }
29
30 int mark_fd_nonblock(int fd)
31 {
32 int flags = fcntl(fd, F_GETFL);
33 if (flags < 0)
34 return -E_F_GETFL;
35 if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
36 return -E_F_SETFL;
37 return 1;
38 }
39