]>
Dogcows Code - chaz/openbox/blob - src/bsd-snprintf.c
1 /**************************************************************
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...
8 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
11 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
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.
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.
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.
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.
41 **************************************************************/
45 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
49 #include <sys/types.h>
51 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
53 #define HAVE_VARARGS_H
55 /* varargs declarations: */
57 #if defined(HAVE_STDARG_H)
59 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
60 # define VA_LOCAL_DECL va_list ap
61 # define VA_START(f) va_start(ap, f)
62 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
63 # define VA_END va_end(ap)
65 # if defined(HAVE_VARARGS_H)
68 # define VA_LOCAL_DECL va_list ap
69 # define VA_START(f) va_start(ap) /* f is ignored! */
70 # define VA_SHIFT(v,t) v = va_arg(ap,t)
71 # define VA_END va_end(ap)
73 /*XX ** NO VARARGS ** XX*/
77 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
78 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
80 static void dopr (char *buffer
, size_t maxlen
, const char *format
,
82 static void fmtstr (char *buffer
, size_t *currlen
, size_t maxlen
,
83 char *value
, int flags
, int min
, int max
);
84 static void fmtint (char *buffer
, size_t *currlen
, size_t maxlen
,
85 long value
, int base
, int min
, int max
, int flags
);
86 static void fmtfp (char *buffer
, size_t *currlen
, size_t maxlen
,
87 long double fvalue
, int min
, int max
, int flags
);
88 static void dopr_outch (char *buffer
, size_t *currlen
, size_t maxlen
, char c
);
91 * dopr(): poor man's version of doprintf
94 /* format read states */
95 #define DP_S_DEFAULT 0
104 /* format flags - Bits */
105 #define DP_F_MINUS (1 << 0)
106 #define DP_F_PLUS (1 << 1)
107 #define DP_F_SPACE (1 << 2)
108 #define DP_F_NUM (1 << 3)
109 #define DP_F_ZERO (1 << 4)
110 #define DP_F_UP (1 << 5)
111 #define DP_F_UNSIGNED (1 << 6)
113 /* Conversion Flags */
116 #define DP_C_LDOUBLE 3
118 #define char_to_int(p) (p - '0')
119 #define MAX(p,q) ((p >= q) ? p : q)
121 static void dopr (char *buffer
, size_t maxlen
, const char *format
, va_list args
)
134 state
= DP_S_DEFAULT
;
135 currlen
= flags
= cflags
= min
= 0;
139 while (state
!= DP_S_DONE
)
141 if ((ch
== '\0') || (currlen
>= maxlen
))
150 dopr_outch (buffer
, &currlen
, maxlen
, ch
);
182 if (isdigit((unsigned char)ch
))
184 min
= 10*min
+ char_to_int (ch
);
189 min
= va_arg (args
, int);
206 if (isdigit((unsigned char)ch
))
210 max
= 10*max
+ char_to_int (ch
);
215 max
= va_arg (args
, int);
223 /* Currently, we don't support Long Long, bummer */
235 cflags
= DP_C_LDOUBLE
;
248 if (cflags
== DP_C_SHORT
)
249 value
= va_arg (args
, short int);
250 else if (cflags
== DP_C_LONG
)
251 value
= va_arg (args
, long int);
253 value
= va_arg (args
, int);
254 fmtint (buffer
, &currlen
, maxlen
, value
, 10, min
, max
, flags
);
257 flags
|= DP_F_UNSIGNED
;
258 if (cflags
== DP_C_SHORT
)
259 value
= va_arg (args
, unsigned short int);
260 else if (cflags
== DP_C_LONG
)
261 value
= va_arg (args
, unsigned long int);
263 value
= va_arg (args
, unsigned int);
264 fmtint (buffer
, &currlen
, maxlen
, value
, 8, min
, max
, flags
);
267 flags
|= DP_F_UNSIGNED
;
268 if (cflags
== DP_C_SHORT
)
269 value
= va_arg (args
, unsigned short int);
270 else if (cflags
== DP_C_LONG
)
271 value
= va_arg (args
, unsigned long int);
273 value
= va_arg (args
, unsigned int);
274 fmtint (buffer
, &currlen
, maxlen
, value
, 10, min
, max
, flags
);
279 flags
|= DP_F_UNSIGNED
;
280 if (cflags
== DP_C_SHORT
)
281 value
= va_arg (args
, unsigned short int);
282 else if (cflags
== DP_C_LONG
)
283 value
= va_arg (args
, unsigned long int);
285 value
= va_arg (args
, unsigned int);
286 fmtint (buffer
, &currlen
, maxlen
, value
, 16, min
, max
, flags
);
289 if (cflags
== DP_C_LDOUBLE
)
290 fvalue
= va_arg (args
, long double);
292 fvalue
= va_arg (args
, double);
293 /* um, floating point? */
294 fmtfp (buffer
, &currlen
, maxlen
, fvalue
, min
, max
, flags
);
299 if (cflags
== DP_C_LDOUBLE
)
300 fvalue
= va_arg (args
, long double);
302 fvalue
= va_arg (args
, double);
307 if (cflags
== DP_C_LDOUBLE
)
308 fvalue
= va_arg (args
, long double);
310 fvalue
= va_arg (args
, double);
313 dopr_outch (buffer
, &currlen
, maxlen
, va_arg (args
, int));
316 strvalue
= va_arg (args
, char *);
318 max
= maxlen
; /* ie, no max */
319 fmtstr (buffer
, &currlen
, maxlen
, strvalue
, flags
, min
, max
);
322 strvalue
= va_arg (args
, void *);
323 fmtint (buffer
, &currlen
, maxlen
, (long) strvalue
, 16, min
, max
, flags
);
326 if (cflags
== DP_C_SHORT
)
329 num
= va_arg (args
, short int *);
332 else if (cflags
== DP_C_LONG
)
335 num
= va_arg (args
, long int *);
341 num
= va_arg (args
, int *);
346 dopr_outch (buffer
, &currlen
, maxlen
, ch
);
349 /* not supported yet, treat as next char */
357 state
= DP_S_DEFAULT
;
358 flags
= cflags
= min
= 0;
365 break; /* some picky compilers need this */
368 if (currlen
< maxlen
- 1)
369 buffer
[currlen
] = '\0';
371 buffer
[maxlen
- 1] = '\0';
374 static void fmtstr (char *buffer
, size_t *currlen
, size_t maxlen
,
375 char *value
, int flags
, int min
, int max
)
377 int padlen
, strln
; /* amount to pad */
385 for (strln
= 0; value
[strln
]; ++strln
); /* strlen */
386 padlen
= min
- strln
;
389 if (flags
& DP_F_MINUS
)
390 padlen
= -padlen
; /* Left Justify */
392 while ((padlen
> 0) && (cnt
< max
))
394 dopr_outch (buffer
, currlen
, maxlen
, ' ');
398 while (*value
&& (cnt
< max
))
400 dopr_outch (buffer
, currlen
, maxlen
, *value
++);
403 while ((padlen
< 0) && (cnt
< max
))
405 dopr_outch (buffer
, currlen
, maxlen
, ' ');
411 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
413 static void fmtint (char *buffer
, size_t *currlen
, size_t maxlen
,
414 long value
, int base
, int min
, int max
, int flags
)
417 unsigned long uvalue
;
420 int spadlen
= 0; /* amount to space pad */
421 int zpadlen
= 0; /* amount to zero pad */
429 if(!(flags
& DP_F_UNSIGNED
))
436 if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
439 if (flags
& DP_F_SPACE
)
443 if (flags
& DP_F_UP
) caps
= 1; /* Should characters be upper case? */
447 (caps
? "0123456789ABCDEF":"0123456789abcdef")
448 [uvalue
% (unsigned)base
];
449 uvalue
= (uvalue
/ (unsigned)base
);
450 } while(uvalue
&& (place
< 20));
451 if (place
== 20) place
--;
454 zpadlen
= max
- place
;
455 spadlen
= min
- MAX (max
, place
) - (signvalue
? 1 : 0);
456 if (zpadlen
< 0) zpadlen
= 0;
457 if (spadlen
< 0) spadlen
= 0;
458 if (flags
& DP_F_ZERO
)
460 zpadlen
= MAX(zpadlen
, spadlen
);
463 if (flags
& DP_F_MINUS
)
464 spadlen
= -spadlen
; /* Left Justifty */
466 #ifdef DEBUG_SNPRINTF
467 dprint (1, (debugfile
, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
468 zpadlen
, spadlen
, min
, max
, place
));
474 dopr_outch (buffer
, currlen
, maxlen
, ' ');
480 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
487 dopr_outch (buffer
, currlen
, maxlen
, '0');
494 dopr_outch (buffer
, currlen
, maxlen
, convert
[--place
]);
496 /* Left Justified spaces */
497 while (spadlen
< 0) {
498 dopr_outch (buffer
, currlen
, maxlen
, ' ');
503 static long double abs_val (long double value
)
505 long double result
= value
;
513 static long double pow10 (int exp
)
515 long double result
= 1;
526 static long round (long double value
)
531 value
= value
- intpart
;
538 static void fmtfp (char *buffer
, size_t *currlen
, size_t maxlen
,
539 long double fvalue
, int min
, int max
, int flags
)
547 int padlen
= 0; /* amount to pad */
554 * AIX manpage says the default is 0, but Solaris says the default
555 * is 6, and sprintf on AIX defaults to 6
560 ufvalue
= abs_val (fvalue
);
565 if (flags
& DP_F_PLUS
) /* Do a sign (+/i) */
568 if (flags
& DP_F_SPACE
)
572 if (flags
& DP_F_UP
) caps
= 1; /* Should characters be upper case? */
578 * Sorry, we only support 9 digits past the decimal because of our
584 /* We "cheat" by converting the fractional part to integer by
585 * multiplying by a factor of 10
587 fracpart
= round ((pow10 (max
)) * (ufvalue
- intpart
));
589 if (fracpart
>= pow10 (max
))
592 fracpart
-= pow10 (max
);
595 #ifdef DEBUG_SNPRINTF
596 dprint (1, (debugfile
, "fmtfp: %f =? %d.%d\n", fvalue
, intpart
, fracpart
));
599 /* Convert integer part */
602 (caps
? "0123456789ABCDEF":"0123456789abcdef")[intpart
% 10];
603 intpart
= (intpart
/ 10);
604 } while(intpart
&& (iplace
< 20));
605 if (iplace
== 20) iplace
--;
606 iconvert
[iplace
] = 0;
608 /* Convert fractional part */
611 (caps
? "0123456789ABCDEF":"0123456789abcdef")[fracpart
% 10];
612 fracpart
= (fracpart
/ 10);
613 } while(fracpart
&& (fplace
< 20));
614 if (fplace
== 20) fplace
--;
615 fconvert
[fplace
] = 0;
617 /* -1 for decimal point, another -1 if we are printing a sign */
618 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
619 zpadlen
= max
- fplace
;
624 if (flags
& DP_F_MINUS
)
625 padlen
= -padlen
; /* Left Justifty */
627 if ((flags
& DP_F_ZERO
) && (padlen
> 0))
631 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
637 dopr_outch (buffer
, currlen
, maxlen
, '0');
643 dopr_outch (buffer
, currlen
, maxlen
, ' ');
647 dopr_outch (buffer
, currlen
, maxlen
, signvalue
);
650 dopr_outch (buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
653 * Decimal point. This should probably use locale to find the correct
656 dopr_outch (buffer
, currlen
, maxlen
, '.');
659 dopr_outch (buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
663 dopr_outch (buffer
, currlen
, maxlen
, '0');
669 dopr_outch (buffer
, currlen
, maxlen
, ' ');
674 static void dopr_outch (char *buffer
, size_t *currlen
, size_t maxlen
, char c
)
676 if (*currlen
< maxlen
)
677 buffer
[(*currlen
)++] = c
;
679 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
681 #ifndef HAVE_VSNPRINTF
682 int vsnprintf (char *str
, size_t count
, const char *fmt
, va_list args
)
685 dopr(str
, count
, fmt
, args
);
688 #endif /* !HAVE_VSNPRINTF */
690 #ifndef HAVE_SNPRINTF
693 int snprintf (char *str
,size_t count
,const char *fmt
,...)
695 int snprintf (va_alist
) va_dcl
706 VA_SHIFT (str
, char *);
707 VA_SHIFT (count
, size_t );
708 VA_SHIFT (fmt
, char *);
709 (void) vsnprintf(str
, count
, fmt
, ap
);
716 #define LONG_STRING 1024
720 char buf1
[LONG_STRING
];
721 char buf2
[LONG_STRING
];
736 double fp_nums
[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
737 0.9996, 1.996, 4.136, 0};
750 long int_nums
[] = { -1, 134, 91340, 341, 0203, 0};
755 printf ("Testing snprintf format codes against system sprintf...\n");
757 for (x
= 0; fp_fmt
[x
] != NULL
; x
++)
758 for (y
= 0; fp_nums
[y
] != 0 ; y
++)
760 snprintf (buf1
, sizeof (buf1
), fp_fmt
[x
], fp_nums
[y
]);
761 sprintf (buf2
, fp_fmt
[x
], fp_nums
[y
]);
762 if (strcmp (buf1
, buf2
))
764 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
765 fp_fmt
[x
], buf1
, buf2
);
771 for (x
= 0; int_fmt
[x
] != NULL
; x
++)
772 for (y
= 0; int_nums
[y
] != 0 ; y
++)
774 snprintf (buf1
, sizeof (buf1
), int_fmt
[x
], int_nums
[y
]);
775 sprintf (buf2
, int_fmt
[x
], int_nums
[y
]);
776 if (strcmp (buf1
, buf2
))
778 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
779 int_fmt
[x
], buf1
, buf2
);
784 printf ("%d tests failed out of %d.\n", fail
, num
);
786 #endif /* SNPRINTF_TEST */
788 #endif /* !HAVE_SNPRINTF */
This page took 0.073085 seconds and 4 git commands to generate.