2 * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 struct snprintf_state
{
58 unsigned char *theend
;
61 void (*append_char
)(struct snprintf_state
*, unsigned char);
65 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
67 sn_reserve (struct snprintf_state
*state
, size_t n
)
69 return state
->s
+ n
> state
->theend
;
73 sn_append_char (struct snprintf_state
*state
, unsigned char c
)
75 if (!sn_reserve (state
, 1))
81 as_reserve (struct snprintf_state
*state
, size_t n
)
83 if (state
->s
+ n
> state
->theend
) {
84 int off
= state
->s
- state
->str
;
87 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
90 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
92 state
->sz
= min(state
->sz
, state
->max_sz
);
93 tmp
= realloc (state
->str
, state
->sz
);
97 state
->s
= state
->str
+ off
;
98 state
->theend
= state
->str
+ state
->sz
- 1;
104 as_append_char (struct snprintf_state
*state
, unsigned char c
)
106 if(!as_reserve (state
, 1))
110 /* longest integer types */
112 #ifdef HAVE_LONG_LONG
113 typedef unsigned long long u_longest
;
114 typedef long long longest
;
116 typedef unsigned long u_longest
;
117 typedef long longest
;
123 pad(struct snprintf_state
*state
, int width
, char c
)
127 (*state
->append_char
)(state
, c
);
133 /* return true if we should use alternatve hex form */
135 use_alternative (int flags
, u_longest num
, unsigned base
)
137 return (flags
& alternate_flag
) && base
== 16 && num
!= 0;
141 append_number(struct snprintf_state
*state
,
142 u_longest num
, unsigned base
, const char *rep
,
143 int width
, int prec
, int flags
, int minusp
)
147 char nstr
[64]; /* enough for <192 bit octal integers */
151 /* given precision, ignore zero flag */
157 /* format number as string */
158 nstart
= sizeof(nstr
);
160 nstr
[--nstart
] = '\0';
163 nstr
[--nstart
] = rep
[n
% base
];
168 /* zero value with zero precision should produce no digits */
169 if(prec
== 0 && num
== 0) {
174 /* figure out what char to use for sign */
177 else if((flags
& plus_flag
))
179 else if((flags
& space_flag
))
184 if((flags
& alternate_flag
) && base
== 8) {
185 /* if necessary, increase the precision to
186 make first digit a zero */
188 /* XXX C99 claims (regarding # and %o) that "if the value and
189 precision are both 0, a single 0 is printed", but there is
190 no such wording for %x. This would mean that %#.o would
191 output "0", but %#.x "". This does not make sense, and is
192 also not what other printf implementations are doing. */
194 if(prec
<= nlen
&& nstr
[nstart
] != '0' && nstr
[nstart
] != '\0')
199 pad | sign | alt | zero | digits
200 sign | alt | zero | digits | pad minus_flag
201 sign | alt | zero | digits zero_flag */
203 /* if not right justifying or padding with zeros, we need to
204 compute the length of the rest of the string, and then pad with
206 if(!(flags
& (minus_flag
| zero_flag
))) {
212 if(use_alternative(flags
, num
, base
))
219 len
+= pad(state
, width
, ' ');
221 if(signchar
!= '\0') {
222 (*state
->append_char
)(state
, signchar
);
225 if(use_alternative(flags
, num
, base
)) {
226 (*state
->append_char
)(state
, '0');
227 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
230 if(flags
& zero_flag
) {
231 /* pad to width with zeros */
232 if(prec
- nlen
> width
- len
- nlen
)
233 len
+= pad(state
, prec
- nlen
, '0');
235 len
+= pad(state
, width
- len
- nlen
, '0');
237 /* pad to prec with zeros */
238 len
+= pad(state
, prec
- nlen
, '0');
240 while(nstr
[nstart
] != '\0') {
241 (*state
->append_char
)(state
, nstr
[nstart
++]);
245 if(flags
& minus_flag
)
246 len
+= pad(state
, width
- len
, ' ');
256 append_string (struct snprintf_state
*state
,
257 const unsigned char *arg
,
265 arg
= (const unsigned char*)"(null)";
270 width
-= strlen((const char *)arg
);
271 if(!(flags
& minus_flag
))
272 len
+= pad(state
, width
, ' ');
275 while (*arg
&& prec
--) {
276 (*state
->append_char
) (state
, *arg
++);
281 (*state
->append_char
) (state
, *arg
++);
285 if(flags
& minus_flag
)
286 len
+= pad(state
, width
, ' ');
291 append_char(struct snprintf_state
*state
,
298 while(!(flags
& minus_flag
) && --width
> 0) {
299 (*state
->append_char
) (state
, ' ') ;
302 (*state
->append_char
) (state
, arg
);
304 while((flags
& minus_flag
) && --width
> 0) {
305 (*state
->append_char
) (state
, ' ');
312 * This can't be made into a function...
315 #ifdef HAVE_LONG_LONG
317 #define PARSE_INT_FORMAT(res, arg, unsig) \
318 if (long_long_flag) \
319 res = (unsig long long)va_arg(arg, unsig long long); \
320 else if (long_flag) \
321 res = (unsig long)va_arg(arg, unsig long); \
322 else if (size_t_flag) \
323 res = (unsig long)va_arg(arg, size_t); \
324 else if (short_flag) \
325 res = (unsig short)va_arg(arg, unsig int); \
327 res = (unsig int)va_arg(arg, unsig int)
331 #define PARSE_INT_FORMAT(res, arg, unsig) \
333 res = (unsig long)va_arg(arg, unsig long); \
334 else if (size_t_flag) \
335 res = (unsig long)va_arg(arg, size_t); \
336 else if (short_flag) \
337 res = (unsig short)va_arg(arg, unsig int); \
339 res = (unsig int)va_arg(arg, unsig int)
344 * zyxprintf - return length, as snprintf
348 xyzprintf (struct snprintf_state
*state
, const char *char_format
, va_list ap
)
350 const unsigned char *format
= (const unsigned char *)char_format
;
354 while((c
= *format
++)) {
360 int long_long_flag
= 0;
365 while((c
= *format
++)){
373 flags
|= alternate_flag
;
382 if((flags
& space_flag
) && (flags
& plus_flag
))
385 if((flags
& minus_flag
) && (flags
& zero_flag
))
391 width
= width
* 10 + c
- '0';
395 width
= va_arg(ap
, int);
405 prec
= prec
* 10 + c
- '0';
409 prec
= va_arg(ap
, int);
419 } else if (c
== 'z') {
422 } else if (c
== 'l') {
431 if(c
!= 'd' && c
!= 'i')
432 flags
&= ~(plus_flag
| space_flag
);
436 append_char(state
, va_arg(ap
, int), width
, flags
);
440 len
+= append_string(state
,
441 va_arg(ap
, unsigned char*),
452 PARSE_INT_FORMAT(arg
, ap
, signed);
460 len
+= append_number (state
, num
, 10, "0123456789",
461 width
, prec
, flags
, minusp
);
467 PARSE_INT_FORMAT(arg
, ap
, unsigned);
469 len
+= append_number (state
, arg
, 10, "0123456789",
470 width
, prec
, flags
, 0);
476 PARSE_INT_FORMAT(arg
, ap
, unsigned);
478 len
+= append_number (state
, arg
, 010, "01234567",
479 width
, prec
, flags
, 0);
485 PARSE_INT_FORMAT(arg
, ap
, unsigned);
487 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
488 width
, prec
, flags
, 0);
494 PARSE_INT_FORMAT(arg
, ap
, unsigned);
496 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
497 width
, prec
, flags
, 0);
501 u_longest arg
= (u_longest
)va_arg(ap
, void*);
503 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
504 width
, prec
, flags
, 0);
508 int *arg
= va_arg(ap
, int*);
509 *arg
= state
->s
- state
->str
;
516 (*state
->append_char
)(state
, c
);
520 (*state
->append_char
)(state
, '%');
521 (*state
->append_char
)(state
, c
);
526 (*state
->append_char
) (state
, c
);
533 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
534 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
535 rk_snprintf (char *str
, size_t sz
, const char *format
, ...)
540 va_start(args
, format
);
541 ret
= vsnprintf (str
, sz
, format
, args
);
553 va_start(args
, format
);
554 ret2
= vsprintf (tmp
, format
, args
);
556 if (ret
!= ret2
|| strcmp(str
, tmp
))
566 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
567 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
568 rk_asprintf (char **ret
, const char *format
, ...)
573 va_start(args
, format
);
574 val
= vasprintf (ret
, format
, args
);
581 tmp
= malloc (val
+ 1);
585 va_start(args
, format
);
586 ret2
= vsprintf (tmp
, format
, args
);
588 if (val
!= ret2
|| strcmp(*ret
, tmp
))
598 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
599 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
600 rk_asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
605 va_start(args
, format
);
606 val
= vasnprintf (ret
, max_sz
, format
, args
);
612 tmp
= malloc (val
+ 1);
616 ret2
= vsprintf (tmp
, format
, args
);
617 if (val
!= ret2
|| strcmp(*ret
, tmp
))
628 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
629 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
630 rk_vasprintf (char **ret
, const char *format
, va_list args
)
632 return vasnprintf (ret
, 0, format
, args
);
637 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
638 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
639 rk_vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
642 struct snprintf_state state
;
644 state
.max_sz
= max_sz
;
646 state
.str
= malloc(state
.sz
);
647 if (state
.str
== NULL
) {
652 state
.theend
= state
.s
+ state
.sz
- 1;
653 state
.append_char
= as_append_char
;
655 st
= xyzprintf (&state
, format
, args
);
664 tmp
= realloc (state
.str
, st
+1);
676 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
677 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
678 rk_vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
680 struct snprintf_state state
;
682 unsigned char *ustr
= (unsigned char *)str
;
688 state
.theend
= ustr
+ sz
- (sz
> 0);
689 state
.append_char
= sn_append_char
;
691 ret
= xyzprintf (&state
, format
, args
);
692 if (state
.s
!= NULL
&& sz
!= 0)