]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/stdio-util.h
logind: Don't match non-leader processes for utmp TTY determination (#38027)
[thirdparty/systemd.git] / src / basic / stdio-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
15a5e950
LP
2#pragma once
3
15a5e950 4#include <printf.h>
15a5e950 5#include <stdio.h>
15a5e950 6
0c15577a 7#include "forward.h"
15a5e950 8
39522906 9_printf_(3, 4)
39a8ef05 10static inline char* snprintf_ok(char *buf, size_t len, const char *format, ...) {
39522906
DDM
11 va_list ap;
12 int r;
13
14 va_start(ap, format);
39522906 15 r = vsnprintf(buf, len, format, ap);
39522906
DDM
16 va_end(ap);
17
18 return r >= 0 && (size_t) r < len ? buf : NULL;
19}
15a5e950 20
73fc96c8 21#define xsprintf(buf, fmt, ...) \
892dc967 22 assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, ##__VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
15a5e950
LP
23
24#define VA_FORMAT_ADVANCE(format, ap) \
25do { \
26 int _argtypes[128]; \
27 size_t _i, _k; \
3e180a25
EV
28 /* See https://github.com/google/sanitizers/issues/992 */ \
29 if (HAS_FEATURE_MEMORY_SANITIZER) \
d80463ea 30 memset(_argtypes, 0, sizeof(_argtypes)); \
15a5e950
LP
31 _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \
32 assert(_k < ELEMENTSOF(_argtypes)); \
33 for (_i = 0; _i < _k; _i++) { \
34 if (_argtypes[_i] & PA_FLAG_PTR) { \
35 (void) va_arg(ap, void*); \
36 continue; \
37 } \
38 \
39 switch (_argtypes[_i]) { \
40 case PA_INT: \
41 case PA_INT|PA_FLAG_SHORT: \
42 case PA_CHAR: \
43 (void) va_arg(ap, int); \
44 break; \
45 case PA_INT|PA_FLAG_LONG: \
46 (void) va_arg(ap, long int); \
47 break; \
48 case PA_INT|PA_FLAG_LONG_LONG: \
49 (void) va_arg(ap, long long int); \
50 break; \
51 case PA_WCHAR: \
52 (void) va_arg(ap, wchar_t); \
53 break; \
54 case PA_WSTRING: \
55 case PA_STRING: \
56 case PA_POINTER: \
57 (void) va_arg(ap, void*); \
58 break; \
59 case PA_FLOAT: \
60 case PA_DOUBLE: \
61 (void) va_arg(ap, double); \
62 break; \
63 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \
64 (void) va_arg(ap, long double); \
65 break; \
66 default: \
04499a70 67 assert_not_reached(); \
15a5e950
LP
68 } \
69 } \
9ed794a3 70} while (false)