xprintf.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @file
  3. *
  4. * @section DESCRIPTION
  5. *
  6. * These functions provide a portable implementation of the common (but not
  7. * yet universal) asprintf & vasprintf routines to allocate a buffer big
  8. * enough to sprintf the arguments to. The XNF variants terminate the server
  9. * if the allocation fails.
  10. */
  11. /*
  12. * Copyright (c) 2004 Alexander Gottwald
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a
  15. * copy of this software and associated documentation files (the "Software"),
  16. * to deal in the Software without restriction, including without limitation
  17. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. * and/or sell copies of the Software, and to permit persons to whom the
  19. * Software is furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. *
  32. * Except as contained in this notice, the name(s) of the above copyright
  33. * holders shall not be used in advertising or otherwise to promote the sale,
  34. * use or other dealings in this Software without prior written authorization.
  35. */
  36. /*
  37. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  38. *
  39. * Permission is hereby granted, free of charge, to any person obtaining a
  40. * copy of this software and associated documentation files (the "Software"),
  41. * to deal in the Software without restriction, including without limitation
  42. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  43. * and/or sell copies of the Software, and to permit persons to whom the
  44. * Software is furnished to do so, subject to the following conditions:
  45. *
  46. * The above copyright notice and this permission notice (including the next
  47. * paragraph) shall be included in all copies or substantial portions of the
  48. * Software.
  49. *
  50. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  51. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  52. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  53. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  54. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  55. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  56. * DEALINGS IN THE SOFTWARE.
  57. */
  58. #ifdef HAVE_DIX_CONFIG_H
  59. #include <dix-config.h>
  60. #endif
  61. #include <X11/Xos.h>
  62. #include "os.h"
  63. #include <stdarg.h>
  64. #include <stdio.h>
  65. #include <errno.h>
  66. #include <string.h>
  67. #ifdef asprintf
  68. #undef asprintf
  69. #endif
  70. #ifdef vasprintf
  71. #undef vasprintf
  72. #endif
  73. #ifndef va_copy
  74. #ifdef __va_copy
  75. #define va_copy __va_copy
  76. #else
  77. #error "no working va_copy was found"
  78. #endif
  79. #endif
  80. /**
  81. * Varargs sprintf that allocates a string buffer the right size for
  82. * the pattern & data provided and prints the requested data to it.
  83. *
  84. * @param ret Pointer to which the newly allocated buffer is written
  85. * (contents undefined on error)
  86. * @param format printf style format string
  87. * @param va variable argument list
  88. * @return size of allocated buffer, or -1 on error.
  89. */
  90. int
  91. Xvasprintf(char **ret, const char *_X_RESTRICT_KYWD format, va_list va)
  92. {
  93. #ifdef HAVE_VASPRINTF
  94. return vasprintf(ret, format, va);
  95. #else
  96. int size;
  97. va_list va2;
  98. va_copy(va2, va);
  99. size = vsnprintf(NULL, 0, format, va2);
  100. va_end(va2);
  101. *ret = malloc(size + 1);
  102. if (*ret == NULL)
  103. return -1;
  104. vsnprintf(*ret, size + 1, format, va);
  105. (*ret)[size] = 0;
  106. return size;
  107. #endif
  108. }
  109. #ifndef HAVE_VASPRINTF
  110. #define vasprintf Xvasprintf
  111. #endif
  112. /**
  113. * sprintf that allocates a string buffer the right size for
  114. * the pattern & data provided and prints the requested data to it.
  115. *
  116. * @param ret Pointer to which the newly allocated buffer is written
  117. * (contents undefined on error)
  118. * @param format printf style format string
  119. * @param ... arguments for specified format
  120. * @return size of allocated buffer, or -1 on error.
  121. */
  122. int
  123. Xasprintf(char **ret, const char *_X_RESTRICT_KYWD format, ...)
  124. {
  125. int size;
  126. va_list va;
  127. va_start(va, format);
  128. size = vasprintf(ret, format, va);
  129. va_end(va);
  130. return size;
  131. }
  132. /**
  133. * Varargs sprintf that allocates a string buffer the right size for
  134. * the pattern & data provided and prints the requested data to it.
  135. * On failure, issues a FatalError message and aborts the server.
  136. *
  137. * @param ret Pointer to which the newly allocated buffer is written
  138. * (contents undefined on error)
  139. * @param format printf style format string
  140. * @param va variable argument list
  141. * @return size of allocated buffer
  142. */
  143. int
  144. XNFvasprintf(char **ret, const char *_X_RESTRICT_KYWD format, va_list va)
  145. {
  146. int size = vasprintf(ret, format, va);
  147. if ((size == -1) || (*ret == NULL)) {
  148. FatalError("XNFvasprintf failed: %s", strerror(errno));
  149. }
  150. return size;
  151. }
  152. /**
  153. * sprintf that allocates a string buffer the right size for
  154. * the pattern & data provided and prints the requested data to it.
  155. * On failure, issues a FatalError message and aborts the server.
  156. *
  157. * @param ret Pointer to which the newly allocated buffer is written
  158. * (contents undefined on error)
  159. * @param format printf style format string
  160. * @param ... arguments for specified format
  161. * @return size of allocated buffer
  162. */
  163. int
  164. XNFasprintf(char **ret, const char *_X_RESTRICT_KYWD format, ...)
  165. {
  166. int size;
  167. va_list va;
  168. va_start(va, format);
  169. size = XNFvasprintf(ret, format, va);
  170. va_end(va);
  171. return size;
  172. }
  173. /**
  174. * Varargs snprintf that returns the actual number of bytes (excluding final
  175. * '\0') that were copied into the buffer.
  176. * This is opposed to the normal sprintf() usually returns the number of bytes
  177. * that would have been written.
  178. *
  179. * @param s buffer to copy into
  180. * @param n size of buffer s
  181. * @param format printf style format string
  182. * @param va variable argument list
  183. * @return number of bytes actually copied, excluding final '\0'
  184. */
  185. int
  186. Xvscnprintf(char *s, int n, const char *format, va_list args)
  187. {
  188. int x;
  189. if (n == 0)
  190. return 0;
  191. x = vsnprintf(s, n , format, args);
  192. return (x >= n) ? (n - 1) : x;
  193. }
  194. /**
  195. * snprintf that returns the actual number of bytes (excluding final '\0') that
  196. * were copied into the buffer.
  197. * This is opposed to the normal sprintf() usually returns the number of bytes
  198. * that would have been written.
  199. *
  200. * @param s buffer to copy into
  201. * @param n size of buffer s
  202. * @param format printf style format string
  203. * @param ... arguments for specified format
  204. * @return number of bytes actually copied, excluding final '\0'
  205. */
  206. int Xscnprintf(char *s, int n, const char *format, ...)
  207. {
  208. int x;
  209. va_list ap;
  210. va_start(ap, format);
  211. x = Xvscnprintf(s, n, format, ap);
  212. va_end(ap);
  213. return x;
  214. }
  215. /* Old api, now deprecated, may be removed in the future */
  216. char *
  217. Xvprintf(const char *format, va_list va)
  218. {
  219. char *ret;
  220. if (vasprintf(&ret, format, va) == -1)
  221. ret = NULL;
  222. return ret;
  223. }
  224. char *
  225. Xprintf(const char *format, ...)
  226. {
  227. char *ret;
  228. va_list va;
  229. va_start(va, format);
  230. if (vasprintf(&ret, format, va) == -1)
  231. ret = NULL;
  232. va_end(va);
  233. return ret;
  234. }
  235. char *
  236. XNFvprintf(const char *format, va_list va)
  237. {
  238. char *ret;
  239. XNFvasprintf(&ret, format, va);
  240. return ret;
  241. }
  242. char *
  243. XNFprintf(const char *format, ...)
  244. {
  245. char *ret;
  246. va_list va;
  247. va_start(va, format);
  248. XNFvasprintf(&ret, format, va);
  249. va_end(va);
  250. return ret;
  251. }