]> git.tdb.fi Git - libs/core.git/blob - source/io/unix/handle.cpp
Fix mismatched fcntls in sys_set_blocking on unix
[libs/core.git] / source / io / unix / handle.cpp
1 #include <cerrno>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <msp/core/systemerror.h>
5 #include "handle.h"
6 #include "handle_private.h"
7
8 namespace Msp {
9 namespace IO {
10
11 void sys_set_blocking(Handle &handle, bool b)
12 {
13         int flags = fcntl(*handle, F_GETFL);
14         fcntl(*handle, F_SETFL, (flags&~O_NONBLOCK)|(b?0:O_NONBLOCK));
15 }
16
17 unsigned sys_read(Handle &handle, char *buf, unsigned size)
18 {
19         int ret = read(*handle, buf, size);
20         if(ret==-1)
21         {
22                 if(errno==EAGAIN)
23                         return 0;
24                 else
25                         throw system_error("read");
26         }
27
28         return ret;
29 }
30
31 unsigned sys_write(Handle &handle, const char *buf, unsigned size)
32 {
33         int ret = write(*handle, buf, size);
34         if(ret==-1)
35         {
36                 if(errno==EAGAIN)
37                         return 0;
38                 else
39                         throw system_error("write");
40         }
41
42         return ret;
43 }
44
45 void sys_close(Handle &handle)
46 {
47         if(handle)
48         {
49                 close(*handle);
50                 *handle = INVALID_HANDLE_VALUE;
51         }
52 }
53
54 } // namespace IO
55 } // namespace Msp