aboutsummaryrefslogtreecommitdiffstats
path: root/man3/libsimple_vasprintf.3
blob: e91c404f7d04d999aef04ea10eaedd5b529848da (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
.TH LIBSIMPLE_VASPRINTF 3 2018-11-04 libsimple
.SH NAME
libsimple_vasprintf \- allocate and format a string
.SH SYNOPSIS
.nf
#include <libsimple.h>

int libsimple_vasprintf(char **\fIstrp\fP, const char *\fIfmt\fP, va_list \fIap\fP);
int libsimple_asprintf(char **\fIstrp\fP, const char *\fIfmt\fP, ...);
char *libsimple_vasprintfa(const char *\fIfmt\fP, va_list \fIap\fP);
char *libsimple_asprintfa(const char *\fIfmt\fP, ...);

#ifndef vasprintf
# define vasprintf libsimple_vasprintf
#endif
#ifndef asprintf
# define asprintf libsimple_asprintf
#endif
#ifndef vasprintfa
# define vasprintfa libsimple_vasprintfa
#endif
#ifndef asprintfa
# define asprintfa libsimple_asprintfa
#endif
.fi
.PP
Link with
.IR \-lsimple .
.SH DESCRIPTION
The
.BR libsimple_vasprintf ()
function is a version of the
.BR vsprintf (3)
function that allocates a sufficiently large string,
writes the string to it, and stores it in
.IR strp .
The parameter
.I fmt
is used as the format string for the
.BR vfprintf (3)
function, and the parameter
.I ap
is used as the formatting arguments.
On failure
.I *strp
is set to
.BR NULL ,
portable applications shall not assume this
is true if the shorthand aliases
.BR vasprintf
and
.BR asprintf
are used.
.PP
The
.BR libsimple_asprintf ()
function is a version of the
.BR libsimple_vasprintf ()
function that uses variadic arguments instead of
.BR va_list .
.PP
The
.BR libsimple_vasprintfa ()
and
.BR libsimple_asprintfa ()
functions are versions of the
.BR libsimple_vasprintf ()
and
.BR libsimple_asprintf ()
functions that allocates the string on the stack
rather than the heap.
.PP
The string stored in
.I strp
by the
.BR libsimple_vasprintf ()
and
.BR libsimple_asprintf ()
functions shall be deallocated with the
.BR free (3)
function when it is no longer needed; the string
returned by the
.BR libsimple_vasprintfa ()
and
.BR libsimple_asprintfa ()
functions shall not be freed but are automatically
deallocated when the calling function returns.
.PP
The
.BR libsimple_vasprintfa ()
and
.BR libsimple_asprintfa ()
functions are implemented as macros and are only
available if compiling with GCC or Clang.
.SH RETURN VALUE
The
.BR libsimple_vasprintf ()
and
.BR libsimple_asprintf ()
functions return
.I strlen(*strp)
upon successful completion; otherwise -1 is returned and
.I errno
set to indicate the error.
.PP
The
.BR libsimple_vasprintfa ()
and
.BR libsimple_asprintfa ()
functions return the constructed string upon successful
completion; otherwise
.B NULL
is returned and
.I errno
set to indicate the error.
.SH ERRORS
The
.BR libsimple_vasprintf (),
.BR libsimple_asprintf (),
.BR libsimple_vasprintfa (),
and
.BR libsimple_asprintfa ()
functions will fail for any reason specified for the
.BR fprintf (3)
function. The
.BR libsimple_vasprintf ()
and
.BR libsimple_asprintf ()
functions will also fail if:
.TP
.B EMFILE
{FOPEN_MAX} streams are currently open in the calling process.
.TP
.B ENOMEM
Could not allocate enough memory.
.PP
If enough memory cannot be allocated by the
.BR libsimple_vasprintfa ()
and
.BR libsimple_asprintfa ()
functions, the kernel will kill the thread with a
.B SIGSEGV
signal.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lb lb lb
l l l.
Interface	Attribute	Value
T{
.BR libsimple_vasprintf (),
.br
.BR libsimple_asprintf (),
.br
.BR libsimple_vasprintfa (),
.br
.BR libsimple_asprintfa ()
T}	Thread safety	MT-Safe
T{
.BR libsimple_vasprintf (),
.br
.BR libsimple_asprintf (),
.br
.BR libsimple_vasprintfa (),
.br
.BR libsimple_asprintfa ()
T}	Async-signal safety	AS-Safe
T{
.BR libsimple_vasprintf (),
.br
.BR libsimple_asprintf (),
.br
.BR libsimple_vasprintfa (),
.br
.BR libsimple_asprintfa ()
T}	Async-cancel safety	AC-Safe
.TE
.SH EXAMPLES
None.
.SH APPLICATION USAGE
None.
.SH RATIONALE
None.
.SH FUTURE DIRECTIONS
None.
.SH NOTES
None.
.SH BUGS
None.
.SH SEE ALSO
.BR libsimple_vweprintf (3),
.BR sprintf (3),
.BR snprintf (3)