]> granicus.if.org Git - strace/blob - xstring.h
strace.c: rename set_sigaction to set_sighandler
[strace] / xstring.h
1 #ifndef STRACE_XSTRING_H
2 #define STRACE_XSTRING_H
3
4 #include <stdarg.h>
5 #include <stdio.h>
6
7 #include "error_prints.h"
8 #include "gcc_compat.h"
9
10 /**
11  * Print to static buffer and die on (really unexpected) errors and overflows.
12  * Shouldn't be used directly; please refer to helper macros xsnprintf and
13  * xsprint instead.
14  *
15  * @param str    String buffer to print into.
16  * @param size   Size of the string buffer in bytes.
17  * @param func   Function name from which this function is called.
18  * @param argstr Stringified arguments (including format argument).
19  * @param format Format string.
20  * @param ...    Format arguments.
21  * @return       Number of characters printed, excluding terminating null byte
22  *               (the same as s(n)printf).
23  */
24 static inline int ATTRIBUTE_FORMAT((printf, 5, 6))
25 xsnprintf_(char *str, size_t size, const char *func, const char *argstr,
26            const char *format, ...)
27 {
28         int ret;
29         va_list ap;
30
31         va_start(ap, format);
32         ret = vsnprintf(str, size, format, ap);
33         va_end(ap);
34
35         if (ret < 0 || (unsigned int) ret >= size)
36                 error_msg_and_die("%s: got unexpected return value %d for "
37                                   "snprintf(buf, %zu, %s)",
38                                   func, ret, size, argstr);
39
40         return ret;
41 }
42
43 /**
44  * snprintf that dies on (really unexpected) errors and overflows.
45  *
46  * @param str_  String buffer to print into.
47  * @param size_ Size of the string buffer in bytes.
48  * @param fmt_  Format string.
49  * @param ...   Format arguments.
50  */
51 #define xsnprintf(str_, size_, fmt_, ...) \
52         xsnprintf_((str_), (size_), __func__, #fmt_ ", " #__VA_ARGS__, \
53                    (fmt_), __VA_ARGS__)
54
55 /**
56  * Print to a character array buffer and die on (really unexpected) errors and
57  * overflows.  Buffer size is obtained with sizeof().
58  *
59  * @param str_  Character array buffer to print into.
60  * @param fmt_  Format string.
61  * @param ...   Format arguments.
62  */
63 #define xsprintf(str_, fmt_, ...) \
64         xsnprintf((str_), sizeof(str_) + MUST_BE_ARRAY(str_), (fmt_), \
65                   __VA_ARGS__)
66
67 #endif /* !STRACE_XSTRING_H */