]>
Commit | Line | Data |
---|---|---|
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 | |
8 | ||
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
23 | #include <printf.h> | |
24 | #include <stdarg.h> | |
25 | #include <stdio.h> | |
26 | #include <sys/types.h> | |
27 | ||
28 | #include "macro.h" | |
29 | ||
30 | #define xsprintf(buf, fmt, ...) \ | |
31 | assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), "xsprintf: " #buf "[] must be big enough") | |
32 | ||
33 | ||
34 | #define VA_FORMAT_ADVANCE(format, ap) \ | |
35 | do { \ | |
36 | int _argtypes[128]; \ | |
37 | size_t _i, _k; \ | |
38 | _k = parse_printf_format((format), ELEMENTSOF(_argtypes), _argtypes); \ | |
39 | assert(_k < ELEMENTSOF(_argtypes)); \ | |
40 | for (_i = 0; _i < _k; _i++) { \ | |
41 | if (_argtypes[_i] & PA_FLAG_PTR) { \ | |
42 | (void) va_arg(ap, void*); \ | |
43 | continue; \ | |
44 | } \ | |
45 | \ | |
46 | switch (_argtypes[_i]) { \ | |
47 | case PA_INT: \ | |
48 | case PA_INT|PA_FLAG_SHORT: \ | |
49 | case PA_CHAR: \ | |
50 | (void) va_arg(ap, int); \ | |
51 | break; \ | |
52 | case PA_INT|PA_FLAG_LONG: \ | |
53 | (void) va_arg(ap, long int); \ | |
54 | break; \ | |
55 | case PA_INT|PA_FLAG_LONG_LONG: \ | |
56 | (void) va_arg(ap, long long int); \ | |
57 | break; \ | |
58 | case PA_WCHAR: \ | |
59 | (void) va_arg(ap, wchar_t); \ | |
60 | break; \ | |
61 | case PA_WSTRING: \ | |
62 | case PA_STRING: \ | |
63 | case PA_POINTER: \ | |
64 | (void) va_arg(ap, void*); \ | |
65 | break; \ | |
66 | case PA_FLOAT: \ | |
67 | case PA_DOUBLE: \ | |
68 | (void) va_arg(ap, double); \ | |
69 | break; \ | |
70 | case PA_DOUBLE|PA_FLAG_LONG_DOUBLE: \ | |
71 | (void) va_arg(ap, long double); \ | |
72 | break; \ | |
73 | default: \ | |
74 | assert_not_reached("Unknown format string argument."); \ | |
75 | } \ | |
76 | } \ | |
9ed794a3 | 77 | } while (false) |