]> git.ipfire.org Git - thirdparty/bird.git/blame - lib/printf.c
Added library progdocs.
[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
121 * format specifiers: |%I| for formatting of IP addresses and |%M| for
122 * error messages (uses strerror() to translate @errno code to
123 * message text). On the other hand, it doesn't support floating
124 * point numbers.
125 *
126 * Result: number of characters of the output string or -1 if
127 * the buffer space was insufficient.
128 */
53a416d3 129int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
ecacdfa4
MM
130{
131 int len;
132 unsigned long num;
133 int i, base;
53a416d3 134 char *str, *start;
ecacdfa4
MM
135 const char *s;
136
137 int flags; /* flags to number() */
138
139 int field_width; /* width of output field */
140 int precision; /* min. # of digits for integers; max
141 number of chars for from string */
142 int qualifier; /* 'h', 'l', or 'L' for integer fields */
143
53a416d3 144 for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
ecacdfa4 145 if (*fmt != '%') {
53a416d3
MM
146 if (!size)
147 return -1;
ecacdfa4
MM
148 *str++ = *fmt;
149 continue;
150 }
151
152 /* process flags */
153 flags = 0;
154 repeat:
155 ++fmt; /* this also skips first '%' */
156 switch (*fmt) {
157 case '-': flags |= LEFT; goto repeat;
158 case '+': flags |= PLUS; goto repeat;
159 case ' ': flags |= SPACE; goto repeat;
160 case '#': flags |= SPECIAL; goto repeat;
161 case '0': flags |= ZEROPAD; goto repeat;
162 }
163
164 /* get field width */
165 field_width = -1;
166 if (is_digit(*fmt))
167 field_width = skip_atoi(&fmt);
168 else if (*fmt == '*') {
169 ++fmt;
170 /* it's the next argument */
171 field_width = va_arg(args, int);
172 if (field_width < 0) {
173 field_width = -field_width;
174 flags |= LEFT;
175 }
176 }
177
178 /* get the precision */
179 precision = -1;
180 if (*fmt == '.') {
181 ++fmt;
182 if (is_digit(*fmt))
183 precision = skip_atoi(&fmt);
184 else if (*fmt == '*') {
185 ++fmt;
186 /* it's the next argument */
187 precision = va_arg(args, int);
188 }
189 if (precision < 0)
190 precision = 0;
191 }
192
193 /* get the conversion qualifier */
194 qualifier = -1;
195 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
196 qualifier = *fmt;
197 ++fmt;
198 }
199
200 /* default base */
201 base = 10;
202
53a416d3
MM
203 if (field_width > size)
204 return -1;
ecacdfa4
MM
205 switch (*fmt) {
206 case 'c':
207 if (!(flags & LEFT))
208 while (--field_width > 0)
209 *str++ = ' ';
210 *str++ = (unsigned char) va_arg(args, int);
211 while (--field_width > 0)
212 *str++ = ' ';
213 continue;
214
d997534f
MM
215 case 'm':
216 s = strerror(errno);
217 goto str;
53943a00
MM
218 case 'M':
219 s = strerror(va_arg(args, int));
220 goto str;
ecacdfa4
MM
221 case 's':
222 s = va_arg(args, char *);
223 if (!s)
224 s = "<NULL>";
225
d997534f 226 str:
ecacdfa4
MM
227 len = strlen(s);
228 if (precision >= 0 && len > precision)
229 len = precision;
53a416d3
MM
230 if (len > size)
231 return -1;
ecacdfa4
MM
232
233 if (!(flags & LEFT))
234 while (len < field_width--)
235 *str++ = ' ';
236 for (i = 0; i < len; ++i)
237 *str++ = *s++;
238 while (len < field_width--)
239 *str++ = ' ';
240 continue;
241
242 case 'p':
243 if (field_width == -1) {
244 field_width = 2*sizeof(void *);
245 flags |= ZEROPAD;
246 }
247 str = number(str,
248 (unsigned long) va_arg(args, void *), 16,
53a416d3
MM
249 field_width, precision, flags, size);
250 if (!str)
251 return -1;
ecacdfa4
MM
252 continue;
253
254
255 case 'n':
256 if (qualifier == 'l') {
257 long * ip = va_arg(args, long *);
258 *ip = (str - buf);
259 } else {
260 int * ip = va_arg(args, int *);
261 *ip = (str - buf);
262 }
263 continue;
264
265 /* IP address */
266 case 'I':
53a416d3
MM
267 if (size < STD_ADDRESS_P_LENGTH)
268 return -1;
ecacdfa4
MM
269 if (flags & SPECIAL)
270 str = ip_ntox(va_arg(args, ip_addr), str);
271 else {
272 len = ip_ntop(va_arg(args, ip_addr), str) - str;
273 str += len;
274 if (field_width >= 0)
275 while (len++ < STD_ADDRESS_P_LENGTH)
276 *str++ = ' ';
277 }
278 continue;
279
280 /* integer number formats - set up the flags and "break" */
281 case 'o':
282 base = 8;
283 break;
284
285 case 'X':
286 flags |= LARGE;
287 case 'x':
288 base = 16;
289 break;
290
291 case 'd':
292 case 'i':
293 flags |= SIGN;
294 case 'u':
295 break;
296
297 default:
53a416d3
MM
298 if (size < 2)
299 return -1;
ecacdfa4
MM
300 if (*fmt != '%')
301 *str++ = '%';
302 if (*fmt)
303 *str++ = *fmt;
304 else
305 --fmt;
306 continue;
307 }
308 if (qualifier == 'l')
309 num = va_arg(args, unsigned long);
310 else if (qualifier == 'h') {
4254dc45 311 num = (unsigned short) va_arg(args, int);
ecacdfa4 312 if (flags & SIGN)
4254dc45 313 num = (short) num;
ecacdfa4
MM
314 } else if (flags & SIGN)
315 num = va_arg(args, int);
316 else
317 num = va_arg(args, unsigned int);
53a416d3
MM
318 str = number(str, num, base, field_width, precision, flags, size);
319 if (!str)
320 return -1;
ecacdfa4 321 }
53a416d3
MM
322 if (!size)
323 return -1;
ecacdfa4
MM
324 *str = '\0';
325 return str-buf;
326}
327
7722938d
MM
328/**
329 * bvsprintf - BIRD's vsprintf()
330 * @buf: buffer
331 * @fmt: format string
332 * @args: a list of arguments to be formatted
333 *
334 * This function is equivalent to bvsnprintf() with an infinite
335 * buffer size. Please use carefully only when you are absolutely
336 * sure the buffer won't overflow.
337 */
53a416d3
MM
338int bvsprintf(char *buf, const char *fmt, va_list args)
339{
340 return bvsnprintf(buf, 1000000000, fmt, args);
341}
342
7722938d
MM
343/**
344 * bsprintf - BIRD's sprintf()
345 * @buf: buffer
346 * @fmt: format string
347 *
348 * This function is equivalent to bvsnprintf() with an infinite
349 * buffer size and variable arguments instead of a &va_list.
350 * Please use carefully only when you are absolutely
351 * sure the buffer won't overflow.
352 */
ecacdfa4
MM
353int bsprintf(char * buf, const char *fmt, ...)
354{
53a416d3
MM
355 va_list args;
356 int i;
ecacdfa4 357
53a416d3
MM
358 va_start(args, fmt);
359 i=bvsnprintf(buf, 1000000000, fmt, args);
360 va_end(args);
361 return i;
362}
363
7722938d
MM
364/**
365 * bsnprintf - BIRD's snprintf()
366 * @buf: buffer
367 * @size: buffer size
368 * @fmt: format string
369 *
370 * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list.
371 */
53a416d3
MM
372int bsnprintf(char * buf, int size, const char *fmt, ...)
373{
374 va_list args;
375 int i;
376
377 va_start(args, fmt);
378 i=bvsnprintf(buf, size, fmt, args);
379 va_end(args);
380 return i;
ecacdfa4 381}