mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
Add function util_concatf() that appends the result of a format string
expansion to the end of an existing string while taking care of the
required memory allocations.
Usage example:
char *str = NULL;
util_concatf(&str, "list:");
for (int i = 1; i <= 3; i++)
util_concatf(&str, "%spart%d", (i > 1 ? "," : ""), i);
printf("%s\n", str); /* list:part1,part2,part3 */
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
141 lines
3.9 KiB
C
141 lines
3.9 KiB
C
/**
|
|
* @defgroup util_libc_h util_libc: Libc wrapper interface
|
|
* @{
|
|
* @brief Handle standard errors for libc functions
|
|
*
|
|
* Copyright IBM Corp. 2016, 2017
|
|
*
|
|
* s390-tools is free software; you can redistribute it and/or modify
|
|
* it under the terms of the MIT license. See LICENSE for details.
|
|
*/
|
|
|
|
#ifndef LIB_UTIL_LIBC_H
|
|
#define LIB_UTIL_LIBC_H
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/**
|
|
* Allocate memory or panic in case of failure
|
|
*
|
|
* @param[in] size Number of bytes to be allocated
|
|
*
|
|
* @returns Pointer to memory buffer created with malloc()
|
|
*/
|
|
#define util_malloc(size) \
|
|
__util_malloc(__func__, __FILE__, __LINE__, size)
|
|
|
|
void *__util_malloc(const char *func, const char *file, int line, size_t size);
|
|
|
|
/**
|
|
* Allocate zero-initialized memory or panic in case of failure
|
|
*
|
|
* @param[in] size Number of bytes to be allocated
|
|
*
|
|
* @returns Pointer to memory buffer created with calloc()
|
|
*/
|
|
#define util_zalloc(size) \
|
|
__util_zalloc(__func__, __FILE__, __LINE__, size)
|
|
|
|
void *__util_zalloc(const char *func, const char *file, int line, size_t size);
|
|
|
|
/**
|
|
* Re-allocate memory or exit in case of failure
|
|
*
|
|
* @param[in] ptr Pointer ot old memory buffer
|
|
* @param[in] size Number of bytes to be allocated
|
|
*
|
|
* @returns Pointer to memory buffer created with realloc()
|
|
*/
|
|
#define util_realloc(ptr, size) \
|
|
__util_realloc(__func__, __FILE__, __LINE__, ptr, size)
|
|
|
|
void *__util_realloc(const char *func, const char *file, int line,
|
|
void *ptr, size_t size);
|
|
|
|
/**
|
|
* Duplicate a string buffer or exit in case of failure
|
|
*
|
|
* @param[in] str String to be duplicated
|
|
*
|
|
* @returns Pointer to copied string allocated with malloc()
|
|
*/
|
|
#define util_strdup(str) \
|
|
__util_strdup(__func__, __FILE__, __LINE__, str)
|
|
|
|
void *__util_strdup(const char *func, const char *file, int line,
|
|
const char *str);
|
|
|
|
/**
|
|
* Print to allocated string or exit in case of failure
|
|
*
|
|
* @param[in,out] strp Pointer for returned string allocated with malloc()
|
|
* @param[in] fmt Format string for generation of string
|
|
* @param[in] ap Parameters for format string
|
|
*
|
|
* @returns num Number of formatted characters
|
|
*/
|
|
#define util_vasprintf(strp, fmt, ap) \
|
|
__util_vasprintf(__func__, __FILE__, __LINE__, strp, fmt, ap)
|
|
|
|
#define UTIL_VASPRINTF(strp, fmt, ap) \
|
|
do { \
|
|
va_start(ap, fmt); \
|
|
util_vasprintf(strp, fmt, ap); \
|
|
va_end(ap); \
|
|
} while (0)
|
|
|
|
int __util_vasprintf(const char *func, const char *file, int line,
|
|
char **strp, const char *fmt, va_list ap);
|
|
|
|
/**
|
|
* Print to newly allocated string or exit in case of failure
|
|
*
|
|
* @param[in,out] strp Pointer for returned string allocated with malloc()
|
|
* @param[in] ... Format string and parameters for format string
|
|
*
|
|
* @returns num Number of formatted characters
|
|
*/
|
|
#define util_asprintf(strp, ...) \
|
|
__util_asprintf(__func__, __FILE__, __LINE__, strp, ##__VA_ARGS__)
|
|
|
|
int __util_asprintf(const char *func, const char *file, int line,
|
|
char **strp, const char *fmt, ...);
|
|
|
|
/**
|
|
* Print to string buffer or exit in case of failure
|
|
*
|
|
* @param[in] str String buffer
|
|
* @param[in] fmt Format string for generation of string
|
|
* @param[in] ap Parameters for format string
|
|
*
|
|
* @returns num Number of formatted characters
|
|
*/
|
|
#define util_vsprintf(str, fmt, ap) \
|
|
__util_vsprintf(__func__, __FILE__, __LINE__, str, fmt, ap)
|
|
|
|
#define UTIL_VSPRINTF(str, fmt, ap) \
|
|
do { \
|
|
va_start(ap, fmt); \
|
|
util_vsprintf(str, fmt, ap); \
|
|
va_end(ap); \
|
|
} while (0)
|
|
|
|
int __util_vsprintf(const char *func, const char *file, int line,
|
|
char *str, const char *fmt, va_list ap);
|
|
char *util_strcat_realloc(char *str1, const char *str2);
|
|
void util_concatf(char **str1, const char *fmt, ...);
|
|
void util_str_toupper(char *str);
|
|
|
|
char *util_strstrip(char *s);
|
|
size_t util_strlcpy(char *dest, const char *src, size_t size);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /** LIB_UTIL_LIBC_H @} */
|