No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / lwres / print.c
blob6cb7b9a3f0b92c17af63b5325f287fb94cd45968
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: print.c,v 1.10 2007/06/19 23:47:22 tbox Exp */
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdio.h> /* for sprintf */
26 #include <string.h>
28 #define LWRES__PRINT_SOURCE /* Used to get the lwres_print_* prototypes. */
30 #include <lwres/stdlib.h>
32 #include "assert_p.h"
33 #include "print_p.h"
35 #define LWRES_PRINT_QUADFORMAT LWRES_PLATFORM_QUADFORMAT
37 int
38 lwres__print_sprintf(char *str, const char *format, ...) {
39 va_list ap;
41 va_start(ap, format);
42 vsprintf(str, format, ap);
43 va_end(ap);
44 return (strlen(str));
48 * Return length of string that would have been written if not truncated.
51 int
52 lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
53 va_list ap;
54 int ret;
56 va_start(ap, format);
57 ret = vsnprintf(str, size, format, ap);
58 va_end(ap);
59 return (ret);
64 * Return length of string that would have been written if not truncated.
67 int
68 lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
69 int h;
70 int l;
71 int q;
72 int alt;
73 int zero;
74 int left;
75 int plus;
76 int space;
77 long long tmpi;
78 unsigned long long tmpui;
79 unsigned long width;
80 unsigned long precision;
81 unsigned int length;
82 char buf[1024];
83 char c;
84 void *v;
85 char *save = str;
86 const char *cp;
87 const char *head;
88 int count = 0;
89 int pad;
90 int zeropad;
91 int dot;
92 double dbl;
93 #ifdef HAVE_LONG_DOUBLE
94 long double ldbl;
95 #endif
96 char fmt[32];
98 INSIST(str != NULL);
99 INSIST(format != NULL);
101 while (*format != '\0') {
102 if (*format != '%') {
103 if (size > 1U) {
104 *str++ = *format;
105 size--;
107 count++;
108 format++;
109 continue;
111 format++;
114 * Reset flags.
116 dot = space = plus = left = zero = alt = h = l = q = 0;
117 width = precision = 0;
118 head = "";
119 length = pad = zeropad = 0;
121 do {
122 if (*format == '#') {
123 alt = 1;
124 format++;
125 } else if (*format == '-') {
126 left = 1;
127 zero = 0;
128 format++;
129 } else if (*format == ' ') {
130 if (!plus)
131 space = 1;
132 format++;
133 } else if (*format == '+') {
134 plus = 1;
135 space = 0;
136 format++;
137 } else if (*format == '0') {
138 if (!left)
139 zero = 1;
140 format++;
141 } else
142 break;
143 } while (1);
146 * Width.
148 if (*format == '*') {
149 width = va_arg(ap, int);
150 format++;
151 } else if (isdigit((unsigned char)*format)) {
152 char *e;
153 width = strtoul(format, &e, 10);
154 format = e;
158 * Precision.
160 if (*format == '.') {
161 format++;
162 dot = 1;
163 if (*format == '*') {
164 precision = va_arg(ap, int);
165 format++;
166 } else if (isdigit((unsigned char)*format)) {
167 char *e;
168 precision = strtoul(format, &e, 10);
169 format = e;
173 switch (*format) {
174 case '\0':
175 continue;
176 case '%':
177 if (size > 1U) {
178 *str++ = *format;
179 size--;
181 count++;
182 break;
183 case 'q':
184 q = 1;
185 format++;
186 goto doint;
187 case 'h':
188 h = 1;
189 format++;
190 goto doint;
191 case 'l':
192 l = 1;
193 format++;
194 if (*format == 'l') {
195 q = 1;
196 format++;
198 goto doint;
199 case 'n':
200 case 'i':
201 case 'd':
202 case 'o':
203 case 'u':
204 case 'x':
205 case 'X':
206 doint:
207 if (precision != 0U)
208 zero = 0;
209 switch (*format) {
210 case 'n':
211 if (h) {
212 short int *p;
213 p = va_arg(ap, short *);
214 REQUIRE(p != NULL);
215 *p = str - save;
216 } else if (l) {
217 long int *p;
218 p = va_arg(ap, long *);
219 REQUIRE(p != NULL);
220 *p = str - save;
221 } else {
222 int *p;
223 p = va_arg(ap, int *);
224 REQUIRE(p != NULL);
225 *p = str - save;
227 break;
228 case 'i':
229 case 'd':
230 if (q)
231 tmpi = va_arg(ap, long long int);
232 else if (l)
233 tmpi = va_arg(ap, long int);
234 else
235 tmpi = va_arg(ap, int);
236 if (tmpi < 0) {
237 head = "-";
238 tmpui = -tmpi;
239 } else {
240 if (plus)
241 head = "+";
242 else if (space)
243 head = " ";
244 else
245 head = "";
246 tmpui = tmpi;
248 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
249 tmpui);
250 goto printint;
251 case 'o':
252 if (q)
253 tmpui = va_arg(ap,
254 unsigned long long int);
255 else if (l)
256 tmpui = va_arg(ap, long int);
257 else
258 tmpui = va_arg(ap, int);
259 sprintf(buf,
260 alt ? "%#" LWRES_PRINT_QUADFORMAT "o"
261 : "%" LWRES_PRINT_QUADFORMAT "o",
262 tmpui);
263 goto printint;
264 case 'u':
265 if (q)
266 tmpui = va_arg(ap,
267 unsigned long long int);
268 else if (l)
269 tmpui = va_arg(ap, unsigned long int);
270 else
271 tmpui = va_arg(ap, unsigned int);
272 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
273 tmpui);
274 goto printint;
275 case 'x':
276 if (q)
277 tmpui = va_arg(ap,
278 unsigned long long int);
279 else if (l)
280 tmpui = va_arg(ap, unsigned long int);
281 else
282 tmpui = va_arg(ap, unsigned int);
283 if (alt) {
284 head = "0x";
285 if (precision > 2U)
286 precision -= 2;
288 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "x",
289 tmpui);
290 goto printint;
291 case 'X':
292 if (q)
293 tmpui = va_arg(ap,
294 unsigned long long int);
295 else if (l)
296 tmpui = va_arg(ap, unsigned long int);
297 else
298 tmpui = va_arg(ap, unsigned int);
299 if (alt) {
300 head = "0X";
301 if (precision > 2U)
302 precision -= 2;
304 sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "X",
305 tmpui);
306 goto printint;
307 printint:
308 if (precision != 0U || width != 0U) {
309 length = strlen(buf);
310 if (length < precision)
311 zeropad = precision - length;
312 else if (length < width && zero)
313 zeropad = width - length;
314 if (width != 0U) {
315 pad = width - length -
316 zeropad - strlen(head);
317 if (pad < 0)
318 pad = 0;
321 count += strlen(head) + strlen(buf) + pad +
322 zeropad;
323 if (!left) {
324 while (pad > 0 && size > 1U) {
325 *str++ = ' ';
326 size--;
327 pad--;
330 cp = head;
331 while (*cp != '\0' && size > 1U) {
332 *str++ = *cp++;
333 size--;
335 while (zeropad > 0 && size > 1U) {
336 *str++ = '0';
337 size--;
338 zeropad--;
340 cp = buf;
341 while (*cp != '\0' && size > 1U) {
342 *str++ = *cp++;
343 size--;
345 while (pad > 0 && size > 1U) {
346 *str++ = ' ';
347 size--;
348 pad--;
350 break;
351 default:
352 break;
354 break;
355 case 's':
356 cp = va_arg(ap, char *);
357 REQUIRE(cp != NULL);
359 if (precision != 0U) {
361 * cp need not be NULL terminated.
363 const char *tp;
364 unsigned long n;
366 n = precision;
367 tp = cp;
368 while (n != 0U && *tp != '\0')
369 n--, tp++;
370 length = precision - n;
371 } else {
372 length = strlen(cp);
374 if (width != 0U) {
375 pad = width - length;
376 if (pad < 0)
377 pad = 0;
379 count += pad + length;
380 if (!left)
381 while (pad > 0 && size > 1U) {
382 *str++ = ' ';
383 size--;
384 pad--;
386 if (precision != 0U)
387 while (precision > 0U && *cp != '\0' &&
388 size > 1U) {
389 *str++ = *cp++;
390 size--;
391 precision--;
393 else
394 while (*cp != '\0' && size > 1U) {
395 *str++ = *cp++;
396 size--;
398 while (pad > 0 && size > 1U) {
399 *str++ = ' ';
400 size--;
401 pad--;
403 break;
404 case 'c':
405 c = va_arg(ap, int);
406 if (width > 0U) {
407 count += width;
408 width--;
409 if (left) {
410 *str++ = c;
411 size--;
413 while (width-- > 0U && size > 1U) {
414 *str++ = ' ';
415 size--;
417 if (!left && size > 1U) {
418 *str++ = c;
419 size--;
421 } else {
422 count++;
423 if (size > 1U) {
424 *str++ = c;
425 size--;
428 break;
429 case 'p':
430 v = va_arg(ap, void *);
431 sprintf(buf, "%p", v);
432 length = strlen(buf);
433 if (precision > length)
434 zeropad = precision - length;
435 if (width > 0U) {
436 pad = width - length - zeropad;
437 if (pad < 0)
438 pad = 0;
440 count += length + pad + zeropad;
441 if (!left)
442 while (pad > 0 && size > 1U) {
443 *str++ = ' ';
444 size--;
445 pad--;
447 cp = buf;
448 if (zeropad > 0 && buf[0] == '0' &&
449 (buf[1] == 'x' || buf[1] == 'X')) {
450 if (size > 1U) {
451 *str++ = *cp++;
452 size--;
454 if (size > 1U) {
455 *str++ = *cp++;
456 size--;
458 while (zeropad > 0 && size > 1U) {
459 *str++ = '0';
460 size--;
461 zeropad--;
464 while (*cp != '\0' && size > 1U) {
465 *str++ = *cp++;
466 size--;
468 while (pad > 0 && size > 1U) {
469 *str++ = ' ';
470 size--;
471 pad--;
473 break;
474 case 'D': /*deprecated*/
475 INSIST("use %ld instead of %D" == NULL);
476 case 'O': /*deprecated*/
477 INSIST("use %lo instead of %O" == NULL);
478 case 'U': /*deprecated*/
479 INSIST("use %lu instead of %U" == NULL);
481 case 'L':
482 #ifdef HAVE_LONG_DOUBLE
483 l = 1;
484 #else
485 INSIST("long doubles are not supported" == NULL);
486 #endif
487 /*FALLTHROUGH*/
488 case 'e':
489 case 'E':
490 case 'f':
491 case 'g':
492 case 'G':
493 if (!dot)
494 precision = 6;
496 * IEEE floating point.
497 * MIN 2.2250738585072014E-308
498 * MAX 1.7976931348623157E+308
499 * VAX floating point has a smaller range than IEEE.
501 * precisions > 324 don't make much sense.
502 * if we cap the precision at 512 we will not
503 * overflow buf.
505 if (precision > 512U)
506 precision = 512;
507 sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
508 plus ? "+" : space ? " " : "",
509 precision, l ? "L" : "", *format);
510 switch (*format) {
511 case 'e':
512 case 'E':
513 case 'f':
514 case 'g':
515 case 'G':
516 #ifdef HAVE_LONG_DOUBLE
517 if (l) {
518 ldbl = va_arg(ap, long double);
519 sprintf(buf, fmt, ldbl);
520 } else
521 #endif
523 dbl = va_arg(ap, double);
524 sprintf(buf, fmt, dbl);
526 length = strlen(buf);
527 if (width > 0U) {
528 pad = width - length;
529 if (pad < 0)
530 pad = 0;
532 count += length + pad;
533 if (!left)
534 while (pad > 0 && size > 1U) {
535 *str++ = ' ';
536 size--;
537 pad--;
539 cp = buf;
540 while (*cp != ' ' && size > 1U) {
541 *str++ = *cp++;
542 size--;
544 while (pad > 0 && size > 1U) {
545 *str++ = ' ';
546 size--;
547 pad--;
549 break;
550 default:
551 continue;
553 break;
554 default:
555 continue;
557 format++;
559 if (size > 0U)
560 *str = '\0';
561 return (count);