-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathircsprintf.c
272 lines (265 loc) · 5.28 KB
/
ircsprintf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "ircsprintf.h"
char num[24] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
char itoa_tab[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char xtoa_tab[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
char nullstring[]="(null)";
#ifdef WANT_SNPRINTF
inline int irc_printf(char *str, size_t size, const char *pattern, va_list vl)
#else
inline int irc_printf(char *str, const char *pattern, va_list vl)
#endif
{
char *s;
char *buf=str;
const char *format=pattern;
unsigned long i, u;
int len=0;
va_list ap;
VA_COPY(ap, vl);
#ifdef WANT_SNPRINTF
if(!size)
{
#endif
while(*format)
{
u = 0;
switch(*format)
{
case '%':
format++;
switch(*format)
{
case 's': /* most popular ;) */
s=va_arg(ap, char *);
while(*s)
buf[len++]=*s++;
format++;
break;
case 'u':
format--; /* falls through and is caught below */
case 'l':
if (*(format+1) == 'u')
{
u=1;
format++;
}
else if (*(format+1) == 'd')
{
u=0;
format++;
}
else
u=0;
i=va_arg(ap, unsigned long);
if(!u)
if(i&0x80000000)
{
buf[len++]='-'; /* it's negative.. */
i = 0x80000000 - (i & ~0x80000000);
}
s=&num[23];
do
{
*--s=itoa_tab[i%10];
i/=10;
} while(i!=0);
while(*s)
buf[len++]=*s++;
format++;
break;
case 'd':
case 'i':
i=va_arg(ap, unsigned int);
if(!u)
if(i&0x80000000)
{
buf[len++]='-'; /* it's negative.. */
i = 0x80000000 - (i & ~0x80000000);
}
s=&num[11];
do
{
*--s=itoa_tab[i%10];
i/=10;
} while(i!=0);
while(*s)
buf[len++]=*s++;
format++;
break;
case 'n':
/* oo, sneaky...it really is just a long, though! */
case 'x':
case 'X':
i=va_arg(ap, long);
buf[len++]='0';
buf[len++]='x';
s=&num[11];
do
{
*--s=xtoa_tab[i%16];
i/=16;
} while(i!=0);
while(*s)
buf[len++]=*s++;
format++;
break;
case 'c':
buf[len++]= (char) va_arg(ap, int);
format++;
break;
default:
/* yick, unknown type...default to returning what our
s[n]printf friend would */
return vsprintf(str, pattern, vl);
break;
}
break;
default:
buf[len++]=*format++;
break;
}
}
buf[len]=0;
va_end(ap);
return len;
#ifdef WANT_SNPRINTF
}
else
{
while(*format && len<size)
{
u = 0;
switch(*format)
{
case '%':
format++;
switch(*format)
{
case 's': /* most popular ;) */
s=va_arg(ap, char *);
if(s==NULL)
s=nullstring;
while(*s && len<size)
buf[len++]=*s++;
format++;
break;
case 'u':
format--; /* now fall through and it's caught, cool */
case 'l':
if (*(format+1) == 'u')
{
u=1;
format++;
}
else if (*(format+1) == 'd')
{
u=0;
format++;
}
else
u=0;
/* fallthrough */
case 'd':
case 'i':
i=va_arg(ap, unsigned long);
if(!u)
if(i&0x80000000)
{
buf[len++]='-'; /* it's negative.. */
i = 0x80000000 - (i & ~0x80000000);
}
s=&num[11];
do
{
*--s=itoa_tab[i%10];
i/=10;
} while(i!=0);
while(*s && len<size)
buf[len++]=*s++;
format++;
break;
case 'n':
/* oo, sneaky...it really is just a long, though! */
case 'x':
case 'X':
i=va_arg(ap, long);
buf[len++]='0';
if(len<size)
buf[len++]='x';
else
break;
s=&num[11];
do
{
*--s=xtoa_tab[i%16];
i/=16;
} while(i!=0);
while(*s && len<size)
buf[len++]=*s++;
format++;
break;
case 'c':
buf[len++]= (char) va_arg(ap, int);
format++;
break;
default:
/* yick, unknown type...default to returning what our
s[n]printf friend would */
return vsnprintf(str, size, pattern, vl);
break;
}
break;
default:
buf[len++]=*format++;
break;
}
}
buf[len]=0;
va_end(ap);
return len;
}
#endif /* WANT_SNPRINTF */
}
int ircsprintf(char *str, const char *format, ...)
{
int ret;
va_list vl;
va_start(vl, format);
#ifdef WANT_SNPRINTF
ret=irc_printf(str, 0, format, vl);
#else
ret=irc_printf(str, format, vl);
#endif
va_end(vl);
return ret;
}
#ifdef WANT_SNPRINTF
int ircsnprintf(char *str, size_t size, const char *format, ...)
{
int ret;
va_list vl;
va_start(vl, format);
ret=irc_printf(str, size, format, vl);
va_end(vl);
return ret;
}
#endif
int ircvsprintf(char *str, const char *format, va_list ap)
{
int ret;
#ifdef WANT_SNPRINTF
ret=irc_printf(str, 0, format, ap);
#else
ret=irc_printf(str, format, ap);
#endif
return ret;
}
#ifdef WANT_SNPRINTF
int ircvsnprintf(char *str, size_t size, const char *format, va_list ap)
{
int ret;
ret=irc_printf(str, size, format, ap);
return ret;
}
#endif