1 /*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2012-2016 Symless Ltd.
4 * Copyright (C) 2002 Chris Schoeneman
5 *
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * found in the file LICENSE that should have accompanied this file.
9 *
10 * This package is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "arch/IArchString.h"
20
21 #if HAVE_VSNPRINTF
22
23 #if !defined(ARCH_VSNPRINTF)
24 # define ARCH_VSNPRINTF vsnprintf
25 #endif
26
27 int
vsnprintf(char * str,int size,const char * fmt,va_list ap)28 IArchString::vsnprintf(char* str, int size, const char* fmt, va_list ap)
29 {
30 int n = ::ARCH_VSNPRINTF(str, size, fmt, ap);
31 if (n > size) {
32 n = -1;
33 }
34 return n;
35 }
36
37 #elif SYSAPI_UNIX // !HAVE_VSNPRINTF
38
39 #include <stdio.h>
40
41 int
vsnprintf(char * str,int size,const char * fmt,va_list ap)42 IArchString::vsnprintf(char* str, int size, const char* fmt, va_list ap)
43 {
44 static FILE* bitbucket = fopen("/dev/null", "w");
45 if (bitbucket == NULL) {
46 // uh oh
47 if (size > 0) {
48 str[0] = '\0';
49 }
50 return 0;
51 }
52 else {
53 // count the characters using the bitbucket
54 int n = vfprintf(bitbucket, fmt, ap);
55 if (n + 1 <= size) {
56 // it'll fit so print it into str
57 vsprintf(str, fmt, ap);
58 }
59 return n;
60 }
61 }
62
63 #else // !HAVE_VSNPRINTF && !SYSAPI_UNIX
64
65 #error vsnprintf not implemented
66
67 #endif // !HAVE_VSNPRINTF
68