Damien Miller | f18462f | 2003-04-01 21:31:56 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright Patrick Powell 1995 |
| 3 | * This code is based on code written by Patrick Powell (papowell@astart.com) |
| 4 | * It may be used for any purpose as long as this notice remains intact |
| 5 | * on all source code distributions |
| 6 | */ |
| 7 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 8 | /************************************************************** |
| 9 | * Original: |
| 10 | * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 |
| 11 | * A bombproof version of doprnt (dopr) included. |
| 12 | * Sigh. This sort of thing is always nasty do deal with. Note that |
| 13 | * the version here does not include floating point... |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 14 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 15 | * snprintf() is used instead of sprintf() as it does limit checks |
| 16 | * for string length. This covers a nasty loophole. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 17 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 18 | * The other functions are there to prevent NULL pointers from |
| 19 | * causing nast effects. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 20 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 21 | * More Recently: |
| 22 | * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 |
| 23 | * This was ugly. It is still ugly. I opted out of floating point |
| 24 | * numbers, but the formatter understands just about everything |
| 25 | * from the normal C string format, at least as far as I can tell from |
| 26 | * the Solaris 2.5 printf(3S) man page. |
| 27 | * |
| 28 | * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 |
| 29 | * Ok, added some minimal floating point support, which means this |
| 30 | * probably requires libm on most operating systems. Don't yet |
| 31 | * support the exponent (e,E) and sigfig (g,G). Also, fmtint() |
| 32 | * was pretty badly broken, it just wasn't being exercised in ways |
| 33 | * which showed it, so that's been fixed. Also, formated the code |
| 34 | * to mutt conventions, and removed dead code left over from the |
| 35 | * original. Also, there is now a builtin-test, just compile with: |
| 36 | * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm |
| 37 | * and run snprintf for results. |
| 38 | * |
| 39 | * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i |
| 40 | * The PGP code was using unsigned hexadecimal formats. |
| 41 | * Unfortunately, unsigned formats simply didn't work. |
| 42 | * |
| 43 | * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 |
| 44 | * The original code assumed that both snprintf() and vsnprintf() were |
| 45 | * missing. Some systems only have snprintf() but not vsnprintf(), so |
| 46 | * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. |
| 47 | * |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 48 | * Andrew Tridgell (tridge@samba.org) Oct 1998 |
| 49 | * fixed handling of %.0f |
| 50 | * added test for HAVE_LONG_DOUBLE |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 51 | * |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 52 | * tridge@samba.org, idra@samba.org, April 2001 |
| 53 | * got rid of fcvt code (twas buggy and made testing harder) |
| 54 | * added C99 semantics |
| 55 | * |
| 56 | * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0 |
| 57 | * actually print args for %g and %e |
| 58 | * |
| 59 | * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0 |
| 60 | * Since includes.h isn't included here, VA_COPY has to be defined here. I don't |
| 61 | * see any include file that is guaranteed to be here, so I'm defining it |
| 62 | * locally. Fixes AIX and Solaris builds. |
| 63 | * |
| 64 | * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13 |
| 65 | * put the ifdef for HAVE_VA_COPY in one place rather than in lots of |
| 66 | * functions |
| 67 | * |
| 68 | * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4 |
| 69 | * Fix usage of va_list passed as an arg. Use __va_copy before using it |
| 70 | * when it exists. |
| 71 | * |
| 72 | * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14 |
| 73 | * Fix incorrect zpadlen handling in fmtfp. |
| 74 | * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it. |
| 75 | * few mods to make it easier to compile the tests. |
| 76 | * addedd the "Ollie" test to the floating point ones. |
| 77 | * |
| 78 | * Martin Pool (mbp@samba.org) April 2003 |
| 79 | * Remove NO_CONFIG_H so that the test case can be built within a source |
| 80 | * tree with less trouble. |
| 81 | * Remove unnecessary SAFE_FREE() definition. |
| 82 | * |
| 83 | * Martin Pool (mbp@samba.org) May 2003 |
| 84 | * Put in a prototype for dummy_snprintf() to quiet compiler warnings. |
| 85 | * |
| 86 | * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even |
| 87 | * if the C library has some snprintf functions already. |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 88 | **************************************************************/ |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 89 | |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 90 | #include "includes.h" |
| 91 | |
Ben Lindstrom | 63941f9 | 2001-02-25 23:20:40 +0000 | [diff] [blame] | 92 | #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */ |
| 93 | # undef HAVE_SNPRINTF |
| 94 | # undef HAVE_VSNPRINTF |
| 95 | #endif |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 96 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 97 | #ifndef VA_COPY |
| 98 | # ifdef HAVE_VA_COPY |
| 99 | # define VA_COPY(dest, src) va_copy(dest, src) |
| 100 | # else |
| 101 | # ifdef HAVE___VA_COPY |
| 102 | # define VA_COPY(dest, src) __va_copy(dest, src) |
| 103 | # else |
| 104 | # define VA_COPY(dest, src) (dest) = (src) |
| 105 | # endif |
| 106 | # endif |
| 107 | #endif |
| 108 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 109 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 110 | |
Darren Tucker | f78fb54 | 2006-08-06 21:25:24 +1000 | [diff] [blame] | 111 | #include <ctype.h> |
Darren Tucker | 08432d5 | 2006-09-09 15:59:43 +1000 | [diff] [blame^] | 112 | #include <stdarg.h> |
Darren Tucker | f78fb54 | 2006-08-06 21:25:24 +1000 | [diff] [blame] | 113 | #include <stdlib.h> |
Damien Miller | 62da44f | 2006-07-24 15:08:35 +1000 | [diff] [blame] | 114 | #include <string.h> |
| 115 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 116 | #ifdef HAVE_LONG_DOUBLE |
| 117 | # define LDOUBLE long double |
| 118 | #else |
| 119 | # define LDOUBLE double |
| 120 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 121 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 122 | #ifdef HAVE_LONG_LONG |
| 123 | # define LLONG long long |
| 124 | #else |
| 125 | # define LLONG long |
| 126 | #endif |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame] | 127 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 128 | /* |
| 129 | * dopr(): poor man's version of doprintf |
| 130 | */ |
| 131 | |
| 132 | /* format read states */ |
| 133 | #define DP_S_DEFAULT 0 |
| 134 | #define DP_S_FLAGS 1 |
| 135 | #define DP_S_MIN 2 |
| 136 | #define DP_S_DOT 3 |
| 137 | #define DP_S_MAX 4 |
| 138 | #define DP_S_MOD 5 |
| 139 | #define DP_S_CONV 6 |
| 140 | #define DP_S_DONE 7 |
| 141 | |
| 142 | /* format flags - Bits */ |
| 143 | #define DP_F_MINUS (1 << 0) |
| 144 | #define DP_F_PLUS (1 << 1) |
| 145 | #define DP_F_SPACE (1 << 2) |
| 146 | #define DP_F_NUM (1 << 3) |
| 147 | #define DP_F_ZERO (1 << 4) |
| 148 | #define DP_F_UP (1 << 5) |
| 149 | #define DP_F_UNSIGNED (1 << 6) |
| 150 | |
| 151 | /* Conversion Flags */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 152 | #define DP_C_SHORT 1 |
| 153 | #define DP_C_LONG 2 |
| 154 | #define DP_C_LDOUBLE 3 |
| 155 | #define DP_C_LLONG 4 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 156 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 157 | #define char_to_int(p) ((p)- '0') |
| 158 | #ifndef MAX |
| 159 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) |
| 160 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 161 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 162 | static size_t dopr(char *buffer, size_t maxlen, const char *format, |
| 163 | va_list args_in); |
| 164 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 165 | char *value, int flags, int min, int max); |
| 166 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 167 | LLONG value, int base, int min, int max, int flags); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 168 | static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
| 169 | LDOUBLE fvalue, int min, int max, int flags); |
| 170 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 171 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 172 | static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in) |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 173 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 174 | char ch; |
| 175 | LLONG value; |
| 176 | LDOUBLE fvalue; |
| 177 | char *strvalue; |
| 178 | int min; |
| 179 | int max; |
| 180 | int state; |
| 181 | int flags; |
| 182 | int cflags; |
| 183 | size_t currlen; |
| 184 | va_list args; |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 185 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 186 | VA_COPY(args, args_in); |
| 187 | |
| 188 | state = DP_S_DEFAULT; |
| 189 | currlen = flags = cflags = min = 0; |
| 190 | max = -1; |
| 191 | ch = *format++; |
| 192 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 193 | while (state != DP_S_DONE) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 194 | if (ch == '\0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 195 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 196 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 197 | switch(state) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 198 | case DP_S_DEFAULT: |
| 199 | if (ch == '%') |
| 200 | state = DP_S_FLAGS; |
| 201 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 202 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 203 | ch = *format++; |
| 204 | break; |
| 205 | case DP_S_FLAGS: |
| 206 | switch (ch) { |
| 207 | case '-': |
| 208 | flags |= DP_F_MINUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 209 | ch = *format++; |
| 210 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 211 | case '+': |
| 212 | flags |= DP_F_PLUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 213 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 214 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 215 | case ' ': |
| 216 | flags |= DP_F_SPACE; |
| 217 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 218 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 219 | case '#': |
| 220 | flags |= DP_F_NUM; |
| 221 | ch = *format++; |
| 222 | break; |
| 223 | case '0': |
| 224 | flags |= DP_F_ZERO; |
| 225 | ch = *format++; |
| 226 | break; |
| 227 | default: |
| 228 | state = DP_S_MIN; |
| 229 | break; |
| 230 | } |
| 231 | break; |
| 232 | case DP_S_MIN: |
| 233 | if (isdigit((unsigned char)ch)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 234 | min = 10*min + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 235 | ch = *format++; |
| 236 | } else if (ch == '*') { |
| 237 | min = va_arg (args, int); |
| 238 | ch = *format++; |
| 239 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 240 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 241 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 242 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 243 | break; |
| 244 | case DP_S_DOT: |
| 245 | if (ch == '.') { |
| 246 | state = DP_S_MAX; |
| 247 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 248 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 249 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 250 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 251 | break; |
| 252 | case DP_S_MAX: |
| 253 | if (isdigit((unsigned char)ch)) { |
| 254 | if (max < 0) |
| 255 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 256 | max = 10*max + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 257 | ch = *format++; |
| 258 | } else if (ch == '*') { |
| 259 | max = va_arg (args, int); |
| 260 | ch = *format++; |
| 261 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 262 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 263 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 264 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 265 | break; |
| 266 | case DP_S_MOD: |
| 267 | switch (ch) { |
| 268 | case 'h': |
| 269 | cflags = DP_C_SHORT; |
| 270 | ch = *format++; |
| 271 | break; |
| 272 | case 'l': |
| 273 | cflags = DP_C_LONG; |
| 274 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 275 | if (ch == 'l') { /* It's a long long */ |
| 276 | cflags = DP_C_LLONG; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 277 | ch = *format++; |
| 278 | } |
| 279 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 280 | case 'L': |
| 281 | cflags = DP_C_LDOUBLE; |
| 282 | ch = *format++; |
| 283 | break; |
| 284 | default: |
| 285 | break; |
| 286 | } |
| 287 | state = DP_S_CONV; |
| 288 | break; |
| 289 | case DP_S_CONV: |
| 290 | switch (ch) { |
| 291 | case 'd': |
| 292 | case 'i': |
| 293 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 294 | value = va_arg (args, int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 295 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 296 | value = va_arg (args, long int); |
| 297 | else if (cflags == DP_C_LLONG) |
| 298 | value = va_arg (args, LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 299 | else |
| 300 | value = va_arg (args, int); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 301 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 302 | break; |
| 303 | case 'o': |
| 304 | flags |= DP_F_UNSIGNED; |
| 305 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 306 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 307 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 308 | value = (long)va_arg (args, unsigned long int); |
| 309 | else if (cflags == DP_C_LLONG) |
| 310 | value = (long)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 311 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 312 | value = (long)va_arg (args, unsigned int); |
| 313 | fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 314 | break; |
| 315 | case 'u': |
| 316 | flags |= DP_F_UNSIGNED; |
| 317 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 318 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 319 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 320 | value = (long)va_arg (args, unsigned long int); |
| 321 | else if (cflags == DP_C_LLONG) |
| 322 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 323 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 324 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 325 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 326 | break; |
| 327 | case 'X': |
| 328 | flags |= DP_F_UP; |
| 329 | case 'x': |
| 330 | flags |= DP_F_UNSIGNED; |
| 331 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 332 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 333 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 334 | value = (long)va_arg (args, unsigned long int); |
| 335 | else if (cflags == DP_C_LLONG) |
| 336 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 337 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 338 | value = (long)va_arg (args, unsigned int); |
| 339 | fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 340 | break; |
| 341 | case 'f': |
| 342 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 343 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 344 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 345 | fvalue = va_arg (args, double); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 346 | /* um, floating point? */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 347 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 348 | break; |
| 349 | case 'E': |
| 350 | flags |= DP_F_UP; |
| 351 | case 'e': |
| 352 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 353 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 354 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 355 | fvalue = va_arg (args, double); |
| 356 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 357 | break; |
| 358 | case 'G': |
| 359 | flags |= DP_F_UP; |
| 360 | case 'g': |
| 361 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 362 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 363 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 364 | fvalue = va_arg (args, double); |
| 365 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 366 | break; |
| 367 | case 'c': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 368 | dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 369 | break; |
| 370 | case 's': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 371 | strvalue = va_arg (args, char *); |
| 372 | if (!strvalue) strvalue = "(NULL)"; |
| 373 | if (max == -1) { |
| 374 | max = strlen(strvalue); |
| 375 | } |
| 376 | if (min > 0 && max >= 0 && min > max) max = min; |
| 377 | fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 378 | break; |
| 379 | case 'p': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 380 | strvalue = va_arg (args, void *); |
| 381 | fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 382 | break; |
| 383 | case 'n': |
| 384 | if (cflags == DP_C_SHORT) { |
| 385 | short int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 386 | num = va_arg (args, short int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 387 | *num = currlen; |
| 388 | } else if (cflags == DP_C_LONG) { |
| 389 | long int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 390 | num = va_arg (args, long int *); |
| 391 | *num = (long int)currlen; |
| 392 | } else if (cflags == DP_C_LLONG) { |
| 393 | LLONG *num; |
| 394 | num = va_arg (args, LLONG *); |
| 395 | *num = (LLONG)currlen; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 396 | } else { |
| 397 | int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 398 | num = va_arg (args, int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 399 | *num = currlen; |
| 400 | } |
| 401 | break; |
| 402 | case '%': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 403 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 404 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 405 | case 'w': |
| 406 | /* not supported yet, treat as next char */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 407 | ch = *format++; |
| 408 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 409 | default: |
| 410 | /* Unknown, skip */ |
| 411 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 412 | } |
| 413 | ch = *format++; |
| 414 | state = DP_S_DEFAULT; |
| 415 | flags = cflags = min = 0; |
| 416 | max = -1; |
| 417 | break; |
| 418 | case DP_S_DONE: |
| 419 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 420 | default: |
| 421 | /* hmm? */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 422 | break; /* some picky compilers need this */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 425 | if (maxlen != 0) { |
| 426 | if (currlen < maxlen - 1) |
| 427 | buffer[currlen] = '\0'; |
| 428 | else if (maxlen > 0) |
| 429 | buffer[maxlen - 1] = '\0'; |
| 430 | } |
| 431 | |
| 432 | return currlen; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 433 | } |
| 434 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 435 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 436 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 437 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 438 | int padlen, strln; /* amount to pad */ |
| 439 | int cnt = 0; |
| 440 | |
| 441 | #ifdef DEBUG_SNPRINTF |
| 442 | printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value); |
| 443 | #endif |
| 444 | if (value == 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 445 | value = "<NULL>"; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 446 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 447 | |
Darren Tucker | 4127f55 | 2004-09-23 21:35:09 +1000 | [diff] [blame] | 448 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 449 | padlen = min - strln; |
| 450 | if (padlen < 0) |
| 451 | padlen = 0; |
| 452 | if (flags & DP_F_MINUS) |
| 453 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 454 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 455 | while ((padlen > 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 456 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 457 | --padlen; |
| 458 | ++cnt; |
| 459 | } |
| 460 | while (*value && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 461 | dopr_outch (buffer, currlen, maxlen, *value++); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 462 | ++cnt; |
| 463 | } |
| 464 | while ((padlen < 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 465 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 466 | ++padlen; |
| 467 | ++cnt; |
| 468 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 469 | } |
| 470 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 471 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 472 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 473 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 474 | LLONG value, int base, int min, int max, int flags) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 475 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 476 | int signvalue = 0; |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 477 | unsigned LLONG uvalue; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 478 | char convert[20]; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 479 | int place = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 480 | int spadlen = 0; /* amount to space pad */ |
| 481 | int zpadlen = 0; /* amount to zero pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 482 | int caps = 0; |
| 483 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 484 | if (max < 0) |
| 485 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 486 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 487 | uvalue = value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 488 | |
| 489 | if(!(flags & DP_F_UNSIGNED)) { |
| 490 | if( value < 0 ) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 491 | signvalue = '-'; |
| 492 | uvalue = -value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 493 | } else { |
| 494 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 495 | signvalue = '+'; |
| 496 | else if (flags & DP_F_SPACE) |
| 497 | signvalue = ' '; |
| 498 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 499 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 500 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 501 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 502 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 503 | do { |
| 504 | convert[place++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 505 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 506 | [uvalue % (unsigned)base ]; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 507 | uvalue = (uvalue / (unsigned)base ); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 508 | } while(uvalue && (place < 20)); |
| 509 | if (place == 20) place--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 510 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 511 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 512 | zpadlen = max - place; |
| 513 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 514 | if (zpadlen < 0) zpadlen = 0; |
| 515 | if (spadlen < 0) spadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 516 | if (flags & DP_F_ZERO) { |
| 517 | zpadlen = MAX(zpadlen, spadlen); |
| 518 | spadlen = 0; |
| 519 | } |
| 520 | if (flags & DP_F_MINUS) |
| 521 | spadlen = -spadlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 522 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 523 | #ifdef DEBUG_SNPRINTF |
| 524 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 525 | zpadlen, spadlen, min, max, place); |
| 526 | #endif |
| 527 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 528 | /* Spaces */ |
| 529 | while (spadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 530 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 531 | --spadlen; |
| 532 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 533 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 534 | /* Sign */ |
| 535 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 536 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 537 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 538 | /* Zeros */ |
| 539 | if (zpadlen > 0) { |
| 540 | while (zpadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 541 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 542 | --zpadlen; |
| 543 | } |
| 544 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 545 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 546 | /* Digits */ |
| 547 | while (place > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 548 | dopr_outch (buffer, currlen, maxlen, convert[--place]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 549 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 550 | /* Left Justified spaces */ |
| 551 | while (spadlen < 0) { |
| 552 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 553 | ++spadlen; |
| 554 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 555 | } |
| 556 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 557 | static LDOUBLE abs_val(LDOUBLE value) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 558 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 559 | LDOUBLE result = value; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 560 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 561 | if (value < 0) |
| 562 | result = -value; |
| 563 | |
| 564 | return result; |
| 565 | } |
| 566 | |
| 567 | static LDOUBLE POW10(int exp) |
| 568 | { |
| 569 | LDOUBLE result = 1; |
| 570 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 571 | while (exp) { |
| 572 | result *= 10; |
| 573 | exp--; |
| 574 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 575 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 576 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 577 | } |
| 578 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 579 | static LLONG ROUND(LDOUBLE value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 580 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 581 | LLONG intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 582 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 583 | intpart = (LLONG)value; |
| 584 | value = value - intpart; |
| 585 | if (value >= 0.5) intpart++; |
| 586 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 587 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 588 | } |
| 589 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 590 | /* a replacement for modf that doesn't need the math library. Should |
| 591 | be portable, but slow */ |
| 592 | static double my_modf(double x0, double *iptr) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 593 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 594 | int i; |
| 595 | long l; |
| 596 | double x = x0; |
| 597 | double f = 1.0; |
| 598 | |
| 599 | for (i=0;i<100;i++) { |
| 600 | l = (long)x; |
| 601 | if (l <= (x+1) && l >= (x-1)) break; |
| 602 | x *= 0.1; |
| 603 | f *= 10.0; |
| 604 | } |
| 605 | |
| 606 | if (i == 100) { |
| 607 | /* yikes! the number is beyond what we can handle. What do we do? */ |
| 608 | (*iptr) = 0; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | if (i != 0) { |
| 613 | double i2; |
| 614 | double ret; |
| 615 | |
| 616 | ret = my_modf(x0-l*f, &i2); |
| 617 | (*iptr) = l*f + i2; |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | (*iptr) = l; |
| 622 | return x - (*iptr); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 627 | LDOUBLE fvalue, int min, int max, int flags) |
| 628 | { |
| 629 | int signvalue = 0; |
| 630 | double ufvalue; |
| 631 | char iconvert[311]; |
| 632 | char fconvert[311]; |
| 633 | int iplace = 0; |
| 634 | int fplace = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 635 | int padlen = 0; /* amount to pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 636 | int zpadlen = 0; |
| 637 | int caps = 0; |
| 638 | int idx; |
| 639 | double intpart; |
| 640 | double fracpart; |
| 641 | double temp; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 642 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 643 | /* |
| 644 | * AIX manpage says the default is 0, but Solaris says the default |
| 645 | * is 6, and sprintf on AIX defaults to 6 |
| 646 | */ |
| 647 | if (max < 0) |
| 648 | max = 6; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 649 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 650 | ufvalue = abs_val (fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 651 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 652 | if (fvalue < 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 653 | signvalue = '-'; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 654 | } else { |
| 655 | if (flags & DP_F_PLUS) { /* Do a sign (+/i) */ |
| 656 | signvalue = '+'; |
| 657 | } else { |
| 658 | if (flags & DP_F_SPACE) |
| 659 | signvalue = ' '; |
| 660 | } |
| 661 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 662 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 663 | #if 0 |
| 664 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 665 | #endif |
| 666 | |
| 667 | #if 0 |
| 668 | if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */ |
| 669 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 670 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 671 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 672 | * Sorry, we only support 16 digits past the decimal because of our |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 673 | * conversion method |
| 674 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 675 | if (max > 16) |
| 676 | max = 16; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 677 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 678 | /* We "cheat" by converting the fractional part to integer by |
| 679 | * multiplying by a factor of 10 |
| 680 | */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 681 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 682 | temp = ufvalue; |
| 683 | my_modf(temp, &intpart); |
| 684 | |
| 685 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); |
| 686 | |
| 687 | if (fracpart >= POW10(max)) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 688 | intpart++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 689 | fracpart -= POW10(max); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 690 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 691 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 692 | /* Convert integer part */ |
| 693 | do { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 694 | temp = intpart*0.1; |
| 695 | my_modf(temp, &intpart); |
| 696 | idx = (int) ((temp -intpart +0.05)* 10.0); |
| 697 | /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ |
| 698 | /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 699 | iconvert[iplace++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 700 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 701 | } while (intpart && (iplace < 311)); |
| 702 | if (iplace == 311) iplace--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 703 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 704 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 705 | /* Convert fractional part */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 706 | if (fracpart) |
| 707 | { |
| 708 | do { |
| 709 | temp = fracpart*0.1; |
| 710 | my_modf(temp, &fracpart); |
| 711 | idx = (int) ((temp -fracpart +0.05)* 10.0); |
| 712 | /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ |
| 713 | /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ |
| 714 | fconvert[fplace++] = |
| 715 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 716 | } while(fracpart && (fplace < 311)); |
| 717 | if (fplace == 311) fplace--; |
| 718 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 719 | fconvert[fplace] = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 720 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 721 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 722 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 723 | zpadlen = max - fplace; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 724 | if (zpadlen < 0) zpadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 725 | if (padlen < 0) |
| 726 | padlen = 0; |
| 727 | if (flags & DP_F_MINUS) |
| 728 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 729 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 730 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 731 | if (signvalue) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 732 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 733 | --padlen; |
| 734 | signvalue = 0; |
| 735 | } |
| 736 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 737 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 738 | --padlen; |
| 739 | } |
| 740 | } |
| 741 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 742 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 743 | --padlen; |
| 744 | } |
| 745 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 746 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 747 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 748 | while (iplace > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 749 | dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
| 750 | |
| 751 | #ifdef DEBUG_SNPRINTF |
| 752 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 753 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 754 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 755 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 756 | * Decimal point. This should probably use locale to find the correct |
| 757 | * char to print out. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 758 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 759 | if (max > 0) { |
| 760 | dopr_outch (buffer, currlen, maxlen, '.'); |
| 761 | |
| 762 | while (zpadlen > 0) { |
| 763 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 764 | --zpadlen; |
| 765 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 766 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 767 | while (fplace > 0) |
| 768 | dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 769 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 770 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 771 | while (padlen < 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 772 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 773 | ++padlen; |
| 774 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 775 | } |
| 776 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 777 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 778 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 779 | if (*currlen < maxlen) { |
| 780 | buffer[(*currlen)] = c; |
| 781 | } |
| 782 | (*currlen)++; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 783 | } |
| 784 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 785 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 786 | #if !defined(HAVE_VSNPRINTF) |
| 787 | int vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 788 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 789 | return dopr(str, count, fmt, args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 790 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 791 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 792 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 793 | #if !defined(HAVE_SNPRINTF) |
Darren Tucker | d40c66c | 2005-12-17 22:32:03 +1100 | [diff] [blame] | 794 | int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 795 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 796 | size_t ret; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 797 | va_list ap; |
| 798 | |
| 799 | va_start(ap, fmt); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 800 | ret = vsnprintf(str, count, fmt, ap); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 801 | va_end(ap); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 802 | return ret; |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 803 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 804 | #endif |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 805 | |