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
36 __RCSID("$Heimdal: snprintf.c 21005 2007-06-08 01:54:35Z lha $"
39 #if defined(TEST_SNPRINTF)
40 #include "snprintf-test.h"
41 #endif /* TEST_SNPRINTF */
62 struct snprintf_state
{
65 unsigned char *theend
;
68 void (*append_char
)(struct snprintf_state
*, unsigned char);
72 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
74 sn_reserve (struct snprintf_state
*state
, size_t n
)
76 return state
->s
+ n
> state
->theend
;
80 sn_append_char (struct snprintf_state
*state
, unsigned char c
)
82 if (!sn_reserve (state
, 1))
88 as_reserve (struct snprintf_state
*state
, size_t n
)
90 if (state
->s
+ n
> state
->theend
) {
91 int off
= state
->s
- state
->str
;
94 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
97 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
99 state
->sz
= min(state
->sz
, state
->max_sz
);
100 tmp
= realloc (state
->str
, state
->sz
);
104 state
->s
= state
->str
+ off
;
105 state
->theend
= state
->str
+ state
->sz
- 1;
111 as_append_char (struct snprintf_state
*state
, unsigned char c
)
113 if(!as_reserve (state
, 1))
117 /* longest integer types */
119 #ifdef HAVE_LONG_LONG
120 typedef unsigned long long u_longest
;
121 typedef long long longest
;
123 typedef unsigned long u_longest
;
124 typedef long longest
;
130 pad(struct snprintf_state
*state
, int width
, char c
)
134 (*state
->append_char
)(state
, c
);
140 /* return true if we should use alternatve hex form */
142 use_alternative (int flags
, u_longest num
, unsigned base
)
144 return (flags
& alternate_flag
) && base
== 16 && num
!= 0;
148 append_number(struct snprintf_state
*state
,
149 u_longest num
, unsigned base
, const char *rep
,
150 int width
, int prec
, int flags
, int minusp
)
154 char nstr
[64]; /* enough for <192 bit octal integers */
158 /* given precision, ignore zero flag */
164 /* format number as string */
165 nstart
= sizeof(nstr
);
167 nstr
[--nstart
] = '\0';
170 nstr
[--nstart
] = rep
[n
% base
];
175 /* zero value with zero precision should produce no digits */
176 if(prec
== 0 && num
== 0) {
181 /* figure out what char to use for sign */
184 else if((flags
& plus_flag
))
186 else if((flags
& space_flag
))
191 if((flags
& alternate_flag
) && base
== 8) {
192 /* if necessary, increase the precision to
193 make first digit a zero */
195 /* XXX C99 claims (regarding # and %o) that "if the value and
196 precision are both 0, a single 0 is printed", but there is
197 no such wording for %x. This would mean that %#.o would
198 output "0", but %#.x "". This does not make sense, and is
199 also not what other printf implementations are doing. */
201 if(prec
<= nlen
&& nstr
[nstart
] != '0' && nstr
[nstart
] != '\0')
206 pad | sign | alt | zero | digits
207 sign | alt | zero | digits | pad minus_flag
208 sign | alt | zero | digits zero_flag */
210 /* if not right justifying or padding with zeros, we need to
211 compute the length of the rest of the string, and then pad with
213 if(!(flags
& (minus_flag
| zero_flag
))) {
219 if(use_alternative(flags
, num
, base
))
226 len
+= pad(state
, width
, ' ');
228 if(signchar
!= '\0') {
229 (*state
->append_char
)(state
, signchar
);
232 if(use_alternative(flags
, num
, base
)) {
233 (*state
->append_char
)(state
, '0');
234 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
237 if(flags
& zero_flag
) {
238 /* pad to width with zeros */
239 if(prec
- nlen
> width
- len
- nlen
)
240 len
+= pad(state
, prec
- nlen
, '0');
242 len
+= pad(state
, width
- len
- nlen
, '0');
244 /* pad to prec with zeros */
245 len
+= pad(state
, prec
- nlen
, '0');
247 while(nstr
[nstart
] != '\0') {
248 (*state
->append_char
)(state
, nstr
[nstart
++]);
252 if(flags
& minus_flag
)
253 len
+= pad(state
, width
- len
, ' ');
263 append_string (struct snprintf_state
*state
,
264 const unsigned char *arg
,
272 arg
= (const unsigned char*)"(null)";
277 width
-= strlen((const char *)arg
);
278 if(!(flags
& minus_flag
))
279 len
+= pad(state
, width
, ' ');
282 while (*arg
&& prec
--) {
283 (*state
->append_char
) (state
, *arg
++);
288 (*state
->append_char
) (state
, *arg
++);
292 if(flags
& minus_flag
)
293 len
+= pad(state
, width
, ' ');
298 append_char(struct snprintf_state
*state
,
305 while(!(flags
& minus_flag
) && --width
> 0) {
306 (*state
->append_char
) (state
, ' ') ;
309 (*state
->append_char
) (state
, arg
);
311 while((flags
& minus_flag
) && --width
> 0) {
312 (*state
->append_char
) (state
, ' ');
319 * This can't be made into a function...
322 #ifdef HAVE_LONG_LONG
324 #define PARSE_INT_FORMAT(res, arg, unsig) \
325 if (long_long_flag) \
326 res = (unsig long long)va_arg(arg, unsig long long); \
327 else if (long_flag) \
328 res = (unsig long)va_arg(arg, unsig long); \
329 else if (size_t_flag) \
330 res = (unsig long)va_arg(arg, size_t); \
331 else if (short_flag) \
332 res = (unsig short)va_arg(arg, unsig int); \
334 res = (unsig int)va_arg(arg, unsig int)
338 #define PARSE_INT_FORMAT(res, arg, unsig) \
340 res = (unsig long)va_arg(arg, unsig long); \
341 else if (size_t_flag) \
342 res = (unsig long)va_arg(arg, size_t); \
343 else if (short_flag) \
344 res = (unsig short)va_arg(arg, unsig int); \
346 res = (unsig int)va_arg(arg, unsig int)
351 * zyxprintf - return length, as snprintf
355 xyzprintf (struct snprintf_state
*state
, const char *char_format
, va_list ap
)
357 const unsigned char *format
= (const unsigned char *)char_format
;
361 while((c
= *format
++)) {
367 int long_long_flag
= 0;
372 while((c
= *format
++)){
380 flags
|= alternate_flag
;
389 if((flags
& space_flag
) && (flags
& plus_flag
))
392 if((flags
& minus_flag
) && (flags
& zero_flag
))
398 width
= width
* 10 + c
- '0';
402 width
= va_arg(ap
, int);
412 prec
= prec
* 10 + c
- '0';
416 prec
= va_arg(ap
, int);
426 } else if (c
== 'z') {
429 } else if (c
== 'l') {
438 if(c
!= 'd' && c
!= 'i')
439 flags
&= ~(plus_flag
| space_flag
);
443 append_char(state
, va_arg(ap
, int), width
, flags
);
447 len
+= append_string(state
,
448 va_arg(ap
, unsigned char*),
459 PARSE_INT_FORMAT(arg
, ap
, signed);
467 len
+= append_number (state
, num
, 10, "0123456789",
468 width
, prec
, flags
, minusp
);
474 PARSE_INT_FORMAT(arg
, ap
, unsigned);
476 len
+= append_number (state
, arg
, 10, "0123456789",
477 width
, prec
, flags
, 0);
483 PARSE_INT_FORMAT(arg
, ap
, unsigned);
485 len
+= append_number (state
, arg
, 010, "01234567",
486 width
, prec
, flags
, 0);
492 PARSE_INT_FORMAT(arg
, ap
, unsigned);
494 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
495 width
, prec
, flags
, 0);
501 PARSE_INT_FORMAT(arg
, ap
, unsigned);
503 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
504 width
, prec
, flags
, 0);
508 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
510 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
511 width
, prec
, flags
, 0);
515 int *arg
= va_arg(ap
, int*);
516 *arg
= state
->s
- state
->str
;
523 (*state
->append_char
)(state
, c
);
527 (*state
->append_char
)(state
, '%');
528 (*state
->append_char
)(state
, c
);
533 (*state
->append_char
) (state
, c
);
540 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
541 int ROKEN_LIB_FUNCTION
542 snprintf (char *str
, size_t sz
, const char *format
, ...)
547 va_start(args
, format
);
548 ret
= vsnprintf (str
, sz
, format
, args
);
560 va_start(args
, format
);
561 ret2
= vsprintf (tmp
, format
, args
);
563 if (ret
!= ret2
|| strcmp(str
, tmp
))
573 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
574 int ROKEN_LIB_FUNCTION
575 asprintf (char **ret
, const char *format
, ...)
580 va_start(args
, format
);
581 val
= vasprintf (ret
, format
, args
);
588 tmp
= malloc (val
+ 1);
592 va_start(args
, format
);
593 ret2
= vsprintf (tmp
, format
, args
);
595 if (val
!= ret2
|| strcmp(*ret
, tmp
))
605 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
606 int ROKEN_LIB_FUNCTION
607 asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
612 va_start(args
, format
);
613 val
= vasnprintf (ret
, max_sz
, format
, args
);
619 tmp
= malloc (val
+ 1);
623 ret2
= vsprintf (tmp
, format
, args
);
624 if (val
!= ret2
|| strcmp(*ret
, tmp
))
635 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
636 int ROKEN_LIB_FUNCTION
637 vasprintf (char **ret
, const char *format
, va_list args
)
639 return vasnprintf (ret
, 0, format
, args
);
644 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
645 int ROKEN_LIB_FUNCTION
646 vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
649 struct snprintf_state state
;
651 state
.max_sz
= max_sz
;
653 state
.str
= malloc(state
.sz
);
654 if (state
.str
== NULL
) {
659 state
.theend
= state
.s
+ state
.sz
- 1;
660 state
.append_char
= as_append_char
;
662 st
= xyzprintf (&state
, format
, args
);
671 tmp
= realloc (state
.str
, st
+1);
683 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
684 int ROKEN_LIB_FUNCTION
685 vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
687 struct snprintf_state state
;
689 unsigned char *ustr
= (unsigned char *)str
;
695 state
.theend
= ustr
+ sz
- (sz
> 0);
696 state
.append_char
= sn_append_char
;
698 ret
= xyzprintf (&state
, format
, args
);
699 if (state
.s
!= NULL
&& sz
!= 0)