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