Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitedd626c

Browse files
committed
Try this snprintf() implementation, used in sendmail...
1 parent370d6cd commitedd626c

File tree

1 file changed

+309
-58
lines changed

1 file changed

+309
-58
lines changed

‎src/backend/port/snprintf.c

Lines changed: 309 additions & 58 deletions
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-
#if0
39-
staticcharsccsid[]="@(#)snprintf.c8.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-
staticconstcharrcsid[]=
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+
staticvoiddopr();
64+
staticchar*end;
65+
staticintSnprfOverflow;
5466

55-
#if__STDC__
67+
/* VARARGS3 */
5668
int
57-
snprintf(char*str,size_tn,charconst*fmt,...)
58-
#else
69+
# ifdef__STDC__
70+
snprintf(char*str,size_tcount,constchar*fmt, ...)
71+
# else
72+
snprintf(str,count,fmt,va_alist)
73+
char*str;
74+
size_tcount;
75+
constchar*fmt;
76+
va_dcl
77+
#endif
78+
{
79+
intlen;
80+
VA_LOCAL_DECL
81+
82+
VA_START(fmt);
83+
len=vsnprintf(str,count,fmt,ap);
84+
VA_END;
85+
returnlen;
86+
}
87+
88+
89+
# ifndefluna2
5990
int
60-
snprintf(str,n,fmt,va_alist)
61-
char*str;
62-
size_tn;
63-
char*fmt;
91+
vsnprintf(str,count,fmt,args)
92+
char*str;
93+
size_tcount;
94+
constchar*fmt;
95+
va_listargs;
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+
returnstrlen(str);
107+
}
64108

65-
va_dcl
66-
#endif
109+
/*
110+
* dopr(): poor man's version of doprintf
111+
*/
112+
113+
staticvoidfmtstr__P((char*value,intljust,intlen,intzpad,intmaxwidth));
114+
staticvoidfmtnum__P((longvalue,intbase,intdosign,intljust,intlen,intzpad));
115+
staticvoiddostr__P((char* ,int ));
116+
staticchar*output;
117+
staticvoiddopr_outch__P((intc ));
118+
119+
staticvoid
120+
dopr(buffer,format,args )
121+
char*buffer;
122+
constchar*format;
123+
va_listargs;
67124
{
68-
size_ton;
69-
intret;
70-
va_listap;
71-
FILEf;
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+
intch;
126+
longvalue;
127+
intlongflag=0;
128+
intpointflag=0;
129+
intmaxwidth=0;
130+
char*strvalue;
131+
intljust;
132+
intlen;
133+
intzpad;
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+
case0:
145+
dostr("**end of format**" ,0);
146+
return;
147+
case'-':ljust=1; gotonextch;
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+
gotonextch;
158+
case'*':
159+
if (pointflag)
160+
maxwidth=va_arg(args,int );
161+
else
162+
len=va_arg(args,int );
163+
gotonextch;
164+
case'.':pointflag=1; gotonextch;
165+
case'l':longflag=1; gotonextch;
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+
staticvoid
228+
fmtstr(value,ljust,len,zpad,maxwidth )
229+
char*value;
230+
intljust,len,zpad,maxwidth;
231+
{
232+
intpadlen,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+
staticvoid
255+
fmtnum(value,base,dosign,ljust,len,zpad )
256+
longvalue;
257+
intbase,dosign,ljust,len,zpad;
258+
{
259+
intsignvalue=0;
260+
unsigned longuvalue;
261+
charconvert[20];
262+
intplace=0;
263+
intpadlen=0;/* amount to pad */
264+
intcaps=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+
staticvoid
315+
dostr(str ,cut)
316+
char*str;
317+
intcut;
318+
{
319+
if (cut) {
320+
while(*str&&cut-->0)dopr_outch(*str++);
321+
}else {
322+
while(*str)dopr_outch(*str++);
323+
}
324+
}
325+
326+
staticvoid
327+
dopr_outch(c )
328+
intc;
329+
{
330+
#if0
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= (unsignedchar*)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-
returnret;
337+
if(end==0||output<end )
338+
*output++=c;
339+
else
340+
SnprfOverflow++;
92341
}
342+
343+
# endif/* !luna2 */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp