xref: /OK3568_Linux_fs/external/xserver/include/Xprintf.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun  * Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #ifndef XPRINTF_H
25*4882a593Smuzhiyun #define XPRINTF_H
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <stdio.h>
28*4882a593Smuzhiyun #include <stdarg.h>
29*4882a593Smuzhiyun #include <X11/Xfuncproto.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifndef _X_RESTRICT_KYWD
32*4882a593Smuzhiyun #if defined(restrict) /* assume autoconf set it correctly */ || \
33*4882a593Smuzhiyun    (defined(__STDC__) && (__STDC_VERSION__ - 0 >= 199901L))     /* C99 */
34*4882a593Smuzhiyun #define _X_RESTRICT_KYWD  restrict
35*4882a593Smuzhiyun #elif defined(__GNUC__) && !defined(__STRICT_ANSI__)    /* gcc w/C89+extensions */
36*4882a593Smuzhiyun #define _X_RESTRICT_KYWD __restrict__
37*4882a593Smuzhiyun #else
38*4882a593Smuzhiyun #define _X_RESTRICT_KYWD
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun  * These functions provide a portable implementation of the common (but not
44*4882a593Smuzhiyun  * yet universal) asprintf & vasprintf routines to allocate a buffer big
45*4882a593Smuzhiyun  * enough to sprintf the arguments to.  The XNF variants terminate the server
46*4882a593Smuzhiyun  * if the allocation fails.
47*4882a593Smuzhiyun  * The buffer allocated is returned in the pointer provided in the first
48*4882a593Smuzhiyun  * argument.   The return value is the size of the allocated buffer, or -1
49*4882a593Smuzhiyun  * on failure.
50*4882a593Smuzhiyun  */
51*4882a593Smuzhiyun extern _X_EXPORT int
52*4882a593Smuzhiyun Xasprintf(char **ret, const char *_X_RESTRICT_KYWD fmt, ...)
53*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(2, 3);
54*4882a593Smuzhiyun extern _X_EXPORT int
55*4882a593Smuzhiyun Xvasprintf(char **ret, const char *_X_RESTRICT_KYWD fmt, va_list va)
56*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(2, 0);
57*4882a593Smuzhiyun extern _X_EXPORT int
58*4882a593Smuzhiyun XNFasprintf(char **ret, const char *_X_RESTRICT_KYWD fmt, ...)
59*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(2, 3);
60*4882a593Smuzhiyun extern _X_EXPORT int
61*4882a593Smuzhiyun XNFvasprintf(char **ret, const char *_X_RESTRICT_KYWD fmt, va_list va)
62*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(2, 0);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #if !defined(HAVE_ASPRINTF) && !defined(HAVE_VASPRINTF)
65*4882a593Smuzhiyun #define asprintf  Xasprintf
66*4882a593Smuzhiyun #define vasprintf Xvasprintf
67*4882a593Smuzhiyun #endif
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * These functions provide a portable implementation of the linux kernel
71*4882a593Smuzhiyun  * scnprintf & vscnprintf routines that return the number of bytes actually
72*4882a593Smuzhiyun  * copied during a snprintf, (excluding the final '\0').
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun extern _X_EXPORT int
75*4882a593Smuzhiyun Xscnprintf(char *s, int n, const char * _X_RESTRICT_KYWD fmt, ...)
76*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(3,4);
77*4882a593Smuzhiyun extern _X_EXPORT int
78*4882a593Smuzhiyun Xvscnprintf(char *s, int n, const char * _X_RESTRICT_KYWD fmt, va_list va)
79*4882a593Smuzhiyun _X_ATTRIBUTE_PRINTF(3,0);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #endif                          /* XPRINTF_H */
82