You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Formatted output to strings, using POSIX/XSI format strings with positions.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  15. USA. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #ifdef __GNUC__
  20. # define alloca __builtin_alloca
  21. # define HAVE_ALLOCA 1
  22. #else
  23. # ifdef _MSC_VER
  24. # include <malloc.h>
  25. # define alloca _alloca
  26. # else
  27. # if defined HAVE_ALLOCA_H || defined _LIBC
  28. # include <alloca.h>
  29. # else
  30. # ifdef _AIX
  31. #pragma alloca
  32. # else
  33. # ifndef alloca
  34. char *alloca ();
  35. # endif
  36. # endif
  37. # endif
  38. # endif
  39. #endif
  40. #include <stdio.h>
  41. #if !HAVE_POSIX_PRINTF
  42. #include <stdlib.h>
  43. #include <string.h>
  44. /* When building a DLL, we must export some functions. Note that because
  45. the functions are only defined for binary backward compatibility, we
  46. don't need to use __declspec(dllimport) in any case. */
  47. #if defined _MSC_VER && BUILDING_DLL
  48. # define DLL_EXPORTED __declspec(dllexport)
  49. #else
  50. # define DLL_EXPORTED
  51. #endif
  52. #define STATIC static
  53. /* Define auxiliary functions declared in "printf-args.h". */
  54. #include "printf-args.c"
  55. /* Define auxiliary functions declared in "printf-parse.h". */
  56. #include "printf-parse.c"
  57. /* Define functions declared in "vasnprintf.h". */
  58. #define vasnprintf libintl_vasnprintf
  59. #include "vasnprintf.c"
  60. #if 0 /* not needed */
  61. #define asnprintf libintl_asnprintf
  62. #include "asnprintf.c"
  63. #endif
  64. DLL_EXPORTED
  65. int
  66. libintl_vfprintf (FILE *stream, const char *format, va_list args)
  67. {
  68. if (strchr (format, '$') == NULL)
  69. return vfprintf (stream, format, args);
  70. else
  71. {
  72. size_t length;
  73. char *result = libintl_vasnprintf (NULL, &length, format, args);
  74. int retval = -1;
  75. if (result != NULL)
  76. {
  77. if (fwrite (result, 1, length, stream) == length)
  78. retval = length;
  79. free (result);
  80. }
  81. return retval;
  82. }
  83. }
  84. DLL_EXPORTED
  85. int
  86. libintl_fprintf (FILE *stream, const char *format, ...)
  87. {
  88. va_list args;
  89. int retval;
  90. va_start (args, format);
  91. retval = libintl_vfprintf (stream, format, args);
  92. va_end (args);
  93. return retval;
  94. }
  95. DLL_EXPORTED
  96. int
  97. libintl_vprintf (const char *format, va_list args)
  98. {
  99. return libintl_vfprintf (stdout, format, args);
  100. }
  101. DLL_EXPORTED
  102. int
  103. libintl_printf (const char *format, ...)
  104. {
  105. va_list args;
  106. int retval;
  107. va_start (args, format);
  108. retval = libintl_vprintf (format, args);
  109. va_end (args);
  110. return retval;
  111. }
  112. DLL_EXPORTED
  113. int
  114. libintl_vsprintf (char *resultbuf, const char *format, va_list args)
  115. {
  116. if (strchr (format, '$') == NULL)
  117. return vsprintf (resultbuf, format, args);
  118. else
  119. {
  120. size_t length = (size_t) ~0 / (4 * sizeof (char));
  121. char *result = libintl_vasnprintf (resultbuf, &length, format, args);
  122. if (result != resultbuf)
  123. {
  124. free (result);
  125. return -1;
  126. }
  127. else
  128. return length;
  129. }
  130. }
  131. DLL_EXPORTED
  132. int
  133. libintl_sprintf (char *resultbuf, const char *format, ...)
  134. {
  135. va_list args;
  136. int retval;
  137. va_start (args, format);
  138. retval = libintl_vsprintf (resultbuf, format, args);
  139. va_end (args);
  140. return retval;
  141. }
  142. #if HAVE_SNPRINTF
  143. # if HAVE_DECL__SNPRINTF
  144. /* Windows. */
  145. # define system_vsnprintf _vsnprintf
  146. # else
  147. /* Unix. */
  148. # define system_vsnprintf vsnprintf
  149. # endif
  150. DLL_EXPORTED
  151. int
  152. libintl_vsnprintf (char *resultbuf, size_t length, const char *format, va_list args)
  153. {
  154. if (strchr (format, '$') == NULL)
  155. return system_vsnprintf (resultbuf, length, format, args);
  156. else
  157. {
  158. size_t maxlength = length;
  159. char *result = libintl_vasnprintf (resultbuf, &length, format, args);
  160. if (result != resultbuf)
  161. {
  162. if (maxlength > 0)
  163. {
  164. if (length < maxlength)
  165. abort ();
  166. memcpy (resultbuf, result, maxlength - 1);
  167. resultbuf[maxlength - 1] = '\0';
  168. }
  169. free (result);
  170. return -1;
  171. }
  172. else
  173. return length;
  174. }
  175. }
  176. DLL_EXPORTED
  177. int
  178. libintl_snprintf (char *resultbuf, size_t length, const char *format, ...)
  179. {
  180. va_list args;
  181. int retval;
  182. va_start (args, format);
  183. retval = libintl_vsnprintf (resultbuf, length, format, args);
  184. va_end (args);
  185. return retval;
  186. }
  187. #endif
  188. #if HAVE_ASPRINTF
  189. DLL_EXPORTED
  190. int
  191. libintl_vasprintf (char **resultp, const char *format, va_list args)
  192. {
  193. size_t length;
  194. char *result = libintl_vasnprintf (NULL, &length, format, args);
  195. if (result == NULL)
  196. return -1;
  197. *resultp = result;
  198. return length;
  199. }
  200. DLL_EXPORTED
  201. int
  202. libintl_asprintf (char **resultp, const char *format, ...)
  203. {
  204. va_list args;
  205. int retval;
  206. va_start (args, format);
  207. retval = libintl_vasprintf (resultp, format, args);
  208. va_end (args);
  209. return retval;
  210. }
  211. #endif
  212. #if HAVE_FWPRINTF
  213. #include <wchar.h>
  214. #define WIDE_CHAR_VERSION 1
  215. /* Define auxiliary functions declared in "wprintf-parse.h". */
  216. #include "printf-parse.c"
  217. /* Define functions declared in "vasnprintf.h". */
  218. #define vasnwprintf libintl_vasnwprintf
  219. #include "vasnprintf.c"
  220. #if 0 /* not needed */
  221. #define asnwprintf libintl_asnwprintf
  222. #include "asnprintf.c"
  223. #endif
  224. # if HAVE_DECL__SNWPRINTF
  225. /* Windows. */
  226. # define system_vswprintf _vsnwprintf
  227. # else
  228. /* Unix. */
  229. # define system_vswprintf vswprintf
  230. # endif
  231. DLL_EXPORTED
  232. int
  233. libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
  234. {
  235. if (wcschr (format, '$') == NULL)
  236. return vfwprintf (stream, format, args);
  237. else
  238. {
  239. size_t length;
  240. wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
  241. int retval = -1;
  242. if (result != NULL)
  243. {
  244. size_t i;
  245. for (i = 0; i < length; i++)
  246. if (fputwc (result[i], stream) == WEOF)
  247. break;
  248. if (i == length)
  249. retval = length;
  250. free (result);
  251. }
  252. return retval;
  253. }
  254. }
  255. DLL_EXPORTED
  256. int
  257. libintl_fwprintf (FILE *stream, const wchar_t *format, ...)
  258. {
  259. va_list args;
  260. int retval;
  261. va_start (args, format);
  262. retval = libintl_vfwprintf (stream, format, args);
  263. va_end (args);
  264. return retval;
  265. }
  266. DLL_EXPORTED
  267. int
  268. libintl_vwprintf (const wchar_t *format, va_list args)
  269. {
  270. return libintl_vfwprintf (stdout, format, args);
  271. }
  272. DLL_EXPORTED
  273. int
  274. libintl_wprintf (const wchar_t *format, ...)
  275. {
  276. va_list args;
  277. int retval;
  278. va_start (args, format);
  279. retval = libintl_vwprintf (format, args);
  280. va_end (args);
  281. return retval;
  282. }
  283. DLL_EXPORTED
  284. int
  285. libintl_vswprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, va_list args)
  286. {
  287. if (wcschr (format, '$') == NULL)
  288. return system_vswprintf (resultbuf, length, format, args);
  289. else
  290. {
  291. size_t maxlength = length;
  292. wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
  293. if (result != resultbuf)
  294. {
  295. if (maxlength > 0)
  296. {
  297. if (length < maxlength)
  298. abort ();
  299. memcpy (resultbuf, result, (maxlength - 1) * sizeof (wchar_t));
  300. resultbuf[maxlength - 1] = 0;
  301. }
  302. free (result);
  303. return -1;
  304. }
  305. else
  306. return length;
  307. }
  308. }
  309. DLL_EXPORTED
  310. int
  311. libintl_swprintf (wchar_t *resultbuf, size_t length, const wchar_t *format, ...)
  312. {
  313. va_list args;
  314. int retval;
  315. va_start (args, format);
  316. retval = libintl_vswprintf (resultbuf, length, format, args);
  317. va_end (args);
  318. return retval;
  319. }
  320. #endif
  321. #endif