Commit | Line | Data |
---|---|---|
02d9c658 MD |
1 | /* |
2 | * Copyright (C) 2009 Pierre-Marc Fournier | |
a09dac63 PMF |
3 | * |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2.1 of the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
8bf5ab2d PMF |
19 | #ifndef UST_SHARE_H |
20 | #define UST_SHARE_H | |
21 | ||
2dae156b | 22 | /* write() */ |
8bf5ab2d | 23 | #include <unistd.h> |
2dae156b PMF |
24 | |
25 | /* send() */ | |
26 | #include <sys/types.h> | |
27 | #include <sys/socket.h> | |
28 | ||
8bf5ab2d PMF |
29 | #include <errno.h> |
30 | ||
02d9c658 MD |
31 | /* |
32 | * This write is patient because it restarts if it was incomplete. | |
8bf5ab2d PMF |
33 | */ |
34 | ||
2dae156b | 35 | static __inline__ ssize_t patient_write(int fd, const void *buf, size_t count) |
8bf5ab2d PMF |
36 | { |
37 | const char *bufc = (const char *) buf; | |
38 | int result; | |
39 | ||
40 | for(;;) { | |
41 | result = write(fd, bufc, count); | |
02d9c658 | 42 | if (result == -1 && errno == EINTR) { |
8bf5ab2d PMF |
43 | continue; |
44 | } | |
02d9c658 | 45 | if (result <= 0) { |
8bf5ab2d PMF |
46 | return result; |
47 | } | |
48 | count -= result; | |
49 | bufc += result; | |
50 | ||
02d9c658 | 51 | if (count == 0) { |
8bf5ab2d PMF |
52 | break; |
53 | } | |
54 | } | |
55 | ||
56 | return bufc-(const char *)buf; | |
57 | } | |
58 | ||
2dae156b PMF |
59 | static __inline__ ssize_t patient_send(int fd, const void *buf, size_t count, int flags) |
60 | { | |
61 | const char *bufc = (const char *) buf; | |
62 | int result; | |
63 | ||
64 | for(;;) { | |
65 | result = send(fd, bufc, count, flags); | |
02d9c658 | 66 | if (result == -1 && errno == EINTR) { |
2dae156b PMF |
67 | continue; |
68 | } | |
02d9c658 | 69 | if (result <= 0) { |
2dae156b PMF |
70 | return result; |
71 | } | |
72 | count -= result; | |
73 | bufc += result; | |
74 | ||
02d9c658 | 75 | if (count == 0) { |
2dae156b PMF |
76 | break; |
77 | } | |
78 | } | |
79 | ||
02d9c658 | 80 | return bufc - (const char *) buf; |
2dae156b PMF |
81 | } |
82 | ||
8bf5ab2d | 83 | #endif /* UST_SHARE_H */ |