diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-10-17 21:21:38 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-10-17 21:21:38 +0200 |
commit | b6ba2a03fd8acfec113f08ab5df52f4297aac344 (patch) | |
tree | 9907517bf64bc458ca34c0f311cbd9544a11a53a /include | |
parent | info: alloca (diff) | |
download | slibc-b6ba2a03fd8acfec113f08ab5df52f4297aac344.tar.gz slibc-b6ba2a03fd8acfec113f08ab5df52f4297aac344.tar.bz2 slibc-b6ba2a03fd8acfec113f08ab5df52f4297aac344.tar.xz |
add asprintfa and aswprintfa
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | include/stdio.h | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/include/stdio.h b/include/stdio.h index 4dc0e18..2b63ded 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -216,6 +216,48 @@ int asprintf(char** restrict, const char* restrict, ...) #endif #if defined(__SLIBC_SOURCE) +# if defined(__GNUC__) +/** + * This function is identical to `sprintf`, + * except it allocates a sufficiently large + * buffer to the stack (using `alloca`.) + * + * This is a slibc extension. It is only available + * if compiling with GCC. + * + * There is no `vasprintfa` function. + * + * @param buffer:char** Output parameter for the output buffer, + * must not be `NULL`. On error the content + * of this pointer is either `NULL` or a + * pointer to memory allocated with `alloca`. + * @param format:char* The formatting-string, must not have + * side-effects or be `NULL`. + * @param ... The formatting-arguments, must not have + * side-effects. + * @return :int The number of written bytes, excluding + * the NUL byte. On error, a negative value + * (namely -1 in this implementation) is + * returned. It is unspecified what shall + * happen if more than `INT_MAX` non-NUL + * bytes are written; in slibc, `INT_MAX` + * is returned if more is written, you can + * use "%zn" to find the actual length. + * + * @throws EINVAL `format` contained unsupported formatting codes. + */ +int asprintfa(buffer, format, ...) \ + ({ \ + ssize_t __size; \ + int __r; \ + __r = snprintf(NULL, 0, format "%zn", ##__VA_ARGS__, &__size); \ + if (*buffer = NULL, __size += 1, __r >= 0) \ + *buffer = __builtin_alloca((size_t)__size * sizeof(char)), \ + __r = sprintf(*buffer, format, ##__VA_ARGS__); \ + __r; \ + }) +# endif + /** * This function is identical to `asprintf`, * except it can reuse allocated buffers. @@ -620,7 +662,48 @@ int aswprintf(wchar_t** restrict, const wchar_t* restrict, ...) __GCC_ONLY(__attribute__((__nonnull__(1, 2), __warn_unused_result__))); # endif -# if defined(__SLIBC_SOURCE) +# if defined(__SLIBC_SOURCE)# if defined(__GNUC__) +/** + * This function is identical to `swprintf`, + * except it allocates a sufficiently large + * buffer to the stack (using `alloca`.) + * + * This is a slibc extension. It is only available + * if compiling with GCC. + * + * There is no `vaswprintfa` function. + * + * @param buffer:char** Output parameter for the output buffer, + * must not be `NULL`. On error the content + * of this pointer is either `NULL` or a + * pointer to memory allocated with `alloca`. + * @param format:char* The formatting-string, must not have + * side-effects or be `NULL`. + * @param ... The formatting-arguments, must not have + * side-effects. + * @return :int The number of written bytes, excluding + * the NUL byte. On error, a negative value + * (namely -1 in this implementation) is + * returned. It is unspecified what shall + * happen if more than `INT_MAX` non-NUL + * bytes are written; in slibc, `INT_MAX` + * is returned if more is written, you can + * use "%zn" to find the actual length. + * + * @throws EINVAL `format` contained unsupported formatting codes. + */ +int aswprintfa(buffer, format, ...) \ + ({ \ + ssize_t __size; \ + int __r; \ + __r = swprintf(NULL, 0, format "%zn", ##__VA_ARGS__, &__size); \ + if (*buffer = NULL, __size += 1, __r >= 0) \ + *buffer = __builtin_alloca((size_t)__size * sizeof(wchar_t)), \ + __r = swprintf(*buffer, (size_t)__size, format, ##__VA_ARGS__); \ + __r; \ + }) +# endif + /** * This function is identical to `bprintf` except * it uses wide characters. |