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