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
8 /**************************************************************
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...
15 * snprintf() is used instead of sprintf() as it does limit checks
16 * for string length. This covers a nasty loophole.
18 * The other functions are there to prevent NULL pointers from
19 * causing nast effects.
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.
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.
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.
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.
48 * Ben Lindstrom <mouring@eviladmin.org> 09/27/00 for OpenSSH
49 * Welcome to the world of %lld and %qd support. With other
50 * long long support. This is needed for sftp-server to work
53 * Ben Lindstrom <mouring@eviladmin.org> 02/12/01 for OpenSSH
54 * Removed all hint of VARARGS stuff and banished it to the void,
55 * and did a bit of KNF style work to make things a bit more
56 * acceptable. Consider stealing from mutt or enlightenment.
57 **************************************************************/
61 RCSID("$Id: bsd-snprintf.c,v 1.9 2004/09/23 11:35:09 dtucker Exp $");
63 #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
65 # undef HAVE_VSNPRINTF
68 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
71 dopr(char *buffer
, size_t maxlen
, const char *format
, va_list args
);
74 fmtstr(char *buffer
, size_t *currlen
, size_t maxlen
, char *value
, int flags
,
78 fmtint(char *buffer
, size_t *currlen
, size_t maxlen
, long value
, int base
,
79 int min
, int max
, int flags
);
82 fmtfp(char *buffer
, size_t *currlen
, size_t maxlen
, long double fvalue
,
83 int min
, int max
, int flags
);
86 dopr_outch(char *buffer
, size_t *currlen
, size_t maxlen
, char c
);
89 * dopr(): poor man's version of doprintf
92 /* format read states */
93 #define DP_S_DEFAULT 0
102 /* format flags - Bits */
103 #define DP_F_MINUS (1 << 0)
104 #define DP_F_PLUS (1 << 1)
105 #define DP_F_SPACE (1 << 2)
106 #define DP_F_NUM (1 << 3)
107 #define DP_F_ZERO (1 << 4)
108 #define DP_F_UP (1 << 5)
109 #define DP_F_UNSIGNED (1 << 6)
111 /* Conversion Flags */
114 #define DP_C_LDOUBLE 3
115 #define DP_C_LONG_LONG 4
117 #define char_to_int(p) (p - '0')
118 #define abs_val(p) (p < 0 ? -p : p)
122 dopr(char *buffer
, size_t maxlen
, const char *format
, va_list args
)
127 int min
= 0, max
= -1, state
= DP_S_DEFAULT
, flags
= 0, cflags
= 0;
132 while (state
!= DP_S_DONE
) {
133 if ((ch
== '\0') || (currlen
>= maxlen
))
141 dopr_outch(buffer
, &currlen
, maxlen
, ch
);
172 if (isdigit((unsigned char)ch
)) {
173 min
= 10 * min
+ char_to_int (ch
);
175 } else if (ch
== '*') {
176 min
= va_arg (args
, int);
190 if (isdigit((unsigned char)ch
)) {
193 max
= 10 * max
+ char_to_int(ch
);
195 } else if (ch
== '*') {
196 max
= va_arg (args
, int);
212 cflags
= DP_C_LONG_LONG
;
217 cflags
= DP_C_LONG_LONG
;
221 cflags
= DP_C_LDOUBLE
;
233 if (cflags
== DP_C_SHORT
)
234 value
= va_arg(args
, int);
235 else if (cflags
== DP_C_LONG
)
236 value
= va_arg(args
, long int);
237 else if (cflags
== DP_C_LONG_LONG
)
238 value
= va_arg (args
, long long);
240 value
= va_arg (args
, int);
241 fmtint(buffer
, &currlen
, maxlen
, value
, 10, min
, max
, flags
);
244 flags
|= DP_F_UNSIGNED
;
245 if (cflags
== DP_C_SHORT
)
246 value
= va_arg(args
, unsigned int);
247 else if (cflags
== DP_C_LONG
)
248 value
= va_arg(args
, unsigned long int);
249 else if (cflags
== DP_C_LONG_LONG
)
250 value
= va_arg(args
, unsigned long long);
252 value
= va_arg(args
, unsigned int);
253 fmtint(buffer
, &currlen
, maxlen
, value
, 8, min
, max
, flags
);
256 flags
|= DP_F_UNSIGNED
;
257 if (cflags
== DP_C_SHORT
)
258 value
= va_arg(args
, unsigned int);
259 else if (cflags
== DP_C_LONG
)
260 value
= va_arg(args
, unsigned long int);
261 else if (cflags
== DP_C_LONG_LONG
)
262 value
= va_arg(args
, unsigned long long);
264 value
= va_arg(args
, unsigned int);
265 fmtint (buffer
, &currlen
, maxlen
, value
, 10, min
, max
, flags
);
270 flags
|= DP_F_UNSIGNED
;
271 if (cflags
== DP_C_SHORT
)
272 value
= va_arg(args
, unsigned int);
273 else if (cflags
== DP_C_LONG
)
274 value
= va_arg(args
, unsigned long int);
275 else if (cflags
== DP_C_LONG_LONG
)
276 value
= va_arg(args
, unsigned long long);
278 value
= va_arg(args
, unsigned int);
279 fmtint(buffer
, &currlen
, maxlen
, value
, 16, min
, max
, flags
);
282 if (cflags
== DP_C_LDOUBLE
)
283 fvalue
= va_arg(args
, long double);
285 fvalue
= va_arg(args
, double);
286 /* um, floating point? */
287 fmtfp(buffer
, &currlen
, maxlen
, fvalue
, min
, max
, flags
);
292 if (cflags
== DP_C_LDOUBLE
)
293 fvalue
= va_arg(args
, long double);
295 fvalue
= va_arg(args
, double);
300 if (cflags
== DP_C_LDOUBLE
)
301 fvalue
= va_arg(args
, long double);
303 fvalue
= va_arg(args
, double);
306 dopr_outch(buffer
, &currlen
, maxlen
, va_arg(args
, int));
309 strvalue
= va_arg(args
, char *);
311 max
= maxlen
; /* ie, no max */
312 fmtstr(buffer
, &currlen
, maxlen
, strvalue
, flags
, min
, max
);
315 strvalue
= va_arg(args
, void *);
316 fmtint(buffer
, &currlen
, maxlen
, (long) strvalue
, 16, min
, max
, flags
);
319 if (cflags
== DP_C_SHORT
) {
321 num
= va_arg(args
, short int *);
323 } else if (cflags
== DP_C_LONG
) {
325 num
= va_arg(args
, long int *);
327 } else if (cflags
== DP_C_LONG_LONG
) {
329 num
= va_arg(args
, long long *);
333 num
= va_arg(args
, int *);
338 dopr_outch(buffer
, &currlen
, maxlen
, ch
);
340 case 'w': /* not supported yet, treat as next char */
343 default: /* Unknown, skip */
347 state
= DP_S_DEFAULT
;
348 flags
= cflags
= min
= 0;
354 break; /* some picky compilers need this */
357 if (currlen
< maxlen
- 1)
358 buffer
[currlen
] = '\0';
360 buffer
[maxlen
- 1] = '\0';
364 fmtstr(char *buffer
, size_t *currlen
, size_t maxlen
,
365 char *value
, int flags
, int min
, int max
)
367 int cnt
= 0, padlen
, strln
; /* amount to pad */
372 for (strln
= 0; strln
< max
&& value
[strln
]; ++strln
); /* strlen */
373 padlen
= min
- strln
;
376 if (flags
& DP_F_MINUS
)
377 padlen
= -padlen
; /* Left Justify */
379 while ((padlen
> 0) && (cnt
< max
)) {
380 dopr_outch(buffer
, currlen
, maxlen
, ' ');
384 while (*value
&& (cnt
< max
)) {
385 dopr_outch(buffer
, currlen
, maxlen
, *value
++);
388 while ((padlen
< 0) && (cnt
< max
)) {
389 dopr_outch(buffer
, currlen
, maxlen
, ' ');
395 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
398 fmtint(char *buffer
, size_t *currlen
, size_t maxlen
,
399 long value
, int base
, int min
, int max
, int flags
)
401 unsigned long uvalue
;
403 int signvalue
= 0, place
= 0, caps
= 0;
404 int spadlen
= 0; /* amount to space pad */
405 int zpadlen
= 0; /* amount to zero pad */
412 if (!(flags
& DP_F_UNSIGNED
)) {
416 } else if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
418 else if (flags
& DP_F_SPACE
)
423 caps
= 1; /* Should characters be upper case? */
426 (caps
? "0123456789ABCDEF" : "0123456789abcdef")
427 [uvalue
% (unsigned)base
];
428 uvalue
= (uvalue
/ (unsigned)base
);
429 } while (uvalue
&& (place
< 20));
434 zpadlen
= max
- place
;
435 spadlen
= min
- MAX (max
, place
) - (signvalue
? 1 : 0);
440 if (flags
& DP_F_ZERO
) {
441 zpadlen
= MAX(zpadlen
, spadlen
);
444 if (flags
& DP_F_MINUS
)
445 spadlen
= -spadlen
; /* Left Justifty */
448 while (spadlen
> 0) {
449 dopr_outch(buffer
, currlen
, maxlen
, ' ');
455 dopr_outch(buffer
, currlen
, maxlen
, signvalue
);
459 while (zpadlen
> 0) {
460 dopr_outch(buffer
, currlen
, maxlen
, '0');
467 dopr_outch(buffer
, currlen
, maxlen
, convert
[--place
]);
469 /* Left Justified spaces */
470 while (spadlen
< 0) {
471 dopr_outch (buffer
, currlen
, maxlen
, ' ');
479 long double result
= 1;
490 round(long double value
)
492 long intpart
= value
;
502 fmtfp(char *buffer
, size_t *currlen
, size_t maxlen
, long double fvalue
,
503 int min
, int max
, int flags
)
505 char iconvert
[20], fconvert
[20];
506 int signvalue
= 0, iplace
= 0, fplace
= 0;
507 int padlen
= 0; /* amount to pad */
508 int zpadlen
= 0, caps
= 0;
509 long intpart
, fracpart
;
513 * AIX manpage says the default is 0, but Solaris says the default
514 * is 6, and sprintf on AIX defaults to 6
519 ufvalue
= abs_val(fvalue
);
523 else if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
525 else if (flags
& DP_F_SPACE
)
531 * Sorry, we only support 9 digits past the decimal because of our
537 /* We "cheat" by converting the fractional part to integer by
538 * multiplying by a factor of 10
540 fracpart
= round((pow10 (max
)) * (ufvalue
- intpart
));
542 if (fracpart
>= pow10 (max
)) {
544 fracpart
-= pow10 (max
);
547 /* Convert integer part */
550 (caps
? "0123456789ABCDEF" : "0123456789abcdef")
552 intpart
= (intpart
/ 10);
553 } while(intpart
&& (iplace
< 20));
556 iconvert
[iplace
] = 0;
558 /* Convert fractional part */
561 (caps
? "0123456789ABCDEF" : "0123456789abcdef")
563 fracpart
= (fracpart
/ 10);
564 } while(fracpart
&& (fplace
< 20));
567 fconvert
[fplace
] = 0;
569 /* -1 for decimal point, another -1 if we are printing a sign */
570 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
571 zpadlen
= max
- fplace
;
576 if (flags
& DP_F_MINUS
)
577 padlen
= -padlen
; /* Left Justifty */
579 if ((flags
& DP_F_ZERO
) && (padlen
> 0)) {
581 dopr_outch(buffer
, currlen
, maxlen
, signvalue
);
586 dopr_outch(buffer
, currlen
, maxlen
, '0');
591 dopr_outch(buffer
, currlen
, maxlen
, ' ');
595 dopr_outch(buffer
, currlen
, maxlen
, signvalue
);
598 dopr_outch(buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
601 * Decimal point. This should probably use locale to find the
602 * correct char to print out.
604 dopr_outch(buffer
, currlen
, maxlen
, '.');
607 dopr_outch(buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
609 while (zpadlen
> 0) {
610 dopr_outch(buffer
, currlen
, maxlen
, '0');
615 dopr_outch(buffer
, currlen
, maxlen
, ' ');
621 dopr_outch(char *buffer
, size_t *currlen
, size_t maxlen
, char c
)
623 if (*currlen
< maxlen
)
624 buffer
[(*currlen
)++] = c
;
626 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
628 #ifndef HAVE_VSNPRINTF
630 vsnprintf(char *str
, size_t count
, const char *fmt
, va_list args
)
633 dopr(str
, count
, fmt
, args
);
637 #endif /* !HAVE_VSNPRINTF */
639 #ifndef HAVE_SNPRINTF
641 snprintf(char *str
,size_t count
,const char *fmt
,...)
646 (void) vsnprintf(str
, count
, fmt
, ap
);
652 #endif /* !HAVE_SNPRINTF */