]> git.tdb.fi Git - libs/core.git/blob - source/io/unix/handle.cpp
Use size_t for sizes in I/O classes
[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 using namespace std;
9
10 namespace Msp {
11 namespace IO {
12
13 void sys_set_blocking(Handle &handle, bool b)
14 {
15         int flags = fcntl(*handle, F_GETFL);
16         fcntl(*handle, F_SETFL, (flags&~O_NONBLOCK)|(b?0:O_NONBLOCK));
17 }
18
19 void sys_set_inherit(Handle &handle, bool i)
20 {
21         int flags = fcntl(*handle, F_GETFD);
22         fcntl(*handle, F_SETFD, (flags&~O_CLOEXEC)|(i?O_CLOEXEC:0));
23 }
24
25 size_t sys_read(Handle &handle, char *buf, size_t size)
26 {
27         ssize_t ret = read(*handle, buf, size);
28         if(ret==-1)
29         {
30                 if(errno==EAGAIN)
31                         return 0;
32                 else
33                         throw system_error("read");
34         }
35
36         return ret;
37 }
38
39 size_t sys_write(Handle &handle, const char *buf, size_t size)
40 {
41         ssize_t ret = write(*handle, buf, size);
42         if(ret==-1)
43         {
44                 if(errno==EAGAIN)
45                         return 0;
46                 else
47                         throw system_error("write");
48         }
49
50         return ret;
51 }
52
53 void sys_close(Handle &handle)
54 {
55         if(handle)
56         {
57                 close(*handle);
58                 *handle = INVALID_HANDLE_VALUE;
59         }
60 }
61
62 } // namespace IO
63 } // namespace Msp