3232 * SUCH DAMAGE.
3333 */
3434
35- /* might be in either frontend or backend */
35+ #ifndef FRONTEND
36+ #include "postgres.h"
37+ #else
3638#include "postgres_fe.h"
39+ #endif
3740
3841#ifndef WIN32
3942#include <sys/ioctl.h>
4043#endif
4144#include <sys/param.h>
4245
43- #ifndef NL_ARGMAX
44- #define NL_ARGMAX 4096
45- #endif
46-
4746/*
4847**SNPRINTF, VSNPRINT -- counted versions of printf
4948**
6665 * causing nasty effects.
6766 **************************************************************/
6867
69- /*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.11 2005/03/0203:21:52 momjian Exp $";*/
68+ /*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.12 2005/03/0205:22:22 momjian Exp $";*/
7069
7170int snprintf (char * str ,size_t count ,const char * fmt ,...);
7271int vsnprintf (char * str ,size_t count ,const char * fmt ,va_list args );
@@ -157,11 +156,9 @@ dopr(char *buffer, const char *format, va_list args, char *end)
157156int realpos = 0 ;
158157int position ;
159158char * output ;
160- /* In thread mode this structure is too large. */
161- #ifndef ENABLE_THREAD_SAFETY
162- static
163- #endif
164- struct {
159+ int percents = 1 ;
160+ const char * p ;
161+ struct fmtpar {
165162const char * fmtbegin ;
166163const char * fmtend ;
167164void * value ;
@@ -179,10 +176,30 @@ dopr(char *buffer, const char *format, va_list args, char *end)
179176int pointflag ;
180177char func ;
181178int realpos ;
182- }fmtpar [NL_ARGMAX + 1 ],* fmtparptr [NL_ARGMAX + 1 ];
183-
179+ }* fmtpar ,* * fmtparptr ;
184180
181+ /* Create enough structures to hold all arguments */
182+ for (p = format ;* p != '\0' ;p ++ )
183+ if (* p == '%' )/* counts %% as two, so overcounts */
184+ percents ++ ;
185+ #ifndef FRONTEND
186+ fmtpar = pgport_palloc (sizeof (struct fmtpar )* percents );
187+ fmtparptr = pgport_palloc (sizeof (struct fmtpar * )* percents );
188+ #else
189+ if ((fmtpar = malloc (sizeof (struct fmtpar )* percents ))== NULL )
190+ {
191+ fprintf (stderr ,_ ("out of memory\n" ));
192+ exit (1 );
193+ }
194+ if ((fmtparptr = malloc (sizeof (struct fmtpar * )* percents ))== NULL )
195+ {
196+ fprintf (stderr ,_ ("out of memory\n" ));
197+ exit (1 );
198+ }
199+ #endif
200+
185201format_save = format ;
202+
186203output = buffer ;
187204while ((ch = * format ++ ))
188205{
@@ -418,9 +435,7 @@ dopr(char *buffer, const char *format, va_list args, char *end)
418435performpr :
419436/* shuffle pointers */
420437for (i = 1 ;i < fmtpos ;i ++ )
421- {
422438fmtparptr [i ]= & fmtpar [fmtpar [i ].realpos ];
423- }
424439output = buffer ;
425440format = format_save ;
426441while ((ch = * format ++ ))
@@ -465,6 +480,14 @@ dopr(char *buffer, const char *format, va_list args, char *end)
465480;/* semicolon required because a goto has to be attached to a statement */
466481}
467482* output = '\0' ;
483+
484+ #ifndef FRONTEND
485+ pgport_pfree (fmtpar );
486+ pgport_pfree (fmtparptr );
487+ #else
488+ free (fmtpar );
489+ free (fmtparptr );
490+ #endif
468491}
469492
470493static void