1 /* $NetBSD: snprintf.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
4 * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <krb5/roken.h>
57 struct snprintf_state
{
60 unsigned char *theend
;
63 void (*append_char
)(struct snprintf_state
*, unsigned char);
67 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
69 sn_reserve (struct snprintf_state
*state
, size_t n
)
71 return state
->s
+ n
> state
->theend
;
75 sn_append_char (struct snprintf_state
*state
, unsigned char c
)
77 if (!sn_reserve (state
, 1))
83 as_reserve (struct snprintf_state
*state
, size_t n
)
85 if (state
->s
+ n
> state
->theend
) {
86 int off
= state
->s
- state
->str
;
89 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
92 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
94 state
->sz
= min(state
->sz
, state
->max_sz
);
95 tmp
= realloc (state
->str
, state
->sz
);
99 state
->s
= state
->str
+ off
;
100 state
->theend
= state
->str
+ state
->sz
- 1;
106 as_append_char (struct snprintf_state
*state
, unsigned char c
)
108 if(!as_reserve (state
, 1))
112 /* longest integer types */
114 #ifdef HAVE_LONG_LONG
115 typedef unsigned long long u_longest
;
116 typedef long long longest
;
118 typedef unsigned long u_longest
;
119 typedef long longest
;
125 pad(struct snprintf_state
*state
, int width
, char c
)
129 (*state
->append_char
)(state
, c
);
135 /* return true if we should use alternatve hex form */
137 use_alternative (int flags
, u_longest num
, unsigned base
)
139 return (flags
& alternate_flag
) && base
== 16 && num
!= 0;
143 append_number(struct snprintf_state
*state
,
144 u_longest num
, unsigned base
, const char *rep
,
145 int width
, int prec
, int flags
, int minusp
)
149 char nstr
[64]; /* enough for <192 bit octal integers */
153 /* given precision, ignore zero flag */
159 /* format number as string */
160 nstart
= sizeof(nstr
);
162 nstr
[--nstart
] = '\0';
165 nstr
[--nstart
] = rep
[n
% base
];
170 /* zero value with zero precision should produce no digits */
171 if(prec
== 0 && num
== 0) {
176 /* figure out what char to use for sign */
179 else if((flags
& plus_flag
))
181 else if((flags
& space_flag
))
186 if((flags
& alternate_flag
) && base
== 8) {
187 /* if necessary, increase the precision to
188 make first digit a zero */
190 /* XXX C99 claims (regarding # and %o) that "if the value and
191 precision are both 0, a single 0 is printed", but there is
192 no such wording for %x. This would mean that %#.o would
193 output "0", but %#.x "". This does not make sense, and is
194 also not what other printf implementations are doing. */
196 if(prec
<= nlen
&& nstr
[nstart
] != '0' && nstr
[nstart
] != '\0')
201 pad | sign | alt | zero | digits
202 sign | alt | zero | digits | pad minus_flag
203 sign | alt | zero | digits zero_flag */
205 /* if not right justifying or padding with zeros, we need to
206 compute the length of the rest of the string, and then pad with
208 if(!(flags
& (minus_flag
| zero_flag
))) {
214 if(use_alternative(flags
, num
, base
))
221 len
+= pad(state
, width
, ' ');
223 if(signchar
!= '\0') {
224 (*state
->append_char
)(state
, signchar
);
227 if(use_alternative(flags
, num
, base
)) {
228 (*state
->append_char
)(state
, '0');
229 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
232 if(flags
& zero_flag
) {
233 /* pad to width with zeros */
234 if(prec
- nlen
> width
- len
- nlen
)
235 len
+= pad(state
, prec
- nlen
, '0');
237 len
+= pad(state
, width
- len
- nlen
, '0');
239 /* pad to prec with zeros */
240 len
+= pad(state
, prec
- nlen
, '0');
242 while(nstr
[nstart
] != '\0') {
243 (*state
->append_char
)(state
, nstr
[nstart
++]);
247 if(flags
& minus_flag
)
248 len
+= pad(state
, width
- len
, ' ');
258 append_string (struct snprintf_state
*state
,
259 const unsigned char *arg
,
267 arg
= (const unsigned char*)"(null)";
272 width
-= strlen((const char *)arg
);
273 if(!(flags
& minus_flag
))
274 len
+= pad(state
, width
, ' ');
277 while (*arg
&& prec
--) {
278 (*state
->append_char
) (state
, *arg
++);
283 (*state
->append_char
) (state
, *arg
++);
287 if(flags
& minus_flag
)
288 len
+= pad(state
, width
, ' ');
293 append_char(struct snprintf_state
*state
,
300 while(!(flags
& minus_flag
) && --width
> 0) {
301 (*state
->append_char
) (state
, ' ') ;
304 (*state
->append_char
) (state
, arg
);
306 while((flags
& minus_flag
) && --width
> 0) {
307 (*state
->append_char
) (state
, ' ');
314 * This can't be made into a function...
317 #ifdef HAVE_LONG_LONG
319 #define PARSE_INT_FORMAT(res, arg, unsig) \
320 if (long_long_flag) \
321 res = (unsig long long)va_arg(arg, unsig long long); \
322 else if (long_flag) \
323 res = (unsig long)va_arg(arg, unsig long); \
324 else if (size_t_flag) \
325 res = (unsig long)va_arg(arg, size_t); \
326 else if (short_flag) \
327 res = (unsig short)va_arg(arg, unsig int); \
329 res = (unsig int)va_arg(arg, unsig int)
333 #define PARSE_INT_FORMAT(res, arg, unsig) \
335 res = (unsig long)va_arg(arg, unsig long); \
336 else if (size_t_flag) \
337 res = (unsig long)va_arg(arg, size_t); \
338 else if (short_flag) \
339 res = (unsig short)va_arg(arg, unsig int); \
341 res = (unsig int)va_arg(arg, unsig int)
346 * zyxprintf - return length, as snprintf
350 xyzprintf (struct snprintf_state
*state
, const char *char_format
, va_list ap
)
352 const unsigned char *format
= (const unsigned char *)char_format
;
356 while((c
= *format
++)) {
362 int long_long_flag
= 0;
367 while((c
= *format
++)){
375 flags
|= alternate_flag
;
384 if((flags
& space_flag
) && (flags
& plus_flag
))
387 if((flags
& minus_flag
) && (flags
& zero_flag
))
393 width
= width
* 10 + c
- '0';
397 width
= va_arg(ap
, int);
407 prec
= prec
* 10 + c
- '0';
411 prec
= va_arg(ap
, int);
421 } else if (c
== 'z') {
424 } else if (c
== 'l') {
433 if(c
!= 'd' && c
!= 'i')
434 flags
&= ~(plus_flag
| space_flag
);
438 append_char(state
, va_arg(ap
, int), width
, flags
);
442 len
+= append_string(state
,
443 va_arg(ap
, unsigned char*),
454 PARSE_INT_FORMAT(arg
, ap
, signed);
462 len
+= append_number (state
, num
, 10, "0123456789",
463 width
, prec
, flags
, minusp
);
469 PARSE_INT_FORMAT(arg
, ap
, unsigned);
471 len
+= append_number (state
, arg
, 10, "0123456789",
472 width
, prec
, flags
, 0);
478 PARSE_INT_FORMAT(arg
, ap
, unsigned);
480 len
+= append_number (state
, arg
, 010, "01234567",
481 width
, prec
, flags
, 0);
487 PARSE_INT_FORMAT(arg
, ap
, unsigned);
489 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
490 width
, prec
, flags
, 0);
496 PARSE_INT_FORMAT(arg
, ap
, unsigned);
498 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
499 width
, prec
, flags
, 0);
503 u_longest arg
= (u_longest
)va_arg(ap
, void*);
505 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
506 width
, prec
, flags
, 0);
510 int *arg
= va_arg(ap
, int*);
511 *arg
= state
->s
- state
->str
;
518 (*state
->append_char
)(state
, c
);
522 (*state
->append_char
)(state
, '%');
523 (*state
->append_char
)(state
, c
);
528 (*state
->append_char
) (state
, c
);
535 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
536 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
537 rk_snprintf (char *str
, size_t sz
, const char *format
, ...)
542 va_start(args
, format
);
543 ret
= vsnprintf (str
, sz
, format
, args
);
555 va_start(args
, format
);
556 ret2
= vsprintf (tmp
, format
, args
);
558 if (ret
!= ret2
|| strcmp(str
, tmp
))
568 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
569 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
570 rk_asprintf (char **ret
, const char *format
, ...)
575 va_start(args
, format
);
576 val
= vasprintf (ret
, format
, args
);
583 tmp
= malloc (val
+ 1);
587 va_start(args
, format
);
588 ret2
= vsprintf (tmp
, format
, args
);
590 if (val
!= ret2
|| strcmp(*ret
, tmp
))
600 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
601 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
602 rk_asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
607 va_start(args
, format
);
608 val
= vasnprintf (ret
, max_sz
, format
, args
);
614 tmp
= malloc (val
+ 1);
618 ret2
= vsprintf (tmp
, format
, args
);
619 if (val
!= ret2
|| strcmp(*ret
, tmp
))
630 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
631 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
632 rk_vasprintf (char **ret
, const char *format
, va_list args
)
634 return vasnprintf (ret
, 0, format
, args
);
639 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
640 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
641 rk_vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
644 struct snprintf_state state
;
646 state
.max_sz
= max_sz
;
648 state
.str
= malloc(state
.sz
);
649 if (state
.str
== NULL
) {
654 state
.theend
= state
.s
+ state
.sz
- 1;
655 state
.append_char
= as_append_char
;
657 st
= xyzprintf (&state
, format
, args
);
666 tmp
= realloc (state
.str
, st
+1);
678 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
679 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
680 rk_vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
682 struct snprintf_state state
;
684 unsigned char *ustr
= (unsigned char *)str
;
690 state
.theend
= ustr
+ sz
- (sz
> 0);
691 state
.append_char
= sn_append_char
;
693 ret
= xyzprintf (&state
, format
, args
);
694 if (state
.s
!= NULL
&& sz
!= 0)