• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_printf
5Section: 3
6Source: libcurl
7See-also:
8  - fprintf (3)
9  - printf (3)
10  - sprintf (3)
11  - vprintf (3)
12---
13
14# NAME
15
16curl_maprintf, curl_mfprintf, curl_mprintf, curl_msnprintf, curl_msprintf
17curl_mvaprintf, curl_mvfprintf, curl_mvprintf, curl_mvsnprintf,
18curl_mvsprintf - formatted output conversion
19
20# SYNOPSIS
21
22~~~c
23#include <curl/mprintf.h>
24
25int curl_mprintf(const char *format, ...);
26int curl_mfprintf(FILE *fd, const char *format, ...);
27int curl_msprintf(char *buffer, const char *format, ...);
28int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...);
29int curl_mvprintf(const char *format, va_list args);
30int curl_mvfprintf(FILE *fd, const char *format, va_list args);
31int curl_mvsprintf(char *buffer, const char *format, va_list args);
32int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format,
33                    va_list args);
34char *curl_maprintf(const char *format , ...);
35char *curl_mvaprintf(const char *format, va_list args);
36~~~
37
38# DESCRIPTION
39
40These functions produce output according to the format string and given
41arguments. They are mostly clones of the well-known C-style functions but
42there are slight differences in behavior.
43
44We discourage users from using any of these functions in new applications.
45
46Functions in the curl_mprintf() family produce output according to a format as
47described below. The functions **curl_mprintf()** and **curl_mvprintf()**
48write output to stdout, the standard output stream; **curl_mfprintf()** and
49**curl_mvfprintf()** write output to the given output stream;
50**curl_msprintf()**, **curl_msnprintf()**, **curl_mvsprintf()**, and
51**curl_mvsnprintf()** write to the character string **buffer**.
52
53The functions **curl_msnprintf()** and **curl_mvsnprintf()** write at most
54*maxlength* bytes (including the terminating null byte ('0')) to
55*buffer*.
56
57The functions **curl_mvprintf()**, **curl_mvfprintf()**,
58**curl_mvsprintf()**, **curl_mvsnprintf()** are equivalent to the
59functions **curl_mprintf()**, **curl_mfprintf()**, **curl_msprintf()**,
60**curl_msnprintf()**, respectively, except that they are called with a
61*va_list* instead of a variable number of arguments. These functions do
62not call the *va_end* macro. Because they invoke the *va_arg* macro,
63the value of *ap* is undefined after the call.
64
65The functions **curl_maprintf()** and **curl_mvaprintf()** return the
66output string as pointer to a newly allocated memory area. The returned string
67must be curl_free(3)ed by the receiver.
68
69All of these functions write the output under the control of a format string
70that specifies how subsequent arguments are converted for output.
71
72# FORMAT STRING
73
74The format string is composed of zero or more directives: ordinary characters
75(not %), which are copied unchanged to the output stream; and conversion
76specifications, each of which results in fetching zero or more subsequent
77arguments. Each conversion specification is introduced by the character %, and
78ends with a conversion specifier. In between there may be (in this order) zero
79or more *flags*, an optional minimum *field width*, an optional
80*precision* and an optional *length modifier*.
81
82# The $ modifier
83
84The arguments must correspond properly with the conversion specifier. By
85default, the arguments are used in the order given, where each '*' (see Field
86width and Precision below) and each conversion specifier asks for the next
87argument (and it is an error if insufficiently many arguments are given). One
88can also specify explicitly which argument is taken, at each place where an
89argument is required, by writing "%m$" instead of '%' and "*m$" instead
90of '*', where the decimal integer m denotes the position in the argument list
91of the desired argument, indexed starting from 1. Thus,
92~~~c
93    curl_mprintf("%*d", width, num);
94~~~
95and
96~~~c
97    curl_mprintf("%2$*1$d", width, num);
98~~~
99are equivalent. The second style allows repeated references to the same
100argument.
101
102If the style using '$' is used, it must be used throughout for all conversions
103taking an argument and all width and precision arguments, but it may be mixed
104with "%%" formats, which do not consume an argument. There may be no gaps in
105the numbers of arguments specified using '$'; for example, if arguments 1 and
1063 are specified, argument 2 must also be specified somewhere in the format
107string.
108
109# Flag characters
110
111The character % is followed by zero or more of the following flags:
112
113## #
114
115The value should be converted to its "alternate form".
116
117## 0
118
119The value should be zero padded.
120
121## -
122
123The converted value is to be left adjusted on the field boundary. (The default
124is right justification.) The converted value is padded on the right with
125blanks, rather than on the left with blanks or zeros. A '-' overrides a &'0'
126if both are given.
127
128## (space)
129
130(a space: ' ') A blank should be left before a positive number (or empty
131string) produced by a signed conversion.
132
133## +
134
135A sign (+ or -) should always be placed before a number produced by a signed
136conversion. By default, a sign is used only for negative numbers. A '+'
137overrides a space if both are used.
138
139# Field width
140
141An optional decimal digit string (with nonzero first digit) specifying a
142minimum field width. If the converted value has fewer characters than the
143field width, it gets padded with spaces on the left (or right, if the
144left-adjustment flag has been given). Instead of a decimal digit string one
145may write "*" or "*m$" (for some decimal integer m) to specify that the field
146width is given in the next argument, or in the *m-th* argument,
147respectively, which must be of type int. A negative field width is taken as
148a '-' flag followed by a positive field width. In no case does a nonexistent
149or small field width cause truncation of a field; if the result of a
150conversion is wider than the field width, the field is expanded to contain the
151conversion result.
152
153# Precision
154
155An optional precision in the form of a period ('.') followed by an optional
156decimal digit string. Instead of a decimal digit string one may write "*" or
157"*m$" (for some decimal integer m) to specify that the precision is given in
158the next argument, or in the *m-th* argument, respectively, which must be of
159type int. If the precision is given as just '.', the precision is taken to be
160zero. A negative precision is taken as if the precision were omitted. This
161gives the minimum number of digits to appear for **d**, **i**, **o**,
162**u**, **x**, and **X** conversions, the number of digits to appear
163after the radix character for **a**, **A**, **e**, **E**, **f**, and
164**F** conversions, the maximum number of significant digits for **g** and
165**G** conversions, or the maximum number of characters to be printed from a
166string for **s** and **S** conversions.
167
168# Length modifier
169
170## h
171
172A following integer conversion corresponds to a *short* or *unsigned short*
173argument.
174
175## l
176
177(ell) A following integer conversion corresponds to a *long* or
178*unsigned long* argument, or a following n conversion corresponds to a
179pointer to a long argument
180
181## ll
182
183(ell-ell). A following integer conversion corresponds to a *long long* or
184*unsigned long long* argument, or a following n conversion corresponds to
185a pointer to a long long argument.
186
187## q
188
189A synonym for **ll**.
190
191## L
192
193A following a, A, e, E, f, F, g, or G conversion corresponds to a long double
194argument.
195
196## z
197
198A following integer conversion corresponds to a *size_t* or *ssize_t*
199argument.
200
201# Conversion specifiers
202
203A character that specifies the type of conversion to be applied. The
204conversion specifiers and their meanings are:
205
206## d, i
207
208The int argument is converted to signed decimal notation. The precision, if
209any, gives the minimum number of digits that must appear; if the converted
210value requires fewer digits, it is padded on the left with zeros. The default
211precision is 1. When 0 is printed with an explicit precision 0, the output is
212empty.
213
214## o, u, x, X
215
216The unsigned int argument is converted to unsigned octal (o), unsigned decimal
217(u), or unsigned hexadecimal (**x** and **X**) notation. The letters
218*abcdef* are used for **x** conversions; the letters *ABCDEF* are
219used for **X** conversions. The precision, if any, gives the minimum number
220of digits that must appear; if the converted value requires fewer digits, it
221is padded on the left with zeros. The default precision is 1. When 0 is
222printed with an explicit precision 0, the output is empty.
223
224## e, E
225
226The double argument is rounded and output in the style **"[-]d.ddde±dd"**
227
228## f, F
229
230The double argument is rounded and output to decimal notation in the style
231**"[-]ddd.ddd"**.
232
233## g, G
234
235The double argument is converted in style f or e.
236
237## c
238
239The int argument is converted to an unsigned char, and the resulting character
240is written.
241
242## s
243
244The *const char ** argument is expected to be a pointer to an array of
245character type (pointer to a string). Characters from the array are written up
246to (but not including) a terminating null byte. If a precision is specified,
247no more than the number specified are written. If a precision is given, no
248null byte need be present; if the precision is not specified, or is greater
249than the size of the array, the array must contain a terminating null byte.
250
251## p
252
253The *void ** pointer argument is printed in hexadecimal.
254
255## n
256
257The number of characters written so far is stored into the integer pointed to
258by the corresponding argument.
259
260## %
261
262A '%' symbol is written. No argument is converted.
263
264# EXAMPLE
265
266~~~c
267const char *name = "John";
268
269int main(void)
270{
271  curl_mprintf("My name is %s\n", name);
272  curl_mprintf("Pi is almost %f\n", (double)25.0/8);
273}
274~~~
275
276# AVAILABILITY
277
278These functions might be removed from the public libcurl API in the future. Do
279not use them in new programs or projects.
280
281# RETURN VALUE
282
283The **curl_maprintf** and **curl_mvaprintf** functions return a pointer to
284a newly allocated string, or NULL if it failed.
285
286All other functions return the number of characters actually printed
287(excluding the null byte used to end output to strings). Note that this
288sometimes differ from how the POSIX versions of these functions work.
289