1 // Copyright (c) 2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #ifndef WINPTY_SNPRINTF_H
22 #define WINPTY_SNPRINTF_H
28 #include "WinptyAssert.h"
30 #if defined(__CYGWIN__) || defined(__MSYS__)
31 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg) \
32 __attribute__((format(printf, (fmtarg), ((vararg)))))
33 #elif defined(__GNUC__)
34 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg) \
35 __attribute__((format(ms_printf, (fmtarg), ((vararg)))))
37 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg)
40 // Returns a value between 0 and size - 1 (inclusive) on success. Returns -1
41 // on failure (including truncation). The output buffer is always
44 winpty_vsnprintf(char *out, size_t size, const char *fmt, va_list ap) {
47 #if defined(_MSC_VER) && _MSC_VER < 1900
48 // MSVC 2015 added a C99-conforming vsnprintf.
49 int count = _vsnprintf_s(out, size, _TRUNCATE, fmt, ap);
51 // MinGW configurations frequently provide a vsnprintf function that simply
52 // calls one of the MS _vsnprintf* functions, which are not C99 conformant.
53 int count = vsnprintf(out, size, fmt, ap);
55 if (count < 0 || static_cast<size_t>(count) >= size) {
56 // On truncation, some *printf* implementations return the
57 // non-truncated size, but other implementations returns -1. Return
58 // -1 for consistency.
60 // Guarantee NUL termination.
63 // Guarantee NUL termination.
69 // Wraps winpty_vsnprintf.
70 inline int winpty_snprintf(char *out, size_t size, const char *fmt, ...)
71 WINPTY_SNPRINTF_FORMAT(3, 4);
72 inline int winpty_snprintf(char *out, size_t size, const char *fmt, ...) {
75 const int count = winpty_vsnprintf(out, size, fmt, ap);
80 // Wraps winpty_vsnprintf with automatic size determination.
81 template <size_t size>
82 int winpty_vsnprintf(char (&out)[size], const char *fmt, va_list ap) {
83 return winpty_vsnprintf(out, size, fmt, ap);
86 // Wraps winpty_vsnprintf with automatic size determination.
87 template <size_t size>
88 int winpty_snprintf(char (&out)[size], const char *fmt, ...)
89 WINPTY_SNPRINTF_FORMAT(2, 3);
90 template <size_t size>
91 int winpty_snprintf(char (&out)[size], const char *fmt, ...) {
94 const int count = winpty_vsnprintf(out, size, fmt, ap);
99 #endif // WINPTY_SNPRINTF_H