Add new demo files
[lttng-ust.git] / snprintf / patient_write.c
1 /*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* write() */
21 #include <unistd.h>
22
23 /* send() */
24 #include <sys/types.h>
25 #include <sys/socket.h>
26
27 #include <errno.h>
28
29 #include <share.h>
30
31 /*
32 * This write is patient because it restarts if it was incomplete.
33 */
34
35 ssize_t patient_write(int fd, const void *buf, size_t count)
36 {
37 const char *bufc = (const char *) buf;
38 int result;
39
40 for(;;) {
41 result = write(fd, bufc, count);
42 if (result == -1 && errno == EINTR) {
43 continue;
44 }
45 if (result <= 0) {
46 return result;
47 }
48 count -= result;
49 bufc += result;
50
51 if (count == 0) {
52 break;
53 }
54 }
55
56 return bufc-(const char *)buf;
57 }
58
59 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);
66 if (result == -1 && errno == EINTR) {
67 continue;
68 }
69 if (result <= 0) {
70 return result;
71 }
72 count -= result;
73 bufc += result;
74
75 if (count == 0) {
76 break;
77 }
78 }
79
80 return bufc - (const char *) buf;
81 }
This page took 0.047014 seconds and 4 git commands to generate.