2 67956560 2002-05-19 alex * ngIRCd -- The Next Generation IRC Daemon
3 67956560 2002-05-19 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 67956560 2002-05-19 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 67956560 2002-05-19 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 67956560 2002-05-19 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 67956560 2002-05-19 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 67956560 2002-05-19 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 67956560 2002-05-19 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 ba258e65 2002-05-27 alex * $Id: vsnprintf.c,v 1.3 2002/05/27 13:01:04 alex Exp $
14 c5461c45 2002-05-19 alex * vsnprintf.c: u.a. Ersatz fuer vsnprintf()
18 67956560 2002-05-19 alex #include "portab.h"
19 67956560 2002-05-19 alex #include "imp.h"
21 67956560 2002-05-19 alex #include "exp.h"
25 67956560 2002-05-19 alex * snprintf.c: Copyright Patrick Powell 1995
26 67956560 2002-05-19 alex * This code is based on code written by Patrick Powell (papowell@astart.com)
27 67956560 2002-05-19 alex * It may be used for any purpose as long as this notice remains intact
28 67956560 2002-05-19 alex * on all source code distributions
30 67956560 2002-05-19 alex * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
31 67956560 2002-05-19 alex * A bombproof version of doprnt (dopr) included.
32 67956560 2002-05-19 alex * Sigh. This sort of thing is always nasty do deal with. Note that
33 67956560 2002-05-19 alex * the version here does not include floating point...
35 67956560 2002-05-19 alex * snprintf() is used instead of sprintf() as it does limit checks
36 67956560 2002-05-19 alex * for string length. This covers a nasty loophole.
38 67956560 2002-05-19 alex * The other functions are there to prevent NULL pointers from
39 67956560 2002-05-19 alex * causing nast effects.
41 67956560 2002-05-19 alex * More Recently:
42 67956560 2002-05-19 alex * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
43 67956560 2002-05-19 alex * This was ugly. It is still ugly. I opted out of floating point
44 67956560 2002-05-19 alex * numbers, but the formatter understands just about everything
45 67956560 2002-05-19 alex * from the normal C string format, at least as far as I can tell from
46 67956560 2002-05-19 alex * the Solaris 2.5 printf(3S) man page.
48 c5461c45 2002-05-19 alex * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
49 c5461c45 2002-05-19 alex * Ok, added some minimal floating point support, which means this
50 c5461c45 2002-05-19 alex * probably requires libm on most operating systems. Don't yet
51 c5461c45 2002-05-19 alex * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
52 c5461c45 2002-05-19 alex * was pretty badly broken, it just wasn't being exercised in ways
53 c5461c45 2002-05-19 alex * which showed it, so that's been fixed. Also, formated the code
54 c5461c45 2002-05-19 alex * to mutt conventions, and removed dead code left over from the
55 c5461c45 2002-05-19 alex * original. Also, there is now a builtin-test, just compile with:
56 c5461c45 2002-05-19 alex * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
57 c5461c45 2002-05-19 alex * and run snprintf for results.
59 c5461c45 2002-05-19 alex * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
60 c5461c45 2002-05-19 alex * The PGP code was using unsigned hexadecimal formats.
61 c5461c45 2002-05-19 alex * Unfortunately, unsigned formats simply didn't work.
63 c5461c45 2002-05-19 alex * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
64 c5461c45 2002-05-19 alex * The original code assumed that both snprintf() and vsnprintf() were
65 c5461c45 2002-05-19 alex * missing. Some systems only have snprintf() but not vsnprintf(), so
66 c5461c45 2002-05-19 alex * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
68 c5461c45 2002-05-19 alex * Andrew Tridgell <tridge@samba.org>, October 1998
69 c5461c45 2002-05-19 alex * fixed handling of %.0f
70 c5461c45 2002-05-19 alex * added test for HAVE_LONG_DOUBLE
72 67956560 2002-05-19 alex * tridge@samba.org, idra@samba.org, April 2001
73 c5461c45 2002-05-19 alex * got rid of fcvt code (twas buggy and made testing harder)
74 c5461c45 2002-05-19 alex * added C99 semantics
76 c5461c45 2002-05-19 alex * Alexander Barton, <alex@barton.de>, 2002-05-19
77 c5461c45 2002-05-19 alex * removed [v]asprintf() and C99 tests: not needed by ngIRCd.
81 67956560 2002-05-19 alex #ifdef HAVE_STRING_H
82 67956560 2002-05-19 alex #include <string.h>
84 67956560 2002-05-19 alex #ifdef HAVE_STRINGS_H
85 67956560 2002-05-19 alex #include <strings.h>
87 67956560 2002-05-19 alex #ifdef HAVE_CTYPE_H
88 67956560 2002-05-19 alex #include <ctype.h>
90 67956560 2002-05-19 alex #include <sys/types.h>
91 67956560 2002-05-19 alex #include <stdarg.h>
92 67956560 2002-05-19 alex #ifdef HAVE_STDLIB_H
93 67956560 2002-05-19 alex #include <stdlib.h>
97 c5461c45 2002-05-19 alex #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
98 67956560 2002-05-19 alex /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
99 67956560 2002-05-19 alex #include <stdio.h>
100 c5461c45 2002-05-19 alex /* make the compiler happy with an empty file */
101 ba258e65 2002-05-27 alex void dummy_snprintf PARAMS(( void )) { }
104 67956560 2002-05-19 alex #ifdef HAVE_LONG_DOUBLE
105 67956560 2002-05-19 alex #define LDOUBLE long double
107 67956560 2002-05-19 alex #define LDOUBLE double
110 67956560 2002-05-19 alex #ifdef HAVE_LONG_LONG
111 67956560 2002-05-19 alex #define LLONG long long
113 67956560 2002-05-19 alex #define LLONG long
116 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format,
117 67956560 2002-05-19 alex va_list args);
118 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
119 67956560 2002-05-19 alex char *value, int flags, int min, int max);
120 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
121 67956560 2002-05-19 alex long value, int base, int min, int max, int flags);
122 67956560 2002-05-19 alex static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
123 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags);
124 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
127 67956560 2002-05-19 alex * dopr(): poor man's version of doprintf
130 67956560 2002-05-19 alex /* format read states */
131 67956560 2002-05-19 alex #define DP_S_DEFAULT 0
132 67956560 2002-05-19 alex #define DP_S_FLAGS 1
133 67956560 2002-05-19 alex #define DP_S_MIN 2
134 67956560 2002-05-19 alex #define DP_S_DOT 3
135 67956560 2002-05-19 alex #define DP_S_MAX 4
136 67956560 2002-05-19 alex #define DP_S_MOD 5
137 67956560 2002-05-19 alex #define DP_S_CONV 6
138 67956560 2002-05-19 alex #define DP_S_DONE 7
140 67956560 2002-05-19 alex /* format flags - Bits */
141 67956560 2002-05-19 alex #define DP_F_MINUS (1 << 0)
142 67956560 2002-05-19 alex #define DP_F_PLUS (1 << 1)
143 67956560 2002-05-19 alex #define DP_F_SPACE (1 << 2)
144 67956560 2002-05-19 alex #define DP_F_NUM (1 << 3)
145 67956560 2002-05-19 alex #define DP_F_ZERO (1 << 4)
146 67956560 2002-05-19 alex #define DP_F_UP (1 << 5)
147 67956560 2002-05-19 alex #define DP_F_UNSIGNED (1 << 6)
149 67956560 2002-05-19 alex /* Conversion Flags */
150 67956560 2002-05-19 alex #define DP_C_SHORT 1
151 67956560 2002-05-19 alex #define DP_C_LONG 2
152 67956560 2002-05-19 alex #define DP_C_LDOUBLE 3
153 67956560 2002-05-19 alex #define DP_C_LLONG 4
155 67956560 2002-05-19 alex #define char_to_int(p) ((p)- '0')
156 67956560 2002-05-19 alex #ifndef MAX
157 67956560 2002-05-19 alex #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
160 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
163 67956560 2002-05-19 alex LLONG value;
164 67956560 2002-05-19 alex LDOUBLE fvalue;
165 67956560 2002-05-19 alex char *strvalue;
170 67956560 2002-05-19 alex int cflags;
171 67956560 2002-05-19 alex size_t currlen;
173 67956560 2002-05-19 alex state = DP_S_DEFAULT;
174 67956560 2002-05-19 alex currlen = flags = cflags = min = 0;
176 67956560 2002-05-19 alex ch = *format++;
178 67956560 2002-05-19 alex while (state != DP_S_DONE) {
179 67956560 2002-05-19 alex if (ch == '\0')
180 67956560 2002-05-19 alex state = DP_S_DONE;
182 67956560 2002-05-19 alex switch(state) {
183 67956560 2002-05-19 alex case DP_S_DEFAULT:
184 67956560 2002-05-19 alex if (ch == '%')
185 67956560 2002-05-19 alex state = DP_S_FLAGS;
187 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
188 67956560 2002-05-19 alex ch = *format++;
190 67956560 2002-05-19 alex case DP_S_FLAGS:
191 67956560 2002-05-19 alex switch (ch) {
193 67956560 2002-05-19 alex flags |= DP_F_MINUS;
194 67956560 2002-05-19 alex ch = *format++;
197 67956560 2002-05-19 alex flags |= DP_F_PLUS;
198 67956560 2002-05-19 alex ch = *format++;
201 67956560 2002-05-19 alex flags |= DP_F_SPACE;
202 67956560 2002-05-19 alex ch = *format++;
205 67956560 2002-05-19 alex flags |= DP_F_NUM;
206 67956560 2002-05-19 alex ch = *format++;
209 67956560 2002-05-19 alex flags |= DP_F_ZERO;
210 67956560 2002-05-19 alex ch = *format++;
213 67956560 2002-05-19 alex state = DP_S_MIN;
217 67956560 2002-05-19 alex case DP_S_MIN:
218 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
219 67956560 2002-05-19 alex min = 10*min + char_to_int (ch);
220 67956560 2002-05-19 alex ch = *format++;
221 67956560 2002-05-19 alex } else if (ch == '*') {
222 67956560 2002-05-19 alex min = va_arg (args, int);
223 67956560 2002-05-19 alex ch = *format++;
224 67956560 2002-05-19 alex state = DP_S_DOT;
226 67956560 2002-05-19 alex state = DP_S_DOT;
229 67956560 2002-05-19 alex case DP_S_DOT:
230 67956560 2002-05-19 alex if (ch == '.') {
231 67956560 2002-05-19 alex state = DP_S_MAX;
232 67956560 2002-05-19 alex ch = *format++;
234 67956560 2002-05-19 alex state = DP_S_MOD;
237 67956560 2002-05-19 alex case DP_S_MAX:
238 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
239 67956560 2002-05-19 alex if (max < 0)
241 67956560 2002-05-19 alex max = 10*max + char_to_int (ch);
242 67956560 2002-05-19 alex ch = *format++;
243 67956560 2002-05-19 alex } else if (ch == '*') {
244 67956560 2002-05-19 alex max = va_arg (args, int);
245 67956560 2002-05-19 alex ch = *format++;
246 67956560 2002-05-19 alex state = DP_S_MOD;
248 67956560 2002-05-19 alex state = DP_S_MOD;
251 67956560 2002-05-19 alex case DP_S_MOD:
252 67956560 2002-05-19 alex switch (ch) {
254 67956560 2002-05-19 alex cflags = DP_C_SHORT;
255 67956560 2002-05-19 alex ch = *format++;
258 67956560 2002-05-19 alex cflags = DP_C_LONG;
259 67956560 2002-05-19 alex ch = *format++;
260 67956560 2002-05-19 alex if (ch == 'l') { /* It's a long long */
261 67956560 2002-05-19 alex cflags = DP_C_LLONG;
262 67956560 2002-05-19 alex ch = *format++;
266 67956560 2002-05-19 alex cflags = DP_C_LDOUBLE;
267 67956560 2002-05-19 alex ch = *format++;
272 67956560 2002-05-19 alex state = DP_S_CONV;
274 67956560 2002-05-19 alex case DP_S_CONV:
275 67956560 2002-05-19 alex switch (ch) {
278 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
279 67956560 2002-05-19 alex value = va_arg (args, int);
280 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
281 67956560 2002-05-19 alex value = va_arg (args, long int);
282 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
283 67956560 2002-05-19 alex value = va_arg (args, LLONG);
285 67956560 2002-05-19 alex value = va_arg (args, int);
286 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
289 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
290 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
291 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
292 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
293 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
294 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
295 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned LLONG);
297 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
298 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
301 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
302 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
303 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
304 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
305 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
306 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
307 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
309 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
310 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
313 67956560 2002-05-19 alex flags |= DP_F_UP;
315 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
316 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
317 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
318 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
319 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
320 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
321 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
323 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
324 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
327 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
328 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
330 67956560 2002-05-19 alex fvalue = va_arg (args, double);
331 67956560 2002-05-19 alex /* um, floating point? */
332 67956560 2002-05-19 alex fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
335 67956560 2002-05-19 alex flags |= DP_F_UP;
337 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
338 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
340 67956560 2002-05-19 alex fvalue = va_arg (args, double);
343 67956560 2002-05-19 alex flags |= DP_F_UP;
345 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
346 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
348 67956560 2002-05-19 alex fvalue = va_arg (args, double);
351 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
354 67956560 2002-05-19 alex strvalue = va_arg (args, char *);
355 67956560 2002-05-19 alex if (max == -1) {
356 67956560 2002-05-19 alex max = strlen(strvalue);
358 67956560 2002-05-19 alex if (min > 0 && max >= 0 && min > max) max = min;
359 67956560 2002-05-19 alex fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
362 67956560 2002-05-19 alex strvalue = va_arg (args, void *);
363 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
366 67956560 2002-05-19 alex if (cflags == DP_C_SHORT) {
367 67956560 2002-05-19 alex short int *num;
368 67956560 2002-05-19 alex num = va_arg (args, short int *);
369 67956560 2002-05-19 alex *num = currlen;
370 67956560 2002-05-19 alex } else if (cflags == DP_C_LONG) {
371 67956560 2002-05-19 alex long int *num;
372 67956560 2002-05-19 alex num = va_arg (args, long int *);
373 67956560 2002-05-19 alex *num = (long int)currlen;
374 67956560 2002-05-19 alex } else if (cflags == DP_C_LLONG) {
375 67956560 2002-05-19 alex LLONG *num;
376 67956560 2002-05-19 alex num = va_arg (args, LLONG *);
377 67956560 2002-05-19 alex *num = (LLONG)currlen;
380 67956560 2002-05-19 alex num = va_arg (args, int *);
381 67956560 2002-05-19 alex *num = currlen;
385 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
388 67956560 2002-05-19 alex /* not supported yet, treat as next char */
389 67956560 2002-05-19 alex ch = *format++;
392 67956560 2002-05-19 alex /* Unknown, skip */
395 67956560 2002-05-19 alex ch = *format++;
396 67956560 2002-05-19 alex state = DP_S_DEFAULT;
397 67956560 2002-05-19 alex flags = cflags = min = 0;
400 67956560 2002-05-19 alex case DP_S_DONE:
404 67956560 2002-05-19 alex break; /* some picky compilers need this */
407 67956560 2002-05-19 alex if (maxlen != 0) {
408 67956560 2002-05-19 alex if (currlen < maxlen - 1)
409 67956560 2002-05-19 alex buffer[currlen] = '\0';
410 67956560 2002-05-19 alex else if (maxlen > 0)
411 67956560 2002-05-19 alex buffer[maxlen - 1] = '\0';
414 67956560 2002-05-19 alex return currlen;
417 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
418 67956560 2002-05-19 alex char *value, int flags, int min, int max)
420 67956560 2002-05-19 alex int padlen, strln; /* amount to pad */
421 67956560 2002-05-19 alex int cnt = 0;
423 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
424 67956560 2002-05-19 alex printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
426 67956560 2002-05-19 alex if (value == 0) {
427 67956560 2002-05-19 alex value = "<NULL>";
430 67956560 2002-05-19 alex for (strln = 0; value[strln]; ++strln); /* strlen */
431 67956560 2002-05-19 alex padlen = min - strln;
432 67956560 2002-05-19 alex if (padlen < 0)
433 67956560 2002-05-19 alex padlen = 0;
434 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
435 67956560 2002-05-19 alex padlen = -padlen; /* Left Justify */
437 67956560 2002-05-19 alex while ((padlen > 0) && (cnt < max)) {
438 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
442 67956560 2002-05-19 alex while (*value && (cnt < max)) {
443 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, *value++);
446 67956560 2002-05-19 alex while ((padlen < 0) && (cnt < max)) {
447 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
453 67956560 2002-05-19 alex /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
455 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
456 67956560 2002-05-19 alex long value, int base, int min, int max, int flags)
458 67956560 2002-05-19 alex int signvalue = 0;
459 67956560 2002-05-19 alex unsigned long uvalue;
460 67956560 2002-05-19 alex char convert[20];
461 67956560 2002-05-19 alex int place = 0;
462 67956560 2002-05-19 alex int spadlen = 0; /* amount to space pad */
463 67956560 2002-05-19 alex int zpadlen = 0; /* amount to zero pad */
464 67956560 2002-05-19 alex int caps = 0;
466 67956560 2002-05-19 alex if (max < 0)
469 67956560 2002-05-19 alex uvalue = value;
471 67956560 2002-05-19 alex if(!(flags & DP_F_UNSIGNED)) {
472 67956560 2002-05-19 alex if( value < 0 ) {
473 67956560 2002-05-19 alex signvalue = '-';
474 67956560 2002-05-19 alex uvalue = -value;
476 67956560 2002-05-19 alex if (flags & DP_F_PLUS) /* Do a sign (+/i) */
477 67956560 2002-05-19 alex signvalue = '+';
478 67956560 2002-05-19 alex else if (flags & DP_F_SPACE)
479 67956560 2002-05-19 alex signvalue = ' ';
483 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
486 67956560 2002-05-19 alex convert[place++] =
487 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")
488 67956560 2002-05-19 alex [uvalue % (unsigned)base ];
489 67956560 2002-05-19 alex uvalue = (uvalue / (unsigned)base );
490 67956560 2002-05-19 alex } while(uvalue && (place < 20));
491 67956560 2002-05-19 alex if (place == 20) place--;
492 67956560 2002-05-19 alex convert[place] = 0;
494 67956560 2002-05-19 alex zpadlen = max - place;
495 67956560 2002-05-19 alex spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
496 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
497 67956560 2002-05-19 alex if (spadlen < 0) spadlen = 0;
498 67956560 2002-05-19 alex if (flags & DP_F_ZERO) {
499 67956560 2002-05-19 alex zpadlen = MAX(zpadlen, spadlen);
500 67956560 2002-05-19 alex spadlen = 0;
502 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
503 67956560 2002-05-19 alex spadlen = -spadlen; /* Left Justifty */
505 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
506 67956560 2002-05-19 alex printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
507 67956560 2002-05-19 alex zpadlen, spadlen, min, max, place);
510 67956560 2002-05-19 alex /* Spaces */
511 67956560 2002-05-19 alex while (spadlen > 0) {
512 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
517 67956560 2002-05-19 alex if (signvalue)
518 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
520 67956560 2002-05-19 alex /* Zeros */
521 67956560 2002-05-19 alex if (zpadlen > 0) {
522 67956560 2002-05-19 alex while (zpadlen > 0) {
523 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
528 67956560 2002-05-19 alex /* Digits */
529 67956560 2002-05-19 alex while (place > 0)
530 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, convert[--place]);
532 67956560 2002-05-19 alex /* Left Justified spaces */
533 67956560 2002-05-19 alex while (spadlen < 0) {
534 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
539 67956560 2002-05-19 alex static LDOUBLE abs_val(LDOUBLE value)
541 67956560 2002-05-19 alex LDOUBLE result = value;
543 67956560 2002-05-19 alex if (value < 0)
544 67956560 2002-05-19 alex result = -value;
546 67956560 2002-05-19 alex return result;
549 67956560 2002-05-19 alex static LDOUBLE POW10(int exp)
551 67956560 2002-05-19 alex LDOUBLE result = 1;
553 67956560 2002-05-19 alex while (exp) {
554 67956560 2002-05-19 alex result *= 10;
558 67956560 2002-05-19 alex return result;
561 67956560 2002-05-19 alex static LLONG ROUND(LDOUBLE value)
563 67956560 2002-05-19 alex LLONG intpart;
565 67956560 2002-05-19 alex intpart = (LLONG)value;
566 67956560 2002-05-19 alex value = value - intpart;
567 67956560 2002-05-19 alex if (value >= 0.5) intpart++;
569 67956560 2002-05-19 alex return intpart;
572 67956560 2002-05-19 alex /* a replacement for modf that doesn't need the math library. Should
573 67956560 2002-05-19 alex be portable, but slow */
574 67956560 2002-05-19 alex static double my_modf(double x0, double *iptr)
578 67956560 2002-05-19 alex double x = x0;
579 67956560 2002-05-19 alex double f = 1.0;
581 67956560 2002-05-19 alex for (i=0;i<100;i++) {
582 67956560 2002-05-19 alex l = (long)x;
583 67956560 2002-05-19 alex if (l <= (x+1) && l >= (x-1)) break;
588 67956560 2002-05-19 alex if (i == 100) {
589 67956560 2002-05-19 alex /* yikes! the number is beyond what we can handle. What do we do? */
590 67956560 2002-05-19 alex (*iptr) = 0;
594 67956560 2002-05-19 alex if (i != 0) {
596 67956560 2002-05-19 alex double ret;
598 67956560 2002-05-19 alex ret = my_modf(x0-l*f, &i2);
599 67956560 2002-05-19 alex (*iptr) = l*f + i2;
600 67956560 2002-05-19 alex return ret;
603 67956560 2002-05-19 alex (*iptr) = l;
604 67956560 2002-05-19 alex return x - (*iptr);
608 67956560 2002-05-19 alex static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
609 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags)
611 67956560 2002-05-19 alex int signvalue = 0;
612 67956560 2002-05-19 alex double ufvalue;
613 67956560 2002-05-19 alex char iconvert[311];
614 67956560 2002-05-19 alex char fconvert[311];
615 67956560 2002-05-19 alex int iplace = 0;
616 67956560 2002-05-19 alex int fplace = 0;
617 67956560 2002-05-19 alex int padlen = 0; /* amount to pad */
618 67956560 2002-05-19 alex int zpadlen = 0;
619 67956560 2002-05-19 alex int caps = 0;
621 67956560 2002-05-19 alex double intpart;
622 67956560 2002-05-19 alex double fracpart;
623 67956560 2002-05-19 alex double temp;
626 67956560 2002-05-19 alex * AIX manpage says the default is 0, but Solaris says the default
627 67956560 2002-05-19 alex * is 6, and sprintf on AIX defaults to 6
629 67956560 2002-05-19 alex if (max < 0)
632 67956560 2002-05-19 alex ufvalue = abs_val (fvalue);
634 67956560 2002-05-19 alex if (fvalue < 0) {
635 67956560 2002-05-19 alex signvalue = '-';
637 67956560 2002-05-19 alex if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
638 67956560 2002-05-19 alex signvalue = '+';
640 67956560 2002-05-19 alex if (flags & DP_F_SPACE)
641 67956560 2002-05-19 alex signvalue = ' ';
646 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
650 c5461c45 2002-05-19 alex if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
654 67956560 2002-05-19 alex * Sorry, we only support 16 digits past the decimal because of our
655 67956560 2002-05-19 alex * conversion method
657 67956560 2002-05-19 alex if (max > 16)
660 67956560 2002-05-19 alex /* We "cheat" by converting the fractional part to integer by
661 67956560 2002-05-19 alex * multiplying by a factor of 10
664 67956560 2002-05-19 alex temp = ufvalue;
665 67956560 2002-05-19 alex my_modf(temp, &intpart);
667 67956560 2002-05-19 alex fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
669 67956560 2002-05-19 alex if (fracpart >= POW10(max)) {
671 67956560 2002-05-19 alex fracpart -= POW10(max);
675 67956560 2002-05-19 alex /* Convert integer part */
677 67956560 2002-05-19 alex temp = intpart;
678 67956560 2002-05-19 alex my_modf(intpart*0.1, &intpart);
679 67956560 2002-05-19 alex temp = temp*0.1;
680 67956560 2002-05-19 alex index = (int) ((temp -intpart +0.05)* 10.0);
681 67956560 2002-05-19 alex /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
682 67956560 2002-05-19 alex /* printf ("%llf, %f, %x\n", temp, intpart, index); */
683 67956560 2002-05-19 alex iconvert[iplace++] =
684 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
685 67956560 2002-05-19 alex } while (intpart && (iplace < 311));
686 67956560 2002-05-19 alex if (iplace == 311) iplace--;
687 67956560 2002-05-19 alex iconvert[iplace] = 0;
689 67956560 2002-05-19 alex /* Convert fractional part */
690 67956560 2002-05-19 alex if (fracpart)
693 67956560 2002-05-19 alex temp = fracpart;
694 67956560 2002-05-19 alex my_modf(fracpart*0.1, &fracpart);
695 67956560 2002-05-19 alex temp = temp*0.1;
696 67956560 2002-05-19 alex index = (int) ((temp -fracpart +0.05)* 10.0);
697 67956560 2002-05-19 alex /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
698 67956560 2002-05-19 alex /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
699 67956560 2002-05-19 alex fconvert[fplace++] =
700 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
701 67956560 2002-05-19 alex } while(fracpart && (fplace < 311));
702 67956560 2002-05-19 alex if (fplace == 311) fplace--;
704 67956560 2002-05-19 alex fconvert[fplace] = 0;
706 67956560 2002-05-19 alex /* -1 for decimal point, another -1 if we are printing a sign */
707 67956560 2002-05-19 alex padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
708 67956560 2002-05-19 alex zpadlen = max - fplace;
709 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
710 67956560 2002-05-19 alex if (padlen < 0)
711 67956560 2002-05-19 alex padlen = 0;
712 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
713 67956560 2002-05-19 alex padlen = -padlen; /* Left Justifty */
715 67956560 2002-05-19 alex if ((flags & DP_F_ZERO) && (padlen > 0)) {
716 67956560 2002-05-19 alex if (signvalue) {
717 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
719 67956560 2002-05-19 alex signvalue = 0;
721 67956560 2002-05-19 alex while (padlen > 0) {
722 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
726 67956560 2002-05-19 alex while (padlen > 0) {
727 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
730 67956560 2002-05-19 alex if (signvalue)
731 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
733 67956560 2002-05-19 alex while (iplace > 0)
734 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
736 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
737 67956560 2002-05-19 alex printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
741 67956560 2002-05-19 alex * Decimal point. This should probably use locale to find the correct
742 67956560 2002-05-19 alex * char to print out.
744 67956560 2002-05-19 alex if (max > 0) {
745 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '.');
747 67956560 2002-05-19 alex while (fplace > 0)
748 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
751 67956560 2002-05-19 alex while (zpadlen > 0) {
752 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
756 67956560 2002-05-19 alex while (padlen < 0) {
757 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
762 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
764 67956560 2002-05-19 alex if (*currlen < maxlen) {
765 67956560 2002-05-19 alex buffer[(*currlen)] = c;
767 67956560 2002-05-19 alex (*currlen)++;
770 c5461c45 2002-05-19 alex #if !defined(HAVE_VSNPRINTF)
771 c5461c45 2002-05-19 alex int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
773 67956560 2002-05-19 alex return dopr(str, count, fmt, args);
777 c5461c45 2002-05-19 alex #if !defined(HAVE_SNPRINTF)
778 c5461c45 2002-05-19 alex int snprintf(char *str,size_t count,const char *fmt,...)
780 67956560 2002-05-19 alex size_t ret;
781 67956560 2002-05-19 alex va_list ap;
783 67956560 2002-05-19 alex va_start(ap, fmt);
784 67956560 2002-05-19 alex ret = vsnprintf(str, count, fmt, ap);
785 67956560 2002-05-19 alex va_end(ap);
786 67956560 2002-05-19 alex return ret;
793 67956560 2002-05-19 alex #ifdef TEST_SNPRINTF
794 67956560 2002-05-19 alex int sprintf(char *str,const char *fmt,...);
795 67956560 2002-05-19 alex int main (void)
797 67956560 2002-05-19 alex char buf1[1024];
798 67956560 2002-05-19 alex char buf2[1024];
799 67956560 2002-05-19 alex char *fp_fmt[] = {
817 67956560 2002-05-19 alex double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
818 67956560 2002-05-19 alex 0.9996, 1.996, 4.136, 0};
819 67956560 2002-05-19 alex char *int_fmt[] = {
826 67956560 2002-05-19 alex "%+22.33d",
832 67956560 2002-05-19 alex long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
833 67956560 2002-05-19 alex char *str_fmt[] = {
846 67956560 2002-05-19 alex char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
848 67956560 2002-05-19 alex int fail = 0;
849 67956560 2002-05-19 alex int num = 0;
851 67956560 2002-05-19 alex printf ("Testing snprintf format codes against system sprintf...\n");
853 67956560 2002-05-19 alex for (x = 0; fp_fmt[x] ; x++) {
854 67956560 2002-05-19 alex for (y = 0; fp_nums[y] != 0 ; y++) {
855 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
856 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
857 67956560 2002-05-19 alex sprintf (buf2, fp_fmt[x], fp_nums[y]);
858 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
859 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
860 67956560 2002-05-19 alex fp_fmt[x], buf1, buf2);
863 67956560 2002-05-19 alex if (l1 != l2) {
864 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
871 67956560 2002-05-19 alex for (x = 0; int_fmt[x] ; x++) {
872 67956560 2002-05-19 alex for (y = 0; int_nums[y] != 0 ; y++) {
873 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
874 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
875 67956560 2002-05-19 alex sprintf (buf2, int_fmt[x], int_nums[y]);
876 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
877 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
878 67956560 2002-05-19 alex int_fmt[x], buf1, buf2);
881 67956560 2002-05-19 alex if (l1 != l2) {
882 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
889 67956560 2002-05-19 alex for (x = 0; str_fmt[x] ; x++) {
890 67956560 2002-05-19 alex for (y = 0; str_vals[y] != 0 ; y++) {
891 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
892 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
893 67956560 2002-05-19 alex sprintf (buf2, str_fmt[x], str_vals[y]);
894 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
895 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
896 67956560 2002-05-19 alex str_fmt[x], buf1, buf2);
899 67956560 2002-05-19 alex if (l1 != l2) {
900 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
907 67956560 2002-05-19 alex printf ("%d tests failed out of %d.\n", fail, num);
909 67956560 2002-05-19 alex printf("seeing how many digits we support\n");
911 67956560 2002-05-19 alex double v0 = 0.12345678901234567890123456789012345678901;
912 67956560 2002-05-19 alex for (x=0; x<100; x++) {
913 67956560 2002-05-19 alex snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
914 67956560 2002-05-19 alex sprintf(buf2, "%1.1f", v0*pow(10, x));
915 67956560 2002-05-19 alex if (strcmp(buf1, buf2)) {
916 67956560 2002-05-19 alex printf("we seem to support %d digits\n", x-1);
923 67956560 2002-05-19 alex #endif /* SNPRINTF_TEST */
926 67956560 2002-05-19 alex /* -eof- */