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