ldb: version 1.5.2
[Samba.git] / lib / util / sys_rw.c
blob9a6cdcaa606343845616e1673622a2736d64425c
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
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.
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.
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/>.
23 #include "replace.h"
24 #include "system/filesys.h"
25 #include "lib/util/sys_rw.h"
27 /*******************************************************************
28 A read wrapper that will deal with EINTR/EWOULDBLOCK
29 ********************************************************************/
31 ssize_t sys_read(int fd, void *buf, size_t count)
33 ssize_t ret;
35 do {
36 ret = read(fd, buf, count);
37 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
38 errno == EWOULDBLOCK));
40 return ret;
43 /**
44 * read wrapper, void variant:
45 * This is intended to be used as a void variant of
46 * read in situations where the caller wants to ignore
47 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
49 void sys_read_v(int fd, void *buf, size_t count)
51 ssize_t ret;
53 do {
54 ret = read(fd, buf, count);
55 } while (ret == -1 && errno == EINTR);
59 /*******************************************************************
60 A write wrapper that will deal with EINTR/EWOULDBLOCK.
61 ********************************************************************/
63 ssize_t sys_write(int fd, const void *buf, size_t count)
65 ssize_t ret;
67 do {
68 ret = write(fd, buf, count);
69 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
70 errno == EWOULDBLOCK));
72 return ret;
75 /**
76 * write wrapper to deal with EINTR and friends.
77 * void-variant that ignores the number of bytes written.
78 * This is intended to be used as a void variant of
79 * write in situations where the caller wants to ignore
80 * the result. Hence not checking for EAGAIN|EWOULDBLOCK.
82 void sys_write_v(int fd, const void *buf, size_t count)
84 ssize_t ret;
86 do {
87 ret = write(fd, buf, count);
88 } while (ret == -1 && errno == EINTR);
92 /*******************************************************************
93 A writev wrapper that will deal with EINTR.
94 ********************************************************************/
96 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
98 ssize_t ret;
100 do {
101 ret = writev(fd, iov, iovcnt);
102 } while (ret == -1 && (errno == EINTR || errno == EAGAIN ||
103 errno == EWOULDBLOCK));
105 return ret;
108 /*******************************************************************
109 A pread wrapper that will deal with EINTR
110 ********************************************************************/
112 ssize_t sys_pread(int fd, void *buf, size_t count, off_t off)
114 ssize_t ret;
116 do {
117 ret = pread(fd, buf, count, off);
118 } while (ret == -1 && errno == EINTR);
119 return ret;
122 /*******************************************************************
123 A write wrapper that will deal with EINTR
124 ********************************************************************/
126 ssize_t sys_pwrite(int fd, const void *buf, size_t count, off_t off)
128 ssize_t ret;
130 do {
131 ret = pwrite(fd, buf, count, off);
132 } while (ret == -1 && errno == EINTR);
133 return ret;