]>
The Tcpdump Group git mirrors - tcpdump/blob - missing/snprintf.c
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
41 #include <sys/types.h>
43 #include "netdissect.h"
60 unsigned char *theend
;
63 int (*append_char
)(struct state
*, unsigned char);
64 int (*reserve
)(struct state
*, size_t);
70 as_reserve (struct state
*state
, size_t n
)
72 if (state
->s
+ n
> state
->theend
) {
73 int off
= state
->s
- state
->str
;
76 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
79 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
81 state
->sz
= min(state
->sz
, state
->max_sz
);
82 tmp
= realloc (state
->str
, state
->sz
);
86 state
->s
= state
->str
+ off
;
87 state
->theend
= state
->str
+ state
->sz
- 1;
93 as_append_char (struct state
*state
, unsigned char c
)
95 if(as_reserve (state
, 1))
105 append_number(struct state
*state
,
106 unsigned long num
, unsigned base
, char *rep
,
107 int width
, int prec
, int flags
, int minusp
)
112 /* given precision, ignore zero flag */
117 /* zero value with zero precision -> "" */
118 if(prec
== 0 && num
== 0)
121 if((*state
->append_char
)(state
, rep
[num
% base
]))
127 /* pad with prec zeros */
129 if((*state
->append_char
)(state
, '0'))
133 /* add length of alternate prefix (added later) to len */
134 if(flags
& alternate_flag
&& (base
== 16 || base
== 8))
137 if(flags
& zero_flag
){
139 if(minusp
|| (flags
& space_flag
) || (flags
& plus_flag
))
142 if((*state
->append_char
)(state
, '0'))
147 /* add alternate prefix */
148 if(flags
& alternate_flag
&& (base
== 16 || base
== 8)){
150 if((*state
->append_char
)(state
, rep
[10] + 23)) /* XXX */
152 if((*state
->append_char
)(state
, '0'))
157 if((*state
->append_char
)(state
, '-'))
160 } else if(flags
& plus_flag
) {
161 if((*state
->append_char
)(state
, '+'))
164 } else if(flags
& space_flag
) {
165 if((*state
->append_char
)(state
, ' '))
169 if(flags
& minus_flag
)
170 /* swap before padding with spaces */
171 for(i
= 0; i
< len
/ 2; i
++){
172 char c
= state
->s
[-i
-1];
173 state
->s
[-i
-1] = state
->s
[-len
+i
];
174 state
->s
[-len
+i
] = c
;
178 if((*state
->append_char
)(state
, ' '))
182 if(!(flags
& minus_flag
))
183 /* swap after padding with spaces */
184 for(i
= 0; i
< len
/ 2; i
++){
185 char c
= state
->s
[-i
-1];
186 state
->s
[-i
-1] = state
->s
[-len
+i
];
187 state
->s
[-len
+i
] = c
;
194 append_string (struct state
*state
,
203 width
-= strlen((char *)arg
);
204 if(!(flags
& minus_flag
))
206 if((*state
->append_char
) (state
, ' '))
209 while (*arg
&& prec
--)
210 if ((*state
->append_char
) (state
, *arg
++))
214 if ((*state
->append_char
) (state
, *arg
++))
217 if(flags
& minus_flag
)
219 if((*state
->append_char
) (state
, ' '))
225 append_char(struct state
*state
,
230 while(!(flags
& minus_flag
) && --width
> 0)
231 if((*state
->append_char
) (state
, ' '))
234 if((*state
->append_char
) (state
, arg
))
236 while((flags
& minus_flag
) && --width
> 0)
237 if((*state
->append_char
) (state
, ' '))
244 * This can't be made into a function...
247 #define PARSE_INT_FORMAT(res, arg, unsig) \
249 res = (unsig long)va_arg(arg, unsig long); \
250 else if (short_flag) \
251 res = (unsig short)va_arg(arg, unsig int); \
253 res = (unsig int)va_arg(arg, unsig int)
256 * zyxprintf - return 0 or -1
260 xyzprintf (struct state
*state
, const char *char_format
, va_list ap
)
262 const unsigned char *format
= (const unsigned char *)char_format
;
265 while((c
= *format
++)) {
274 while((c
= *format
++)){
282 flags
|= alternate_flag
;
289 if((flags
& space_flag
) && (flags
& plus_flag
))
292 if((flags
& minus_flag
) && (flags
& zero_flag
))
298 width
= width
* 10 + c
- '0';
302 width
= va_arg(ap
, int);
312 prec
= prec
* 10 + c
- '0';
316 prec
= va_arg(ap
, int);
326 } else if (c
== 'l') {
333 if(append_char(state
, va_arg(ap
, int), width
, flags
))
337 if (append_string(state
,
338 va_arg(ap
, unsigned char*),
350 PARSE_INT_FORMAT(arg
, ap
, signed);
358 if (append_number (state
, num
, 10, "0123456789",
359 width
, prec
, flags
, minusp
))
366 PARSE_INT_FORMAT(arg
, ap
, unsigned);
368 if (append_number (state
, arg
, 10, "0123456789",
369 width
, prec
, flags
, 0))
376 PARSE_INT_FORMAT(arg
, ap
, unsigned);
378 if (append_number (state
, arg
, 010, "01234567",
379 width
, prec
, flags
, 0))
386 PARSE_INT_FORMAT(arg
, ap
, unsigned);
388 if (append_number (state
, arg
, 0x10, "0123456789abcdef",
389 width
, prec
, flags
, 0))
396 PARSE_INT_FORMAT(arg
, ap
, unsigned);
398 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
399 width
, prec
, flags
, 0))
404 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
406 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
407 width
, prec
, flags
, 0))
412 int *arg
= va_arg(ap
, int *);
413 *arg
= state
->s
- state
->str
;
420 if ((*state
->append_char
)(state
, c
))
424 if ( (*state
->append_char
)(state
, '%')
425 || (*state
->append_char
)(state
, c
))
430 if ((*state
->append_char
) (state
, c
))
437 #ifndef HAVE_ASPRINTF
439 asprintf (char **ret
, const char *format
, ...)
444 va_start(args
, format
);
445 val
= vasprintf (ret
, format
, args
);
451 tmp
= malloc (val
+ 1);
455 ret2
= vsprintf (tmp
, format
, args
);
456 if (val
!= ret2
|| strcmp(*ret
, tmp
))
467 #ifndef HAVE_VASNPRINTF
469 nd_vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
475 state
.max_sz
= max_sz
;
477 state
.str
= malloc(state
.sz
);
478 if (state
.str
== NULL
) {
483 state
.theend
= state
.s
+ state
.sz
- 1;
484 state
.append_char
= as_append_char
;
485 state
.reserve
= as_reserve
;
487 st
= xyzprintf (&state
, format
, args
);
496 len
= state
.s
- state
.str
;
497 tmp
= realloc (state
.str
, len
+1);