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> |
| 112 | #include <stdlib.h> |
Damien Miller | 62da44f | 2006-07-24 15:08:35 +1000 | [diff] [blame] | 113 | #include <string.h> |
| 114 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 115 | #ifdef HAVE_LONG_DOUBLE |
| 116 | # define LDOUBLE long double |
| 117 | #else |
| 118 | # define LDOUBLE double |
| 119 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 120 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 121 | #ifdef HAVE_LONG_LONG |
| 122 | # define LLONG long long |
| 123 | #else |
| 124 | # define LLONG long |
| 125 | #endif |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame] | 126 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 127 | /* |
| 128 | * dopr(): poor man's version of doprintf |
| 129 | */ |
| 130 | |
| 131 | /* format read states */ |
| 132 | #define DP_S_DEFAULT 0 |
| 133 | #define DP_S_FLAGS 1 |
| 134 | #define DP_S_MIN 2 |
| 135 | #define DP_S_DOT 3 |
| 136 | #define DP_S_MAX 4 |
| 137 | #define DP_S_MOD 5 |
| 138 | #define DP_S_CONV 6 |
| 139 | #define DP_S_DONE 7 |
| 140 | |
| 141 | /* format flags - Bits */ |
| 142 | #define DP_F_MINUS (1 << 0) |
| 143 | #define DP_F_PLUS (1 << 1) |
| 144 | #define DP_F_SPACE (1 << 2) |
| 145 | #define DP_F_NUM (1 << 3) |
| 146 | #define DP_F_ZERO (1 << 4) |
| 147 | #define DP_F_UP (1 << 5) |
| 148 | #define DP_F_UNSIGNED (1 << 6) |
| 149 | |
| 150 | /* Conversion Flags */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 151 | #define DP_C_SHORT 1 |
| 152 | #define DP_C_LONG 2 |
| 153 | #define DP_C_LDOUBLE 3 |
| 154 | #define DP_C_LLONG 4 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 155 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 156 | #define char_to_int(p) ((p)- '0') |
| 157 | #ifndef MAX |
| 158 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) |
| 159 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 160 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 161 | static size_t dopr(char *buffer, size_t maxlen, const char *format, |
| 162 | va_list args_in); |
| 163 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 164 | char *value, int flags, int min, int max); |
| 165 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 166 | LLONG value, int base, int min, int max, int flags); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 167 | static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
| 168 | LDOUBLE fvalue, int min, int max, int flags); |
| 169 | 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] | 170 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 171 | 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] | 172 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 173 | char ch; |
| 174 | LLONG value; |
| 175 | LDOUBLE fvalue; |
| 176 | char *strvalue; |
| 177 | int min; |
| 178 | int max; |
| 179 | int state; |
| 180 | int flags; |
| 181 | int cflags; |
| 182 | size_t currlen; |
| 183 | va_list args; |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 184 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 185 | VA_COPY(args, args_in); |
| 186 | |
| 187 | state = DP_S_DEFAULT; |
| 188 | currlen = flags = cflags = min = 0; |
| 189 | max = -1; |
| 190 | ch = *format++; |
| 191 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 192 | while (state != DP_S_DONE) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 193 | if (ch == '\0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 194 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 195 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 196 | switch(state) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 197 | case DP_S_DEFAULT: |
| 198 | if (ch == '%') |
| 199 | state = DP_S_FLAGS; |
| 200 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 201 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 202 | ch = *format++; |
| 203 | break; |
| 204 | case DP_S_FLAGS: |
| 205 | switch (ch) { |
| 206 | case '-': |
| 207 | flags |= DP_F_MINUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 208 | ch = *format++; |
| 209 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 210 | case '+': |
| 211 | flags |= DP_F_PLUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 212 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 213 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 214 | case ' ': |
| 215 | flags |= DP_F_SPACE; |
| 216 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 217 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 218 | case '#': |
| 219 | flags |= DP_F_NUM; |
| 220 | ch = *format++; |
| 221 | break; |
| 222 | case '0': |
| 223 | flags |= DP_F_ZERO; |
| 224 | ch = *format++; |
| 225 | break; |
| 226 | default: |
| 227 | state = DP_S_MIN; |
| 228 | break; |
| 229 | } |
| 230 | break; |
| 231 | case DP_S_MIN: |
| 232 | if (isdigit((unsigned char)ch)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 233 | min = 10*min + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 234 | ch = *format++; |
| 235 | } else if (ch == '*') { |
| 236 | min = va_arg (args, int); |
| 237 | ch = *format++; |
| 238 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 239 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 240 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 241 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 242 | break; |
| 243 | case DP_S_DOT: |
| 244 | if (ch == '.') { |
| 245 | state = DP_S_MAX; |
| 246 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 247 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 248 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 249 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 250 | break; |
| 251 | case DP_S_MAX: |
| 252 | if (isdigit((unsigned char)ch)) { |
| 253 | if (max < 0) |
| 254 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 255 | max = 10*max + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 256 | ch = *format++; |
| 257 | } else if (ch == '*') { |
| 258 | max = va_arg (args, int); |
| 259 | ch = *format++; |
| 260 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 261 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 262 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 263 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 264 | break; |
| 265 | case DP_S_MOD: |
| 266 | switch (ch) { |
| 267 | case 'h': |
| 268 | cflags = DP_C_SHORT; |
| 269 | ch = *format++; |
| 270 | break; |
| 271 | case 'l': |
| 272 | cflags = DP_C_LONG; |
| 273 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 274 | if (ch == 'l') { /* It's a long long */ |
| 275 | cflags = DP_C_LLONG; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 276 | ch = *format++; |
| 277 | } |
| 278 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 279 | case 'L': |
| 280 | cflags = DP_C_LDOUBLE; |
| 281 | ch = *format++; |
| 282 | break; |
| 283 | default: |
| 284 | break; |
| 285 | } |
| 286 | state = DP_S_CONV; |
| 287 | break; |
| 288 | case DP_S_CONV: |
| 289 | switch (ch) { |
| 290 | case 'd': |
| 291 | case 'i': |
| 292 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 293 | value = va_arg (args, int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 294 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 295 | value = va_arg (args, long int); |
| 296 | else if (cflags == DP_C_LLONG) |
| 297 | value = va_arg (args, LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 298 | else |
| 299 | value = va_arg (args, int); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 300 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 301 | break; |
| 302 | case 'o': |
| 303 | flags |= DP_F_UNSIGNED; |
| 304 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 305 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 306 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 307 | value = (long)va_arg (args, unsigned long int); |
| 308 | else if (cflags == DP_C_LLONG) |
| 309 | value = (long)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 310 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 311 | value = (long)va_arg (args, unsigned int); |
| 312 | fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 313 | break; |
| 314 | case 'u': |
| 315 | flags |= DP_F_UNSIGNED; |
| 316 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 317 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 318 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 319 | value = (long)va_arg (args, unsigned long int); |
| 320 | else if (cflags == DP_C_LLONG) |
| 321 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 322 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 323 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 324 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 325 | break; |
| 326 | case 'X': |
| 327 | flags |= DP_F_UP; |
| 328 | case 'x': |
| 329 | flags |= DP_F_UNSIGNED; |
| 330 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 331 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 332 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 333 | value = (long)va_arg (args, unsigned long int); |
| 334 | else if (cflags == DP_C_LLONG) |
| 335 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 336 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 337 | value = (long)va_arg (args, unsigned int); |
| 338 | fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 339 | break; |
| 340 | case 'f': |
| 341 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 342 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 343 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 344 | fvalue = va_arg (args, double); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 345 | /* um, floating point? */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 346 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 347 | break; |
| 348 | case 'E': |
| 349 | flags |= DP_F_UP; |
| 350 | case 'e': |
| 351 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 352 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 353 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 354 | fvalue = va_arg (args, double); |
| 355 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 356 | break; |
| 357 | case 'G': |
| 358 | flags |= DP_F_UP; |
| 359 | case 'g': |
| 360 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 361 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 362 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 363 | fvalue = va_arg (args, double); |
| 364 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 365 | break; |
| 366 | case 'c': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 367 | dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 368 | break; |
| 369 | case 's': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 370 | strvalue = va_arg (args, char *); |
| 371 | if (!strvalue) strvalue = "(NULL)"; |
| 372 | if (max == -1) { |
| 373 | max = strlen(strvalue); |
| 374 | } |
| 375 | if (min > 0 && max >= 0 && min > max) max = min; |
| 376 | fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 377 | break; |
| 378 | case 'p': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 379 | strvalue = va_arg (args, void *); |
| 380 | fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 381 | break; |
| 382 | case 'n': |
| 383 | if (cflags == DP_C_SHORT) { |
| 384 | short int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 385 | num = va_arg (args, short int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 386 | *num = currlen; |
| 387 | } else if (cflags == DP_C_LONG) { |
| 388 | long int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 389 | num = va_arg (args, long int *); |
| 390 | *num = (long int)currlen; |
| 391 | } else if (cflags == DP_C_LLONG) { |
| 392 | LLONG *num; |
| 393 | num = va_arg (args, LLONG *); |
| 394 | *num = (LLONG)currlen; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 395 | } else { |
| 396 | int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 397 | num = va_arg (args, int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 398 | *num = currlen; |
| 399 | } |
| 400 | break; |
| 401 | case '%': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 402 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 403 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 404 | case 'w': |
| 405 | /* not supported yet, treat as next char */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 406 | ch = *format++; |
| 407 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 408 | default: |
| 409 | /* Unknown, skip */ |
| 410 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 411 | } |
| 412 | ch = *format++; |
| 413 | state = DP_S_DEFAULT; |
| 414 | flags = cflags = min = 0; |
| 415 | max = -1; |
| 416 | break; |
| 417 | case DP_S_DONE: |
| 418 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 419 | default: |
| 420 | /* hmm? */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 421 | break; /* some picky compilers need this */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 424 | if (maxlen != 0) { |
| 425 | if (currlen < maxlen - 1) |
| 426 | buffer[currlen] = '\0'; |
| 427 | else if (maxlen > 0) |
| 428 | buffer[maxlen - 1] = '\0'; |
| 429 | } |
| 430 | |
| 431 | return currlen; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 432 | } |
| 433 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 434 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 435 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 436 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 437 | int padlen, strln; /* amount to pad */ |
| 438 | int cnt = 0; |
| 439 | |
| 440 | #ifdef DEBUG_SNPRINTF |
| 441 | printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value); |
| 442 | #endif |
| 443 | if (value == 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 444 | value = "<NULL>"; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 445 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 446 | |
Darren Tucker | 4127f55 | 2004-09-23 21:35:09 +1000 | [diff] [blame] | 447 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 448 | padlen = min - strln; |
| 449 | if (padlen < 0) |
| 450 | padlen = 0; |
| 451 | if (flags & DP_F_MINUS) |
| 452 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 453 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 454 | while ((padlen > 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 455 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 456 | --padlen; |
| 457 | ++cnt; |
| 458 | } |
| 459 | while (*value && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 460 | dopr_outch (buffer, currlen, maxlen, *value++); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 461 | ++cnt; |
| 462 | } |
| 463 | while ((padlen < 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 464 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 465 | ++padlen; |
| 466 | ++cnt; |
| 467 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 468 | } |
| 469 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 470 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 471 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 472 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 473 | LLONG value, int base, int min, int max, int flags) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 474 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 475 | int signvalue = 0; |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 476 | unsigned LLONG uvalue; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 477 | char convert[20]; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 478 | int place = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 479 | int spadlen = 0; /* amount to space pad */ |
| 480 | int zpadlen = 0; /* amount to zero pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 481 | int caps = 0; |
| 482 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 483 | if (max < 0) |
| 484 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 485 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 486 | uvalue = value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 487 | |
| 488 | if(!(flags & DP_F_UNSIGNED)) { |
| 489 | if( value < 0 ) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 490 | signvalue = '-'; |
| 491 | uvalue = -value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 492 | } else { |
| 493 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 494 | signvalue = '+'; |
| 495 | else if (flags & DP_F_SPACE) |
| 496 | signvalue = ' '; |
| 497 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 498 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 499 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 500 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 501 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 502 | do { |
| 503 | convert[place++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 504 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 505 | [uvalue % (unsigned)base ]; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 506 | uvalue = (uvalue / (unsigned)base ); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 507 | } while(uvalue && (place < 20)); |
| 508 | if (place == 20) place--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 509 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 510 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 511 | zpadlen = max - place; |
| 512 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 513 | if (zpadlen < 0) zpadlen = 0; |
| 514 | if (spadlen < 0) spadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 515 | if (flags & DP_F_ZERO) { |
| 516 | zpadlen = MAX(zpadlen, spadlen); |
| 517 | spadlen = 0; |
| 518 | } |
| 519 | if (flags & DP_F_MINUS) |
| 520 | spadlen = -spadlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 521 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 522 | #ifdef DEBUG_SNPRINTF |
| 523 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 524 | zpadlen, spadlen, min, max, place); |
| 525 | #endif |
| 526 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 527 | /* Spaces */ |
| 528 | while (spadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 529 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 530 | --spadlen; |
| 531 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 532 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 533 | /* Sign */ |
| 534 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 535 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 536 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 537 | /* Zeros */ |
| 538 | if (zpadlen > 0) { |
| 539 | while (zpadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 540 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 541 | --zpadlen; |
| 542 | } |
| 543 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 544 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 545 | /* Digits */ |
| 546 | while (place > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 547 | dopr_outch (buffer, currlen, maxlen, convert[--place]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 548 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 549 | /* Left Justified spaces */ |
| 550 | while (spadlen < 0) { |
| 551 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 552 | ++spadlen; |
| 553 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 554 | } |
| 555 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 556 | static LDOUBLE abs_val(LDOUBLE value) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 557 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 558 | LDOUBLE result = value; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 559 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 560 | if (value < 0) |
| 561 | result = -value; |
| 562 | |
| 563 | return result; |
| 564 | } |
| 565 | |
| 566 | static LDOUBLE POW10(int exp) |
| 567 | { |
| 568 | LDOUBLE result = 1; |
| 569 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 570 | while (exp) { |
| 571 | result *= 10; |
| 572 | exp--; |
| 573 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 574 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 575 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 576 | } |
| 577 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 578 | static LLONG ROUND(LDOUBLE value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 579 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 580 | LLONG intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 581 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 582 | intpart = (LLONG)value; |
| 583 | value = value - intpart; |
| 584 | if (value >= 0.5) intpart++; |
| 585 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 586 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 587 | } |
| 588 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 589 | /* a replacement for modf that doesn't need the math library. Should |
| 590 | be portable, but slow */ |
| 591 | static double my_modf(double x0, double *iptr) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 592 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 593 | int i; |
| 594 | long l; |
| 595 | double x = x0; |
| 596 | double f = 1.0; |
| 597 | |
| 598 | for (i=0;i<100;i++) { |
| 599 | l = (long)x; |
| 600 | if (l <= (x+1) && l >= (x-1)) break; |
| 601 | x *= 0.1; |
| 602 | f *= 10.0; |
| 603 | } |
| 604 | |
| 605 | if (i == 100) { |
| 606 | /* yikes! the number is beyond what we can handle. What do we do? */ |
| 607 | (*iptr) = 0; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | if (i != 0) { |
| 612 | double i2; |
| 613 | double ret; |
| 614 | |
| 615 | ret = my_modf(x0-l*f, &i2); |
| 616 | (*iptr) = l*f + i2; |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | (*iptr) = l; |
| 621 | return x - (*iptr); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 626 | LDOUBLE fvalue, int min, int max, int flags) |
| 627 | { |
| 628 | int signvalue = 0; |
| 629 | double ufvalue; |
| 630 | char iconvert[311]; |
| 631 | char fconvert[311]; |
| 632 | int iplace = 0; |
| 633 | int fplace = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 634 | int padlen = 0; /* amount to pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 635 | int zpadlen = 0; |
| 636 | int caps = 0; |
| 637 | int idx; |
| 638 | double intpart; |
| 639 | double fracpart; |
| 640 | double temp; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 641 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 642 | /* |
| 643 | * AIX manpage says the default is 0, but Solaris says the default |
| 644 | * is 6, and sprintf on AIX defaults to 6 |
| 645 | */ |
| 646 | if (max < 0) |
| 647 | max = 6; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 648 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 649 | ufvalue = abs_val (fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 650 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 651 | if (fvalue < 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 652 | signvalue = '-'; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 653 | } else { |
| 654 | if (flags & DP_F_PLUS) { /* Do a sign (+/i) */ |
| 655 | signvalue = '+'; |
| 656 | } else { |
| 657 | if (flags & DP_F_SPACE) |
| 658 | signvalue = ' '; |
| 659 | } |
| 660 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 661 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 662 | #if 0 |
| 663 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 664 | #endif |
| 665 | |
| 666 | #if 0 |
| 667 | if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */ |
| 668 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 669 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 670 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 671 | * Sorry, we only support 16 digits past the decimal because of our |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 672 | * conversion method |
| 673 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 674 | if (max > 16) |
| 675 | max = 16; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 676 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 677 | /* We "cheat" by converting the fractional part to integer by |
| 678 | * multiplying by a factor of 10 |
| 679 | */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 680 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 681 | temp = ufvalue; |
| 682 | my_modf(temp, &intpart); |
| 683 | |
| 684 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); |
| 685 | |
| 686 | if (fracpart >= POW10(max)) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 687 | intpart++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 688 | fracpart -= POW10(max); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 689 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 690 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 691 | /* Convert integer part */ |
| 692 | do { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 693 | temp = intpart*0.1; |
| 694 | my_modf(temp, &intpart); |
| 695 | idx = (int) ((temp -intpart +0.05)* 10.0); |
| 696 | /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ |
| 697 | /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 698 | iconvert[iplace++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 699 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 700 | } while (intpart && (iplace < 311)); |
| 701 | if (iplace == 311) iplace--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 702 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 703 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 704 | /* Convert fractional part */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 705 | if (fracpart) |
| 706 | { |
| 707 | do { |
| 708 | temp = fracpart*0.1; |
| 709 | my_modf(temp, &fracpart); |
| 710 | idx = (int) ((temp -fracpart +0.05)* 10.0); |
| 711 | /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ |
| 712 | /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ |
| 713 | fconvert[fplace++] = |
| 714 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 715 | } while(fracpart && (fplace < 311)); |
| 716 | if (fplace == 311) fplace--; |
| 717 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 718 | fconvert[fplace] = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 719 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 720 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 721 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 722 | zpadlen = max - fplace; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 723 | if (zpadlen < 0) zpadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 724 | if (padlen < 0) |
| 725 | padlen = 0; |
| 726 | if (flags & DP_F_MINUS) |
| 727 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 728 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 729 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 730 | if (signvalue) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 731 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 732 | --padlen; |
| 733 | signvalue = 0; |
| 734 | } |
| 735 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 736 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 737 | --padlen; |
| 738 | } |
| 739 | } |
| 740 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 741 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 742 | --padlen; |
| 743 | } |
| 744 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 745 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 746 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 747 | while (iplace > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 748 | dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
| 749 | |
| 750 | #ifdef DEBUG_SNPRINTF |
| 751 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 752 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 753 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 754 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 755 | * Decimal point. This should probably use locale to find the correct |
| 756 | * char to print out. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 757 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 758 | if (max > 0) { |
| 759 | dopr_outch (buffer, currlen, maxlen, '.'); |
| 760 | |
| 761 | while (zpadlen > 0) { |
| 762 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 763 | --zpadlen; |
| 764 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 765 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 766 | while (fplace > 0) |
| 767 | dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 768 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 769 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 770 | while (padlen < 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 771 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 772 | ++padlen; |
| 773 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 774 | } |
| 775 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 776 | 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] | 777 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 778 | if (*currlen < maxlen) { |
| 779 | buffer[(*currlen)] = c; |
| 780 | } |
| 781 | (*currlen)++; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 782 | } |
| 783 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 784 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 785 | #if !defined(HAVE_VSNPRINTF) |
| 786 | 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] | 787 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 788 | return dopr(str, count, fmt, args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 789 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 790 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 791 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 792 | #if !defined(HAVE_SNPRINTF) |
Darren Tucker | d40c66c | 2005-12-17 22:32:03 +1100 | [diff] [blame] | 793 | int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 794 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 795 | size_t ret; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 796 | va_list ap; |
| 797 | |
| 798 | va_start(ap, fmt); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 799 | ret = vsnprintf(str, count, fmt, ap); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 800 | va_end(ap); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 801 | return ret; |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 802 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 803 | #endif |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 804 | |