Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit edd626c

Browse files
committed
Try this snprintf() implementation, used in sendmail...
1 parent 370d6cd commit edd626c

File tree

1 file changed

+309
-58
lines changed

1 file changed

+309
-58
lines changed

src/backend/port/snprintf.c

+309-58
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
/*-
2-
* Copyright (c) 1990, 1993
1+
/*
2+
* Copyright (c) 1983, 1995, 1996 Eric P. Allman
3+
* Copyright (c) 1988, 1993
34
* The Regents of the University of California. All rights reserved.
45
*
5-
* This code is derived from software contributed to Berkeley by
6-
* Chris Torek.
7-
*
86
* Redistribution and use in source and binary forms, with or without
97
* modification, are permitted provided that the following conditions
108
* are met:
119
* 1. Redistributions of source code must retain the above copyright
12-
* notice, this list of conditions and the following disclaimer.
10+
* notice, this list of conditions and the following disclaimer.
1311
* 2. Redistributions in binary form must reproduce the above copyright
14-
* notice, this list of conditions and the following disclaimer in the
15-
* documentation and/or other materials provided with the distribution.
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
1614
* 3. All advertising materials mentioning features or use of this software
17-
* must display the following acknowledgement:
15+
* must display the following acknowledgement:
1816
* This product includes software developed by the University of
1917
* California, Berkeley and its contributors.
2018
* 4. Neither the name of the University nor the names of its contributors
21-
* may be used to endorse or promote products derived from this software
22-
* without specific prior written permission.
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
2321
*
2422
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2523
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2624
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27-
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2826
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2927
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3028
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@@ -34,59 +32,312 @@
3432
* SUCH DAMAGE.
3533
*/
3634

37-
#if defined(LIBC_SCCS) && !defined(lint)
38-
#if 0
39-
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
35+
# include "sendmail.h"
36+
# include "pathnames.h"
37+
# include <sys/ioctl.h>
38+
# include <sys/param.h>
4039

41-
#endif
42-
static const char rcsid[] =
43-
"$Id: snprintf.c,v 1.3 1998/09/01 04:30:44 momjian Exp $";
40+
/*
41+
** SNPRINTF, VSNPRINT -- counted versions of printf
42+
**
43+
** These versions have been grabbed off the net. They have been
44+
** cleaned up to compile properly and support for .precision and
45+
** %lx has been added.
46+
*/
4447

45-
#endif /* LIBC_SCCS and not lint */
48+
/**************************************************************
49+
* Original:
50+
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
51+
* A bombproof version of doprnt (dopr) included.
52+
* Sigh. This sort of thing is always nasty do deal with. Note that
53+
* the version here does not include floating point...
54+
*
55+
* snprintf() is used instead of sprintf() as it does limit checks
56+
* for string length. This covers a nasty loophole.
57+
*
58+
* The other functions are there to prevent NULL pointers from
59+
* causing nast effects.
60+
**************************************************************/
4661

47-
#include <limits.h>
48-
#include <stdio.h>
49-
#if __STDC__
50-
#include <stdarg.h>
51-
#else
52-
#include <varargs.h>
53-
#endif
62+
/*static char _id[] = "$Id: snprintf.c,v 1.4 1998/09/04 14:34:23 scrappy Exp $";*/
63+
static void dopr();
64+
static char *end;
65+
static int SnprfOverflow;
5466

55-
#if __STDC__
67+
/* VARARGS3 */
5668
int
57-
snprintf(char *str, size_t n, char const * fmt,...)
58-
#else
69+
# ifdef __STDC__
70+
snprintf(char *str, size_t count, const char *fmt, ...)
71+
# else
72+
snprintf(str, count, fmt, va_alist)
73+
char *str;
74+
size_t count;
75+
const char *fmt;
76+
va_dcl
77+
#endif
78+
{
79+
int len;
80+
VA_LOCAL_DECL
81+
82+
VA_START(fmt);
83+
len = vsnprintf(str, count, fmt, ap);
84+
VA_END;
85+
return len;
86+
}
87+
88+
89+
# ifndef luna2
5990
int
60-
snprintf(str, n, fmt, va_alist)
61-
char *str;
62-
size_t n;
63-
char *fmt;
91+
vsnprintf(str, count, fmt, args)
92+
char *str;
93+
size_t count;
94+
const char *fmt;
95+
va_list args;
96+
{
97+
str[0] = 0;
98+
end = str + count - 1;
99+
SnprfOverflow = 0;
100+
dopr( str, fmt, args );
101+
if (count > 0)
102+
end[0] = 0;
103+
if (SnprfOverflow && tTd(57, 2))
104+
printf("\nvsnprintf overflow, len = %d, str = %s",
105+
count, shortenstring(str, 203));
106+
return strlen(str);
107+
}
64108

