| 1 |
adx |
59 |
/* |
| 2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
|
|
* snprintf.c: Functions for printing to a length-limited string. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
|
|
* |
| 7 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU General Public License as published by |
| 9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
* (at your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* This program is distributed in the hope that it will be useful, |
| 13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
* GNU General Public License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU General Public License |
| 18 |
|
|
* along with this program; if not, write to the Free Software |
| 19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
|
|
* USA |
| 21 |
|
|
* |
| 22 |
knight |
71 |
* $Id$ |
| 23 |
adx |
59 |
*/ |
| 24 |
|
|
|
| 25 |
|
|
/************************************************************** |
| 26 |
|
|
* Original header, taken from mutt...: |
| 27 |
|
|
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995 |
| 28 |
|
|
* A bombproof version of doprnt (dopr) included. |
| 29 |
|
|
* Sigh. This sort of thing is always nasty do deal with. Note that |
| 30 |
|
|
* the version here does not include floating point... |
| 31 |
|
|
* |
| 32 |
|
|
* snprintf() is used instead of sprintf() as it does limit checks |
| 33 |
|
|
* for string length. This covers a nasty loophole. |
| 34 |
|
|
* |
| 35 |
|
|
* The other functions are there to prevent NULL pointers from |
| 36 |
|
|
* causing nast effects. |
| 37 |
|
|
* |
| 38 |
|
|
* More Recently: |
| 39 |
|
|
* Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 |
| 40 |
|
|
* This was ugly. It is still ugly. I opted out of floating point |
| 41 |
|
|
* numbers, but the formatter understands just about everything |
| 42 |
|
|
* from the normal C string format, at least as far as I can tell from |
| 43 |
|
|
* the Solaris 2.5 printf(3S) man page. |
| 44 |
|
|
* |
| 45 |
|
|
* Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 |
| 46 |
|
|
* Ok, added some minimal floating point support, which means this |
| 47 |
|
|
* probably requires libm on most operating systems. Don't yet |
| 48 |
|
|
* support the exponent (e,E) and sigfig (g,G). Also, fmtint() |
| 49 |
|
|
* was pretty badly broken, it just wasn't being exercised in ways |
| 50 |
|
|
* which showed it, so that's been fixed. Also, formated the code |
| 51 |
|
|
* to mutt conventions, and removed dead code left over from the |
| 52 |
|
|
* original. Also, there is now a builtin-test, just compile with: |
| 53 |
|
|
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm |
| 54 |
|
|
* and run snprintf for results. |
| 55 |
|
|
* |
| 56 |
|
|
* Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i |
| 57 |
|
|
* The PGP code was using unsigned hexadecimal formats. |
| 58 |
|
|
* Unfortunately, unsigned formats simply didn't work. |
| 59 |
|
|
* |
| 60 |
|
|
* Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 |
| 61 |
|
|
* The original code assumed that both snprintf() and vsnprintf() were |
| 62 |
|
|
* missing. Some systems only have snprintf() but not vsnprintf(), so |
| 63 |
|
|
* the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. |
| 64 |
|
|
* |
| 65 |
|
|
**************************************************************/ |
| 66 |
|
|
|
| 67 |
|
|
#include "stdinc.h" |
| 68 |
|
|
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
| 69 |
|
|
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
/* varargs declarations: */ |
| 73 |
|
|
|
| 74 |
|
|
# include <stdarg.h> |
| 75 |
|
|
# define HAVE_STDARGS /* let's hope that works everywhere (mj) */ |
| 76 |
|
|
# define VA_LOCAL_DECL va_list ap |
| 77 |
|
|
# define VA_START(f) va_start(ap, f) |
| 78 |
|
|
# define VA_SHIFT(v,t) ; /* no-op for ANSI */ |
| 79 |
|
|
# define VA_END va_end(ap) |
| 80 |
|
|
|
| 81 |
|
|
/*int snprintf (char *str, size_t count, const char *fmt, ...);*/ |
| 82 |
|
|
/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/ |
| 83 |
|
|
|
| 84 |
|
|
static void dopr (char *buffer, size_t maxlen, const char *format, |
| 85 |
|
|
va_list args); |
| 86 |
|
|
static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
| 87 |
|
|
char *value, int flags, int min, int max); |
| 88 |
|
|
static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
| 89 |
|
|
long value, int base, int min, int max, int flags); |
| 90 |
|
|
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 91 |
|
|
long double fvalue, int min, int max, int flags); |
| 92 |
|
|
static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); |
| 93 |
|
|
|
| 94 |
|
|
/* |
| 95 |
|
|
* dopr(): poor man's version of doprintf |
| 96 |
|
|
*/ |
| 97 |
|
|
|
| 98 |
|
|
/* format read states */ |
| 99 |
|
|
#define DP_S_DEFAULT 0 |
| 100 |
|
|
#define DP_S_FLAGS 1 |
| 101 |
|
|
#define DP_S_MIN 2 |
| 102 |
|
|
#define DP_S_DOT 3 |
| 103 |
|
|
#define DP_S_MAX 4 |
| 104 |
|
|
#define DP_S_MOD 5 |
| 105 |
|
|
#define DP_S_CONV 6 |
| 106 |
|
|
#define DP_S_DONE 7 |
| 107 |
|
|
|
| 108 |
|
|
/* format flags - Bits */ |
| 109 |
|
|
#define DP_F_MINUS (1 << 0) |
| 110 |
|
|
#define DP_F_PLUS (1 << 1) |
| 111 |
|
|
#define DP_F_SPACE (1 << 2) |
| 112 |
|
|
#define DP_F_NUM (1 << 3) |
| 113 |
|
|
#define DP_F_ZERO (1 << 4) |
| 114 |
|
|
#define DP_F_UP (1 << 5) |
| 115 |
|
|
#define DP_F_UNSIGNED (1 << 6) |
| 116 |
|
|
|
| 117 |
|
|
/* Conversion Flags */ |
| 118 |
|
|
#define DP_C_SHORT 1 |
| 119 |
|
|
#define DP_C_LONG 2 |
| 120 |
|
|
#define DP_C_LDOUBLE 3 |
| 121 |
|
|
|
| 122 |
|
|
#define char_to_int(p) (p - '0') |
| 123 |
|
|
#undef MAX |
| 124 |
|
|
#define MAX(p,q) ((p >= q) ? p : q) |
| 125 |
|
|
|
| 126 |
|
|
static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) |
| 127 |
|
|
{ |
| 128 |
|
|
char ch; |
| 129 |
|
|
long value; |
| 130 |
|
|
long double fvalue; |
| 131 |
|
|
char *strvalue; |
| 132 |
|
|
int min; |
| 133 |
|
|
int max; |
| 134 |
|
|
int state; |
| 135 |
|
|
int flags; |
| 136 |
|
|
int cflags; |
| 137 |
|
|
size_t currlen; |
| 138 |
|
|
|
| 139 |
|
|
state = DP_S_DEFAULT; |
| 140 |
|
|
currlen = flags = cflags = min = 0; |
| 141 |
|
|
max = -1; |
| 142 |
|
|
ch = *format++; |
| 143 |
|
|
|
| 144 |
|
|
while (state != DP_S_DONE) |
| 145 |
|
|
{ |
| 146 |
|
|
if ((ch == '\0') || (currlen >= maxlen)) |
| 147 |
|
|
state = DP_S_DONE; |
| 148 |
|
|
|
| 149 |
|
|
switch(state) |
| 150 |
|
|
{ |
| 151 |
|
|
case DP_S_DEFAULT: |
| 152 |
|
|
if (ch == '%') |
| 153 |
|
|
state = DP_S_FLAGS; |
| 154 |
|
|
else |
| 155 |
|
|
dopr_outch (buffer, &currlen, maxlen, ch); |
| 156 |
|
|
ch = *format++; |
| 157 |
|
|
break; |
| 158 |
|
|
case DP_S_FLAGS: |
| 159 |
|
|
switch (ch) |
| 160 |
|
|
{ |
| 161 |
|
|
case '-': |
| 162 |
|
|
flags |= DP_F_MINUS; |
| 163 |
|
|
ch = *format++; |
| 164 |
|
|
break; |
| 165 |
|
|
case '+': |
| 166 |
|
|
flags |= DP_F_PLUS; |
| 167 |
|
|
ch = *format++; |
| 168 |
|
|
break; |
| 169 |
|
|
case ' ': |
| 170 |
|
|
flags |= DP_F_SPACE; |
| 171 |
|
|
ch = *format++; |
| 172 |
|
|
break; |
| 173 |
|
|
case '#': |
| 174 |
|
|
flags |= DP_F_NUM; |
| 175 |
|
|
ch = *format++; |
| 176 |
|
|
break; |
| 177 |
|
|
case '0': |
| 178 |
|
|
flags |= DP_F_ZERO; |
| 179 |
|
|
ch = *format++; |
| 180 |
|
|
break; |
| 181 |
|
|
default: |
| 182 |
|
|
state = DP_S_MIN; |
| 183 |
|
|
break; |
| 184 |
|
|
} |
| 185 |
|
|
break; |
| 186 |
|
|
case DP_S_MIN: |
| 187 |
|
|
if (isdigit((unsigned char)ch)) |
| 188 |
|
|
{ |
| 189 |
|
|
min = 10*min + char_to_int (ch); |
| 190 |
|
|
ch = *format++; |
| 191 |
|
|
} |
| 192 |
|
|
else if (ch == '*') |
| 193 |
|
|
{ |
| 194 |
|
|
min = va_arg (args, int); |
| 195 |
|
|
ch = *format++; |
| 196 |
|
|
state = DP_S_DOT; |
| 197 |
|
|
} |
| 198 |
|
|
else |
| 199 |
|
|
state = DP_S_DOT; |
| 200 |
|
|
break; |
| 201 |
|
|
case DP_S_DOT: |
| 202 |
|
|
if (ch == '.') |
| 203 |
|
|
{ |
| 204 |
|
|
state = DP_S_MAX; |
| 205 |
|
|
ch = *format++; |
| 206 |
|
|
} |
| 207 |
|
|
else |
| 208 |
|
|
state = DP_S_MOD; |
| 209 |
|
|
break; |
| 210 |
|
|
case DP_S_MAX: |
| 211 |
|
|
if (isdigit((unsigned char)ch)) |
| 212 |
|
|
{ |
| 213 |
|
|
if (max < 0) |
| 214 |
|
|
max = 0; |
| 215 |
|
|
max = 10*max + char_to_int (ch); |
| 216 |
|
|
ch = *format++; |
| 217 |
|
|
} |
| 218 |
|
|
else if (ch == '*') |
| 219 |
|
|
{ |
| 220 |
|
|
max = va_arg (args, int); |
| 221 |
|
|
ch = *format++; |
| 222 |
|
|
state = DP_S_MOD; |
| 223 |
|
|
} |
| 224 |
|
|
else |
| 225 |
|
|
state = DP_S_MOD; |
| 226 |
|
|
break; |
| 227 |
|
|
case DP_S_MOD: |
| 228 |
|
|
/* Currently, we don't support Long Long, bummer */ |
| 229 |
|
|
switch (ch) |
| 230 |
|
|
{ |
| 231 |
|
|
case 'h': |
| 232 |
|
|
cflags = DP_C_SHORT; |
| 233 |
|
|
ch = *format++; |
| 234 |
|
|
break; |
| 235 |
|
|
case 'l': |
| 236 |
|
|
cflags = DP_C_LONG; |
| 237 |
|
|
ch = *format++; |
| 238 |
|
|
break; |
| 239 |
|
|
case 'L': |
| 240 |
|
|
cflags = DP_C_LDOUBLE; |
| 241 |
|
|
ch = *format++; |
| 242 |
|
|
break; |
| 243 |
|
|
default: |
| 244 |
|
|
break; |
| 245 |
|
|
} |
| 246 |
|
|
state = DP_S_CONV; |
| 247 |
|
|
break; |
| 248 |
|
|
case DP_S_CONV: |
| 249 |
|
|
switch (ch) |
| 250 |
|
|
{ |
| 251 |
|
|
case 'd': |
| 252 |
|
|
case 'i': |
| 253 |
|
|
if (cflags == DP_C_SHORT) |
| 254 |
|
|
value = va_arg (args, short int); |
| 255 |
|
|
else if (cflags == DP_C_LONG) |
| 256 |
|
|
value = va_arg (args, long int); |
| 257 |
|
|
else |
| 258 |
|
|
value = va_arg (args, int); |
| 259 |
|
|
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 260 |
|
|
break; |
| 261 |
|
|
case 'o': |
| 262 |
|
|
flags |= DP_F_UNSIGNED; |
| 263 |
|
|
if (cflags == DP_C_SHORT) |
| 264 |
|
|
value = va_arg (args, unsigned short int); |
| 265 |
|
|
else if (cflags == DP_C_LONG) |
| 266 |
|
|
value = va_arg (args, unsigned long int); |
| 267 |
|
|
else |
| 268 |
|
|
value = va_arg (args, unsigned int); |
| 269 |
|
|
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
| 270 |
|
|
break; |
| 271 |
|
|
case 'u': |
| 272 |
|
|
flags |= DP_F_UNSIGNED; |
| 273 |
|
|
if (cflags == DP_C_SHORT) |
| 274 |
|
|
value = va_arg (args, unsigned short int); |
| 275 |
|
|
else if (cflags == DP_C_LONG) |
| 276 |
|
|
value = va_arg (args, unsigned long int); |
| 277 |
|
|
else |
| 278 |
|
|
value = va_arg (args, unsigned int); |
| 279 |
|
|
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 280 |
|
|
break; |
| 281 |
|
|
case 'X': |
| 282 |
|
|
flags |= DP_F_UP; |
| 283 |
|
|
case 'x': |
| 284 |
|
|
flags |= DP_F_UNSIGNED; |
| 285 |
|
|
if (cflags == DP_C_SHORT) |
| 286 |
|
|
value = va_arg (args, unsigned short int); |
| 287 |
|
|
else if (cflags == DP_C_LONG) |
| 288 |
|
|
value = va_arg (args, unsigned long int); |
| 289 |
|
|
else |
| 290 |
|
|
value = va_arg (args, unsigned int); |
| 291 |
|
|
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
| 292 |
|
|
break; |
| 293 |
|
|
case 'f': |
| 294 |
|
|
if (cflags == DP_C_LDOUBLE) |
| 295 |
|
|
fvalue = va_arg (args, long double); |
| 296 |
|
|
else |
| 297 |
|
|
fvalue = va_arg (args, double); |
| 298 |
|
|
/* um, floating point? */ |
| 299 |
|
|
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
| 300 |
|
|
break; |
| 301 |
|
|
case 'E': |
| 302 |
|
|
flags |= DP_F_UP; |
| 303 |
|
|
case 'e': |
| 304 |
|
|
if (cflags == DP_C_LDOUBLE) |
| 305 |
|
|
fvalue = va_arg (args, long double); |
| 306 |
|
|
else |
| 307 |
|
|
fvalue = va_arg (args, double); |
| 308 |
|
|
break; |
| 309 |
|
|
case 'G': |
| 310 |
|
|
flags |= DP_F_UP; |
| 311 |
|
|
case 'g': |
| 312 |
|
|
if (cflags == DP_C_LDOUBLE) |
| 313 |
|
|
fvalue = va_arg (args, long double); |
| 314 |
|
|
else |
| 315 |
|
|
fvalue = va_arg (args, double); |
| 316 |
|
|
break; |
| 317 |
|
|
case 'c': |
| 318 |
|
|
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
| 319 |
|
|
break; |
| 320 |
|
|
case 's': |
| 321 |
|
|
strvalue = va_arg (args, char *); |
| 322 |
|
|
if (max < 0) |
| 323 |
|
|
max = maxlen; /* ie, no max */ |
| 324 |
|
|
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
| 325 |
|
|
break; |
| 326 |
|
|
case 'p': |
| 327 |
|
|
strvalue = va_arg (args, void *); |
| 328 |
|
|
fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
| 329 |
|
|
break; |
| 330 |
|
|
case 'n': |
| 331 |
|
|
if (cflags == DP_C_SHORT) |
| 332 |
|
|
{ |
| 333 |
|
|
short int *num; |
| 334 |
|
|
num = va_arg (args, short int *); |
| 335 |
|
|
*num = currlen; |
| 336 |
|
|
} |
| 337 |
|
|
else if (cflags == DP_C_LONG) |
| 338 |
|
|
{ |
| 339 |
|
|
long int *num; |
| 340 |
|
|
num = va_arg (args, long int *); |
| 341 |
|
|
*num = currlen; |
| 342 |
|
|
} |
| 343 |
|
|
else |
| 344 |
|
|
{ |
| 345 |
|
|
int *num; |
| 346 |
|
|
num = va_arg (args, int *); |
| 347 |
|
|
*num = currlen; |
| 348 |
|
|
} |
| 349 |
|
|
break; |
| 350 |
|
|
case '%': |
| 351 |
|
|
dopr_outch (buffer, &currlen, maxlen, ch); |
| 352 |
|
|
break; |
| 353 |
|
|
case 'w': |
| 354 |
|
|
/* not supported yet, treat as next char */ |
| 355 |
|
|
ch = *format++; |
| 356 |
|
|
break; |
| 357 |
|
|
default: |
| 358 |
|
|
/* Unknown, skip */ |
| 359 |
|
|
break; |
| 360 |
|
|
} |
| 361 |
|
|
ch = *format++; |
| 362 |
|
|
state = DP_S_DEFAULT; |
| 363 |
|
|
flags = cflags = min = 0; |
| 364 |
|
|
max = -1; |
| 365 |
|
|
break; |
| 366 |
|
|
case DP_S_DONE: |
| 367 |
|
|
break; |
| 368 |
|
|
default: |
| 369 |
|
|
/* hmm? */ |
| 370 |
|
|
break; /* some picky compilers need this */ |
| 371 |
|
|
} |
| 372 |
|
|
} |
| 373 |
|
|
if (currlen < maxlen - 1) |
| 374 |
|
|
buffer[currlen] = '\0'; |
| 375 |
|
|
else |
| 376 |
|
|
buffer[maxlen - 1] = '\0'; |
| 377 |
|
|
} |
| 378 |
|
|
|
| 379 |
|
|
static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, |
| 380 |
|
|
char *value, int flags, int min, int max) |
| 381 |
|
|
{ |
| 382 |
|
|
int padlen, strln; /* amount to pad */ |
| 383 |
|
|
int cnt = 0; |
| 384 |
|
|
|
| 385 |
|
|
if (value == 0) |
| 386 |
|
|
{ |
| 387 |
|
|
value = "<NULL>"; |
| 388 |
|
|
} |
| 389 |
|
|
|
| 390 |
|
|
for (strln = 0; value[strln]; ++strln); /* strlen */ |
| 391 |
|
|
padlen = min - strln; |
| 392 |
|
|
if (padlen < 0) |
| 393 |
|
|
padlen = 0; |
| 394 |
|
|
if (flags & DP_F_MINUS) |
| 395 |
|
|
padlen = -padlen; /* Left Justify */ |
| 396 |
|
|
|
| 397 |
|
|
while ((padlen > 0) && (cnt < max)) |
| 398 |
|
|
{ |
| 399 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 400 |
|
|
--padlen; |
| 401 |
|
|
++cnt; |
| 402 |
|
|
} |
| 403 |
|
|
while (*value && (cnt < max)) |
| 404 |
|
|
{ |
| 405 |
|
|
dopr_outch (buffer, currlen, maxlen, *value++); |
| 406 |
|
|
++cnt; |
| 407 |
|
|
} |
| 408 |
|
|
while ((padlen < 0) && (cnt < max)) |
| 409 |
|
|
{ |
| 410 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 411 |
|
|
++padlen; |
| 412 |
|
|
++cnt; |
| 413 |
|
|
} |
| 414 |
|
|
} |
| 415 |
|
|
|
| 416 |
|
|
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 417 |
|
|
|
| 418 |
|
|
static void fmtint (char *buffer, size_t *currlen, size_t maxlen, |
| 419 |
|
|
long value, int base, int min, int max, int flags) |
| 420 |
|
|
{ |
| 421 |
|
|
int signvalue = 0; |
| 422 |
|
|
unsigned long uvalue; |
| 423 |
|
|
char convert[20]; |
| 424 |
|
|
int place = 0; |
| 425 |
|
|
int spadlen = 0; /* amount to space pad */ |
| 426 |
|
|
int zpadlen = 0; /* amount to zero pad */ |
| 427 |
|
|
int caps = 0; |
| 428 |
|
|
|
| 429 |
|
|
if (max < 0) |
| 430 |
|
|
max = 0; |
| 431 |
|
|
|
| 432 |
|
|
uvalue = value; |
| 433 |
|
|
|
| 434 |
|
|
if(!(flags & DP_F_UNSIGNED)) |
| 435 |
|
|
{ |
| 436 |
|
|
if( value < 0 ) { |
| 437 |
|
|
signvalue = '-'; |
| 438 |
|
|
uvalue = -value; |
| 439 |
|
|
} |
| 440 |
|
|
else |
| 441 |
|
|
if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 442 |
|
|
signvalue = '+'; |
| 443 |
|
|
else |
| 444 |
|
|
if (flags & DP_F_SPACE) |
| 445 |
|
|
signvalue = ' '; |
| 446 |
|
|
} |
| 447 |
|
|
|
| 448 |
|
|
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 449 |
|
|
|
| 450 |
|
|
do { |
| 451 |
|
|
convert[place++] = |
| 452 |
|
|
(caps? "0123456789ABCDEF":"0123456789abcdef") |
| 453 |
|
|
[uvalue % (unsigned)base ]; |
| 454 |
|
|
uvalue = (uvalue / (unsigned)base ); |
| 455 |
|
|
} while(uvalue && (place < 20)); |
| 456 |
|
|
if (place == 20) place--; |
| 457 |
|
|
convert[place] = 0; |
| 458 |
|
|
|
| 459 |
|
|
zpadlen = max - place; |
| 460 |
|
|
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
| 461 |
|
|
if (zpadlen < 0) zpadlen = 0; |
| 462 |
|
|
if (spadlen < 0) spadlen = 0; |
| 463 |
|
|
if (flags & DP_F_ZERO) |
| 464 |
|
|
{ |
| 465 |
|
|
zpadlen = MAX(zpadlen, spadlen); |
| 466 |
|
|
spadlen = 0; |
| 467 |
|
|
} |
| 468 |
|
|
if (flags & DP_F_MINUS) |
| 469 |
|
|
spadlen = -spadlen; /* Left Justifty */ |
| 470 |
|
|
|
| 471 |
|
|
#ifdef DEBUG_SNPRINTF |
| 472 |
|
|
dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 473 |
|
|
zpadlen, spadlen, min, max, place)); |
| 474 |
|
|
#endif |
| 475 |
|
|
|
| 476 |
|
|
/* Spaces */ |
| 477 |
|
|
while (spadlen > 0) |
| 478 |
|
|
{ |
| 479 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 480 |
|
|
--spadlen; |
| 481 |
|
|
} |
| 482 |
|
|
|
| 483 |
|
|
/* Sign */ |
| 484 |
|
|
if (signvalue) |
| 485 |
|
|
dopr_outch (buffer, currlen, maxlen, signvalue); |
| 486 |
|
|
|
| 487 |
|
|
/* Zeros */ |
| 488 |
|
|
if (zpadlen > 0) |
| 489 |
|
|
{ |
| 490 |
|
|
while (zpadlen > 0) |
| 491 |
|
|
{ |
| 492 |
|
|
dopr_outch (buffer, currlen, maxlen, '0'); |
| 493 |
|
|
--zpadlen; |
| 494 |
|
|
} |
| 495 |
|
|
} |
| 496 |
|
|
|
| 497 |
|
|
/* Digits */ |
| 498 |
|
|
while (place > 0) |
| 499 |
|
|
dopr_outch (buffer, currlen, maxlen, convert[--place]); |
| 500 |
|
|
|
| 501 |
|
|
/* Left Justified spaces */ |
| 502 |
|
|
while (spadlen < 0) { |
| 503 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 504 |
|
|
++spadlen; |
| 505 |
|
|
} |
| 506 |
|
|
} |
| 507 |
|
|
|
| 508 |
|
|
static long double abs_val (long double value) |
| 509 |
|
|
{ |
| 510 |
|
|
long double result = value; |
| 511 |
|
|
|
| 512 |
|
|
if (value < 0) |
| 513 |
|
|
result = -value; |
| 514 |
|
|
|
| 515 |
|
|
return result; |
| 516 |
|
|
} |
| 517 |
|
|
|
| 518 |
|
|
static long double pow10 (int exp) |
| 519 |
|
|
{ |
| 520 |
|
|
long double result = 1; |
| 521 |
|
|
|
| 522 |
|
|
while (exp) |
| 523 |
|
|
{ |
| 524 |
|
|
result *= 10; |
| 525 |
|
|
exp--; |
| 526 |
|
|
} |
| 527 |
|
|
|
| 528 |
|
|
return result; |
| 529 |
|
|
} |
| 530 |
|
|
|
| 531 |
|
|
static long round (long double value) |
| 532 |
|
|
{ |
| 533 |
|
|
long intpart; |
| 534 |
|
|
|
| 535 |
|
|
intpart = value; |
| 536 |
|
|
value = value - intpart; |
| 537 |
|
|
if (value >= 0.5) |
| 538 |
|
|
intpart++; |
| 539 |
|
|
|
| 540 |
|
|
return intpart; |
| 541 |
|
|
} |
| 542 |
|
|
|
| 543 |
|
|
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 544 |
|
|
long double fvalue, int min, int max, int flags) |
| 545 |
|
|
{ |
| 546 |
|
|
int signvalue = 0; |
| 547 |
|
|
long double ufvalue; |
| 548 |
|
|
char iconvert[20]; |
| 549 |
|
|
char fconvert[20]; |
| 550 |
|
|
int iplace = 0; |
| 551 |
|
|
int fplace = 0; |
| 552 |
|
|
int padlen = 0; /* amount to pad */ |
| 553 |
|
|
int zpadlen = 0; |
| 554 |
|
|
int caps = 0; |
| 555 |
|
|
long intpart; |
| 556 |
|
|
long fracpart; |
| 557 |
|
|
|
| 558 |
|
|
/* |
| 559 |
|
|
* AIX manpage says the default is 0, but Solaris says the default |
| 560 |
|
|
* is 6, and sprintf on AIX defaults to 6 |
| 561 |
|
|
*/ |
| 562 |
|
|
if (max < 0) |
| 563 |
|
|
max = 6; |
| 564 |
|
|
|
| 565 |
|
|
ufvalue = abs_val (fvalue); |
| 566 |
|
|
|
| 567 |
|
|
if (fvalue < 0) |
| 568 |
|
|
signvalue = '-'; |
| 569 |
|
|
else |
| 570 |
|
|
if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 571 |
|
|
signvalue = '+'; |
| 572 |
|
|
else |
| 573 |
|
|
if (flags & DP_F_SPACE) |
| 574 |
|
|
signvalue = ' '; |
| 575 |
|
|
|
| 576 |
|
|
intpart = ufvalue; |
| 577 |
|
|
|
| 578 |
|
|
/* |
| 579 |
|
|
* Sorry, we only support 9 digits past the decimal because of our |
| 580 |
|
|
* conversion method |
| 581 |
|
|
*/ |
| 582 |
|
|
if (max > 9) |
| 583 |
|
|
max = 9; |
| 584 |
|
|
|
| 585 |
|
|
/* We "cheat" by converting the fractional part to integer by |
| 586 |
|
|
* multiplying by a factor of 10 |
| 587 |
|
|
*/ |
| 588 |
|
|
fracpart = round ((pow10 (max)) * (ufvalue - intpart)); |
| 589 |
|
|
|
| 590 |
|
|
if (fracpart >= pow10 (max)) |
| 591 |
|
|
{ |
| 592 |
|
|
intpart++; |
| 593 |
|
|
fracpart -= pow10 (max); |
| 594 |
|
|
} |
| 595 |
|
|
|
| 596 |
|
|
#ifdef DEBUG_SNPRINTF |
| 597 |
|
|
dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart)); |
| 598 |
|
|
#endif |
| 599 |
|
|
|
| 600 |
|
|
/* Convert integer part */ |
| 601 |
|
|
do { |
| 602 |
|
|
iconvert[iplace++] = |
| 603 |
|
|
(caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; |
| 604 |
|
|
intpart = (intpart / 10); |
| 605 |
|
|
} while(intpart && (iplace < 20)); |
| 606 |
|
|
if (iplace == 20) iplace--; |
| 607 |
|
|
iconvert[iplace] = 0; |
| 608 |
|
|
|
| 609 |
|
|
/* Convert fractional part */ |
| 610 |
|
|
do { |
| 611 |
|
|
fconvert[fplace++] = |
| 612 |
|
|
(caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; |
| 613 |
|
|
fracpart = (fracpart / 10); |
| 614 |
|
|
} while(fracpart && (fplace < 20)); |
| 615 |
|
|
if (fplace == 20) fplace--; |
| 616 |
|
|
fconvert[fplace] = 0; |
| 617 |
|
|
|
| 618 |
|
|
/* -1 for decimal point, another -1 if we are printing a sign */ |
| 619 |
|
|
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 620 |
|
|
zpadlen = max - fplace; |
| 621 |
|
|
if (zpadlen < 0) |
| 622 |
|
|
zpadlen = 0; |
| 623 |
|
|
if (padlen < 0) |
| 624 |
|
|
padlen = 0; |
| 625 |
|
|
if (flags & DP_F_MINUS) |
| 626 |
|
|
padlen = -padlen; /* Left Justifty */ |
| 627 |
|
|
|
| 628 |
|
|
if ((flags & DP_F_ZERO) && (padlen > 0)) |
| 629 |
|
|
{ |
| 630 |
|
|
if (signvalue) |
| 631 |
|
|
{ |
| 632 |
|
|
dopr_outch (buffer, currlen, maxlen, signvalue); |
| 633 |
|
|
--padlen; |
| 634 |
|
|
signvalue = 0; |
| 635 |
|
|
} |
| 636 |
|
|
while (padlen > 0) |
| 637 |
|
|
{ |
| 638 |
|
|
dopr_outch (buffer, currlen, maxlen, '0'); |
| 639 |
|
|
--padlen; |
| 640 |
|
|
} |
| 641 |
|
|
} |
| 642 |
|
|
while (padlen > 0) |
| 643 |
|
|
{ |
| 644 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 645 |
|
|
--padlen; |
| 646 |
|
|
} |
| 647 |
|
|
if (signvalue) |
| 648 |
|
|
dopr_outch (buffer, currlen, maxlen, signvalue); |
| 649 |
|
|
|
| 650 |
|
|
while (iplace > 0) |
| 651 |
|
|
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
| 652 |
|
|
|
| 653 |
|
|
/* |
| 654 |
|
|
* Decimal point. This should probably use locale to find the correct |
| 655 |
|
|
* char to print out. |
| 656 |
|
|
*/ |
| 657 |
|
|
dopr_outch (buffer, currlen, maxlen, '.'); |
| 658 |
|
|
|
| 659 |
|
|
while (fplace > 0) |
| 660 |
|
|
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
| 661 |
|
|
|
| 662 |
|
|
while (zpadlen > 0) |
| 663 |
|
|
{ |
| 664 |
|
|
dopr_outch (buffer, currlen, maxlen, '0'); |
| 665 |
|
|
--zpadlen; |
| 666 |
|
|
} |
| 667 |
|
|
|
| 668 |
|
|
while (padlen < 0) |
| 669 |
|
|
{ |
| 670 |
|
|
dopr_outch (buffer, currlen, maxlen, ' '); |
| 671 |
|
|
++padlen; |
| 672 |
|
|
} |
| 673 |
|
|
} |
| 674 |
|
|
|
| 675 |
|
|
static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) |
| 676 |
|
|
{ |
| 677 |
|
|
if (*currlen < maxlen) |
| 678 |
|
|
buffer[(*currlen)++] = c; |
| 679 |
|
|
} |
| 680 |
|
|
#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 681 |
|
|
|
| 682 |
|
|
#ifndef HAVE_VSNPRINTF |
| 683 |
|
|
int vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
| 684 |
|
|
{ |
| 685 |
|
|
str[0] = 0; |
| 686 |
|
|
dopr(str, count, fmt, args); |
| 687 |
|
|
return(strlen(str)); |
| 688 |
|
|
} |
| 689 |
|
|
#endif /* !HAVE_VSNPRINTF */ |
| 690 |
|
|
|
| 691 |
|
|
#ifndef HAVE_SNPRINTF |
| 692 |
|
|
/* VARARGS3 */ |
| 693 |
|
|
#ifdef HAVE_STDARGS |
| 694 |
|
|
int snprintf (char *str,size_t count,const char *fmt,...) |
| 695 |
|
|
#else |
| 696 |
|
|
int snprintf (va_alist) va_dcl |
| 697 |
|
|
#endif |
| 698 |
|
|
{ |
| 699 |
|
|
#ifndef HAVE_STDARGS |
| 700 |
|
|
char *str; |
| 701 |
|
|
size_t count; |
| 702 |
|
|
char *fmt; |
| 703 |
|
|
#endif |
| 704 |
|
|
VA_LOCAL_DECL; |
| 705 |
|
|
|
| 706 |
|
|
VA_START (fmt); |
| 707 |
|
|
VA_SHIFT (str, char *); |
| 708 |
|
|
VA_SHIFT (count, size_t ); |
| 709 |
|
|
VA_SHIFT (fmt, char *); |
| 710 |
|
|
(void) vsnprintf(str, count, fmt, ap); |
| 711 |
|
|
VA_END; |
| 712 |
|
|
return(strlen(str)); |
| 713 |
|
|
} |
| 714 |
|
|
|
| 715 |
|
|
#ifdef TEST_SNPRINTF |
| 716 |
|
|
#ifndef LONG_STRING |
| 717 |
|
|
#define LONG_STRING 1024 |
| 718 |
|
|
#endif |
| 719 |
|
|
int main (void) |
| 720 |
|
|
{ |
| 721 |
|
|
char buf1[LONG_STRING]; |
| 722 |
|
|
char buf2[LONG_STRING]; |
| 723 |
|
|
char *fp_fmt[] = { |
| 724 |
|
|
"%-1.5f", |
| 725 |
|
|
"%1.5f", |
| 726 |
|
|
"%123.9f", |
| 727 |
|
|
"%10.5f", |
| 728 |
|
|
"% 10.5f", |
| 729 |
|
|
"%+22.9f", |
| 730 |
|
|
"%+4.9f", |
| 731 |
|
|
"%01.3f", |
| 732 |
|
|
"%4f", |
| 733 |
|
|
"%3.1f", |
| 734 |
|
|
"%3.2f", |
| 735 |
|
|
NULL |
| 736 |
|
|
}; |
| 737 |
|
|
double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, |
| 738 |
|
|
0.9996, 1.996, 4.136, 0}; |
| 739 |
|
|
char *int_fmt[] = { |
| 740 |
|
|
"%-1.5d", |
| 741 |
|
|
"%1.5d", |
| 742 |
|
|
"%123.9d", |
| 743 |
|
|
"%5.5d", |
| 744 |
|
|
"%10.5d", |
| 745 |
|
|
"% 10.5d", |
| 746 |
|
|
"%+22.33d", |
| 747 |
|
|
"%01.3d", |
| 748 |
|
|
"%4d", |
| 749 |
|
|
NULL |
| 750 |
|
|
}; |
| 751 |
|
|
long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; |
| 752 |
|
|
int x, y; |
| 753 |
|
|
int fail = 0; |
| 754 |
|
|
int num = 0; |
| 755 |
|
|
|
| 756 |
|
|
printf ("Testing snprintf format codes against system sprintf...\n"); |
| 757 |
|
|
|
| 758 |
|
|
for (x = 0; fp_fmt[x] != NULL ; x++) |
| 759 |
|
|
for (y = 0; fp_nums[y] != 0 ; y++) |
| 760 |
|
|
{ |
| 761 |
|
|
snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); |
| 762 |
|
|
sprintf (buf2, fp_fmt[x], fp_nums[y]); |
| 763 |
|
|
if (strcmp (buf1, buf2)) |
| 764 |
|
|
{ |
| 765 |
|
|
printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */ |
| 766 |
|
|
fp_fmt[x], buf1, buf2); |
| 767 |
|
|
fail++; |
| 768 |
|
|
} |
| 769 |
|
|
num++; |
| 770 |
|
|
} |
| 771 |
|
|
|
| 772 |
|
|
for (x = 0; int_fmt[x] != NULL ; x++) |
| 773 |
|
|
for (y = 0; int_nums[y] != 0 ; y++) |
| 774 |
|
|
{ |
| 775 |
|
|
snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); |
| 776 |
|
|
sprintf (buf2, int_fmt[x], int_nums[y]); |
| 777 |
|
|
if (strcmp (buf1, buf2)) |
| 778 |
|
|
{ |
| 779 |
|
|
printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", /* __SPRINTF_CHECKED__ */ |
| 780 |
|
|
int_fmt[x], buf1, buf2); |
| 781 |
|
|
fail++; |
| 782 |
|
|
} |
| 783 |
|
|
num++; |
| 784 |
|
|
} |
| 785 |
|
|
printf ("%d tests failed out of %d.\n", fail, num); |
| 786 |
|
|
} |
| 787 |
|
|
#endif /* SNPRINTF_TEST */ |
| 788 |
|
|
|
| 789 |
|
|
#endif /* !HAVE_SNPRINTF */ |