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
38 #if defined(TEST_SNPRINTF)
39 #include "snprintf-test.h"
40 #endif /* TEST_SNPRINTF */
61 struct snprintf_state
{
64 unsigned char *theend
;
67 void (*append_char
)(struct snprintf_state
*, unsigned char);
71 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
73 sn_reserve (struct snprintf_state
*state
, size_t n
)
75 return state
->s
+ n
> state
->theend
;
79 sn_append_char (struct snprintf_state
*state
, unsigned char c
)
81 if (!sn_reserve (state
, 1))
87 as_reserve (struct snprintf_state
*state
, size_t n
)
89 if (state
->s
+ n
> state
->theend
) {
90 int off
= state
->s
- state
->str
;
93 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
96 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
98 state
->sz
= min(state
->sz
, state
->max_sz
);
99 tmp
= realloc (state
->str
, state
->sz
);
103 state
->s
= state
->str
+ off
;
104 state
->theend
= state
->str
+ state
->sz
- 1;
110 as_append_char (struct snprintf_state
*state
, unsigned char c
)
112 if(!as_reserve (state
, 1))
116 /* longest integer types */
118 #ifdef HAVE_LONG_LONG
119 typedef unsigned long long u_longest
;
120 typedef long long longest
;
122 typedef unsigned long u_longest
;
123 typedef long longest
;
129 pad(struct snprintf_state
*state
, int width
, char c
)
133 (*state
->append_char
)(state
, c
);
139 /* return true if we should use alternatve hex form */
141 use_alternative (int flags
, u_longest num
, unsigned base
)
143 return (flags
& alternate_flag
) && base
== 16 && num
!= 0;
147 append_number(struct snprintf_state
*state
,
148 u_longest num
, unsigned base
, const char *rep
,
149 int width
, int prec
, int flags
, int minusp
)
153 char nstr
[64]; /* enough for <192 bit octal integers */
157 /* given precision, ignore zero flag */
163 /* format number as string */
164 nstart
= sizeof(nstr
);
166 nstr
[--nstart
] = '\0';
169 nstr
[--nstart
] = rep
[n
% base
];
174 /* zero value with zero precision should produce no digits */
175 if(prec
== 0 && num
== 0) {
180 /* figure out what char to use for sign */
183 else if((flags
& plus_flag
))
185 else if((flags
& space_flag
))
190 if((flags
& alternate_flag
) && base
== 8) {
191 /* if necessary, increase the precision to
192 make first digit a zero */
194 /* XXX C99 claims (regarding # and %o) that "if the value and
195 precision are both 0, a single 0 is printed", but there is
196 no such wording for %x. This would mean that %#.o would
197 output "0", but %#.x "". This does not make sense, and is
198 also not what other printf implementations are doing. */
200 if(prec
<= nlen
&& nstr
[nstart
] != '0' && nstr
[nstart
] != '\0')
205 pad | sign | alt | zero | digits
206 sign | alt | zero | digits | pad minus_flag
207 sign | alt | zero | digits zero_flag */
209 /* if not right justifying or padding with zeros, we need to
210 compute the length of the rest of the string, and then pad with
212 if(!(flags
& (minus_flag
| zero_flag
))) {
218 if(use_alternative(flags
, num
, base
))
225 len
+= pad(state
, width
, ' ');
227 if(signchar
!= '\0') {
228 (*state
->append_char
)(state
, signchar
);
231 if(use_alternative(flags
, num
, base
)) {
232 (*state
->append_char
)(state
, '0');
233 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
236 if(flags
& zero_flag
) {
237 /* pad to width with zeros */
238 if(prec
- nlen
> width
- len
- nlen
)
239 len
+= pad(state
, prec
- nlen
, '0');
241 len
+= pad(state
, width
- len
- nlen
, '0');
243 /* pad to prec with zeros */
244 len
+= pad(state
, prec
- nlen
, '0');
246 while(nstr
[nstart
] != '\0') {
247 (*state
->append_char
)(state
, nstr
[nstart
++]);
251 if(flags
& minus_flag
)
252 len
+= pad(state
, width
- len
, ' ');
262 append_string (struct snprintf_state
*state
,
263 const unsigned char *arg
,
271 arg
= (const unsigned char*)"(null)";
276 width
-= strlen((const char *)arg
);
277 if(!(flags
& minus_flag
))
278 len
+= pad(state
, width
, ' ');
281 while (*arg
&& prec
--) {
282 (*state
->append_char
) (state
, *arg
++);
287 (*state
->append_char
) (state
, *arg
++);
291 if(flags
& minus_flag
)
292 len
+= pad(state
, width
, ' ');
297 append_char(struct snprintf_state
*state
,
304 while(!(flags
& minus_flag
) && --width
> 0) {
305 (*state
->append_char
) (state
, ' ') ;
308 (*state
->append_char
) (state
, arg
);
310 while((flags
& minus_flag
) && --width
> 0) {
311 (*state
->append_char
) (state
, ' ');
318 * This can't be made into a function...
321 #ifdef HAVE_LONG_LONG
323 #define PARSE_INT_FORMAT(res, arg, unsig) \
324 if (long_long_flag) \
325 res = (unsig long long)va_arg(arg, unsig long long); \
326 else if (long_flag) \
327 res = (unsig long)va_arg(arg, unsig long); \
328 else if (size_t_flag) \
329 res = (unsig long)va_arg(arg, size_t); \
330 else if (short_flag) \
331 res = (unsig short)va_arg(arg, unsig int); \
333 res = (unsig int)va_arg(arg, unsig int)
337 #define PARSE_INT_FORMAT(res, arg, unsig) \
339 res = (unsig long)va_arg(arg, unsig long); \
340 else if (size_t_flag) \
341 res = (unsig long)va_arg(arg, size_t); \
342 else if (short_flag) \
343 res = (unsig short)va_arg(arg, unsig int); \
345 res = (unsig int)va_arg(arg, unsig int)
350 * zyxprintf - return length, as snprintf
354 xyzprintf (struct snprintf_state
*state
, const char *char_format
, va_list ap
)
356 const unsigned char *format
= (const unsigned char *)char_format
;
360 while((c
= *format
++)) {
366 int long_long_flag
= 0;
371 while((c
= *format
++)){
379 flags
|= alternate_flag
;
388 if((flags
& space_flag
) && (flags
& plus_flag
))
391 if((flags
& minus_flag
) && (flags
& zero_flag
))
397 width
= width
* 10 + c
- '0';
401 width
= va_arg(ap
, int);
411 prec
= prec
* 10 + c
- '0';
415 prec
= va_arg(ap
, int);
425 } else if (c
== 'z') {
428 } else if (c
== 'l') {
437 if(c
!= 'd' && c
!= 'i')
438 flags
&= ~(plus_flag
| space_flag
);
442 append_char(state
, va_arg(ap
, int), width
, flags
);
446 len
+= append_string(state
,
447 va_arg(ap
, unsigned char*),
458 PARSE_INT_FORMAT(arg
, ap
, signed);
466 len
+= append_number (state
, num
, 10, "0123456789",
467 width
, prec
, flags
, minusp
);
473 PARSE_INT_FORMAT(arg
, ap
, unsigned);
475 len
+= append_number (state
, arg
, 10, "0123456789",
476 width
, prec
, flags
, 0);
482 PARSE_INT_FORMAT(arg
, ap
, unsigned);
484 len
+= append_number (state
, arg
, 010, "01234567",
485 width
, prec
, flags
, 0);
491 PARSE_INT_FORMAT(arg
, ap
, unsigned);
493 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
494 width
, prec
, flags
, 0);
500 PARSE_INT_FORMAT(arg
, ap
, unsigned);
502 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
503 width
, prec
, flags
, 0);
507 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
509 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
510 width
, prec
, flags
, 0);
514 int *arg
= va_arg(ap
, int*);
515 *arg
= state
->s
- state
->str
;
522 (*state
->append_char
)(state
, c
);
526 (*state
->append_char
)(state
, '%');
527 (*state
->append_char
)(state
, c
);
532 (*state
->append_char
) (state
, c
);
539 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
540 int ROKEN_LIB_FUNCTION
541 snprintf (char *str
, size_t sz
, const char *format
, ...)
546 va_start(args
, format
);
547 ret
= vsnprintf (str
, sz
, format
, args
);
559 va_start(args
, format
);
560 ret2
= vsprintf (tmp
, format
, args
);
562 if (ret
!= ret2
|| strcmp(str
, tmp
))
572 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
573 int ROKEN_LIB_FUNCTION
574 asprintf (char **ret
, const char *format
, ...)
579 va_start(args
, format
);
580 val
= vasprintf (ret
, format
, args
);
587 tmp
= malloc (val
+ 1);
591 va_start(args
, format
);
592 ret2
= vsprintf (tmp
, format
, args
);
594 if (val
!= ret2
|| strcmp(*ret
, tmp
))
604 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
605 int ROKEN_LIB_FUNCTION
606 asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
611 va_start(args
, format
);
612 val
= vasnprintf (ret
, max_sz
, format
, args
);
618 tmp
= malloc (val
+ 1);
622 ret2
= vsprintf (tmp
, format
, args
);
623 if (val
!= ret2
|| strcmp(*ret
, tmp
))
634 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
635 int ROKEN_LIB_FUNCTION
636 vasprintf (char **ret
, const char *format
, va_list args
)
638 return vasnprintf (ret
, 0, format
, args
);
643 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
644 int ROKEN_LIB_FUNCTION
645 vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
648 struct snprintf_state state
;
650 state
.max_sz
= max_sz
;
652 state
.str
= malloc(state
.sz
);
653 if (state
.str
== NULL
) {
658 state
.theend
= state
.s
+ state
.sz
- 1;
659 state
.append_char
= as_append_char
;
661 st
= xyzprintf (&state
, format
, args
);
670 tmp
= realloc (state
.str
, st
+1);
682 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
683 int ROKEN_LIB_FUNCTION
684 vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
686 struct snprintf_state state
;
688 unsigned char *ustr
= (unsigned char *)str
;
694 state
.theend
= ustr
+ sz
- (sz
> 0);
695 state
.append_char
= sn_append_char
;
697 ret
= xyzprintf (&state
, format
, args
);
698 if (state
.s
!= NULL
&& sz
!= 0)