65-
va_dcl
66-
#endif
109+
/*
110+
* dopr(): poor man's version of doprintf
111+
*/
112+
113+
static void fmtstr __P((char *value, int ljust, int len, int zpad, int maxwidth));
114+
static void fmtnum __P((long value, int base, int dosign, int ljust, int len, int zpad));
115+
static void dostr __P(( char * , int ));
116+
static char *output;
117+
static void dopr_outch __P(( int c ));
118+
119+
static void
120+
dopr( buffer, format, args )
121+
char *buffer;
122+
const char *format;
123+
va_list args;
67124
{
68-
size_t on;
69-
int ret;
70-
va_list ap;
71-
FILE f;
72-
73-
on = n;
74-
if (n != 0)
75-
n--;
76-
if (n > INT_MAX)
77-
n = INT_MAX;
78-
#if __STDC__
79-
va_start(ap, fmt);
80-
#else
81-
va_start(ap);
125+
int ch;
126+
long value;
127+
int longflag = 0;
128+
int pointflag = 0;
129+
int maxwidth = 0;
130+
char *strvalue;
131+
int ljust;
132+
int len;
133+
int zpad;
134+
135+
output = buffer;
136+
while( (ch = *format++) ){
137+
switch( ch ){
138+
case '%':
139+
ljust = len = zpad = maxwidth = 0;
140+
longflag = pointflag = 0;
141+
nextch:
142+
ch = *format++;
143+
switch( ch ){
144+
case 0:
145+
dostr( "**end of format**" , 0);
146+
return;
147+
case '-': ljust = 1; goto nextch;
148+
case '0': /* set zero padding if len not set */
149+
if(len==0 && !pointflag) zpad = '0';
150+
case '1': case '2': case '3':
151+
case '4': case '5': case '6':
152+
case '7': case '8': case '9':
153+
if (pointflag)
154+
maxwidth = maxwidth*10 + ch - '0';
155+
else
156+
len = len*10 + ch - '0';
157+
goto nextch;
158+
case '*':
159+
if (pointflag)
160+
maxwidth = va_arg( args, int );
161+
else
162+
len = va_arg( args, int );
163+
goto nextch;
164+
case '.': pointflag = 1; goto nextch;
165+
case 'l': longflag = 1; goto nextch;
166+
case 'u': case 'U':
167+
/*fmtnum(value,base,dosign,ljust,len,zpad) */
168+
if( longflag ){
169+
value = va_arg( args, long );
170+
} else {
171+
value = va_arg( args, int );
172+
}
173+
fmtnum( value, 10,0, ljust, len, zpad ); break;
174+
case 'o': case 'O':
175+
/*fmtnum(value,base,dosign,ljust,len,zpad) */
176+
if( longflag ){
177+
value = va_arg( args, long );
178+
} else {
179+
value = va_arg( args, int );
180+
}
181+
fmtnum( value, 8,0, ljust, len, zpad ); break;
182+
case 'd': case 'D':
183+
if( longflag ){
184+
value = va_arg( args, long );
185+
} else {
186+
value = va_arg( args, int );
187+
}
188+
fmtnum( value, 10,1, ljust, len, zpad ); break;
189+
case 'x':
190+
if( longflag ){
191+
value = va_arg( args, long );
192+
} else {
193+
value = va_arg( args, int );
194+
}
195+
fmtnum( value, 16,0, ljust, len, zpad ); break;
196+
case 'X':
197+
if( longflag ){
198+
value = va_arg( args, long );
199+
} else {
200+
value = va_arg( args, int );
201+
}
202+
fmtnum( value,-16,0, ljust, len, zpad ); break;
203+
case 's':
204+
strvalue = va_arg( args, char *);
205+
if (maxwidth > 0 || !pointflag) {
206+
if (pointflag && len > maxwidth)
207+
len = maxwidth; /* Adjust padding */
208+
fmtstr( strvalue,ljust,len,zpad, maxwidth);
209+
}
210+
break;
211+
case 'c':
212+
ch = va_arg( args, int );
213+
dopr_outch( ch ); break;
214+
case '%': dopr_outch( ch ); continue;
215+
default:
216+
dostr( "???????" , 0);
217+
}
218+
break;
219+
default:
220+
dopr_outch( ch );
221+
break;
222+
}
223+
}
224+
*output = 0;
225+
}
226+
227+
static void
228+
fmtstr( value, ljust, len, zpad, maxwidth )
229+
char *value;
230+
int ljust, len, zpad, maxwidth;
231+
{
232+
int padlen, strlen; /* amount to pad */
233+
234+
if( value == 0 ){
235+
value = "<NULL>";
236+
}
237+
for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
238+
if (strlen > maxwidth && maxwidth)
239+
strlen = maxwidth;
240+
padlen = len - strlen;
241+
if( padlen < 0 ) padlen = 0;
242+
if( ljust ) padlen = -padlen;
243+
while( padlen > 0 ) {
244+
dopr_outch( ' ' );
245+
--padlen;
246+
}
247+
dostr( value, maxwidth );
248+
while( padlen < 0 ) {
249+
dopr_outch( ' ' );
250+
++padlen;
251+
}
252+
}
253+
254+
static void
255+
fmtnum( value, base, dosign, ljust, len, zpad )
256+
long value;
257+
int base, dosign, ljust, len, zpad;
258+
{
259+
int signvalue = 0;
260+
unsigned long uvalue;
261+
char convert[20];
262+
int place = 0;
263+
int padlen = 0; /* amount to pad */
264+
int caps = 0;
265+
266+
/* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
267+
value, base, dosign, ljust, len, zpad )); */
268+
uvalue = value;
269+
if( dosign ){
270+
if( value < 0 ) {
271+
signvalue = '-';
272+
uvalue = -value;
273+
}
274+
}
275+
if( base < 0 ){
276+
caps = 1;
277+
base = -base;
278+
}
279+
do{
280+
convert[place++] =
281+
(caps? "0123456789ABCDEF":"0123456789abcdef")
282+
[uvalue % (unsigned)base ];
283+
uvalue = (uvalue / (unsigned)base );
284+
}while(uvalue);
285+
convert[place] = 0;
286+
padlen = len - place;
287+
if( padlen < 0 ) padlen = 0;
288+
if( ljust ) padlen = -padlen;
289+
/* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
290+
convert,place,signvalue,padlen)); */
291+
if( zpad && padlen > 0 ){
292+
if( signvalue ){
293+
dopr_outch( signvalue );
294+
--padlen;
295+
signvalue = 0;
296+
}
297+
while( padlen > 0 ){
298+
dopr_outch( zpad );
299+
--padlen;
300+
}
301+
}
302+
while( padlen > 0 ) {
303+
dopr_outch( ' ' );
304+
--padlen;
305+
}
306+
if( signvalue ) dopr_outch( signvalue );
307+
while( place > 0 ) dopr_outch( convert[--place] );
308+
while( padlen < 0 ){
309+
dopr_outch( ' ' );
310+
++padlen;
311+
}
312+
}
313+
314+
static void
315+
dostr( str , cut)
316+
char *str;
317+
int cut;
318+
{
319+
if (cut) {
320+
while(*str && cut-- > 0) dopr_outch(*str++);
321+
} else {
322+
while(*str) dopr_outch(*str++);
323+
}
324+
}
325+
326+
static void
327+
dopr_outch( c )
328+
int c;
329+
{
330+
#if 0
331+
if( iscntrl(c) && c != '\n' && c != '\t' ){
332+
c = '@' + (c & 0x1F);
333+
if( end == 0 || output < end )
334+
*output++ = '^';
335+
}
82336
#endif
83-
f._file = -1;
84-
f._flags = __SWR | __SSTR;
85-
f._bf._base = f._p = (unsigned char *) str;
86-
f._bf._size = f._w = n;
87-
ret = vfprintf(&f, fmt, ap);
88-
if (on > 0)
89-
*f._p = '\0';
90-
va_end(ap);
91-
return ret;
337+
if( end == 0 || output < end )
338+
*output++ = c;
339+
else
340+
SnprfOverflow++;
92341
}
342+
343+
# endif /* !luna2 */

0 commit comments

Comments
 (0)