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 **************************************************************/
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)
130 int state = DP_S_DEFAULT;
137 while (state != DP_S_DONE) {
138 if ((ch == '\0') || (currlen >= maxlen))
146 dopr_outch(buffer, &currlen, maxlen, ch);
177 if (isdigit((unsigned char)ch)) {
178 min = 10*min + char_to_int (ch);
180 } else if (ch == '*') {
181 min = va_arg (args, int);
195 if (isdigit((unsigned char)ch)) {
198 max = 10*max + char_to_int(ch);
200 } else if (ch == '*') {
201 max = va_arg (args, int);
217 cflags = DP_C_LONG_LONG;
222 cflags = DP_C_LONG_LONG;
226 cflags = DP_C_LDOUBLE;
238 if (cflags == DP_C_SHORT)
239 value = va_arg(args, int);
240 else if (cflags == DP_C_LONG)
241 value = va_arg(args, long int);
242 else if (cflags == DP_C_LONG_LONG)
243 value = va_arg (args, long long);
245 value = va_arg (args, int);
246 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
249 flags |= DP_F_UNSIGNED;
250 if (cflags == DP_C_SHORT)
251 value = va_arg(args, unsigned int);
252 else if (cflags == DP_C_LONG)
253 value = va_arg(args, unsigned long int);
254 else if (cflags == DP_C_LONG_LONG)
255 value = va_arg(args, unsigned long long);
257 value = va_arg(args, unsigned int);
258 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
261 flags |= DP_F_UNSIGNED;
262 if (cflags == DP_C_SHORT)
263 value = va_arg(args, unsigned int);
264 else if (cflags == DP_C_LONG)
265 value = va_arg(args, unsigned long int);
266 else if (cflags == DP_C_LONG_LONG)
267 value = va_arg(args, unsigned long long);
269 value = va_arg(args, unsigned int);
270 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
275 flags |= DP_F_UNSIGNED;
276 if (cflags == DP_C_SHORT)
277 value = va_arg(args, unsigned int);
278 else if (cflags == DP_C_LONG)
279 value = va_arg(args, unsigned long int);
280 else if (cflags == DP_C_LONG_LONG)
281 value = va_arg(args, unsigned long long);
283 value = va_arg(args, unsigned int);
284 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
287 if (cflags == DP_C_LDOUBLE)
288 fvalue = va_arg(args, long double);
290 fvalue = va_arg(args, double);
291 /* um, floating point? */
292 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
297 if (cflags == DP_C_LDOUBLE)
298 fvalue = va_arg(args, long double);
300 fvalue = va_arg(args, double);
305 if (cflags == DP_C_LDOUBLE)
306 fvalue = va_arg(args, long double);
308 fvalue = va_arg(args, double);
311 dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
314 strvalue = va_arg(args, char *);
316 max = maxlen; /* ie, no max */
317 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
320 strvalue = va_arg(args, void *);
321 fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
324 if (cflags == DP_C_SHORT) {
326 num = va_arg(args, short int *);
328 } else if (cflags == DP_C_LONG) {
330 num = va_arg(args, long int *);
332 } else if (cflags == DP_C_LONG_LONG) {
334 num = va_arg(args, long long *);
338 num = va_arg(args, int *);
343 dopr_outch(buffer, &currlen, maxlen, ch);
345 case 'w': /* not supported yet, treat as next char */
348 default: /* Unknown, skip */
352 state = DP_S_DEFAULT;
353 flags = cflags = min = 0;
359 break; /* some picky compilers need this */
362 if (currlen < maxlen - 1)
363 buffer[currlen] = '\0';
365 buffer[maxlen - 1] = '\0';
369 fmtstr(char *buffer, size_t *currlen, size_t maxlen,
370 char *value, int flags, int min, int max)
372 int padlen, strln; /* amount to pad */
378 for (strln = 0; value[strln]; ++strln); /* strlen */
379 padlen = min - strln;
382 if (flags & DP_F_MINUS)
383 padlen = -padlen; /* Left Justify */
385 while ((padlen > 0) && (cnt < max)) {
386 dopr_outch(buffer, currlen, maxlen, ' ');
390 while (*value && (cnt < max)) {
391 dopr_outch(buffer, currlen, maxlen, *value++);
394 while ((padlen < 0) && (cnt < max)) {
395 dopr_outch(buffer, currlen, maxlen, ' ');
401 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
404 fmtint(char *buffer, size_t *currlen, size_t maxlen,
405 long value, int base, int min, int max, int flags)
407 unsigned long uvalue;
411 int spadlen = 0; /* amount to space pad */
412 int zpadlen = 0; /* amount to zero pad */
420 if (!(flags & DP_F_UNSIGNED)) {
424 } else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
426 else if (flags & DP_F_SPACE)
431 caps = 1; /* Should characters be upper case? */
435 (caps? "0123456789ABCDEF":"0123456789abcdef")
436 [uvalue % (unsigned)base];
437 uvalue = (uvalue / (unsigned)base );
438 } while (uvalue && (place < 20));
443 zpadlen = max - place;
444 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
449 if (flags & DP_F_ZERO) {
450 zpadlen = MAX(zpadlen, spadlen);
453 if (flags & DP_F_MINUS)
454 spadlen = -spadlen; /* Left Justifty */
458 while (spadlen > 0) {
459 dopr_outch(buffer, currlen, maxlen, ' ');
465 dopr_outch(buffer, currlen, maxlen, signvalue);
469 while (zpadlen > 0) {
470 dopr_outch(buffer, currlen, maxlen, '0');
477 dopr_outch(buffer, currlen, maxlen, convert[--place]);
479 /* Left Justified spaces */
480 while (spadlen < 0) {
481 dopr_outch (buffer, currlen, maxlen, ' ');
489 long double result = 1;
500 round(long double value)
502 long intpart = value;
512 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
513 int min, int max, int flags)
520 int padlen = 0; /* amount to pad */
528 * AIX manpage says the default is 0, but Solaris says the default
529 * is 6, and sprintf on AIX defaults to 6
534 ufvalue = abs_val(fvalue);
538 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
540 else if (flags & DP_F_SPACE)
546 * Sorry, we only support 9 digits past the decimal because of our
552 /* We "cheat" by converting the fractional part to integer by
553 * multiplying by a factor of 10
555 fracpart = round((pow10 (max)) * (ufvalue - intpart));
557 if (fracpart >= pow10 (max)) {
559 fracpart -= pow10 (max);
562 /* Convert integer part */
565 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
566 intpart = (intpart / 10);
567 } while(intpart && (iplace < 20));
570 iconvert[iplace] = 0;
572 /* Convert fractional part */
575 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
576 fracpart = (fracpart / 10);
577 } while(fracpart && (fplace < 20));
580 fconvert[fplace] = 0;
582 /* -1 for decimal point, another -1 if we are printing a sign */
583 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
584 zpadlen = max - fplace;
589 if (flags & DP_F_MINUS)
590 padlen = -padlen; /* Left Justifty */
592 if ((flags & DP_F_ZERO) && (padlen > 0)) {
594 dopr_outch(buffer, currlen, maxlen, signvalue);
599 dopr_outch(buffer, currlen, maxlen, '0');
604 dopr_outch(buffer, currlen, maxlen, ' ');
608 dopr_outch(buffer, currlen, maxlen, signvalue);
611 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
614 * Decimal point. This should probably use locale to find the correct
617 dopr_outch(buffer, currlen, maxlen, '.');
620 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
622 while (zpadlen > 0) {
623 dopr_outch(buffer, currlen, maxlen, '0');
628 dopr_outch(buffer, currlen, maxlen, ' ');
634 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
636 if (*currlen < maxlen)
637 buffer[(*currlen)++] = c;
639 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
641 #ifndef HAVE_VSNPRINTF
643 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
646 dopr(str, count, fmt, args);
650 #endif /* !HAVE_VSNPRINTF */
652 #ifndef HAVE_SNPRINTF
654 snprintf(char *str,size_t count,const char *fmt,...)
659 (void) vsnprintf(str, count, fmt, ap);
669 #define LONG_STRING 1024
670 char buf1[LONG_STRING];
671 char buf2[LONG_STRING];
713 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
718 printf("Testing snprintf format codes against system sprintf...\n");
720 for (x = 0; fp_fmt[x] != NULL ; x++) {
721 for (y = 0; fp_nums[y] != 0 ; y++) {
722 snprintf(buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
723 sprintf (buf2, fp_fmt[x], fp_nums[y]);
724 if (strcmp (buf1, buf2)) {
725 printf("snprintf doesn't match Format: %s\n\t"
726 "snprintf = %s\n\tsprintf = %s\n",
727 fp_fmt[x], buf1, buf2);
733 for (x = 0; int_fmt[x] != NULL ; x++) {
734 for (y = 0; int_nums[y] != 0 ; y++) {
735 snprintf(buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
736 sprintf(buf2, int_fmt[x], int_nums[y]);
737 if (strcmp (buf1, buf2)) {
738 printf("snprintf doesn't match Format: %s\n\t"
739 "snprintf = %s\n\tsprintf = %s\n",
740 int_fmt[x], buf1, buf2);
746 printf("%d tests failed out of %d.\n", fail, num);
749 #endif /* SNPRINTF_TEST */
751 #endif /* !HAVE_SNPRINTF */