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