-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.c
More file actions
113 lines (99 loc) · 2.38 KB
/
print.c
File metadata and controls
113 lines (99 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
This file (was) part of GNUnet.
Copyright (C) 2018 Christian Grothoff
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file print.c
* @brief Helper functions for printing and communication with the parent
* @author Christian Grothoff
*/
/**
* Helper function to deal with partial writes.
* Fails hard (calls exit() on failures)!
*
* @param fd where to write to
* @param buf what to write
* @param buf_size number of bytes in @a buf
*/
static void
write_all (int fd,
const void *buf,
size_t buf_size)
{
const char *cbuf = buf;
size_t off;
off = 0;
while (off < buf_size)
{
ssize_t ret;
ret = write (fd,
&cbuf[off],
buf_size - off);
if (ret <= 0)
{
fprintf (stderr,
"Writing %u bytes to %d failed: %s\n",
(unsigned int) (buf_size - off),
fd,
strerror (errno));
exit (1);
}
off += ret;
}
}
/**
* Print message to the user by sending to parent.
*
* @param fmt format string
* @param ... arguments for @a fmt
*/
static void
print (const char *fmt,
...) __attribute__ ((format (gnu_printf, 1, 2)));
/**
* Print message to the user by sending to parent.
*
* @param fmt format string
* @param ... arguments for @a fmt
*/
static void
print (const char *fmt,
...)
{
char *str;
va_list ap;
va_start (ap,
fmt);
vasprintf (&str,
fmt,
ap);
va_end (ap);
{
size_t slen = strlen (str);
struct GLAB_MessageHeader hdr = {
.size = htons (slen + sizeof (struct GLAB_MessageHeader)),
.type = htons (0)
};
char buf[sizeof (hdr) + slen];
memcpy (buf,
&hdr,
sizeof (hdr));
memcpy (&buf[sizeof(hdr)],
str,
slen);
write_all (STDOUT_FILENO,
buf,
sizeof (buf));
}
free (str);
}