2 * Copyright (c) 1995-1999 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
43 #include <sys/types.h>
62 unsigned char *theend
;
65 int (*append_char
)(struct state
*, unsigned char);
66 int (*reserve
)(struct state
*, size_t);
70 #ifndef HAVE_VSNPRINTF
72 sn_reserve (struct state
*state
, size_t n
)
74 return state
->s
+ n
> state
->theend
;
78 sn_append_char (struct state
*state
, unsigned char c
)
80 if (sn_reserve (state
, 1)) {
91 as_reserve (struct state
*state
, size_t n
)
93 if (state
->s
+ n
> state
->theend
) {
94 int off
= state
->s
- state
->str
;
97 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
100 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
102 state
->sz
= min(state
->sz
, state
->max_sz
);
103 tmp
= realloc (state
->str
, state
->sz
);
107 state
->s
= state
->str
+ off
;
108 state
->theend
= state
->str
+ state
->sz
- 1;
114 as_append_char (struct state
*state
, unsigned char c
)
116 if(as_reserve (state
, 1))
126 append_number(struct state
*state
,
127 unsigned long num
, unsigned base
, char *rep
,
128 int width
, int prec
, int flags
, int minusp
)
133 /* given precision, ignore zero flag */
138 /* zero value with zero precision -> "" */
139 if(prec
== 0 && num
== 0)
142 if((*state
->append_char
)(state
, rep
[num
% base
]))
148 /* pad with prec zeros */
150 if((*state
->append_char
)(state
, '0'))
154 /* add length of alternate prefix (added later) to len */
155 if(flags
& alternate_flag
&& (base
== 16 || base
== 8))
158 if(flags
& zero_flag
){
160 if(minusp
|| (flags
& space_flag
) || (flags
& plus_flag
))
163 if((*state
->append_char
)(state
, '0'))
168 /* add alternate prefix */
169 if(flags
& alternate_flag
&& (base
== 16 || base
== 8)){
171 if((*state
->append_char
)(state
, rep
[10] + 23)) /* XXX */
173 if((*state
->append_char
)(state
, '0'))
178 if((*state
->append_char
)(state
, '-'))
181 } else if(flags
& plus_flag
) {
182 if((*state
->append_char
)(state
, '+'))
185 } else if(flags
& space_flag
) {
186 if((*state
->append_char
)(state
, ' '))
190 if(flags
& minus_flag
)
191 /* swap before padding with spaces */
192 for(i
= 0; i
< len
/ 2; i
++){
193 char c
= state
->s
[-i
-1];
194 state
->s
[-i
-1] = state
->s
[-len
+i
];
195 state
->s
[-len
+i
] = c
;
199 if((*state
->append_char
)(state
, ' '))
203 if(!(flags
& minus_flag
))
204 /* swap after padding with spaces */
205 for(i
= 0; i
< len
/ 2; i
++){
206 char c
= state
->s
[-i
-1];
207 state
->s
[-i
-1] = state
->s
[-len
+i
];
208 state
->s
[-len
+i
] = c
;
215 append_string (struct state
*state
,
224 width
-= strlen((char *)arg
);
225 if(!(flags
& minus_flag
))
227 if((*state
->append_char
) (state
, ' '))
230 while (*arg
&& prec
--)
231 if ((*state
->append_char
) (state
, *arg
++))
235 if ((*state
->append_char
) (state
, *arg
++))
238 if(flags
& minus_flag
)
240 if((*state
->append_char
) (state
, ' '))
246 append_char(struct state
*state
,
251 while(!(flags
& minus_flag
) && --width
> 0)
252 if((*state
->append_char
) (state
, ' '))
255 if((*state
->append_char
) (state
, arg
))
257 while((flags
& minus_flag
) && --width
> 0)
258 if((*state
->append_char
) (state
, ' '))
265 * This can't be made into a function...
268 #define PARSE_INT_FORMAT(res, arg, unsig) \
270 res = (unsig long)va_arg(arg, unsig long); \
271 else if (short_flag) \
272 res = (unsig short)va_arg(arg, unsig int); \
274 res = (unsig int)va_arg(arg, unsig int)
277 * zyxprintf - return 0 or -1
281 xyzprintf (struct state
*state
, const char *char_format
, va_list ap
)
283 const unsigned char *format
= (const unsigned char *)char_format
;
286 while((c
= *format
++)) {
295 while((c
= *format
++)){
303 flags
|= alternate_flag
;
310 if((flags
& space_flag
) && (flags
& plus_flag
))
313 if((flags
& minus_flag
) && (flags
& zero_flag
))
319 width
= width
* 10 + c
- '0';
323 width
= va_arg(ap
, int);
333 prec
= prec
* 10 + c
- '0';
337 prec
= va_arg(ap
, int);
347 } else if (c
== 'l') {
354 if(append_char(state
, va_arg(ap
, int), width
, flags
))
358 if (append_string(state
,
359 va_arg(ap
, unsigned char*),
371 PARSE_INT_FORMAT(arg
, ap
, signed);
379 if (append_number (state
, num
, 10, "0123456789",
380 width
, prec
, flags
, minusp
))
387 PARSE_INT_FORMAT(arg
, ap
, unsigned);
389 if (append_number (state
, arg
, 10, "0123456789",
390 width
, prec
, flags
, 0))
397 PARSE_INT_FORMAT(arg
, ap
, unsigned);
399 if (append_number (state
, arg
, 010, "01234567",
400 width
, prec
, flags
, 0))
407 PARSE_INT_FORMAT(arg
, ap
, unsigned);
409 if (append_number (state
, arg
, 0x10, "0123456789abcdef",
410 width
, prec
, flags
, 0))
417 PARSE_INT_FORMAT(arg
, ap
, unsigned);
419 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
420 width
, prec
, flags
, 0))
425 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
427 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
428 width
, prec
, flags
, 0))
433 int *arg
= va_arg(ap
, int*);
434 *arg
= state
->s
- state
->str
;
441 if ((*state
->append_char
)(state
, c
))
445 if ( (*state
->append_char
)(state
, '%')
446 || (*state
->append_char
)(state
, c
))
451 if ((*state
->append_char
) (state
, c
))
457 #ifndef HAVE_SNPRINTF
459 snprintf (char *str
, size_t sz
, const char *format
, ...)
464 va_start(args
, format
);
465 ret
= vsnprintf (str
, sz
, format
, args
);
476 ret2
= vsprintf (tmp
, format
, args
);
477 if (ret
!= ret2
|| strcmp(str
, tmp
))
489 #ifndef HAVE_ASPRINTF
491 asprintf (char **ret
, const char *format
, ...)
496 va_start(args
, format
);
497 val
= vasprintf (ret
, format
, args
);
503 tmp
= malloc (val
+ 1);
507 ret2
= vsprintf (tmp
, format
, args
);
508 if (val
!= ret2
|| strcmp(*ret
, tmp
))
519 #ifndef HAVE_ASNPRINTF
521 asnprintf (char **ret
, size_t max_sz
, const char *format
, ...)
526 va_start(args
, format
);
527 val
= vasnprintf (ret
, max_sz
, format
, args
);
533 tmp
= malloc (val
+ 1);
537 ret2
= vsprintf (tmp
, format
, args
);
538 if (val
!= ret2
|| strcmp(*ret
, tmp
))
549 #ifndef HAVE_VASPRINTF
551 vasprintf (char **ret
, const char *format
, va_list args
)
553 return vasnprintf (ret
, 0, format
, args
);
558 #ifndef HAVE_VASNPRINTF
560 vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
566 state
.max_sz
= max_sz
;
568 state
.str
= malloc(state
.sz
);
569 if (state
.str
== NULL
) {
574 state
.theend
= state
.s
+ state
.sz
- 1;
575 state
.append_char
= as_append_char
;
576 state
.reserve
= as_reserve
;
578 st
= xyzprintf (&state
, format
, args
);
587 len
= state
.s
- state
.str
;
588 tmp
= realloc (state
.str
, len
+1);
601 #ifndef HAVE_VSNPRINTF
603 vsnprintf (char *str
, size_t sz
, const char *format
, va_list args
)
607 unsigned char *ustr
= (unsigned char *)str
;
613 state
.theend
= ustr
+ sz
- 1;
614 state
.append_char
= sn_append_char
;
615 state
.reserve
= sn_reserve
;
617 ret
= xyzprintf (&state
, format
, args
);
622 return state
.s
- state
.str
;