Initial dockapps git repo
[dockapps.git] / wmweather+-2.12 / b0rken / vsnprintf.c
blob49ce8fcda04ab814420d0cab7fef3b2f0bbe4c49
1 #include "config.h"
3 /* Copyright (C) 2002 Brad Jorsch <anomie@users.sourceforge.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifdef vsnprintf
21 # undef vsnprintf
22 #endif
24 #include <stdio.h>
25 #include <stdarg.h>
27 #if defined(HAVE_WORKING_VSNPRINTF)
29 /* vsnprintf works, nothing to do */
31 #elif !defined(VSNPRINTF_BOGUS_RETVAL)
33 /* vsnprintf is b0rken, but the return value is ok (thus, the only problem is
34 * NULL) */
36 int rpl_vsnprintf(char *str, size_t size, const char *format, va_list ap){
37 if(str==NULL || size==0){{
38 char foo[3];
39 return vsnprintf(foo, 3, format, ap);
40 }} else {
41 return vsnprintf(str, size, format, ap);
45 #elif !defined(VSNPRINTF_IS_VSPRINTF) && defined(HAVE_VPRINTF)
47 /* vsnprintf's retval is bogus, so we compensate. */
49 static FILE *devnull;
51 #ifndef va_copy
52 # ifdef __va_copy
53 # define va_copy(dest, src) __va_copy(dest, src)
54 # else
55 # include <string.h>
56 # define va_copy(dest, src) memcpy(&dest, &src, sizeof(va_list))
57 # endif
58 #endif
60 int rpl_vsnprintf(char *str, size_t size, const char *format, va_list ap){
61 va_list ap2;
62 int r;
64 va_copy(ap2, ap);
66 #ifdef VSNPRINTF_NULL_OK
67 vsnprintf(str, size, format, ap);
68 #else
69 if(str==NULL || size==0){{
70 char foo[3];
71 vsnprintf(foo, 3, format, ap);
72 }} else {
73 vsnprintf(str, size, format, ap);
75 #endif
77 if(devnull==NULL){
78 if((devnull=fopen("/dev/null", "w"))==NULL){
79 perror("Couldn't open /dev/null for writing");
80 exit(72);
84 r=vfprintf(devnull, format, ap2);
85 va_end(ap2);
86 return r;
89 #else
91 /* OK, we're screwed */
93 # error "vsnprintf is so broken we can't compensate. Sorry."
95 #endif