curl_mprintf.3 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. .\" generated by cd2nroff 0.1 from curl_mprintf.md
  2. .TH curl_printf 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_maprintf, curl_mfprintf, curl_mprintf, curl_msnprintf, curl_msprintf,
  5. curl_mvaprintf, curl_mvfprintf, curl_mvprintf, curl_mvsnprintf,
  6. curl_mvsprintf \- formatted output conversion
  7. .SH SYNOPSIS
  8. .nf
  9. #include <curl/mprintf.h>
  10. int curl_mprintf(const char *format, ...);
  11. int curl_mfprintf(FILE *fd, const char *format, ...);
  12. int curl_msprintf(char *buffer, const char *format, ...);
  13. int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...);
  14. int curl_mvprintf(const char *format, va_list args);
  15. int curl_mvfprintf(FILE *fd, const char *format, va_list args);
  16. int curl_mvsprintf(char *buffer, const char *format, va_list args);
  17. int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format,
  18. va_list args);
  19. char *curl_maprintf(const char *format , ...);
  20. char *curl_mvaprintf(const char *format, va_list args);
  21. .fi
  22. .SH DESCRIPTION
  23. These functions produce output according to the format string and given
  24. arguments. They are mostly clones of the well\-known C\-style functions but
  25. there are slight differences in behavior.
  26. We discourage users from using any of these functions in new applications.
  27. Functions in the curl_mprintf() family produce output according to a format as
  28. described below. The functions \fBcurl_mprintf()\fP and \fBcurl_mvprintf()\fP
  29. write output to stdout, the standard output stream; \fBcurl_mfprintf()\fP and
  30. \fBcurl_mvfprintf()\fP write output to the given output stream;
  31. \fBcurl_msprintf()\fP, \fBcurl_msnprintf()\fP, \fBcurl_mvsprintf()\fP, and
  32. \fBcurl_mvsnprintf()\fP write to the character string \fBbuffer\fP.
  33. The functions \fBcurl_msnprintf()\fP and \fBcurl_mvsnprintf()\fP write at most
  34. \fImaxlength\fP bytes (including the terminating null byte (\(aq0\(aq)) to
  35. \fIbuffer\fP.
  36. The functions \fBcurl_mvprintf()\fP, \fBcurl_mvfprintf()\fP,
  37. \fBcurl_mvsprintf()\fP, \fBcurl_mvsnprintf()\fP are equivalent to the
  38. functions \fBcurl_mprintf()\fP, \fBcurl_mfprintf()\fP, \fBcurl_msprintf()\fP,
  39. \fBcurl_msnprintf()\fP, respectively, except that they are called with a
  40. \fIva_list\fP instead of a variable number of arguments. These functions do
  41. not call the \fIva_end\fP macro. Because they invoke the \fIva_arg\fP macro,
  42. the value of \fIap\fP is undefined after the call.
  43. The functions \fBcurl_maprintf()\fP and \fBcurl_mvaprintf()\fP return the
  44. output string as pointer to a newly allocated memory area. The returned string
  45. must be \fIcurl_free(3)\fPed by the receiver.
  46. All of these functions write the output under the control of a format string
  47. that specifies how subsequent arguments are converted for output.
  48. .SH FORMAT STRING
  49. The format string is composed of zero or more directives: ordinary characters
  50. (not %), which are copied unchanged to the output stream; and conversion
  51. specifications, each of which results in fetching zero or more subsequent
  52. arguments. Each conversion specification is introduced by the character %, and
  53. ends with a conversion specifier. In between there may be (in this order) zero
  54. or more \fIflags\fP, an optional minimum \fIfield width\fP, an optional
  55. \fIprecision\fP and an optional \fIlength modifier\fP.
  56. .SH The $ modifier
  57. The arguments must correspond properly with the conversion specifier. By
  58. default, the arguments are used in the order given, where each \(aq*\(aq (see Field
  59. width and Precision below) and each conversion specifier asks for the next
  60. argument (and it is an error if insufficiently many arguments are given). One
  61. can also specify explicitly which argument is taken, at each place where an
  62. argument is required, by writing "%m$" instead of \(aq%\(aq and "*m$" instead
  63. of \(aq*\(aq, where the decimal integer m denotes the position in the argument list
  64. of the desired argument, indexed starting from 1. Thus,
  65. .nf
  66. curl_mprintf("%*d", width, num);
  67. .fi
  68. and
  69. .nf
  70. curl_mprintf("%2$*1$d", width, num);
  71. .fi
  72. are equivalent. The second style allows repeated references to the same
  73. argument.
  74. If the style using \(aq$\(aq is used, it must be used throughout for all conversions
  75. taking an argument and all width and precision arguments, but it may be mixed
  76. with "%%" formats, which do not consume an argument. There may be no gaps in
  77. the numbers of arguments specified using \(aq$\(aq; for example, if arguments 1 and
  78. 3 are specified, argument 2 must also be specified somewhere in the format
  79. string.
  80. .SH Flag characters
  81. The character % is followed by zero or more of the following flags:
  82. .IP #
  83. The value should be converted to its "alternate form".
  84. .IP 0
  85. The value should be zero padded.
  86. .IP -
  87. The converted value is to be left adjusted on the field boundary. (The default
  88. is right justification.) The converted value is padded on the right with
  89. blanks, rather than on the left with blanks or zeros. A \(aq\-\(aq overrides a &\(aq0\(aq
  90. if both are given.
  91. .IP (space)
  92. (a space: \(aq \(aq) A blank should be left before a positive number (or empty
  93. string) produced by a signed conversion.
  94. .IP +
  95. A sign (+ or \-) should always be placed before a number produced by a signed
  96. conversion. By default, a sign is used only for negative numbers. A \(aq+\(aq
  97. overrides a space if both are used.
  98. .SH Field width
  99. An optional decimal digit string (with nonzero first digit) specifying a
  100. minimum field width. If the converted value has fewer characters than the
  101. field width, it gets padded with spaces on the left (or right, if the
  102. left\-adjustment flag has been given). Instead of a decimal digit string one
  103. may write "\fI" or "\fPm$" (for some decimal integer m) to specify that the field
  104. width is given in the next argument, or in the \fIm\-th\fP argument,
  105. respectively, which must be of type int. A negative field width is taken as
  106. a \(aq\-\(aq flag followed by a positive field width. In no case does a nonexistent
  107. or small field width cause truncation of a field; if the result of a
  108. conversion is wider than the field width, the field is expanded to contain the
  109. conversion result.
  110. .SH Precision
  111. An optional precision in the form of a period (\(aq.\(aq) followed by an optional
  112. decimal digit string. Instead of a decimal digit string one may write "*" or
  113. \&"*m$" (for some decimal integer m) to specify that the precision is given in
  114. the next argument, or in the \fIm\-th\fP argument, respectively, which must be of
  115. type int. If the precision is given as just \(aq.\(aq, the precision is taken to be
  116. zero. A negative precision is taken as if the precision were omitted. This
  117. gives the minimum number of digits to appear for \fBd\fP, \fBi\fP, \fBo\fP,
  118. \fBu\fP, \fBx\fP, and \fBX\fP conversions, the number of digits to appear
  119. after the radix character for \fBa\fP, \fBA\fP, \fBe\fP, \fBE\fP, \fBf\fP, and
  120. \fBF\fP conversions, the maximum number of significant digits for \fBg\fP and
  121. \fBG\fP conversions, or the maximum number of characters to be printed from a
  122. string for \fBs\fP and \fBS\fP conversions.
  123. .SH Length modifier
  124. .IP h
  125. A following integer conversion corresponds to a \fIshort\fP or \fIunsigned short\fP
  126. argument.
  127. .IP l
  128. (ell) A following integer conversion corresponds to a \fIlong\fP or
  129. \fIunsigned long\fP argument, or a following n conversion corresponds to a
  130. pointer to a long argument
  131. .IP ll
  132. (ell\-ell). A following integer conversion corresponds to a \fIlong long\fP or
  133. \fIunsigned long long\fP argument, or a following n conversion corresponds to
  134. a pointer to a long long argument.
  135. .IP q
  136. A synonym for \fBll\fP.
  137. .IP L
  138. A following a, A, e, E, f, F, g, or G conversion corresponds to a long double
  139. argument.
  140. .IP z
  141. A following integer conversion corresponds to a \fIsize_t\fP or \fIssize_t\fP
  142. argument.
  143. .SH Conversion specifiers
  144. A character that specifies the type of conversion to be applied. The
  145. conversion specifiers and their meanings are:
  146. .IP "d, i"
  147. The int argument is converted to signed decimal notation. The precision, if
  148. any, gives the minimum number of digits that must appear; if the converted
  149. value requires fewer digits, it is padded on the left with zeros. The default
  150. precision is 1. When 0 is printed with an explicit precision 0, the output is
  151. empty.
  152. .IP "o, u, x, X"
  153. The unsigned int argument is converted to unsigned octal (o), unsigned decimal
  154. (u), or unsigned hexadecimal (\fBx\fP and \fBX\fP) notation. The letters
  155. \fIabcdef\fP are used for \fBx\fP conversions; the letters \fIABCDEF\fP are
  156. used for \fBX\fP conversions. The precision, if any, gives the minimum number
  157. of digits that must appear; if the converted value requires fewer digits, it
  158. is padded on the left with zeros. The default precision is 1. When 0 is
  159. printed with an explicit precision 0, the output is empty.
  160. .IP "e, E"
  161. The double argument is rounded and output in the style \fB"[\-]d.ddde±dd"\fP
  162. .IP "f, F"
  163. The double argument is rounded and output to decimal notation in the style
  164. \fB"[\-]ddd.ddd"\fP.
  165. .IP "g, G"
  166. The double argument is converted in style f or e.
  167. .IP c
  168. The int argument is converted to an unsigned char, and the resulting character
  169. is written.
  170. .IP s
  171. The \fIconst char \fP* argument is expected to be a pointer to an array of
  172. character type (pointer to a string). Characters from the array are written up
  173. to (but not including) a terminating null byte. If a precision is specified,
  174. no more than the number specified are written. If a precision is given, no
  175. null byte need be present; if the precision is not specified, or is greater
  176. than the size of the array, the array must contain a terminating null byte.
  177. .IP p
  178. The \fIvoid \fP* pointer argument is printed in hexadecimal.
  179. .IP n
  180. The number of characters written so far is stored into the integer pointed to
  181. by the corresponding argument.
  182. .IP %
  183. A \(aq%\(aq symbol is written. No argument is converted.
  184. .SH PROTOCOLS
  185. This functionality affects all supported protocols
  186. .SH EXAMPLE
  187. .nf
  188. const char *name = "John";
  189. int main(void)
  190. {
  191. curl_mprintf("My name is %s\\n", name);
  192. curl_mprintf("Pi is almost %f\\n", (double)25.0/8);
  193. }
  194. .fi
  195. .SH AVAILABILITY
  196. Added in curl 7.1
  197. .SH RETURN VALUE
  198. The \fBcurl_maprintf\fP and \fBcurl_mvaprintf\fP functions return a pointer to
  199. a newly allocated string, or NULL if it failed.
  200. All other functions return the number of characters actually printed
  201. (excluding the null byte used to end output to strings). Note that this
  202. sometimes differ from how the POSIX versions of these functions work.
  203. .SH SEE ALSO
  204. .BR fprintf (3),
  205. .BR printf (3),
  206. .BR sprintf (3),
  207. .BR vprintf (3)