File: util_libc.h

package info (click to toggle)
s390-tools 2.35.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,248 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (140 lines) | stat: -rw-r--r-- 3,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * @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 @} */