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. */
38 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
43 RCSID("$Id: snprintf.c,v 1.2 2009/06/30 02:44:52 agc Exp $");
55 #define min(a,b) ((a) < (b) ? (a) : (b))
57 #define max(a,b) ((a) > (b) ? (a) : (b))
74 unsigned char *theend
;
77 void (*append_char
)(struct state
*, unsigned char);
82 #include "snprintf-test.h"
83 #endif /* TEST_SNPRINTF */
85 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
87 sn_reserve (struct state
*state
, size_t n
)
89 return state
->s
+ n
> state
->theend
;
93 sn_append_char (struct state
*state
, unsigned char c
)
95 if (!sn_reserve (state
, 1))
101 as_reserve (struct state
*state
, size_t n
)
103 if (state
->s
+ n
> state
->theend
) {
104 int off
= state
->s
- state
->str
;
107 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
110 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
112 state
->sz
= min(state
->sz
, state
->max_sz
);
113 tmp
= realloc (state
->str
, state
->sz
);
117 state
->s
= state
->str
+ off
;
118 state
->theend
= state
->str
+ state
->sz
- 1;
124 as_append_char (struct state
*state
, unsigned char c
)
126 if(!as_reserve (state
, 1))
130 /* longest integer types */
132 #ifdef HAVE_LONG_LONG
133 typedef unsigned long long u_longest
;
134 typedef long long longest
;
136 typedef unsigned long u_longest
;
137 typedef long longest
;
141 * is # supposed to do anything?
145 use_alternative (int flags
, u_longest num
, unsigned base
)
147 return flags
& alternate_flag
&& (base
== 16 || base
== 8) && num
!= 0;
151 append_number(struct state
*state
,
152 u_longest num
, unsigned base
, char *rep
,
153 int width
, int prec
, int flags
, int minusp
)
159 /* given precision, ignore zero flag */
164 /* zero value with zero precision -> "" */
165 if(prec
== 0 && n
== 0)
168 (*state
->append_char
)(state
, rep
[n
% base
]);
173 /* pad with prec zeros */
175 (*state
->append_char
)(state
, '0');
178 /* add length of alternate prefix (added later) to len */
179 if(use_alternative(flags
, num
, base
))
182 if(flags
& zero_flag
){
184 if(minusp
|| (flags
& space_flag
) || (flags
& plus_flag
))
187 (*state
->append_char
)(state
, '0');
191 /* add alternate prefix */
192 if(use_alternative(flags
, num
, base
)){
194 (*state
->append_char
)(state
, rep
[10] + 23); /* XXX */
195 (*state
->append_char
)(state
, '0');
199 (*state
->append_char
)(state
, '-');
201 } else if(flags
& plus_flag
) {
202 (*state
->append_char
)(state
, '+');
204 } else if(flags
& space_flag
) {
205 (*state
->append_char
)(state
, ' ');
208 if(flags
& minus_flag
)
209 /* swap before padding with spaces */
210 for(i
= 0; i
< len
/ 2; i
++){
211 char c
= state
->s
[-i
-1];
212 state
->s
[-i
-1] = state
->s
[-len
+i
];
213 state
->s
[-len
+i
] = c
;
217 (*state
->append_char
)(state
, ' ');
220 if(!(flags
& minus_flag
))
221 /* swap after padding with spaces */
222 for(i
= 0; i
< len
/ 2; i
++){
223 char c
= state
->s
[-i
-1];
224 state
->s
[-i
-1] = state
->s
[-len
+i
];
225 state
->s
[-len
+i
] = c
;
235 append_string (struct state
*state
,
236 const unsigned char *arg
,
244 arg
= (const unsigned char*)"(null)";
249 width
-= strlen((const char *)arg
);
250 if(!(flags
& minus_flag
))
252 (*state
->append_char
) (state
, ' ');
256 while (*arg
&& prec
--) {
257 (*state
->append_char
) (state
, *arg
++);
262 (*state
->append_char
) (state
, *arg
++);
266 if(flags
& minus_flag
)
268 (*state
->append_char
) (state
, ' ');
275 append_char(struct state
*state
,
282 while(!(flags
& minus_flag
) && --width
> 0) {
283 (*state
->append_char
) (state
, ' ') ;
286 (*state
->append_char
) (state
, arg
);
288 while((flags
& minus_flag
) && --width
> 0) {
289 (*state
->append_char
) (state
, ' ');
296 * This can't be made into a function...
299 #ifdef HAVE_LONG_LONG
301 #define PARSE_INT_FORMAT(res, arg, unsig) \
302 if (long_long_flag) \
303 res = (unsig long long)va_arg(arg, unsig long long); \
304 else if (long_flag) \
305 res = (unsig long)va_arg(arg, unsig long); \
306 else if (short_flag) \
307 res = (unsig short)va_arg(arg, unsig int); \
309 res = (unsig int)va_arg(arg, unsig int)
313 #define PARSE_INT_FORMAT(res, arg, unsig) \
315 res = (unsig long)va_arg(arg, unsig long); \
316 else if (short_flag) \
317 res = (unsig short)va_arg(arg, unsig int); \
319 res = (unsig int)va_arg(arg, unsig int)
324 * zyxprintf - return length, as snprintf
328 xyzprintf (struct state
*state
, const char *char_format
, va_list ap
)
330 const unsigned char *format
= (const unsigned char *)char_format
;
334 while((c
= *format
++)) {
339 int long_long_flag
= 0;
344 while((c
= *format
++)){
352 flags
|= alternate_flag
;
359 if((flags
& space_flag
) && (flags
& plus_flag
))
362 if((flags
& minus_flag
) && (flags
& zero_flag
))
368 width
= width
* 10 + c
- '0';
372 width
= va_arg(ap
, int);
382 prec
= prec
* 10 + c
- '0';
386 prec
= va_arg(ap
, int);
396 } else if (c
== 'l') {
407 append_char(state
, va_arg(ap
, int), width
, flags
);
411 len
+= append_string(state
,
412 va_arg(ap
, unsigned char*),
423 PARSE_INT_FORMAT(arg
, ap
, signed);
431 len
+= append_number (state
, num
, 10, "0123456789",
432 width
, prec
, flags
, minusp
);
438 PARSE_INT_FORMAT(arg
, ap
, unsigned);
440 len
+= append_number (state
, arg
, 10, "0123456789",
441 width
, prec
, flags
, 0);
447 PARSE_INT_FORMAT(arg
, ap
, unsigned);
449 len
+= append_number (state
, arg
, 010, "01234567",
450 width
, prec
, flags
, 0);
456 PARSE_INT_FORMAT(arg
, ap
, unsigned);
458 len
+= append_number (state
, arg
, 0x10, "0123456789abcdef",
459 width
, prec
, flags
, 0);
465 PARSE_INT_FORMAT(arg
, ap
, unsigned);
467 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
468 width
, prec
, flags
, 0);
472 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
474 len
+= append_number (state
, arg
, 0x10, "0123456789ABCDEF",
475 width
, prec
, flags
, 0);
479 int *arg
= va_arg(ap
, int*);
480 *arg
= state
->s
- state
->str
;
487 (*state
->append_char
)(state
, c
);
491 (*state
->append_char
)(state
, '%');
492 (*state
->append_char
)(state
, c
);
497 (*state
->append_char
) (state
, c
);
504 #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
506 snprintf (char *str
, size_t sz
, const char *format
, ...)
511 va_start(args
, format
);
512 ret
= vsnprintf (str
, sz
, format
, args
);
524 va_start(args
, format
);
525 ret2
= vsprintf (tmp
, format
, args
);
527 if (ret
!= ret2
|| strcmp(str
, tmp
))
537 #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
539 asprintf (char **ret
, const char *format
, ...)
544 va_start(args
, format
);
545 val
= vasprintf (ret
, format
, args
);
551 tmp
= malloc (val
+ 1);
555 ret2
= vsprintf (tmp
, format
, args
);
556 if (val
!= ret2
|| strcmp(*ret
, tmp
))
567 #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
569 vasprintf (char **ret
, const char *format
, va_list args
)
571 return vasnprintf (ret
, 0, format
, args
);
576 #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
578 vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
583 state
.max_sz
= max_sz
;
585 state
.str
= malloc(state
.sz
);
586 if (state
.str
== NULL
) {
591 state
.theend
= state
.s
+ state
.sz
- 1;
592 state
.append_char
= as_append_char
;
594 st
= xyzprintf (&state
, format
, args
);
603 tmp
= realloc (state
.str
, st
+1);
615 #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
617 vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
621 unsigned char *ustr
= (unsigned char *)str
;
627 state
.theend
= ustr
+ sz
- (sz
> 0);
628 state
.append_char
= sn_append_char
;
630 ret
= xyzprintf (&state
, format
, args
);
637 #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
639 asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
644 va_start(args
, format
);
645 val
= vasnprintf (ret
, max_sz
, format
, args
);
651 tmp
= malloc (val
+ 1);
655 ret2
= vsprintf (tmp
, format
, args
);
656 if (val
!= ret2
|| strcmp(*ret
, tmp
))