lib: Split out sys_[read|write] & friends
[obnox/samba/samba-obnox.git] / source3 / lib / sys_rw.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba system utilities
4  * Copyright (C) Andrew Tridgell 1992-1998
5  * Copyright (C) Jeremy Allison  1998-2005
6  * Copyright (C) Timur Bakeyev        2005
7  * Copyright (C) Bjoern Jacke    2006-2007
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "replace.h"
24 #include "system/filesys.h"
25 #include "lib/sys_rw.h"
26
27 /*******************************************************************
28 A read wrapper that will deal with EINTR/EWOULDBLOCK
29 ********************************************************************/
30
31 ssize_t sys_read(int fd, void *buf, size_t count)
32 {
33         ssize_t ret;
34
35         do {
36                 ret = read(fd, buf, count);
37         } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
38                                errno == EWOULDBLOCK));
39
40         return ret;
41 }
42
43 /*******************************************************************
44 A write wrapper that will deal with EINTR/EWOULDBLOCK.
45 ********************************************************************/
46
47 ssize_t sys_write(int fd, const void *buf, size_t count)
48 {
49         ssize_t ret;
50
51         do {
52                 ret = write(fd, buf, count);
53         } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
54                                errno == EWOULDBLOCK));
55
56         return ret;
57 }
58
59 /*******************************************************************
60 A writev wrapper that will deal with EINTR.
61 ********************************************************************/
62
63 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
64 {
65         ssize_t ret;
66
67         do {
68                 ret = writev(fd, iov, iovcnt);
69         } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
70                                errno == EWOULDBLOCK));
71
72         return ret;
73 }
74
75 /*******************************************************************
76 A pread wrapper that will deal with EINTR
77 ********************************************************************/
78
79 ssize_t sys_pread(int fd, void *buf, size_t count, off_t off)
80 {
81         ssize_t ret;
82
83         do {
84                 ret = pread(fd, buf, count, off);
85         } while (ret == -1 && errno == EINTR);
86         return ret;
87 }
88
89 /*******************************************************************
90 A write wrapper that will deal with EINTR
91 ********************************************************************/
92
93 ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t off)
94 {
95         ssize_t ret;
96
97         do {
98                 ret = pwrite(fd, buf, count, off);
99         } while (ret == -1 && errno == EINTR);
100         return ret;
101 }