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