HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
posix_ioutils.c
Go to the documentation of this file.
1 #include <sys/select.h>
2 #include <unistd.h>
3 
4 #include <assert.h>
5 
6 #include <hipc/posix/ioutils.h>
7 
17 int hipcPosixReadReady(int fd)
18 {
19  fd_set fds;
20  struct timeval timeout;
21 
22  FD_ZERO(&fds);
23  FD_SET(fd, &fds);
24  timeout.tv_sec = 0;
25  timeout.tv_usec = 0;
26  return select(fd + 1, &fds, NULL, NULL, &timeout);
27 }
28 
41 ssize_t hipcPosixReadExactly(const int fd, void *buf, const size_t count)
42 {
43  ssize_t n;
44  ssize_t i;
45 
46  n = count;
47  while (0 < n) {
48  i = read(fd, buf, n);
49  if (0 >= i) { /* read(2) returns 0 when fd is at the end of the file */
50  return i;
51  }
52  buf = ((unsigned char *) buf) + i;
53  assert(n >= i);
54  n -= i;
55  }
56  assert(0 == n);
57  return count - n;
58 }
59 
71 ssize_t hipcPosixWriteExactly(const int fd,
72  void const *buf, const size_t count)
73 {
74  ssize_t n;
75  ssize_t i;
76 
77  n = count;
78  while (0 < n) {
79  i = write(fd, buf, n);
80  if (0 > i) {
81  return i;
82  }
83  buf = ((unsigned char *) buf) + i;
84  assert(n >= i);
85  n -= i;
86  }
87  assert(0 == n);
88  return count - n;
89 }
ssize_t hipcPosixWriteExactly(const int fd, void const *buf, const size_t count)
Writes count bytes to fd by using write(2).
Definition: posix_ioutils.c:71
ssize_t hipcPosixReadExactly(const int fd, void *buf, const size_t count)
Read count bytes from fd by using read(2).
Definition: posix_ioutils.c:41
int hipcPosixReadReady(int fd)
Check a file descriptor is ready to read by using select(2) and returns the result of select(2)...
Definition: posix_ioutils.c:17