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