]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/printf.c
Adds %R printf directive for Router ID.
[thirdparty/bird.git] / lib / printf.c
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>
7 * Buffer size limitation implemented by Martin Mares.
8 */
9
10 #include "nest/bird.h"
11 #include "string.h"
12
13 #include <errno.h>
14
15 /* we use this so that we can do without the ctype library */
16 #define is_digit(c) ((c) >= '0' && (c) <= '9')
17
18 static 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) ({ \
36 int __res; \
37 __res = ((unsigned long) n) % (unsigned) base; \
38 n = ((unsigned long) n) / (unsigned) base; \
39 __res; })
40
41 static char * number(char * str, long num, int base, int size, int precision,
42 int type, int remains)
43 {
44 char c,sign,tmp[66];
45 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
46 int i;
47
48 if (size >= 0 && (remains -= size) < 0)
49 return NULL;
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;
85 if (size < 0 && -size > remains)
86 return NULL;
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
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
121 * format specifiers: |%I| for formatting of IP addresses (any non-zero
122 * width is automatically replaced by standard IP address width which
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)
125 * and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to
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 */
132 int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
133 {
134 int len;
135 unsigned long num;
136 int i, base;
137 u32 x;
138 char *str, *start;
139 const char *s;
140 char ipbuf[STD_ADDRESS_P_LENGTH+1];
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
149 for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
150 if (*fmt != '%') {
151 if (!size)
152 return -1;
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
208 if (field_width > size)
209 return -1;
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
220 case 'm':
221 s = strerror(errno);
222 goto str;
223 case 'M':
224 s = strerror(va_arg(args, int));
225 goto str;
226 case 's':
227 s = va_arg(args, char *);
228 if (!s)
229 s = "<NULL>";
230
231 str:
232 len = strlen(s);
233 if (precision >= 0 && len > precision)
234 len = precision;
235 if (len > size)
236 return -1;
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,
254 field_width, precision, flags, size);
255 if (!str)
256 return -1;
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)
273 ip_ntox(va_arg(args, ip_addr), ipbuf);
274 else {
275 ip_ntop(va_arg(args, ip_addr), ipbuf);
276 if (field_width > 0)
277 field_width = STD_ADDRESS_P_LENGTH;
278 }
279 s = ipbuf;
280 goto str;
281
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
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:
311 if (size < 2)
312 return -1;
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') {
324 num = (unsigned short) va_arg(args, int);
325 if (flags & SIGN)
326 num = (short) num;
327 } else if (flags & SIGN)
328 num = va_arg(args, int);
329 else
330 num = va_arg(args, unsigned int);
331 str = number(str, num, base, field_width, precision, flags, size);
332 if (!str)
333 return -1;
334 }
335 if (!size)
336 return -1;
337 *str = '\0';
338 return str-buf;
339 }
340
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 */
351 int bvsprintf(char *buf, const char *fmt, va_list args)
352 {
353 return bvsnprintf(buf, 1000000000, fmt, args);
354 }
355
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 */
366 int bsprintf(char * buf, const char *fmt, ...)
367 {
368 va_list args;
369 int i;
370
371 va_start(args, fmt);
372 i=bvsnprintf(buf, 1000000000, fmt, args);
373 va_end(args);
374 return i;
375 }
376
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 */
385 int 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;
394 }