]>
jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/snprintf.c
3 * Modified and hacked into libratbox by Aaron Sethman <androsyn@ratbox.org>
4 * The original headers are below..
5 * Note that this implementation does not process floating point numbers so
6 * you will likely need to fall back to using sprintf yourself to do those...
7 * $Id: snprintf.c 25038 2008-01-23 16:03:08Z androsyn $
11 * linux/lib/vsprintf.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
16 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
18 * Wirzenius wrote this portably, Torvalds fucked it up :-)
22 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
23 * - changed to provide snprintf and vsnprintf functions
24 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
25 * - scnprintf and vscnprintf
27 #include <libratbox_config.h>
28 #include <ratbox_lib.h>
30 static int skip_atoi(const char **s
)
35 i
= i
*10 + *((*s
)++) - '0';
39 /* Decimal conversion is by far the most typical, and is used
40 * for /proc and /sys data. This directly impacts e.g. top performance
41 * with many processes running. We optimize it for speed
43 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
44 * (with permission from the author, Douglas W. Jones). */
46 /* Formats correctly any integer in [0,99999].
47 * Outputs from one to five digits depending on input.
48 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
49 static char* put_dec_trunc(char *buf
, unsigned q
)
51 unsigned d3
, d2
, d1
, d0
;
56 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
57 q
= (d0
* 0xcd) >> 11;
59 *buf
++ = d0
+ '0'; /* least significant digit */
60 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
62 q
= (d1
* 0xcd) >> 11;
64 *buf
++ = d1
+ '0'; /* next digit */
67 if ((d2
!= 0) || (d3
!= 0)) {
70 *buf
++ = d2
+ '0'; /* next digit */
74 q
= (d3
* 0xcd) >> 11;
76 *buf
++ = d3
+ '0'; /* next digit */
78 *buf
++ = q
+ '0'; /* most sign. digit */
84 /* Same with if's removed. Always emits five digits */
85 static char* put_dec_full(char *buf
, unsigned q
)
87 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
88 /* but anyway, gcc produces better code with full-sized ints */
89 unsigned d3
, d2
, d1
, d0
;
94 /* Possible ways to approx. divide by 10 */
95 /* gcc -O2 replaces multiply with shifts and adds */
96 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
97 // (x * 0x67) >> 10: 1100111
98 // (x * 0x34) >> 9: 110100 - same
99 // (x * 0x1a) >> 8: 11010 - same
100 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
102 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
103 q
= (d0
* 0xcd) >> 11;
106 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
107 q
= (d1
* 0xcd) >> 11;
117 q
= (d3
* 0xcd) >> 11; /* - shorter code */
118 /* q = (d3 * 0x67) >> 10; - would also work */
125 static char* put_dec(char *buf
, unsigned long long int num
)
130 return put_dec_trunc(buf
, num
);
133 buf
= put_dec_full(buf
, rem
);
137 #define ZEROPAD 1 /* pad with zero */
138 #define SIGN 2 /* unsigned/signed long */
139 #define PLUS 4 /* show plus */
140 #define SPACE 8 /* space if plus */
141 #define LEFT 16 /* left justified */
142 #define SPECIAL 32 /* 0x */
143 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
145 static char *number(char *buf
, char *end
, unsigned long long int num
, int base
, int size
, int precision
, int type
)
149 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
150 static const char small_digits
[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
151 static const char large_digits
[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
152 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
155 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
158 if (base
< 2 || base
> 36)
162 if ((signed long long int) num
< 0) {
164 num
= - (signed long long int) num
;
166 } else if (type
& PLUS
) {
169 } else if (type
& SPACE
) {
180 /* generate full string in tmp[], in reverse order */
184 /* Generic code, for any base:
186 tmp[i++] = digits[do_div(num,base)];
189 else if (base
!= 10) { /* 8 or 16 */
192 if (base
== 16) shift
= 4;
194 tmp
[i
++] = digits
[((unsigned char)num
) & mask
];
197 } else { /* base 10 */
198 i
= put_dec(tmp
, num
) - tmp
;
201 /* printing 100 using %2d gives "100", not "00" */
204 /* leading space padding */
206 if (!(type
& (ZEROPAD
+LEFT
))) {
219 /* "0x" / "0" prefix */
226 *buf
= digits
[16]; /* for arbitrary base: digits[33]; */
230 /* zero or space padding */
231 if (!(type
& LEFT
)) {
232 char c
= (type
& ZEROPAD
) ? '0' : ' ';
233 while (--size
>= 0) {
239 /* hmm even more zero padding? */
240 while (i
<= --precision
) {
245 /* actual digits of result */
251 /* trailing space padding */
252 while (--size
>= 0) {
261 * vsnprintf - Format a string and place it in a buffer
262 * @buf: The buffer to place the result into
263 * @size: The size of the buffer, including the trailing null space
264 * @fmt: The format string to use
265 * @args: Arguments for the format string
267 * The return value is the number of characters which would
268 * be generated for the given input, excluding the trailing
269 * '\0', as per ISO C99. If you want to have the exact
270 * number of characters written into @buf as return value
271 * (not including the trailing '\0'), use vscnprintf(). If the
272 * return is greater than or equal to @size, the resulting
273 * string is truncated.
275 * Call this function if you are already dealing with a va_list.
276 * You probably want snprintf() instead.
278 int rb_vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
281 unsigned long long int num
;
286 int flags
; /* flags to number() */
288 int field_width
; /* width of output field */
289 int precision
; /* min. # of digits for integers; max
290 number of chars for from string */
291 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
292 /* 'z' support added 23/7/1999 S.H. */
293 /* 'z' changed to 'Z' --davidm 1/25/99 */
294 /* 't' added for ptrdiff_t */
296 /* Reject out-of-range values early. Large positive sizes are
297 used for unknown buffer sizes. */
298 if (unlikely((int) size
< 0)) {
305 /* Make sure end is always >= buf */
311 for (; *fmt
; ++fmt
) {
322 ++fmt
; /* this also skips first '%' */
324 case '-': flags
|= LEFT
; goto repeat
;
325 case '+': flags
|= PLUS
; goto repeat
;
326 case ' ': flags
|= SPACE
; goto repeat
;
327 case '#': flags
|= SPECIAL
; goto repeat
;
328 case '0': flags
|= ZEROPAD
; goto repeat
;
331 /* get field width */
334 field_width
= skip_atoi(&fmt
);
335 else if (*fmt
== '*') {
337 /* it's the next argument */
338 field_width
= va_arg(args
, int);
339 if (field_width
< 0) {
340 field_width
= -field_width
;
345 /* get the precision */
350 precision
= skip_atoi(&fmt
);
351 else if (*fmt
== '*') {
353 /* it's the next argument */
354 precision
= va_arg(args
, int);
360 /* get the conversion qualifier */
362 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
363 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
366 if (qualifier
== 'l' && *fmt
== 'l') {
377 if (!(flags
& LEFT
)) {
378 while (--field_width
> 0) {
384 c
= (unsigned char) va_arg(args
, int);
388 while (--field_width
> 0) {
396 s
= va_arg(args
, char *);
398 abort(); /* prefer blowing up vs corrupt data */
400 len
= rb_strnlen(s
, precision
);
402 if (!(flags
& LEFT
)) {
403 while (len
< field_width
--) {
409 for (i
= 0; i
< len
; ++i
) {
414 while (len
< field_width
--) {
422 if (field_width
== -1) {
423 field_width
= 2*sizeof(void *);
426 str
= number(str
, end
,
427 (unsigned long) va_arg(args
, void *),
428 16, field_width
, precision
, flags
);
434 * What does C99 say about the overflow case here? */
435 if (qualifier
== 'l') {
436 long * ip
= va_arg(args
, long *);
438 } else if (qualifier
== 'Z' || qualifier
== 'z') {
439 size_t * ip
= va_arg(args
, size_t *);
442 int * ip
= va_arg(args
, int *);
453 /* integer number formats - set up the flags and "break" */
483 if (qualifier
== 'L')
484 num
= va_arg(args
, long long int);
485 else if (qualifier
== 'l') {
486 num
= va_arg(args
, unsigned long);
488 num
= (signed long) num
;
489 } else if (qualifier
== 'Z' || qualifier
== 'z') {
490 num
= va_arg(args
, size_t);
491 } else if (qualifier
== 't') {
492 num
= va_arg(args
, ptrdiff_t);
493 } else if (qualifier
== 'h') {
494 num
= (unsigned short) va_arg(args
, int);
496 num
= (signed short) num
;
498 num
= va_arg(args
, unsigned int);
500 num
= (signed int) num
;
502 str
= number(str
, end
, num
, base
,
503 field_width
, precision
, flags
);
511 /* the trailing null byte doesn't count towards the total */
516 * snprintf - Format a string and place it in a buffer
517 * @buf: The buffer to place the result into
518 * @size: The size of the buffer, including the trailing null space
519 * @fmt: The format string to use
520 * @...: Arguments for the format string
522 * The return value is the number of characters which would be
523 * generated for the given input, excluding the trailing null,
524 * as per ISO C99. If the return is greater than or equal to
525 * @size, the resulting string is truncated.
527 int rb_snprintf(char * buf
, size_t size
, const char *fmt
, ...)
533 i
=rb_vsnprintf(buf
,size
,fmt
,args
);
539 * vsprintf - Format a string and place it in a buffer
540 * @buf: The buffer to place the result into
541 * @fmt: The format string to use
542 * @args: Arguments for the format string
544 * The function returns the number of characters written
545 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
548 * Call this function if you are already dealing with a va_list.
549 * You probably want sprintf() instead.
551 int rb_vsprintf(char *buf
, const char *fmt
, va_list args
)
553 return rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
557 * sprintf - Format a string and place it in a buffer
558 * @buf: The buffer to place the result into
559 * @fmt: The format string to use
560 * @...: Arguments for the format string
562 * The function returns the number of characters written
563 * into @buf. Use snprintf() or scnprintf() in order to avoid
566 int rb_sprintf(char * buf
, const char *fmt
, ...)
572 i
=rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
578 * rb_vsprintf_append()
579 * appends sprintf formatted string to the end of the buffer
583 rb_vsprintf_append(char *str
, const char *format
, va_list ap
)
585 size_t x
= strlen(str
);
586 return(rb_vsprintf(str
+x
, format
, ap
) + x
);
590 * rb_sprintf_append()
591 * appends sprintf formatted string to the end of the buffer
594 rb_sprintf_append(char *str
, const char *format
, ...)
598 va_start(ap
, format
);
599 x
= rb_vsprintf_append(str
, format
, ap
);
605 * rb_vsnprintf_append()
606 * appends sprintf formatted string to the end of the buffer but not
611 rb_vsnprintf_append(char *str
, size_t len
, const char *format
, va_list ap
)
613 size_t x
= strlen(str
);
614 return(rb_vsnprintf(str
+x
, len
- x
, format
, ap
) + x
);
618 * rb_snprintf_append()
619 * appends snprintf formatted string to the end of the buffer but not
624 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
628 va_start(ap
, format
);
629 x
= rb_vsnprintf_append(str
, len
, format
, ap
);