@@ -82,24 +82,22 @@ typedef unsigned long ulong_long;
82
82
* for string length. This covers a nasty loophole.
83
83
*
84
84
* The other functions are there to prevent NULL pointers from
85
- * causing nast effects.
85
+ * causing nasty effects.
86
86
**************************************************************/
87
87
88
- /*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.7 2005/02/28 14:16:16 momjian Exp $";*/
89
- static char * end ;
90
- static int SnprfOverflow ;
88
+ /*static char _id[] = "$PostgreSQL: pgsql/src/port/snprintf.c,v 1.8 2005/03/01 00:38:11 momjian Exp $";*/
91
89
92
90
int snprintf (char * str , size_t count , const char * fmt ,...);
93
91
int vsnprintf (char * str , size_t count , const char * fmt , va_list args );
94
92
int printf (const char * format , ...);
95
- static void dopr (char * buffer , const char * format , va_list args );
93
+ static void dopr (char * buffer , const char * format , va_list args , char * end );
96
94
97
95
int
98
96
printf (const char * fmt ,...)
99
97
{
100
98
int len ;
101
99
va_list args ;
102
- static char * buffer [4096 ];
100
+ char * buffer [4096 ];
103
101
char * p ;
104
102
105
103
va_start (args , fmt );
@@ -127,10 +125,10 @@ snprintf(char *str, size_t count, const char *fmt,...)
127
125
int
128
126
vsnprintf (char * str , size_t count , const char * fmt , va_list args )
129
127
{
128
+ char * end ;
130
129
str [0 ] = '\0' ;
131
130
end = str + count - 1 ;
132
- SnprfOverflow = 0 ;
133
- dopr (str , fmt , args );
131
+ dopr (str , fmt , args , end );
134
132
if (count > 0 )
135
133
end [0 ] = '\0' ;
136
134
return strlen (str );
@@ -140,11 +138,11 @@ vsnprintf(char *str, size_t count, const char *fmt, va_list args)
140
138
* dopr(): poor man's version of doprintf
141
139
*/
142
140
143
- static void fmtstr (char * value , int ljust , int len , int zpad , int maxwidth );
144
- static void fmtnum (long_long value , int base , int dosign , int ljust , int len , int zpad );
145
- static void fmtfloat (double value , char type , int ljust , int len , int precision , int pointflag );
146
- static void dostr (char * str , int cut );
147
- static void dopr_outch (int c );
141
+ static void fmtstr (char * value , int ljust , int len , int zpad , int maxwidth , char * end );
142
+ static void fmtnum (long_long value , int base , int dosign , int ljust , int len , int zpad , char * end );
143
+ static void fmtfloat (double value , char type , int ljust , int len , int precision , int pointflag , char * end );
144
+ static void dostr (char * str , int cut , char * end );
145
+ static void dopr_outch (int c , char * end );
148
146
149
147
static char * output ;
150
148
@@ -154,7 +152,7 @@ static char *output;
154
152
#define FMTCHAR 4
155
153
156
154
static void
157
- dopr (char * buffer , const char * format , va_list args )
155
+ dopr (char * buffer , const char * format , va_list args , char * end )
158
156
{
159
157
int ch ;
160
158
long_long value ;
@@ -417,11 +415,11 @@ dopr(char *buffer, const char *format, va_list args)
417
415
case '%' :
418
416
break ;
419
417
default :
420
- dostr ("???????" , 0 );
418
+ dostr ("???????" , 0 , end );
421
419
}
422
420
break ;
423
421
default :
424
- dopr_outch (ch );
422
+ dopr_outch (ch , end );
425
423
break ;
426
424
}
427
425
}
@@ -448,27 +446,28 @@ dopr(char *buffer, const char *format, va_list args)
448
446
case FMTSTR :
449
447
fmtstr (fmtparptr [i ]-> value , fmtparptr [i ]-> ljust ,
450
448
fmtparptr [i ]-> len , fmtparptr [i ]-> zpad ,
451
- fmtparptr [i ]-> maxwidth );
449
+ fmtparptr [i ]-> maxwidth , end );
452
450
break ;
453
451
case FMTNUM :
454
452
fmtnum (fmtparptr [i ]-> numvalue , fmtparptr [i ]-> base ,
455
453
fmtparptr [i ]-> dosign , fmtparptr [i ]-> ljust ,
456
- fmtparptr [i ]-> len , fmtparptr [i ]-> zpad );
454
+ fmtparptr [i ]-> len , fmtparptr [i ]-> zpad , end );
457
455
break ;
458
456
case FMTFLOAT :
459
457
fmtfloat (fmtparptr [i ]-> fvalue , fmtparptr [i ]-> type ,
460
458
fmtparptr [i ]-> ljust , fmtparptr [i ]-> len ,
461
- fmtparptr [i ]-> precision , fmtparptr [i ]-> pointflag );
459
+ fmtparptr [i ]-> precision , fmtparptr [i ]-> pointflag ,
460
+ end );
462
461
break ;
463
462
case FMTCHAR :
464
- dopr_outch (fmtparptr [i ]-> charvalue );
463
+ dopr_outch (fmtparptr [i ]-> charvalue , end );
465
464
break ;
466
465
}
467
466
format = fmtpar [i ].fmtend ;
468
467
goto nochar ;
469
468
}
470
469
}
471
- dopr_outch (ch );
470
+ dopr_outch (ch , end );
472
471
nochar :
473
472
/* nothing */
474
473
; /* semicolon required because a goto has to be attached to a statement */
@@ -477,7 +476,7 @@ dopr(char *buffer, const char *format, va_list args)
477
476
}
478
477
479
478
static void
480
- fmtstr (char * value , int ljust , int len , int zpad , int maxwidth )
479
+ fmtstr (char * value , int ljust , int len , int zpad , int maxwidth , char * end )
481
480
{
482
481
int padlen ,
483
482
strlen ; /* amount to pad */
@@ -494,19 +493,19 @@ fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
494
493
padlen = - padlen ;
495
494
while (padlen > 0 )
496
495
{
497
- dopr_outch (' ' );
496
+ dopr_outch (' ' , end );
498
497
-- padlen ;
499
498
}
500
- dostr (value , maxwidth );
499
+ dostr (value , maxwidth , end );
501
500
while (padlen < 0 )
502
501
{
503
- dopr_outch (' ' );
502
+ dopr_outch (' ' , end );
504
503
++ padlen ;
505
504
}
506
505
}
507
506
508
507
static void
509
- fmtnum (long_long value , int base , int dosign , int ljust , int len , int zpad )
508
+ fmtnum (long_long value , int base , int dosign , int ljust , int len , int zpad , char * end )
510
509
{
511
510
int signvalue = 0 ;
512
511
ulong_long uvalue ;
@@ -561,34 +560,34 @@ fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad)
561
560
{
562
561
if (signvalue )
563
562
{
564
- dopr_outch (signvalue );
563
+ dopr_outch (signvalue , end );
565
564
-- padlen ;
566
565
signvalue = 0 ;
567
566
}
568
567
while (padlen > 0 )
569
568
{
570
- dopr_outch (zpad );
569
+ dopr_outch (zpad , end );
571
570
-- padlen ;
572
571
}
573
572
}
574
573
while (padlen > 0 )
575
574
{
576
- dopr_outch (' ' );
575
+ dopr_outch (' ' , end );
577
576
-- padlen ;
578
577
}
579
578
if (signvalue )
580
- dopr_outch (signvalue );
579
+ dopr_outch (signvalue , end );
581
580
while (place > 0 )
582
- dopr_outch (convert [-- place ]);
581
+ dopr_outch (convert [-- place ], end );
583
582
while (padlen < 0 )
584
583
{
585
- dopr_outch (' ' );
584
+ dopr_outch (' ' , end );
586
585
++ padlen ;
587
586
}
588
587
}
589
588
590
589
static void
591
- fmtfloat (double value , char type , int ljust , int len , int precision , int pointflag )
590
+ fmtfloat (double value , char type , int ljust , int len , int precision , int pointflag , char * end )
592
591
{
593
592
char fmt [32 ];
594
593
char convert [512 ];
@@ -615,34 +614,34 @@ fmtfloat(double value, char type, int ljust, int len, int precision, int pointfl
615
614
616
615
while (padlen > 0 )
617
616
{
618
- dopr_outch (' ' );
617
+ dopr_outch (' ' , end );
619
618
-- padlen ;
620
619
}
621
- dostr (convert , 0 );
620
+ dostr (convert , 0 , end );
622
621
while (padlen < 0 )
623
622
{
624
- dopr_outch (' ' );
623
+ dopr_outch (' ' , end );
625
624
++ padlen ;
626
625
}
627
626
}
628
627
629
628
static void
630
- dostr (char * str , int cut )
629
+ dostr (char * str , int cut , char * end )
631
630
{
632
631
if (cut )
633
632
{
634
633
while (* str && cut -- > 0 )
635
- dopr_outch (* str ++ );
634
+ dopr_outch (* str ++ , end );
636
635
}
637
636
else
638
637
{
639
638
while (* str )
640
- dopr_outch (* str ++ );
639
+ dopr_outch (* str ++ , end );
641
640
}
642
641
}
643
642
644
643
static void
645
- dopr_outch (int c )
644
+ dopr_outch (int c , char * end )
646
645
{
647
646
#ifdef NOT_USED
648
647
if (iscntrl ((unsigned char ) c ) && c != '\n' && c != '\t' )
@@ -654,6 +653,4 @@ dopr_outch(int c)
654
653
#endif
655
654
if (end == 0 || output < end )
656
655
* output ++ = c ;
657
- else
658
- SnprfOverflow ++ ;
659
656
}
0 commit comments