Mobile API Reference  MicroStrategy 2019
PDCunistd.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : PDCunistd.h
3 // AUTHOR : Perl Script
4 // CREATION : 6/13/02
5 // Copyright (C) MicroStrategy, Inc. 2002
6 //==============================================================================================
7 #ifndef PDCunistd_h
8 #define PDCunistd_h
9 
10 // this must be the *first* file included
11 #include "ProtectedSource/Prolog.h"
12 #ifdef __ANDROID__
13 #include "PDCselect.h"
14 #else
15 #include "PDCHeader/PDCselect.h"
16 #endif
17 
18 #ifdef WIN32
19  #define STDIN_FILENO 0
20  #define STDOUT_FILENO 1
21  #define STDERR_FILENO 2
22 #else
23  #include <unistd.h>
24 #endif
25 
26 #ifdef __hpux
27 // On HP-UX, seems that sys/time.h must be included before time.h to
28 // make select work.
29 #include<sys/time.h>
30 #endif
31 
32 #ifndef WIN32
33 #include "PDCerrno.h"
34 
35 #ifdef _AIX
36 #include <sys/select.h>
37 #else
38 #include "PDCtime.h"
39 #endif
40 
41 namespace MPDCHeader
42 {
43  // a signal-safe wrapper
44  inline ssize_t read(int fildes, void *buf, size_t nbyte)
45  {
46  ssize_t lBytesRead = 0;
47 
48  for (;;)
49  {
50  const ssize_t lResult = ::read(fildes, buf, nbyte);
51 
52  if (lResult == -1)
53  {
54  if (errno == EINTR)
55  {
56  // interrupted by signal, retry
57  continue;
58  }
59 
60  return (lBytesRead > 0) ? lBytesRead : -1;
61  }
62  else if (lResult == 0)
63  {
64  // end-of-file
65  return lBytesRead;
66  }
67  else if (lResult < nbyte)
68  {
69  // may be interrupted by signal, read more
70  lBytesRead += lResult;
71  buf = reinterpret_cast<char*>(buf) + lResult;
72  nbyte -= lResult;
73  continue;
74  }
75 
76  // success
77  return lBytesRead + lResult;
78  }
79  }
80 
81  // a signal-safe wrapper
82  inline ssize_t write(int fildes, const void *buf, size_t nbyte)
83  {
84  ssize_t lBytesWritten = 0;
85 
86  for (;;)
87  {
88  const ssize_t lResult = ::write(fildes, buf, nbyte);
89 
90  if (lResult == -1)
91  {
92  if (errno == EINTR)
93  {
94  // interrupted by signal, retry
95  continue;
96  }
97 
98  // error
99  return (lBytesWritten > 0) ? lBytesWritten : -1;
100  }
101  else if (lResult < nbyte)
102  {
103  // may be interrupted by signal, write more
104  lBytesWritten += lResult;
105  buf = reinterpret_cast<const char*>(buf) + lResult;
106  nbyte -= lResult;
107  continue;
108  }
109 
110  // success
111  return lBytesWritten + lResult;
112  }
113  }
114 
115  // a signal-safe wrapper
116  inline int fsync(int fildes)
117  {
118  for (;;)
119  {
120  const ssize_t lResult = ::fsync(fildes);
121 
122  if (lResult == -1)
123  {
124  if (errno == EINTR)
125  {
126  // interrupted by signal, retry
127  continue;
128  }
129 
130  // error
131  return -1;
132  }
133 
134  return lResult;
135  }
136  }
137 
138  // a signal-safe wrapper
139  inline int ftruncate(int fildes, off_t length)
140  {
141  for (;;)
142  {
143  const ssize_t lResult = ::ftruncate(fildes, length);
144 
145  if (lResult == -1)
146  {
147  if (errno == EINTR)
148  {
149  // interrupted by signal, retry
150  continue;
151  }
152 
153  // error
154  return -1;
155  }
156 
157  return lResult;
158  }
159  }
160 
161  // a signal-safe wrapper
162  inline int close(int fildes)
163  {
164  const int RETRY = 10;
165  int retry = 0;
166  for (;;)
167  {
168  const ssize_t lResult = ::close(fildes);
169 
170  if (lResult == -1)
171  {
172  if (errno == EINTR)
173  {
174  retry++;
175  if (retry==RETRY)
176  break;
177  // interrupted by signal, retry
178  continue;
179  }
180 
181  // error
182  return -1;
183  }
184 
185  return lResult;
186  }
187 
188  return -1;
189  }
190 
191  // a signal-safe wrapper
192  inline int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds, struct timeval* timeout)
193  {
194  for (;;)
195  {
196  const int lResult = mstr_select(nfds, readfds, writefds, errorfds, timeout);
197 
198  if (lResult == -1)
199  {
200  if (errno == EINTR)
201  {
202  // interrupted by signal, retry
203  continue;
204  }
205 
206  // error
207  return -1;
208  }
209 
210  return lResult;
211  }
212  }
213 
214  // a signal-safe wrapper
215  inline int fchown(int fildes, uid_t owner, gid_t group)
216  {
217  for (;;)
218  {
219  const int lResult = ::fchown(fildes, owner, group);
220 
221  if (lResult == -1)
222  {
223  if (errno == EINTR)
224  {
225  // interrupted by signal, retry
226  continue;
227  }
228 
229  // error
230  return -1;
231  }
232 
233  return lResult;
234  }
235  }
236 
237  // a signal-safe wrapper
238  inline int dup(int fildes)
239  {
240  for (;;)
241  {
242  const ssize_t lResult = ::dup(fildes);
243 
244  if (lResult == -1)
245  {
246  if (errno == EINTR)
247  {
248  // interrupted by signal, retry
249  continue;
250  }
251 
252  // error
253  return -1;
254  }
255 
256  return lResult;
257  }
258  }
259  // a signal-safe wrapper
260  inline int dup2(int fildes, int fildes2)
261  {
262  for (;;)
263  {
264  const ssize_t lResult = ::dup2(fildes, fildes2);
265 
266  if (lResult == -1)
267  {
268  if (errno == EINTR)
269  {
270  // interrupted by signal, retry
271  continue;
272  }
273 
274  // error
275  return -1;
276  }
277 
278  return lResult;
279  }
280  }
281 }
282 
283 // provide closesocket() like on Windows
284 inline int closesocket(int fildes)
285 {
286  return MPDCHeader::close(fildes);
287 }
288 
289 #endif // WIN32
290 
291 // this must be the *last* file included
292 #include "ProtectedSource/Epilog.h"
293 
294 #endif // PDCunistd_h
#define mstr_select(A, B, C, D, E)
Definition: PDCselect.h:54
int dup2(int fildes, int fildes2)
Definition: PDCunistd.h:260
int fsync(int fildes)
Definition: PDCunistd.h:116
int ftruncate(int fildes, off_t length)
Definition: PDCunistd.h:139
Definition: PDCtime.h:23
ssize_t write(int fildes, const void *buf, size_t nbyte)
Definition: PDCunistd.h:82
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
Definition: PDCunistd.h:192
int closesocket(int fildes)
Definition: PDCunistd.h:284
int dup(int fildes)
Definition: PDCunistd.h:238
int fchown(int fildes, uid_t owner, gid_t group)
Definition: PDCunistd.h:215
int close(int fildes)
Definition: PDCunistd.h:162
ssize_t read(int fildes, void *buf, size_t nbyte)
Definition: PDCunistd.h:44