2 * Copyright (c) 1995-2001 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
34 /* From heimdal lib/roken/snprintf.c. */
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
41 RCSID("$Id: snprintf.c,v 1.3 2003/10/27 00:12:43 lukem Exp $");
53 #define min(a,b) ((a) < (b) ? (a) : (b))
55 #define max(a,b) ((a) > (b) ? (a) : (b))
72 unsigned char *theend
;
75 void (*append_char
)(struct state
*, unsigned char);
80 #include "snprintf-test.h"
81 #endif /* TEST_SNPRINTF */
83 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
85 sn_reserve (struct state
*state
, size_t n
)
87 return state
->s
+ n
> state
->theend
;
91 sn_append_char (struct state
*state
, unsigned char c
)
93 if (!sn_reserve (state
, 1))
99 as_reserve (struct state
*state
, size_t n
)
101 if (state
->s
+ n
> state
->theend
) {
102 int off
= state
->s
- state
->str
;
105 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
108 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
110 state
->sz
= min(state
->sz
, state
->max_sz
);
111 tmp
= realloc (state
->str
, state
->sz
);
115 state
->s
= state
->str
+ off
;
116 state
->theend
= state
->str
+ state
->sz
- 1;
122 as_append_char (struct state
*state
, unsigned char c
)
124 if(!as_reserve (state
, 1))
128 /* longest integer types */
130 #ifdef HAVE_LONG_LONG
131 typedef unsigned long long u_longest
;
132 typedef long long longest
;
134 typedef unsigned long u_longest
;
135 typedef long longest
;
139 * is # supposed to do anything?
143 use_alternative (int flags
, u_longest num
, unsigned base
)
145 return flags
& alternate_flag
&& (base
== 16 || base
== 8) && num
!= 0;
149 append_number(struct state
*state
,
150 u_longest num
, unsigned base
, char *rep
,
151 int width
, int prec
, int flags
, int minusp
)
157 /* given precision, ignore zero flag */
162 /* zero value with zero precision -> "" */
163 if(prec
== 0 && n
== 0)
166 (*state
->append_char
)(state
, rep
[n
% base
]);
171 /* pad with prec zeros */
173 (*state
->append_char
)(state
, '0');
176 /* add length of alternate prefix (added later) to len */
177 if(use_alternative(flags
, num
, base
))
180 if(flags
& zero_flag
){
182 if(minusp
|| (flags
& space_flag
) || (flags
& plus_flag
))
185 (*state
->append_char
)(state
, '0');
189 /* add alternate prefix */
190 if(use_alternative(flags
, num
, base
)){
192 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
193 (*state
->append_char
)(state
, '0');
197 (*state
->append_char
)(state
, '-');
199 } else if(flags
& plus_flag
) {
200 (*state
->append_char
)(state
, '+');
202 } else if(flags
& space_flag
) {
203 (*state
->append_char
)(state
, ' ');
206 if(flags
& minus_flag
)
207 /* swap before padding with spaces */
208 for(i
= 0; i
< len
/ 2; i
++){
209 char c
= state
->s
[-i
-1];
210 state
->s
[-i
-1] = state
->s
[-len
+i
];
211 state
->s
[-len
+i
] = c
;
215 (*state
->append_char
)(state
, ' ');
218 if(!(flags
& minus_flag
))
219 /* swap after padding with spaces */
220 for(i
= 0; i
< len
/ 2; i
++){
221 char c
= state
->s
[-i
-1];
222 state
->s
[-i
-1] = state
->s
[-len
+i
];
223 state
->s
[-len
+i
] = c
;
233 append_string (struct state
*state
,
234 const unsigned char *arg
,
242 arg
= (const unsigned char*)"(null)";
247 width
-= strlen((const char *)arg
);
248 if(!(flags
& minus_flag
))
250 (*state
->append_char
) (state
, ' ');
254 while (*arg
&& prec
--) {
255 (*state
->append_char
) (state
, *arg
++);
260 (*state
->append_char
) (state
, *arg
++);
264 if(flags
& minus_flag
)
266 (*state
->append_char
) (state
, ' ');
273 append_char(struct state
*state
,
280 while(!(flags
& minus_flag
) && --width
> 0) {
281 (*state
->append_char
) (state
, ' ') ;
284 (*state
->append_char
) (state
, arg
);
286 while((flags
& minus_flag
) && --width
> 0) {
287 (*state
->append_char
) (state
, ' ');
294 * This can't be made into a function...
297 #ifdef HAVE_LONG_LONG
299 #define PARSE_INT_FORMAT(res, arg, unsig) \
300 if (long_long_flag) \
301 res = (unsig long long)va_arg(arg, unsig long long); \
302 else if (long_flag) \
303 res = (unsig long)va_arg(arg, unsig long); \
304 else if (short_flag) \
305 res = (unsig short)va_arg(arg, unsig int); \
307 res = (unsig int)va_arg(arg, unsig int)
311 #define PARSE_INT_FORMAT(res, arg, unsig) \
313 res = (unsig long)va_arg(arg, unsig long); \
314 else if (short_flag) \
315 res = (unsig short)va_arg(arg, unsig int); \
317 res = (unsig int)va_arg(arg, unsig int)
322 * zyxprintf - return length, as snprintf
326 xyzprintf (struct state
*state
, const char *char_format
, va_list ap
)
328 const unsigned char *format
= (const unsigned char *)char_format
;
332 while((c
= *format
++)) {
337 int long_long_flag
= 0;
342 while((c
= *format
++)){
350 flags
|= alternate_flag
;
357 if((flags
& space_flag
) && (flags
& plus_flag
))
360 if((flags
& minus_flag
) && (flags
& zero_flag
))
366 width
= width
* 10 + c
- '0';
370 width
= va_arg(ap
, int);
380 prec
= prec
* 10 + c
- '0';
384 prec
= va_arg(ap
, int);
394 } else if (c
== 'l') {
405 append_char(state
, va_arg(ap
, int), width
, flags
);
409 len
+= append_string(state
,
410 va_arg(ap
, unsigned char*),
421 PARSE_INT_FORMAT(arg
, ap
, signed);
429 len
+= append_number (state
, num
, 10, "0123456789",
430 width
, prec
, flags
, minusp
);
436 PARSE_INT_FORMAT(arg
, ap
, unsigned);
438 len
+= append_number (state
, arg
, 10, "0123456789",
439 width
, prec
, flags
, 0);
445 PARSE_INT_FORMAT(arg
, ap
, unsigned);
447 len
+= append_number (state
, arg
, 010, "01234567",
448 width
, prec
, flags
, 0);
454 PARSE_INT_FORMAT(arg
, ap
, unsigned);
456 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
457 width
, prec
, flags
, 0);
463 PARSE_INT_FORMAT(arg
, ap
, unsigned);
465 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
466 width
, prec
, flags
, 0);
470 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
472 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
473 width
, prec
, flags
, 0);
477 int *arg
= va_arg(ap
, int*);
478 *arg
= state
->s
- state
->str
;
485 (*state
->append_char
)(state
, c
);
489 (*state
->append_char
)(state
, '%');
490 (*state
->append_char
)(state
, c
);
495 (*state
->append_char
) (state
, c
);
502 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
504 snprintf (char *str
, size_t sz
, const char *format
, ...)
509 va_start(args
, format
);
510 ret
= vsnprintf (str
, sz
, format
, args
);
522 va_start(args
, format
);
523 ret2
= vsprintf (tmp
, format
, args
);
525 if (ret
!= ret2
|| strcmp(str
, tmp
))
535 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
537 asprintf (char **ret
, const char *format
, ...)
542 va_start(args
, format
);
543 val
= vasprintf (ret
, format
, args
);
549 tmp
= malloc (val
+ 1);
553 ret2
= vsprintf (tmp
, format
, args
);
554 if (val
!= ret2
|| strcmp(*ret
, tmp
))
565 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
567 asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
572 va_start(args
, format
);
573 val
= vasnprintf (ret
, max_sz
, format
, args
);
579 tmp
= malloc (val
+ 1);
583 ret2
= vsprintf (tmp
, format
, args
);
584 if (val
!= ret2
|| strcmp(*ret
, tmp
))
595 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
597 vasprintf (char **ret
, const char *format
, va_list args
)
599 return vasnprintf (ret
, 0, format
, args
);
604 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
606 vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
611 state
.max_sz
= max_sz
;
613 state
.str
= malloc(state
.sz
);
614 if (state
.str
== NULL
) {
619 state
.theend
= state
.s
+ state
.sz
- 1;
620 state
.append_char
= as_append_char
;
622 st
= xyzprintf (&state
, format
, args
);
631 tmp
= realloc (state
.str
, st
+1);
643 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
645 vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
649 unsigned char *ustr
= (unsigned char *)str
;
655 state
.theend
= ustr
+ sz
- (sz
> 0);
656 state
.append_char
= sn_append_char
;
658 ret
= xyzprintf (&state
, format
, args
);