]> git.ipfire.org Git - thirdparty/bird.git/blame - lib/printf.c
Adds %R printf directive for Router ID.
[thirdparty/bird.git] / lib / printf.c
CommitLineData
ecacdfa4
MM
1/*
2 * BIRD Library -- Formatted Output
3 *
4 * (c) 1991, 1992 Lars Wirzenius & Linus Torvalds
5 *
6 * Hacked up for BIRD by Martin Mares <mj@ucw.cz>
53a416d3 7 * Buffer size limitation implemented by Martin Mares.
ecacdfa4
MM
8 */
9
10#include "nest/bird.h"
11#include "string.h"
12
d997534f 13#include <errno.h>
d997534f 14
ecacdfa4
MM
15/* we use this so that we can do without the ctype library */
16#define is_digit(c) ((c) >= '0' && (c) <= '9')
17
18static int skip_atoi(const char **s)
19{
20 int i=0;
21
22 while (is_digit(**s))
23 i = i*10 + *((*s)++) - '0';
24 return i;
25}
26
27#define ZEROPAD 1 /* pad with zero */
28#define SIGN 2 /* unsigned/signed long */
29#define PLUS 4 /* show plus */
30#define SPACE 8 /* space if plus */
31#define LEFT 16 /* left justified */
32#define SPECIAL 32 /* 0x */
33#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
34
35#define do_div(n,base) ({ \
36int __res; \
37__res = ((unsigned long) n) % (unsigned) base; \
38n = ((unsigned long) n) / (unsigned) base; \
39__res; })
40
53a416d3
MM
41static char * number(char * str, long num, int base, int size, int precision,
42 int type, int remains)
ecacdfa4
MM
43{
44 char c,sign,tmp[66];
45 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
46 int i;
47
53a416d3
MM
48 if (size >= 0 && (remains -= size) < 0)
49 return NULL;
ecacdfa4
MM
50 if (type & LARGE)
51 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52 if (type & LEFT)
53 type &= ~ZEROPAD;
54 if (base < 2 || base > 36)
55 return 0;
56 c = (type & ZEROPAD) ? '0' : ' ';
57 sign = 0;
58 if (type & SIGN) {
59 if (num < 0) {
60 sign = '-';
61 num = -num;
62 size--;
63 } else if (type & PLUS) {
64 sign = '+';
65 size--;
66 } else if (type & SPACE) {
67 sign = ' ';
68 size--;
69 }
70 }
71 if (type & SPECIAL) {
72 if (base == 16)
73 size -= 2;
74 else if (base == 8)
75 size--;
76 }
77 i = 0;
78 if (num == 0)
79 tmp[i++]='0';
80 else while (num != 0)
81 tmp[i++] = digits[do_div(num,base)];
82 if (i > precision)
83 precision = i;
84 size -= precision;
53a416d3
MM
85 if (size < 0 && -size > remains)
86 return NULL;
ecacdfa4
MM
87 if (!(type&(ZEROPAD+LEFT)))
88 while(size-->0)
89 *str++ = ' ';
90 if (sign)
91 *str++ = sign;
92 if (type & SPECIAL) {
93 if (base==8)
94 *str++ = '0';
95 else if (base==16) {
96 *str++ = '0';
97 *str++ = digits[33];
98 }
99 }
100 if (!(type & LEFT))
101 while (size-- > 0)
102 *str++ = c;
103 while (i < precision--)
104 *str++ = '0';
105 while (i-- > 0)
106 *str++ = tmp[i];
107 while (size-- > 0)
108 *str++ = ' ';
109 return str;
110}
111
7722938d
MM
112/**
113 * bvsnprintf - BIRD's vsnprintf()
114 * @buf: destination buffer
115 * @size: size of the buffer
116 * @fmt: format string
117 * @args: a list of arguments to be formatted
118 *
119 * This functions acts like ordinary sprintf() except that it checks
120 * available space to avoid buffer overflows and it allows some more
30b77304
MM
121 * format specifiers: |%I| for formatting of IP addresses (any non-zero
122 * width is automatically replaced by standard IP address width which
2f6483cd
OZ
123 * depends on whether we use IPv4 or IPv6; |%#I| gives hexadecimal format),
124 * |%R| for Router / Network ID (u32 value printed as IPv4 address)
30b77304 125 * and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to
7722938d
MM
126 * message text). On the other hand, it doesn't support floating
127 * point numbers.
128 *
129 * Result: number of characters of the output string or -1 if
130 * the buffer space was insufficient.
131 */
53a416d3 132int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
ecacdfa4
MM
133{
134 int len;
135 unsigned long num;
136 int i, base;
2f6483cd 137 u32 x;
53a416d3 138 char *str, *start;
ecacdfa4 139 const char *s;
30b77304 140 char ipbuf[STD_ADDRESS_P_LENGTH+1];
ecacdfa4
MM
141
142 int flags; /* flags to number() */
143
144 int field_width; /* width of output field */
145 int precision; /* min. # of digits for integers; max
146 number of chars for from string */
147 int qualifier; /* 'h', 'l', or 'L' for integer fields */
148
53a416d3 149 for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
ecacdfa4 150 if (*fmt != '%') {
53a416d3
MM
151 if (!size)
152 return -1;
ecacdfa4
MM
153 *str++ = *fmt;
154 continue;
155 }
156
157 /* process flags */
158 flags = 0;
159 repeat:
160 ++fmt; /* this also skips first '%' */
161 switch (*fmt) {
162 case '-': flags |= LEFT; goto repeat;
163 case '+': flags |= PLUS; goto repeat;
164 case ' ': flags |= SPACE; goto repeat;
165 case '#': flags |= SPECIAL; goto repeat;
166 case '0': flags |= ZEROPAD; goto repeat;
167 }
168
169 /* get field width */
170 field_width = -1;
171 if (is_digit(*fmt))
172 field_width = skip_atoi(&fmt);
173 else if (*fmt == '*') {
174 ++fmt;
175 /* it's the next argument */
176 field_width = va_arg(args, int);
177 if (field_width < 0) {
178 field_width = -field_width;
179 flags |= LEFT;
180 }
181 }
182
183 /* get the precision */
184 precision = -1;
185 if (*fmt == '.') {
186 ++fmt;
187 if (is_digit(*fmt))
188 precision = skip_atoi(&fmt);
189 else if (*fmt == '*') {
190 ++fmt;
191 /* it's the next argument */
192 precision = va_arg(args, int);
193 }
194 if (precision < 0)
195 precision = 0;
196 }
197
198 /* get the conversion qualifier */
199 qualifier = -1;
200 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
201 qualifier = *fmt;
202 ++fmt;
203 }
204
205 /* default base */
206 base = 10;
207
53a416d3
MM
208 if (field_width > size)
209 return -1;
ecacdfa4
MM
210 switch (*fmt) {
211 case 'c':
212 if (!(flags & LEFT))
213 while (--field_width > 0)
214 *str++ = ' ';
215 *str++ = (unsigned char) va_arg(args, int);
216 while (--field_width > 0)
217 *str++ = ' ';
218 continue;
219
d997534f
MM
220 case 'm':
221 s = strerror(errno);
222 goto str;
53943a00
MM
223 case 'M':
224 s = strerror(va_arg(args, int));
225 goto str;
ecacdfa4
MM
226 case 's':
227 s = va_arg(args, char *);
228 if (!s)
229 s = "<NULL>";
230
d997534f 231 str:
ecacdfa4
MM
232 len = strlen(s);
233 if (precision >= 0 && len > precision)
234 len = precision;
53a416d3
MM
235 if (len > size)
236 return -1;
ecacdfa4
MM
237
238 if (!(flags & LEFT))
239 while (len < field_width--)
240 *str++ = ' ';
241 for (i = 0; i < len; ++i)
242 *str++ = *s++;
243 while (len < field_width--)
244 *str++ = ' ';
245 continue;
246
247 case 'p':
248 if (field_width == -1) {
249 field_width = 2*sizeof(void *);
250 flags |= ZEROPAD;
251 }
252 str = number(str,
253 (unsigned long) va_arg(args, void *), 16,
53a416d3
MM
254 field_width, precision, flags, size);
255 if (!str)
256 return -1;
ecacdfa4
MM
257 continue;
258
259
260 case 'n':
261 if (qualifier == 'l') {
262 long * ip = va_arg(args, long *);
263 *ip = (str - buf);
264 } else {
265 int * ip = va_arg(args, int *);
266 *ip = (str - buf);
267 }
268 continue;
269
270 /* IP address */
271 case 'I':
272 if (flags & SPECIAL)
30b77304 273 ip_ntox(va_arg(args, ip_addr), ipbuf);
ecacdfa4 274 else {
30b77304
MM
275 ip_ntop(va_arg(args, ip_addr), ipbuf);
276 if (field_width > 0)
277 field_width = STD_ADDRESS_P_LENGTH;
ecacdfa4 278 }
30b77304
MM
279 s = ipbuf;
280 goto str;
ecacdfa4 281
2f6483cd
OZ
282 /* Router/Network ID - essentially IPv4 address in u32 value */
283 case 'R':
284 x = va_arg(args, u32);
285 bsprintf(ipbuf, "%d.%d.%d.%d",
286 ((x >> 24) & 0xff),
287 ((x >> 16) & 0xff),
288 ((x >> 8) & 0xff),
289 (x & 0xff));
290 s = ipbuf;
291 goto str;
292
ecacdfa4
MM
293 /* integer number formats - set up the flags and "break" */
294 case 'o':
295 base = 8;
296 break;
297
298 case 'X':
299 flags |= LARGE;
300 case 'x':
301 base = 16;
302 break;
303
304 case 'd':
305 case 'i':
306 flags |= SIGN;
307 case 'u':
308 break;
309
310 default:
53a416d3
MM
311 if (size < 2)
312 return -1;
ecacdfa4
MM
313 if (*fmt != '%')
314 *str++ = '%';
315 if (*fmt)
316 *str++ = *fmt;
317 else
318 --fmt;
319 continue;
320 }
321 if (qualifier == 'l')
322 num = va_arg(args, unsigned long);
323 else if (qualifier == 'h') {
4254dc45 324 num = (unsigned short) va_arg(args, int);
ecacdfa4 325 if (flags & SIGN)
4254dc45 326 num = (short) num;
ecacdfa4
MM
327 } else if (flags & SIGN)
328 num = va_arg(args, int);
329 else
330 num = va_arg(args, unsigned int);
53a416d3
MM
331 str = number(str, num, base, field_width, precision, flags, size);
332 if (!str)
333 return -1;
ecacdfa4 334 }
53a416d3
MM
335 if (!size)
336 return -1;
ecacdfa4
MM
337 *str = '\0';
338 return str-buf;
339}
340
7722938d
MM
341/**
342 * bvsprintf - BIRD's vsprintf()
343 * @buf: buffer
344 * @fmt: format string
345 * @args: a list of arguments to be formatted
346 *
347 * This function is equivalent to bvsnprintf() with an infinite
348 * buffer size. Please use carefully only when you are absolutely
349 * sure the buffer won't overflow.
350 */
53a416d3
MM
351int bvsprintf(char *buf, const char *fmt, va_list args)
352{
353 return bvsnprintf(buf, 1000000000, fmt, args);
354}
355
7722938d
MM
356/**
357 * bsprintf - BIRD's sprintf()
358 * @buf: buffer
359 * @fmt: format string
360 *
361 * This function is equivalent to bvsnprintf() with an infinite
362 * buffer size and variable arguments instead of a &va_list.
363 * Please use carefully only when you are absolutely
364 * sure the buffer won't overflow.
365 */
ecacdfa4
MM
366int bsprintf(char * buf, const char *fmt, ...)
367{
53a416d3
MM
368 va_list args;
369 int i;
ecacdfa4 370
53a416d3
MM
371 va_start(args, fmt);
372 i=bvsnprintf(buf, 1000000000, fmt, args);
373 va_end(args);
374 return i;
375}
376
7722938d
MM
377/**
378 * bsnprintf - BIRD's snprintf()
379 * @buf: buffer
380 * @size: buffer size
381 * @fmt: format string
382 *
383 * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list.
384 */
53a416d3
MM
385int bsnprintf(char * buf, int size, const char *fmt, ...)
386{
387 va_list args;
388 int i;
389
390 va_start(args, fmt);
391 i=bvsnprintf(buf, size, fmt, args);
392 va_end(args);
393 return i;
ecacdfa4 394}