]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/stdio-util.h
core: reduce scope of variants
[thirdparty/systemd.git] / src / basic / stdio-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <printf.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8
9 #include "macro.h"
10 #include "memory-util.h"
11
12 #define snprintf_ok(buf, len, fmt, ...) \
13 ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
14
15 #define xsprintf(buf, fmt, ...) \
16 assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
17
18 #define VA_FORMAT_ADVANCE(format, ap) \
19 do { \
20 int _argtypes[128]; \
21 size_t _i, _k; \
22 /* See https://github.com/google/sanitizers/issues/992 */ \
23 if (HAS_FEATURE_MEMORY_SANITIZER) \
24 zero(_argtypes); \
25 _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
26 assert(_k < ELEMENTSOF(_argtypes)); \
27 for (_i = 0; _i < _k; _i++) { \
28 if (_argtypes[_i] & PA_FLAG_PTR) { \
29 (void) va_arg(ap, void*); \
30 continue; \
31 } \
32 \
33 switch (_argtypes[_i]) { \
34 case PA_INT: \
35 case PA_INT|PA_FLAG_SHORT: \
36 case PA_CHAR: \
37 (void) va_arg(ap, int); \
38 break; \
39 case PA_INT|PA_FLAG_LONG: \
40 (void) va_arg(ap, long int); \
41 break; \
42 case PA_INT|PA_FLAG_LONG_LONG: \
43 (void) va_arg(ap, long long int); \
44 break; \
45 case PA_WCHAR: \
46 (void) va_arg(ap, wchar_t); \
47 break; \
48 case PA_WSTRING: \
49 case PA_STRING: \
50 case PA_POINTER: \
51 (void) va_arg(ap, void*); \
52 break; \
53 case PA_FLOAT: \
54 case PA_DOUBLE: \
55 (void) va_arg(ap, double); \
56 break; \
57 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
58 (void) va_arg(ap, long double); \
59 break; \
60 default: \
61 assert_not_reached("Unknown format string argument."); \
62 } \
63 } \
64 } while (false)