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 f0d633b5 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 f0d633b5 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 f0d633b5 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 f0d633b5 2002-12-12 alex * (at your option) any later version.
9 f0d633b5 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 f0d633b5 2002-12-12 alex * snprintf() and vsnprintf() replacement functions
15 67956560 2002-05-19 alex #include "portab.h"
17 daa996f1 2003-04-21 alex static char UNUSED id[] = "$Id: vsnprintf.c,v 1.5 2003/04/21 10:53:38 alex Exp $";
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 daa996f1 2003-04-21 alex void dummy_snprintf PARAMS(( void ));
102 ba258e65 2002-05-27 alex void dummy_snprintf PARAMS(( void )) { }
105 67956560 2002-05-19 alex #ifdef HAVE_LONG_DOUBLE
106 67956560 2002-05-19 alex #define LDOUBLE long double
108 67956560 2002-05-19 alex #define LDOUBLE double
111 67956560 2002-05-19 alex #ifdef HAVE_LONG_LONG
112 67956560 2002-05-19 alex #define LLONG long long
114 67956560 2002-05-19 alex #define LLONG long
117 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format,
118 67956560 2002-05-19 alex va_list args);
119 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
120 67956560 2002-05-19 alex char *value, int flags, int min, int max);
121 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
122 67956560 2002-05-19 alex long value, int base, int min, int max, int flags);
123 67956560 2002-05-19 alex static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
124 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags);
125 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
128 67956560 2002-05-19 alex * dopr(): poor man's version of doprintf
131 67956560 2002-05-19 alex /* format read states */
132 67956560 2002-05-19 alex #define DP_S_DEFAULT 0
133 67956560 2002-05-19 alex #define DP_S_FLAGS 1
134 67956560 2002-05-19 alex #define DP_S_MIN 2
135 67956560 2002-05-19 alex #define DP_S_DOT 3
136 67956560 2002-05-19 alex #define DP_S_MAX 4
137 67956560 2002-05-19 alex #define DP_S_MOD 5
138 67956560 2002-05-19 alex #define DP_S_CONV 6
139 67956560 2002-05-19 alex #define DP_S_DONE 7
141 67956560 2002-05-19 alex /* format flags - Bits */
142 67956560 2002-05-19 alex #define DP_F_MINUS (1 << 0)
143 67956560 2002-05-19 alex #define DP_F_PLUS (1 << 1)
144 67956560 2002-05-19 alex #define DP_F_SPACE (1 << 2)
145 67956560 2002-05-19 alex #define DP_F_NUM (1 << 3)
146 67956560 2002-05-19 alex #define DP_F_ZERO (1 << 4)
147 67956560 2002-05-19 alex #define DP_F_UP (1 << 5)
148 67956560 2002-05-19 alex #define DP_F_UNSIGNED (1 << 6)
150 67956560 2002-05-19 alex /* Conversion Flags */
151 67956560 2002-05-19 alex #define DP_C_SHORT 1
152 67956560 2002-05-19 alex #define DP_C_LONG 2
153 67956560 2002-05-19 alex #define DP_C_LDOUBLE 3
154 67956560 2002-05-19 alex #define DP_C_LLONG 4
156 67956560 2002-05-19 alex #define char_to_int(p) ((p)- '0')
157 67956560 2002-05-19 alex #ifndef MAX
158 67956560 2002-05-19 alex #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
161 67956560 2002-05-19 alex static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
164 67956560 2002-05-19 alex LLONG value;
165 67956560 2002-05-19 alex LDOUBLE fvalue;
166 67956560 2002-05-19 alex char *strvalue;
171 67956560 2002-05-19 alex int cflags;
172 67956560 2002-05-19 alex size_t currlen;
174 67956560 2002-05-19 alex state = DP_S_DEFAULT;
175 67956560 2002-05-19 alex currlen = flags = cflags = min = 0;
177 67956560 2002-05-19 alex ch = *format++;
179 67956560 2002-05-19 alex while (state != DP_S_DONE) {
180 67956560 2002-05-19 alex if (ch == '\0')
181 67956560 2002-05-19 alex state = DP_S_DONE;
183 67956560 2002-05-19 alex switch(state) {
184 67956560 2002-05-19 alex case DP_S_DEFAULT:
185 67956560 2002-05-19 alex if (ch == '%')
186 67956560 2002-05-19 alex state = DP_S_FLAGS;
188 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
189 67956560 2002-05-19 alex ch = *format++;
191 67956560 2002-05-19 alex case DP_S_FLAGS:
192 67956560 2002-05-19 alex switch (ch) {
194 67956560 2002-05-19 alex flags |= DP_F_MINUS;
195 67956560 2002-05-19 alex ch = *format++;
198 67956560 2002-05-19 alex flags |= DP_F_PLUS;
199 67956560 2002-05-19 alex ch = *format++;
202 67956560 2002-05-19 alex flags |= DP_F_SPACE;
203 67956560 2002-05-19 alex ch = *format++;
206 67956560 2002-05-19 alex flags |= DP_F_NUM;
207 67956560 2002-05-19 alex ch = *format++;
210 67956560 2002-05-19 alex flags |= DP_F_ZERO;
211 67956560 2002-05-19 alex ch = *format++;
214 67956560 2002-05-19 alex state = DP_S_MIN;
218 67956560 2002-05-19 alex case DP_S_MIN:
219 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
220 67956560 2002-05-19 alex min = 10*min + char_to_int (ch);
221 67956560 2002-05-19 alex ch = *format++;
222 67956560 2002-05-19 alex } else if (ch == '*') {
223 67956560 2002-05-19 alex min = va_arg (args, int);
224 67956560 2002-05-19 alex ch = *format++;
225 67956560 2002-05-19 alex state = DP_S_DOT;
227 67956560 2002-05-19 alex state = DP_S_DOT;
230 67956560 2002-05-19 alex case DP_S_DOT:
231 67956560 2002-05-19 alex if (ch == '.') {
232 67956560 2002-05-19 alex state = DP_S_MAX;
233 67956560 2002-05-19 alex ch = *format++;
235 67956560 2002-05-19 alex state = DP_S_MOD;
238 67956560 2002-05-19 alex case DP_S_MAX:
239 67956560 2002-05-19 alex if (isdigit((unsigned char)ch)) {
240 67956560 2002-05-19 alex if (max < 0)
242 67956560 2002-05-19 alex max = 10*max + char_to_int (ch);
243 67956560 2002-05-19 alex ch = *format++;
244 67956560 2002-05-19 alex } else if (ch == '*') {
245 67956560 2002-05-19 alex max = va_arg (args, int);
246 67956560 2002-05-19 alex ch = *format++;
247 67956560 2002-05-19 alex state = DP_S_MOD;
249 67956560 2002-05-19 alex state = DP_S_MOD;
252 67956560 2002-05-19 alex case DP_S_MOD:
253 67956560 2002-05-19 alex switch (ch) {
255 67956560 2002-05-19 alex cflags = DP_C_SHORT;
256 67956560 2002-05-19 alex ch = *format++;
259 67956560 2002-05-19 alex cflags = DP_C_LONG;
260 67956560 2002-05-19 alex ch = *format++;
261 67956560 2002-05-19 alex if (ch == 'l') { /* It's a long long */
262 67956560 2002-05-19 alex cflags = DP_C_LLONG;
263 67956560 2002-05-19 alex ch = *format++;
267 67956560 2002-05-19 alex cflags = DP_C_LDOUBLE;
268 67956560 2002-05-19 alex ch = *format++;
273 67956560 2002-05-19 alex state = DP_S_CONV;
275 67956560 2002-05-19 alex case DP_S_CONV:
276 67956560 2002-05-19 alex switch (ch) {
279 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
280 67956560 2002-05-19 alex value = va_arg (args, int);
281 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
282 67956560 2002-05-19 alex value = va_arg (args, long int);
283 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
284 67956560 2002-05-19 alex value = va_arg (args, LLONG);
286 67956560 2002-05-19 alex value = va_arg (args, int);
287 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
290 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
291 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
292 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
293 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
294 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
295 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
296 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned LLONG);
298 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
299 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
302 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
303 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
304 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
305 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
306 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
307 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
308 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
310 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
311 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
314 67956560 2002-05-19 alex flags |= DP_F_UP;
316 67956560 2002-05-19 alex flags |= DP_F_UNSIGNED;
317 67956560 2002-05-19 alex if (cflags == DP_C_SHORT)
318 67956560 2002-05-19 alex value = va_arg (args, unsigned int);
319 67956560 2002-05-19 alex else if (cflags == DP_C_LONG)
320 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned long int);
321 67956560 2002-05-19 alex else if (cflags == DP_C_LLONG)
322 67956560 2002-05-19 alex value = (LLONG)va_arg (args, unsigned LLONG);
324 67956560 2002-05-19 alex value = (long)va_arg (args, unsigned int);
325 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
328 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
329 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
331 67956560 2002-05-19 alex fvalue = va_arg (args, double);
332 67956560 2002-05-19 alex /* um, floating point? */
333 67956560 2002-05-19 alex fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
336 67956560 2002-05-19 alex flags |= DP_F_UP;
338 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
339 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
341 67956560 2002-05-19 alex fvalue = va_arg (args, double);
344 67956560 2002-05-19 alex flags |= DP_F_UP;
346 67956560 2002-05-19 alex if (cflags == DP_C_LDOUBLE)
347 67956560 2002-05-19 alex fvalue = va_arg (args, LDOUBLE);
349 67956560 2002-05-19 alex fvalue = va_arg (args, double);
352 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
355 67956560 2002-05-19 alex strvalue = va_arg (args, char *);
356 67956560 2002-05-19 alex if (max == -1) {
357 67956560 2002-05-19 alex max = strlen(strvalue);
359 67956560 2002-05-19 alex if (min > 0 && max >= 0 && min > max) max = min;
360 67956560 2002-05-19 alex fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
363 67956560 2002-05-19 alex strvalue = va_arg (args, void *);
364 67956560 2002-05-19 alex fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
367 67956560 2002-05-19 alex if (cflags == DP_C_SHORT) {
368 67956560 2002-05-19 alex short int *num;
369 67956560 2002-05-19 alex num = va_arg (args, short int *);
370 67956560 2002-05-19 alex *num = currlen;
371 67956560 2002-05-19 alex } else if (cflags == DP_C_LONG) {
372 67956560 2002-05-19 alex long int *num;
373 67956560 2002-05-19 alex num = va_arg (args, long int *);
374 67956560 2002-05-19 alex *num = (long int)currlen;
375 67956560 2002-05-19 alex } else if (cflags == DP_C_LLONG) {
376 67956560 2002-05-19 alex LLONG *num;
377 67956560 2002-05-19 alex num = va_arg (args, LLONG *);
378 67956560 2002-05-19 alex *num = (LLONG)currlen;
381 67956560 2002-05-19 alex num = va_arg (args, int *);
382 67956560 2002-05-19 alex *num = currlen;
386 67956560 2002-05-19 alex dopr_outch (buffer, &currlen, maxlen, ch);
389 67956560 2002-05-19 alex /* not supported yet, treat as next char */
390 67956560 2002-05-19 alex ch = *format++;
393 67956560 2002-05-19 alex /* Unknown, skip */
396 67956560 2002-05-19 alex ch = *format++;
397 67956560 2002-05-19 alex state = DP_S_DEFAULT;
398 67956560 2002-05-19 alex flags = cflags = min = 0;
401 67956560 2002-05-19 alex case DP_S_DONE:
405 67956560 2002-05-19 alex break; /* some picky compilers need this */
408 67956560 2002-05-19 alex if (maxlen != 0) {
409 67956560 2002-05-19 alex if (currlen < maxlen - 1)
410 67956560 2002-05-19 alex buffer[currlen] = '\0';
411 67956560 2002-05-19 alex else if (maxlen > 0)
412 67956560 2002-05-19 alex buffer[maxlen - 1] = '\0';
415 67956560 2002-05-19 alex return currlen;
418 67956560 2002-05-19 alex static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
419 67956560 2002-05-19 alex char *value, int flags, int min, int max)
421 67956560 2002-05-19 alex int padlen, strln; /* amount to pad */
422 67956560 2002-05-19 alex int cnt = 0;
424 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
425 67956560 2002-05-19 alex printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
427 67956560 2002-05-19 alex if (value == 0) {
428 67956560 2002-05-19 alex value = "<NULL>";
431 67956560 2002-05-19 alex for (strln = 0; value[strln]; ++strln); /* strlen */
432 67956560 2002-05-19 alex padlen = min - strln;
433 67956560 2002-05-19 alex if (padlen < 0)
434 67956560 2002-05-19 alex padlen = 0;
435 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
436 67956560 2002-05-19 alex padlen = -padlen; /* Left Justify */
438 67956560 2002-05-19 alex while ((padlen > 0) && (cnt < max)) {
439 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
443 67956560 2002-05-19 alex while (*value && (cnt < max)) {
444 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, *value++);
447 67956560 2002-05-19 alex while ((padlen < 0) && (cnt < max)) {
448 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
454 67956560 2002-05-19 alex /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
456 67956560 2002-05-19 alex static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
457 67956560 2002-05-19 alex long value, int base, int min, int max, int flags)
459 67956560 2002-05-19 alex int signvalue = 0;
460 67956560 2002-05-19 alex unsigned long uvalue;
461 67956560 2002-05-19 alex char convert[20];
462 67956560 2002-05-19 alex int place = 0;
463 67956560 2002-05-19 alex int spadlen = 0; /* amount to space pad */
464 67956560 2002-05-19 alex int zpadlen = 0; /* amount to zero pad */
465 67956560 2002-05-19 alex int caps = 0;
467 67956560 2002-05-19 alex if (max < 0)
470 67956560 2002-05-19 alex uvalue = value;
472 67956560 2002-05-19 alex if(!(flags & DP_F_UNSIGNED)) {
473 67956560 2002-05-19 alex if( value < 0 ) {
474 67956560 2002-05-19 alex signvalue = '-';
475 67956560 2002-05-19 alex uvalue = -value;
477 67956560 2002-05-19 alex if (flags & DP_F_PLUS) /* Do a sign (+/i) */
478 67956560 2002-05-19 alex signvalue = '+';
479 67956560 2002-05-19 alex else if (flags & DP_F_SPACE)
480 67956560 2002-05-19 alex signvalue = ' ';
484 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
487 67956560 2002-05-19 alex convert[place++] =
488 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")
489 67956560 2002-05-19 alex [uvalue % (unsigned)base ];
490 67956560 2002-05-19 alex uvalue = (uvalue / (unsigned)base );
491 67956560 2002-05-19 alex } while(uvalue && (place < 20));
492 67956560 2002-05-19 alex if (place == 20) place--;
493 67956560 2002-05-19 alex convert[place] = 0;
495 67956560 2002-05-19 alex zpadlen = max - place;
496 67956560 2002-05-19 alex spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
497 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
498 67956560 2002-05-19 alex if (spadlen < 0) spadlen = 0;
499 67956560 2002-05-19 alex if (flags & DP_F_ZERO) {
500 67956560 2002-05-19 alex zpadlen = MAX(zpadlen, spadlen);
501 67956560 2002-05-19 alex spadlen = 0;
503 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
504 67956560 2002-05-19 alex spadlen = -spadlen; /* Left Justifty */
506 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
507 67956560 2002-05-19 alex printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
508 67956560 2002-05-19 alex zpadlen, spadlen, min, max, place);
511 67956560 2002-05-19 alex /* Spaces */
512 67956560 2002-05-19 alex while (spadlen > 0) {
513 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
518 67956560 2002-05-19 alex if (signvalue)
519 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
521 67956560 2002-05-19 alex /* Zeros */
522 67956560 2002-05-19 alex if (zpadlen > 0) {
523 67956560 2002-05-19 alex while (zpadlen > 0) {
524 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
529 67956560 2002-05-19 alex /* Digits */
530 67956560 2002-05-19 alex while (place > 0)
531 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, convert[--place]);
533 67956560 2002-05-19 alex /* Left Justified spaces */
534 67956560 2002-05-19 alex while (spadlen < 0) {
535 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
540 67956560 2002-05-19 alex static LDOUBLE abs_val(LDOUBLE value)
542 67956560 2002-05-19 alex LDOUBLE result = value;
544 67956560 2002-05-19 alex if (value < 0)
545 67956560 2002-05-19 alex result = -value;
547 67956560 2002-05-19 alex return result;
550 67956560 2002-05-19 alex static LDOUBLE POW10(int exp)
552 67956560 2002-05-19 alex LDOUBLE result = 1;
554 67956560 2002-05-19 alex while (exp) {
555 67956560 2002-05-19 alex result *= 10;
559 67956560 2002-05-19 alex return result;
562 67956560 2002-05-19 alex static LLONG ROUND(LDOUBLE value)
564 67956560 2002-05-19 alex LLONG intpart;
566 67956560 2002-05-19 alex intpart = (LLONG)value;
567 67956560 2002-05-19 alex value = value - intpart;
568 67956560 2002-05-19 alex if (value >= 0.5) intpart++;
570 67956560 2002-05-19 alex return intpart;
573 67956560 2002-05-19 alex /* a replacement for modf that doesn't need the math library. Should
574 67956560 2002-05-19 alex be portable, but slow */
575 67956560 2002-05-19 alex static double my_modf(double x0, double *iptr)
579 67956560 2002-05-19 alex double x = x0;
580 67956560 2002-05-19 alex double f = 1.0;
582 67956560 2002-05-19 alex for (i=0;i<100;i++) {
583 67956560 2002-05-19 alex l = (long)x;
584 67956560 2002-05-19 alex if (l <= (x+1) && l >= (x-1)) break;
589 67956560 2002-05-19 alex if (i == 100) {
590 67956560 2002-05-19 alex /* yikes! the number is beyond what we can handle. What do we do? */
591 67956560 2002-05-19 alex (*iptr) = 0;
595 67956560 2002-05-19 alex if (i != 0) {
597 67956560 2002-05-19 alex double ret;
599 67956560 2002-05-19 alex ret = my_modf(x0-l*f, &i2);
600 67956560 2002-05-19 alex (*iptr) = l*f + i2;
601 67956560 2002-05-19 alex return ret;
604 67956560 2002-05-19 alex (*iptr) = l;
605 67956560 2002-05-19 alex return x - (*iptr);
609 67956560 2002-05-19 alex static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
610 67956560 2002-05-19 alex LDOUBLE fvalue, int min, int max, int flags)
612 67956560 2002-05-19 alex int signvalue = 0;
613 67956560 2002-05-19 alex double ufvalue;
614 67956560 2002-05-19 alex char iconvert[311];
615 67956560 2002-05-19 alex char fconvert[311];
616 67956560 2002-05-19 alex int iplace = 0;
617 67956560 2002-05-19 alex int fplace = 0;
618 67956560 2002-05-19 alex int padlen = 0; /* amount to pad */
619 67956560 2002-05-19 alex int zpadlen = 0;
620 67956560 2002-05-19 alex int caps = 0;
622 67956560 2002-05-19 alex double intpart;
623 67956560 2002-05-19 alex double fracpart;
624 67956560 2002-05-19 alex double temp;
627 67956560 2002-05-19 alex * AIX manpage says the default is 0, but Solaris says the default
628 67956560 2002-05-19 alex * is 6, and sprintf on AIX defaults to 6
630 67956560 2002-05-19 alex if (max < 0)
633 67956560 2002-05-19 alex ufvalue = abs_val (fvalue);
635 67956560 2002-05-19 alex if (fvalue < 0) {
636 67956560 2002-05-19 alex signvalue = '-';
638 67956560 2002-05-19 alex if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
639 67956560 2002-05-19 alex signvalue = '+';
641 67956560 2002-05-19 alex if (flags & DP_F_SPACE)
642 67956560 2002-05-19 alex signvalue = ' ';
647 67956560 2002-05-19 alex if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
651 c5461c45 2002-05-19 alex if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
655 67956560 2002-05-19 alex * Sorry, we only support 16 digits past the decimal because of our
656 67956560 2002-05-19 alex * conversion method
658 67956560 2002-05-19 alex if (max > 16)
661 67956560 2002-05-19 alex /* We "cheat" by converting the fractional part to integer by
662 67956560 2002-05-19 alex * multiplying by a factor of 10
665 67956560 2002-05-19 alex temp = ufvalue;
666 67956560 2002-05-19 alex my_modf(temp, &intpart);
668 67956560 2002-05-19 alex fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
670 67956560 2002-05-19 alex if (fracpart >= POW10(max)) {
672 67956560 2002-05-19 alex fracpart -= POW10(max);
676 67956560 2002-05-19 alex /* Convert integer part */
678 67956560 2002-05-19 alex temp = intpart;
679 67956560 2002-05-19 alex my_modf(intpart*0.1, &intpart);
680 67956560 2002-05-19 alex temp = temp*0.1;
681 67956560 2002-05-19 alex index = (int) ((temp -intpart +0.05)* 10.0);
682 67956560 2002-05-19 alex /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
683 67956560 2002-05-19 alex /* printf ("%llf, %f, %x\n", temp, intpart, index); */
684 67956560 2002-05-19 alex iconvert[iplace++] =
685 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
686 67956560 2002-05-19 alex } while (intpart && (iplace < 311));
687 67956560 2002-05-19 alex if (iplace == 311) iplace--;
688 67956560 2002-05-19 alex iconvert[iplace] = 0;
690 67956560 2002-05-19 alex /* Convert fractional part */
691 67956560 2002-05-19 alex if (fracpart)
694 67956560 2002-05-19 alex temp = fracpart;
695 67956560 2002-05-19 alex my_modf(fracpart*0.1, &fracpart);
696 67956560 2002-05-19 alex temp = temp*0.1;
697 67956560 2002-05-19 alex index = (int) ((temp -fracpart +0.05)* 10.0);
698 67956560 2002-05-19 alex /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
699 67956560 2002-05-19 alex /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
700 67956560 2002-05-19 alex fconvert[fplace++] =
701 67956560 2002-05-19 alex (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
702 67956560 2002-05-19 alex } while(fracpart && (fplace < 311));
703 67956560 2002-05-19 alex if (fplace == 311) fplace--;
705 67956560 2002-05-19 alex fconvert[fplace] = 0;
707 67956560 2002-05-19 alex /* -1 for decimal point, another -1 if we are printing a sign */
708 67956560 2002-05-19 alex padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
709 67956560 2002-05-19 alex zpadlen = max - fplace;
710 67956560 2002-05-19 alex if (zpadlen < 0) zpadlen = 0;
711 67956560 2002-05-19 alex if (padlen < 0)
712 67956560 2002-05-19 alex padlen = 0;
713 67956560 2002-05-19 alex if (flags & DP_F_MINUS)
714 67956560 2002-05-19 alex padlen = -padlen; /* Left Justifty */
716 67956560 2002-05-19 alex if ((flags & DP_F_ZERO) && (padlen > 0)) {
717 67956560 2002-05-19 alex if (signvalue) {
718 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
720 67956560 2002-05-19 alex signvalue = 0;
722 67956560 2002-05-19 alex while (padlen > 0) {
723 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
727 67956560 2002-05-19 alex while (padlen > 0) {
728 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
731 67956560 2002-05-19 alex if (signvalue)
732 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, signvalue);
734 67956560 2002-05-19 alex while (iplace > 0)
735 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
737 67956560 2002-05-19 alex #ifdef DEBUG_SNPRINTF
738 67956560 2002-05-19 alex printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
742 67956560 2002-05-19 alex * Decimal point. This should probably use locale to find the correct
743 67956560 2002-05-19 alex * char to print out.
745 67956560 2002-05-19 alex if (max > 0) {
746 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '.');
748 67956560 2002-05-19 alex while (fplace > 0)
749 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
752 67956560 2002-05-19 alex while (zpadlen > 0) {
753 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, '0');
757 67956560 2002-05-19 alex while (padlen < 0) {
758 67956560 2002-05-19 alex dopr_outch (buffer, currlen, maxlen, ' ');
763 67956560 2002-05-19 alex static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
765 67956560 2002-05-19 alex if (*currlen < maxlen) {
766 67956560 2002-05-19 alex buffer[(*currlen)] = c;
768 67956560 2002-05-19 alex (*currlen)++;
771 c5461c45 2002-05-19 alex #if !defined(HAVE_VSNPRINTF)
772 c5461c45 2002-05-19 alex int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
774 67956560 2002-05-19 alex return dopr(str, count, fmt, args);
778 c5461c45 2002-05-19 alex #if !defined(HAVE_SNPRINTF)
779 c5461c45 2002-05-19 alex int snprintf(char *str,size_t count,const char *fmt,...)
781 67956560 2002-05-19 alex size_t ret;
782 67956560 2002-05-19 alex va_list ap;
784 67956560 2002-05-19 alex va_start(ap, fmt);
785 67956560 2002-05-19 alex ret = vsnprintf(str, count, fmt, ap);
786 67956560 2002-05-19 alex va_end(ap);
787 67956560 2002-05-19 alex return ret;
794 67956560 2002-05-19 alex #ifdef TEST_SNPRINTF
795 67956560 2002-05-19 alex int sprintf(char *str,const char *fmt,...);
796 67956560 2002-05-19 alex int main (void)
798 67956560 2002-05-19 alex char buf1[1024];
799 67956560 2002-05-19 alex char buf2[1024];
800 67956560 2002-05-19 alex char *fp_fmt[] = {
818 67956560 2002-05-19 alex double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
819 67956560 2002-05-19 alex 0.9996, 1.996, 4.136, 0};
820 67956560 2002-05-19 alex char *int_fmt[] = {
827 67956560 2002-05-19 alex "%+22.33d",
833 67956560 2002-05-19 alex long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
834 67956560 2002-05-19 alex char *str_fmt[] = {
847 67956560 2002-05-19 alex char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
849 67956560 2002-05-19 alex int fail = 0;
850 67956560 2002-05-19 alex int num = 0;
852 67956560 2002-05-19 alex printf ("Testing snprintf format codes against system sprintf...\n");
854 67956560 2002-05-19 alex for (x = 0; fp_fmt[x] ; x++) {
855 67956560 2002-05-19 alex for (y = 0; fp_nums[y] != 0 ; y++) {
856 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
857 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
858 67956560 2002-05-19 alex sprintf (buf2, fp_fmt[x], fp_nums[y]);
859 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
860 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
861 67956560 2002-05-19 alex fp_fmt[x], buf1, buf2);
864 67956560 2002-05-19 alex if (l1 != l2) {
865 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
872 67956560 2002-05-19 alex for (x = 0; int_fmt[x] ; x++) {
873 67956560 2002-05-19 alex for (y = 0; int_nums[y] != 0 ; y++) {
874 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
875 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
876 67956560 2002-05-19 alex sprintf (buf2, int_fmt[x], int_nums[y]);
877 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
878 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
879 67956560 2002-05-19 alex int_fmt[x], buf1, buf2);
882 67956560 2002-05-19 alex if (l1 != l2) {
883 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
890 67956560 2002-05-19 alex for (x = 0; str_fmt[x] ; x++) {
891 67956560 2002-05-19 alex for (y = 0; str_vals[y] != 0 ; y++) {
892 67956560 2002-05-19 alex int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
893 67956560 2002-05-19 alex int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
894 67956560 2002-05-19 alex sprintf (buf2, str_fmt[x], str_vals[y]);
895 67956560 2002-05-19 alex if (strcmp (buf1, buf2)) {
896 67956560 2002-05-19 alex printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
897 67956560 2002-05-19 alex str_fmt[x], buf1, buf2);
900 67956560 2002-05-19 alex if (l1 != l2) {
901 67956560 2002-05-19 alex printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
908 67956560 2002-05-19 alex printf ("%d tests failed out of %d.\n", fail, num);
910 67956560 2002-05-19 alex printf("seeing how many digits we support\n");
912 67956560 2002-05-19 alex double v0 = 0.12345678901234567890123456789012345678901;
913 67956560 2002-05-19 alex for (x=0; x<100; x++) {
914 67956560 2002-05-19 alex snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
915 67956560 2002-05-19 alex sprintf(buf2, "%1.1f", v0*pow(10, x));
916 67956560 2002-05-19 alex if (strcmp(buf1, buf2)) {
917 67956560 2002-05-19 alex printf("we seem to support %d digits\n", x-1);
924 67956560 2002-05-19 alex #endif /* SNPRINTF_TEST */
927 67956560 2002-05-19 alex /* -eof- */