1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* snprintf.c: Functions for printing to a length-limited string. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
/************************************************************** |
26 |
* Original header, taken from mutt...: |
27 |
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995 |
28 |
* A bombproof version of doprnt (dopr) included. |
29 |
* Sigh. This sort of thing is always nasty do deal with. Note that |
30 |
* the version here does not include floating point... |
31 |
* |
32 |
* snprintf() is used instead of sprintf() as it does limit checks |
33 |
* for string length. This covers a nasty loophole. |
34 |
* |
35 |
* The other functions are there to prevent NULL pointers from |
36 |
* causing nast effects. |
37 |
* |
38 |
* More Recently: |
39 |
* Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 |
40 |
* This was ugly. It is still ugly. I opted out of floating point |
41 |
* numbers, but the formatter understands just about everything |
42 |
* from the normal C string format, at least as far as I can tell from |
43 |
* the Solaris 2.5 printf(3S) man page. |
44 |
* |
45 |
* Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 |
46 |
* Ok, added some minimal floating point support, which means this |
47 |
* probably requires libm on most operating systems. Don't yet |
48 |
* support the exponent (e,E) and sigfig (g,G). Also, fmtint() |
49 |
* was pretty badly broken, it just wasn't being exercised in ways |
50 |
* which showed it, so that's been fixed. Also, formated the code |
51 |
* to mutt conventions, and removed dead code left over from the |
52 |
* original. Also, there is now a builtin-test, just compile with: |
53 |
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm |
54 |
* and run snprintf for results. |
55 |
* |
56 |
* Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i |
57 |
* The PGP code was using unsigned hexadecimal formats. |
58 |
* Unfortunately, unsigned formats simply didn't work. |
59 |
* |
60 |
* Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 |
61 |
* The original code assumed that both snprintf() and vsnprintf() were |
62 |
* missing. Some systems only have snprintf() but not vsnprintf(), so |
63 |
* the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. |
64 |
* |
65 |
**************************************************************/ |
66 |
|
67 |
#include "stdinc.h" |
68 |
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
69 |
|
70 |
|
71 |
#include "irc_string.h" |
72 |
|
73 |
/* varargs declarations: */ |
74 |
|
75 |
# include <stdarg.h> |
76 |
# define HAVE_STDARGS /* let's hope that works everywhere (mj) */ |
77 |
# define VA_LOCAL_DECL va_list ap |
78 |
# define VA_START(f) va_start(ap, f) |
79 |
# define VA_SHIFT(v,t) ; /* no-op for ANSI */ |
80 |
# define VA_END va_end(ap) |
81 |
|
82 |
/*int snprintf (char *str, size_t count, const char *fmt, ...);*/ |
83 |
/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/ |
84 |
|
85 |
static void dopr (char *buffer, size_t maxlen, const char *format, |
86 |
va_list args); |
87 |
static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
88 |
char *value, int flags, int min, int max); |
89 |
static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
90 |
long value, int base, int min, int max, int flags); |
91 |
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
92 |
long double fvalue, int min, int max, int flags); |
93 |
static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); |
94 |
|
95 |
/* |
96 |
* dopr(): poor man's version of doprintf |
97 |
*/ |
98 |
|
99 |
/* format read states */ |
100 |
#define DP_S_DEFAULT 0 |
101 |
#define DP_S_FLAGS 1 |
102 |
#define DP_S_MIN 2 |
103 |
#define DP_S_DOT 3 |
104 |
#define DP_S_MAX 4 |
105 |
#define DP_S_MOD 5 |
106 |
#define DP_S_CONV 6 |
107 |
#define DP_S_DONE 7 |
108 |
|
109 |
/* format flags - Bits */ |
110 |
#define DP_F_MINUS (1 << 0) |
111 |
#define DP_F_PLUS (1 << 1) |
112 |
#define DP_F_SPACE (1 << 2) |
113 |
#define DP_F_NUM (1 << 3) |
114 |
#define DP_F_ZERO (1 << 4) |
115 |
#define DP_F_UP (1 << 5) |
116 |
#define DP_F_UNSIGNED (1 << 6) |
117 |
|
118 |
/* Conversion Flags */ |
119 |
#define DP_C_SHORT 1 |
120 |
#define DP_C_LONG 2 |
121 |
#define DP_C_LDOUBLE 3 |
122 |
|
123 |
#define char_to_int(p) (p - '0') |
124 |
#undef MAX |
125 |
#define MAX(p,q) ((p >= q) ? p : q) |
126 |
|
127 |
static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) |
128 |
{ |
129 |
char ch; |
130 |
long value; |
131 |
long double fvalue; |
132 |
char *strvalue; |
133 |
int min; |
134 |
int max; |
135 |
int state; |
136 |
int flags; |
137 |
int cflags; |
138 |
size_t currlen; |
139 |
|
140 |
state = DP_S_DEFAULT; |
141 |
currlen = flags = cflags = min = 0; |
142 |
max = -1; |
143 |
ch = *format++; |
144 |
|
145 |
while (state != DP_S_DONE) |
146 |
{ |
147 |
if ((ch == '\0') || (currlen >= maxlen)) |
148 |
state = DP_S_DONE; |
149 |
|
150 |
switch(state) |
151 |
{ |
152 |
case DP_S_DEFAULT: |
153 |
if (ch == '%') |
154 |
state = DP_S_FLAGS; |
155 |
else |
156 |
dopr_outch (buffer, &currlen, maxlen, ch); |
157 |
ch = *format++; |
158 |
break; |
159 |
case DP_S_FLAGS: |
160 |
switch (ch) |
161 |
{ |
162 |
case '-': |
163 |
flags |= DP_F_MINUS; |
164 |
ch = *format++; |
165 |
break; |
166 |
case '+': |
167 |
flags |= DP_F_PLUS; |
168 |
ch = *format++; |
169 |
break; |
170 |
case ' ': |
171 |
flags |= DP_F_SPACE; |
172 |
ch = *format++; |
173 |
break; |
174 |
case '#': |
175 |
flags |= DP_F_NUM; |
176 |
ch = *format++; |
177 |
break; |
178 |
case '0': |
179 |
flags |= DP_F_ZERO; |
180 |
ch = *format++; |
181 |
break; |
182 |
default: |
183 |
state = DP_S_MIN; |
184 |
break; |
185 |
} |
186 |
break; |
187 |
case DP_S_MIN: |
188 |
if (isdigit((unsigned char)ch)) |
189 |
{ |
190 |
min = 10*min + char_to_int (ch); |
191 |
ch = *format++; |
192 |
} |
193 |
else if (ch == '*') |
194 |
{ |
195 |
min = va_arg (args, int); |
196 |
ch = *format++; |
197 |
state = DP_S_DOT; |
198 |
} |
199 |
else |
200 |
state = DP_S_DOT; |
201 |
break; |
202 |
case DP_S_DOT: |
203 |
if (ch == '.') |
204 |
{ |
205 |
state = DP_S_MAX; |
206 |
ch = *format++; |
207 |
} |
208 |
else |
209 |
state = DP_S_MOD; |
210 |
break; |
211 |
case DP_S_MAX: |
212 |
if (isdigit((unsigned char)ch)) |
213 |
{ |
214 |
if (max < 0) |
215 |
max = 0; |
216 |
max = 10*max + char_to_int (ch); |
217 |
ch = *format++; |
218 |
} |
219 |
else if (ch == '*') |
220 |
{ |
221 |
max = va_arg (args, int); |
222 |
ch = *format++; |
223 |
state = DP_S_MOD; |
224 |
} |
225 |
else |
226 |
state = DP_S_MOD; |
227 |
break; |
228 |
case DP_S_MOD: |
229 |
/* Currently, we don't support Long Long, bummer */ |
230 |
switch (ch) |
231 |
{ |
232 |
case 'h': |
233 |
cflags = DP_C_SHORT; |
234 |
ch = *format++; |
235 |
break; |
236 |
case 'l': |
237 |
cflags = DP_C_LONG; |
238 |
ch = *format++; |
239 |
break; |
240 |
case 'L': |
241 |
cflags = DP_C_LDOUBLE; |
242 |
ch = *format++; |
243 |
break; |
244 |
default: |
245 |
break; |
246 |
} |
247 |
state = DP_S_CONV; |
248 |
break; |
249 |
case DP_S_CONV: |
250 |
switch (ch) |
251 |
{ |
252 |
case 'd': |
253 |
case 'i': |
254 |
if (cflags == DP_C_SHORT) |
255 |
value = va_arg (args, short int); |
256 |
else if (cflags == DP_C_LONG) |
257 |
value = va_arg (args, long int); |
258 |
else |
259 |
value = va_arg (args, int); |
260 |
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
261 |
break; |
262 |
case 'o': |
263 |
flags |= DP_F_UNSIGNED; |
264 |
if (cflags == DP_C_SHORT) |
265 |
value = va_arg (args, unsigned short int); |
266 |
else if (cflags == DP_C_LONG) |
267 |
value = va_arg (args, unsigned long int); |
268 |
else |
269 |
value = va_arg (args, unsigned int); |
270 |
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
271 |
break; |
272 |
case 'u': |
273 |
flags |= DP_F_UNSIGNED; |
274 |
if (cflags == DP_C_SHORT) |
275 |
value = va_arg (args, unsigned short int); |
276 |
else if (cflags == DP_C_LONG) |
277 |
value = va_arg (args, unsigned long int); |
278 |
else |
279 |
value = va_arg (args, unsigned int); |
280 |
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
281 |
break; |
282 |
case 'X': |
283 |
flags |= DP_F_UP; |
284 |
case 'x': |
285 |
flags |= DP_F_UNSIGNED; |
286 |
if (cflags == DP_C_SHORT) |
287 |
value = va_arg (args, unsigned short int); |
288 |
else if (cflags == DP_C_LONG) |
289 |
value = va_arg (args, unsigned long int); |
290 |
else |
291 |
value = va_arg (args, unsigned int); |
292 |
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
293 |
break; |
294 |
case 'f': |
295 |
if (cflags == DP_C_LDOUBLE) |
296 |
fvalue = va_arg (args, long double); |
297 |
else |
298 |
fvalue = va_arg (args, double); |
299 |
/* um, floating point? */ |
300 |
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
301 |
break; |
302 |
case 'E': |
303 |
flags |= DP_F_UP; |
304 |
case 'e': |
305 |
if (cflags == DP_C_LDOUBLE) |
306 |
fvalue = va_arg (args, long double); |
307 |
else |
308 |
fvalue = va_arg (args, double); |
309 |
break; |
310 |
case 'G': |
311 |
flags |= DP_F_UP; |
312 |
case 'g': |
313 |
if (cflags == DP_C_LDOUBLE) |
314 |
fvalue = va_arg (args, long double); |
315 |
else |
316 |
fvalue = va_arg (args, double); |
317 |
break; |
318 |
case 'c': |
319 |
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
320 |
break; |
321 |
case 's': |
322 |
strvalue = va_arg (args, char *); |
323 |
if (max < 0) |
324 |
max = maxlen; /* ie, no max */ |
325 |
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
326 |
break; |
327 |
case 'p': |
328 |
strvalue = va_arg (args, void *); |
329 |
fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
330 |
break; |
331 |
case 'n': |
332 |
if (cflags == DP_C_SHORT) |
333 |
{ |
334 |
short int *num; |
335 |
num = va_arg (args, short int *); |
336 |
*num = currlen; |
337 |
} |
338 |
else if (cflags == DP_C_LONG) |
339 |
{ |
340 |
long int *num; |
341 |
num = va_arg (args, long int *); |
342 |
*num = currlen; |
343 |
} |
344 |
else |
345 |
{ |
346 |
int *num; |
347 |
num = va_arg (args, int *); |
348 |
*num = currlen; |
349 |
} |
350 |
break; |
351 |
case '%': |
352 |
dopr_outch (buffer, &currlen, maxlen, ch); |
353 |
break; |
354 |
case 'w': |
355 |
/* not supported yet, treat as next char */ |
356 |
ch = *format++; |
357 |
break; |
358 |
default: |
359 |
/* Unknown, skip */ |
360 |
break; |
361 |
} |
362 |
ch = *format++; |
363 |
state = DP_S_DEFAULT; |
364 |
flags = cflags = min = 0; |
365 |
max = -1; |
366 |
break; |
367 |
case DP_S_DONE: |
368 |
break; |
369 |
default: |
370 |
/* hmm? */ |
371 |
break; /* some picky compilers need this */ |
372 |
} |
373 |
} |
374 |
if (currlen < maxlen - 1) |
375 |
buffer[currlen] = '\0'; |
376 |
else |
377 |
buffer[maxlen - 1] = '\0'; |
378 |
} |
379 |
|
380 |
static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
381 |
char *value, int flags, int min, int max) |
382 |
{ |
383 |
int padlen, strln; /* amount to pad */ |
384 |
int cnt = 0; |
385 |
|
386 |
if (value == 0) |
387 |
{ |
388 |
value = "<NULL>"; |
389 |
} |
390 |
|
391 |
for (strln = 0; value[strln]; ++strln); /* strlen */ |
392 |
padlen = min - strln; |
393 |
if (padlen < 0) |
394 |
padlen = 0; |
395 |
if (flags & DP_F_MINUS) |
396 |
padlen = -padlen; /* Left Justify */ |
397 |
|
398 |
while ((padlen > 0) && (cnt < max)) |
399 |
{ |
400 |
dopr_outch (buffer, currlen, maxlen, ' '); |
401 |
--padlen; |
402 |
++cnt; |
403 |
} |
404 |
while (*value && (cnt < max)) |
405 |
{ |
406 |
dopr_outch (buffer, currlen, maxlen, *value++); |
407 |
++cnt; |
408 |
} |
409 |
while ((padlen < 0) && (cnt < max)) |
410 |
{ |
411 |
dopr_outch (buffer, currlen, maxlen, ' '); |
412 |
++padlen; |
413 |
++cnt; |
414 |
} |
415 |
} |
416 |
|
417 |
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
418 |
|
419 |
static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
420 |
long value, int base, int min, int max, int flags) |
421 |
{ |
422 |
int signvalue = 0; |
423 |
unsigned long uvalue; |
424 |
char convert[20]; |
425 |
int place = 0; |
426 |
int spadlen = 0; /* amount to space pad */ |
427 |
int zpadlen = 0; /* amount to zero pad */ |
428 |
int caps = 0; |
429 |
|
430 |
if (max < 0) |
431 |
max = 0; |
432 |
|
433 |
uvalue = value; |
434 |
|
435 |
if(!(flags & DP_F_UNSIGNED)) |
436 |
{ |
437 |
if( value < 0 ) { |
438 |
signvalue = '-'; |
439 |
uvalue = -value; |
440 |
} |
441 |
else |
442 |
if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
443 |
signvalue = '+'; |
444 |
else |
445 |
if (flags & DP_F_SPACE) |
446 |
signvalue = ' '; |
447 |
} |
448 |
|
449 |
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
450 |
|
451 |
do { |
452 |
convert[place++] = |
453 |
(caps? "0123456789ABCDEF":"0123456789abcdef") |
454 |
[uvalue % (unsigned)base ]; |
455 |
uvalue = (uvalue / (unsigned)base ); |
456 |
} while(uvalue && (place < 20)); |
457 |
if (place == 20) place--; |
458 |
convert[place] = 0; |
459 |
|
460 |
zpadlen = max - place; |
461 |
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
462 |
if (zpadlen < 0) zpadlen = 0; |
463 |
if (spadlen < 0) spadlen = 0; |
464 |
if (flags & DP_F_ZERO) |
465 |
{ |
466 |
zpadlen = MAX(zpadlen, spadlen); |
467 |
spadlen = 0; |
468 |
} |
469 |
if (flags & DP_F_MINUS) |
470 |
spadlen = -spadlen; /* Left Justifty */ |
471 |
|
472 |
#ifdef DEBUG_SNPRINTF |
473 |
dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
474 |
zpadlen, spadlen, min, max, place)); |
475 |
#endif |
476 |
|
477 |
/* Spaces */ |
478 |
while (spadlen > 0) |
479 |
{ |
480 |
dopr_outch (buffer, currlen, maxlen, ' '); |
481 |
--spadlen; |
482 |
} |
483 |
|
484 |
/* Sign */ |
485 |
if (signvalue) |
486 |
dopr_outch (buffer, currlen, maxlen, signvalue); |
487 |
|
488 |
/* Zeros */ |
489 |
if (zpadlen > 0) |
490 |
{ |
491 |
while (zpadlen > 0) |
492 |
{ |
493 |
dopr_outch (buffer, currlen, maxlen, '0'); |
494 |
--zpadlen; |
495 |
} |
496 |
} |
497 |
|
498 |
/* Digits */ |
499 |
while (place > 0) |
500 |
dopr_outch (buffer, currlen, maxlen, convert[--place]); |
501 |
|
502 |
/* Left Justified spaces */ |
503 |
while (spadlen < 0) { |
504 |
dopr_outch (buffer, currlen, maxlen, ' '); |
505 |
++spadlen; |
506 |
} |
507 |
} |
508 |
|
509 |
static long double abs_val (long double value) |
510 |
{ |
511 |
long double result = value; |
512 |
|
513 |
if (value < 0) |
514 |
result = -value; |
515 |
|
516 |
return result; |
517 |
} |
518 |
|
519 |
static long double pow10 (int exp) |
520 |
{ |
521 |
long double result = 1; |
522 |
|
523 |
while (exp) |
524 |
{ |
525 |
result *= 10; |
526 |
exp--; |
527 |
} |
528 |
|
529 |
return result; |
530 |
} |
531 |
|
532 |
static long round (long double value) |
533 |
{ |
534 |
long intpart; |
535 |
|
536 |
intpart = value; |
537 |
value = value - intpart; |
538 |
if (value >= 0.5) |
539 |
intpart++; |
540 |
|
541 |
return intpart; |
542 |
} |
543 |
|
544 |
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
545 |
long double fvalue, int min, int max, int flags) |
546 |
{ |
547 |
int signvalue = 0; |
548 |
long double ufvalue; |
549 |
char iconvert[20]; |
550 |
char fconvert[20]; |
551 |
int iplace = 0; |
552 |
int fplace = 0; |
553 |
int padlen = 0; /* amount to pad */ |
554 |
int zpadlen = 0; |
555 |
int caps = 0; |
556 |
long intpart; |
557 |
long fracpart; |
558 |
|
559 |
/* |
560 |
* AIX manpage says the default is 0, but Solaris says the default |
561 |
* is 6, and sprintf on AIX defaults to 6 |
562 |
*/ |
563 |
if (max < 0) |
564 |
max = 6; |
565 |
|
566 |
ufvalue = abs_val (fvalue); |
567 |
|
568 |
if (fvalue < 0) |
569 |
signvalue = '-'; |
570 |
else |
571 |
if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
572 |
signvalue = '+'; |
573 |
else |
574 |
if (flags & DP_F_SPACE) |
575 |
signvalue = ' '; |
576 |
|
577 |
intpart = ufvalue; |
578 |
|
579 |
/* |
580 |
* Sorry, we only support 9 digits past the decimal because of our |
581 |
* conversion method |
582 |
*/ |
583 |
if (max > 9) |
584 |
max = 9; |
585 |
|
586 |
/* We "cheat" by converting the fractional part to integer by |
587 |
* multiplying by a factor of 10 |
588 |
*/ |
589 |
fracpart = round ((pow10 (max)) * (ufvalue - intpart)); |
590 |
|
591 |
if (fracpart >= pow10 (max)) |
592 |
{ |
593 |
intpart++; |
594 |
fracpart -= pow10 (max); |
595 |
} |
596 |
|
597 |
#ifdef DEBUG_SNPRINTF |
598 |
dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart)); |
599 |
#endif |
600 |
|
601 |
/* Convert integer part */ |
602 |
do { |
603 |
iconvert[iplace++] = |
604 |
(caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; |
605 |
intpart = (intpart / 10); |
606 |
} while(intpart && (iplace < 20)); |
607 |
if (iplace == 20) iplace--; |
608 |
iconvert[iplace] = 0; |
609 |
|
610 |
/* Convert fractional part */ |
611 |
do { |
612 |
fconvert[fplace++] = |
613 |
(caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; |
614 |
fracpart = (fracpart / 10); |
615 |
} while(fracpart && (fplace < 20)); |
616 |
if (fplace == 20) fplace--; |
617 |
fconvert[fplace] = 0; |
618 |
|
619 |
/* -1 for decimal point, another -1 if we are printing a sign */ |
620 |
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
621 |
zpadlen = max - fplace; |
622 |
if (zpadlen < 0) |
623 |
zpadlen = 0; |
624 |
if (padlen < 0) |
625 |
padlen = 0; |
626 |
if (flags & DP_F_MINUS) |
627 |
padlen = -padlen; /* Left Justifty */ |
628 |
|
629 |
if ((flags & DP_F_ZERO) && (padlen > 0)) |
630 |
{ |
631 |
if (signvalue) |
632 |
{ |
633 |
dopr_outch (buffer, currlen, maxlen, signvalue); |
634 |
--padlen; |
635 |
signvalue = 0; |
636 |
} |
637 |
while (padlen > 0) |
638 |
{ |
639 |
dopr_outch (buffer, currlen, maxlen, '0'); |
640 |
--padlen; |
641 |
} |
642 |
} |
643 |
while (padlen > 0) |
644 |
{ |
645 |
dopr_outch (buffer, currlen, maxlen, ' '); |
646 |
--padlen; |
647 |
} |
648 |
if (signvalue) |
649 |
dopr_outch (buffer, currlen, maxlen, signvalue); |
650 |
|
651 |
while (iplace > 0) |
652 |
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
653 |
|
654 |
/* |
655 |
* Decimal point. This should probably use locale to find the correct |
656 |
* char to print out. |
657 |
*/ |
658 |
dopr_outch (buffer, currlen, maxlen, '.'); |
659 |
|
660 |
while (fplace > 0) |
661 |
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
662 |
|
663 |
while (zpadlen > 0) |
664 |
{ |
665 |
dopr_outch (buffer, currlen, maxlen, '0'); |
666 |
--zpadlen; |
667 |
} |
668 |
|
669 |
while (padlen < 0) |
670 |
{ |
671 |
dopr_outch (buffer, currlen, maxlen, ' '); |
672 |
++padlen; |
673 |
} |
674 |
} |
675 |
|
676 |
static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) |
677 |
{ |
678 |
if (*currlen < maxlen) |
679 |
buffer[(*currlen)++] = c; |
680 |
} |
681 |
#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
682 |
|
683 |
#ifndef HAVE_VSNPRINTF |
684 |
int vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
685 |
{ |
686 |
str[0] = 0; |
687 |
dopr(str, count, fmt, args); |
688 |
return(strlen(str)); |
689 |
} |
690 |
#endif /* !HAVE_VSNPRINTF */ |
691 |
|
692 |
#ifndef HAVE_SNPRINTF |
693 |
/* VARARGS3 */ |
694 |
#ifdef HAVE_STDARGS |
695 |
int snprintf (char *str,size_t count,const char *fmt,...) |
696 |
#else |
697 |
int snprintf (va_alist) va_dcl |
698 |
#endif |
699 |
{ |
700 |
#ifndef HAVE_STDARGS |
701 |
char *str; |
702 |
size_t count; |
703 |
char *fmt; |
704 |
#endif |
705 |
VA_LOCAL_DECL; |
706 |
|
707 |
VA_START (fmt); |
708 |
VA_SHIFT (str, char *); |
709 |
VA_SHIFT (count, size_t ); |
710 |
VA_SHIFT (fmt, char *); |
711 |
(void) vsnprintf(str, count, fmt, ap); |
712 |
VA_END; |
713 |
return(strlen(str)); |
714 |
} |
715 |
|
716 |
#ifdef TEST_SNPRINTF |
717 |
#ifndef LONG_STRING |
718 |
#define LONG_STRING 1024 |
719 |
#endif |
720 |
int main (void) |
721 |
{ |
722 |
char buf1[LONG_STRING]; |
723 |
char buf2[LONG_STRING]; |
724 |
char *fp_fmt[] = { |
725 |
"%-1.5f", |
726 |
"%1.5f", |
727 |
"%123.9f", |
728 |
"%10.5f", |
729 |
"% 10.5f", |
730 |
"%+22.9f", |
731 |
"%+4.9f", |
732 |
"%01.3f", |
733 |
"%4f", |
734 |
"%3.1f", |
735 |
"%3.2f", |
736 |
NULL |
737 |
}; |
738 |
double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, |
739 |
0.9996, 1.996, 4.136, 0}; |
740 |
char *int_fmt[] = { |
741 |
"%-1.5d", |
742 |
"%1.5d", |
743 |
"%123.9d", |
744 |
"%5.5d", |
745 |
"%10.5d", |
746 |
"% 10.5d", |
747 |
"%+22.33d", |
748 |
"%01.3d", |
749 |
"%4d", |
750 |
NULL |
751 |
}; |
752 |
long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; |
753 |
int x, y; |
754 |
int fail = 0; |
755 |
int num = 0; |
756 |
|
757 |
printf ("Testing snprintf format codes against system sprintf...\n"); |
758 |
|
759 |
for (x = 0; fp_fmt[x] != NULL ; x++) |
760 |
for (y = 0; fp_nums[y] != 0 ; y++) |
761 |
{ |
762 |
snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); |
763 |
sprintf (buf2, fp_fmt[x], fp_nums[y]); |
764 |
if (strcmp (buf1, buf2)) |
765 |
{ |
766 |
printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */ |
767 |
fp_fmt[x], buf1, buf2); |
768 |
fail++; |
769 |
} |
770 |
num++; |
771 |
} |
772 |
|
773 |
for (x = 0; int_fmt[x] != NULL ; x++) |
774 |
for (y = 0; int_nums[y] != 0 ; y++) |
775 |
{ |
776 |
snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); |
777 |
sprintf (buf2, int_fmt[x], int_nums[y]); |
778 |
if (strcmp (buf1, buf2)) |
779 |
{ |
780 |
printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */ |
781 |
int_fmt[x], buf1, buf2); |
782 |
fail++; |
783 |
} |
784 |
num++; |
785 |
} |
786 |
printf ("%d tests failed out of %d.\n", fail, num); |
787 |
} |
788 |
#endif /* SNPRINTF_TEST */ |
789 |
|
790 |
#endif /* !HAVE_SNPRINTF */ |