xref: /minix3/external/bsd/bind/dist/lib/lwres/print.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: print.c,v 1.6 2014/12/10 04:38:02 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 #include <config.h>
21 
22 #include <ctype.h>
23 #include <stdio.h>		/* for sprintf */
24 #include <string.h>
25 
26 #define	LWRES__PRINT_SOURCE	/* Used to get the lwres_print_* prototypes. */
27 
28 #include <lwres/stdlib.h>
29 #include <lwres/string.h>
30 
31 #include "assert_p.h"
32 #include "print_p.h"
33 
34 #define LWRES_PRINT_QUADFORMAT LWRES_PLATFORM_QUADFORMAT
35 
36 int
lwres__print_sprintf(char * str,const char * format,...)37 lwres__print_sprintf(char *str, const char *format, ...) {
38 	va_list ap;
39 
40 	va_start(ap, format);
41 	vsprintf(str, format, ap);
42 	va_end(ap);
43 	return (strlen(str));
44 }
45 
46 /*
47  * Return length of string that would have been written if not truncated.
48  */
49 
50 int
lwres__print_snprintf(char * str,size_t size,const char * format,...)51 lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
52 	va_list ap;
53 	int ret;
54 
55 	va_start(ap, format);
56 	ret = vsnprintf(str, size, format, ap);
57 	va_end(ap);
58 	return (ret);
59 
60 }
61 
62 /*
63  * Return length of string that would have been written if not truncated.
64  */
65 
66 int
lwres__print_vsnprintf(char * str,size_t size,const char * format,va_list ap)67 lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
68 	int h;
69 	int l;
70 	int q;
71 	int alt;
72 	int zero;
73 	int left;
74 	int plus;
75 	int space;
76 	long long tmpi;
77 	unsigned long long tmpui;
78 	unsigned long width;
79 	unsigned long precision;
80 	unsigned int length;
81 	char buf[1024];
82 	char c;
83 	void *v;
84 	char *save = str;
85 	const char *cp;
86 	const char *head;
87 	int count = 0;
88 	int pad;
89 	int zeropad;
90 	int dot;
91 	double dbl;
92 #ifdef HAVE_LONG_DOUBLE
93 	long double ldbl;
94 #endif
95 	char fmt[32];
96 
97 	INSIST(str != NULL);
98 	INSIST(format != NULL);
99 
100 	while (*format != '\0') {
101 		if (*format != '%') {
102 			if (size > 1U) {
103 				*str++ = *format;
104 				size--;
105 			}
106 			count++;
107 			format++;
108 			continue;
109 		}
110 		format++;
111 
112 		/*
113 		 * Reset flags.
114 		 */
115 		dot = space = plus = left = zero = alt = h = l = q = 0;
116 		width = precision = 0;
117 		head = "";
118 		length = pad = zeropad = 0;
119 		POST(length);
120 
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);
144 
145 		/*
146 		 * Width.
147 		 */
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;
155 		}
156 
157 		/*
158 		 * Precision.
159 		 */
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;
170 			}
171 		}
172 
173 		switch (*format) {
174 		case '\0':
175 			continue;
176 		case '%':
177 			if (size > 1U) {
178 				*str++ = *format;
179 				size--;
180 			}
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++;
197 			}
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;
226 				}
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;
247 				}
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;
287 				}
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;
303 				}
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;
319 					}
320 				}
321 				count += strlen(head) + strlen(buf) + pad +
322 					 zeropad;
323 				if (!left) {
324 					while (pad > 0 && size > 1U) {
325 						*str++ = ' ';
326 						size--;
327 						pad--;
328 					}
329 				}
330 				cp = head;
331 				while (*cp != '\0' && size > 1U) {
332 					*str++ = *cp++;
333 					size--;
334 				}
335 				while (zeropad > 0 && size > 1U) {
336 					*str++ = '0';
337 					size--;
338 					zeropad--;
339 				}
340 				cp = buf;
341 				while (*cp != '\0' && size > 1U) {
342 					*str++ = *cp++;
343 					size--;
344 				}
345 				while (pad > 0 && size > 1U) {
346 					*str++ = ' ';
347 					size--;
348 					pad--;
349 				}
350 				break;
351 			default:
352 				break;
353 			}
354 			break;
355 		case 's':
356 			cp = va_arg(ap, char *);
357 			REQUIRE(cp != NULL);
358 
359 			if (precision != 0U) {
360 				/*
361 				 * cp need not be NULL terminated.
362 				 */
363 				const char *tp;
364 				unsigned long n;
365 
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);
373 			}
374 			if (width != 0U) {
375 				pad = width - length;
376 				if (pad < 0)
377 					pad = 0;
378 			}
379 			count += pad + length;
380 			if (!left)
381 				while (pad > 0 && size > 1U) {
382 					*str++ = ' ';
383 					size--;
384 					pad--;
385 				}
386 			if (precision != 0U)
387 				while (precision > 0U && *cp != '\0' &&
388 				       size > 1U) {
389 					*str++ = *cp++;
390 					size--;
391 					precision--;
392 				}
393 			else
394 				while (*cp != '\0' && size > 1U) {
395 					*str++ = *cp++;
396 					size--;
397 				}
398 			while (pad > 0 && size > 1U) {
399 				*str++ = ' ';
400 				size--;
401 				pad--;
402 			}
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--;
412 				}
413 				while (width-- > 0U && size > 1U) {
414 					*str++ = ' ';
415 					size--;
416 				}
417 				if (!left && size > 1U) {
418 					*str++ = c;
419 					size--;
420 				}
421 			} else {
422 				count++;
423 				if (size > 1U) {
424 					*str++ = c;
425 					size--;
426 				}
427 			}
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;
439 			}
440 			count += length + pad + zeropad;
441 			if (!left)
442 				while (pad > 0 && size > 1U) {
443 					*str++ = ' ';
444 					size--;
445 					pad--;
446 				}
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--;
453 				}
454 				if (size > 1U) {
455 					*str++ = *cp++;
456 					size--;
457 				}
458 				while (zeropad > 0 && size > 1U) {
459 					*str++ = '0';
460 					size--;
461 					zeropad--;
462 				}
463 			}
464 			while (*cp != '\0' && size > 1U) {
465 				*str++ = *cp++;
466 				size--;
467 			}
468 			while (pad > 0 && size > 1U) {
469 				*str++ = ' ';
470 				size--;
471 				pad--;
472 			}
473 			break;
474 
475 		case 'D':	/*deprecated*/
476 			INSIST("use %ld instead of %D" == NULL);
477 			break;
478 		case 'O':	/*deprecated*/
479 			INSIST("use %lo instead of %O" == NULL);
480 			break;
481 		case 'U':	/*deprecated*/
482 			INSIST("use %lu instead of %U" == NULL);
483 			break;
484 
485 		case 'L':
486 #ifdef HAVE_LONG_DOUBLE
487 			l = 1;
488 #else
489 			INSIST("long doubles are not supported" == NULL);
490 #endif
491 			/*FALLTHROUGH*/
492 		case 'e':
493 		case 'E':
494 		case 'f':
495 		case 'g':
496 		case 'G':
497 			if (!dot)
498 				precision = 6;
499 			/*
500 			 * IEEE floating point.
501 			 * MIN 2.2250738585072014E-308
502 			 * MAX 1.7976931348623157E+308
503 			 * VAX floating point has a smaller range than IEEE.
504 			 *
505 			 * precisions > 324 don't make much sense.
506 			 * if we cap the precision at 512 we will not
507 			 * overflow buf.
508 			 */
509 			if (precision > 512U)
510 				precision = 512;
511 			sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
512 				plus ? "+" : space ? " " : "",
513 				precision, l ? "L" : "", *format);
514 			switch (*format) {
515 			case 'e':
516 			case 'E':
517 			case 'f':
518 			case 'g':
519 			case 'G':
520 #ifdef HAVE_LONG_DOUBLE
521 				if (l) {
522 					ldbl = va_arg(ap, long double);
523 					sprintf(buf, fmt, ldbl);
524 				} else
525 #endif
526 				{
527 					dbl = va_arg(ap, double);
528 					sprintf(buf, fmt, dbl);
529 				}
530 				length = strlen(buf);
531 				if (width > 0U) {
532 					pad = width - length;
533 					if (pad < 0)
534 						pad = 0;
535 				}
536 				count += length + pad;
537 				if (!left)
538 					while (pad > 0 && size > 1U) {
539 						*str++ = ' ';
540 						size--;
541 						pad--;
542 					}
543 				cp = buf;
544 				while (*cp != ' ' && size > 1U) {
545 					*str++ = *cp++;
546 					size--;
547 				}
548 				while (pad > 0 && size > 1U) {
549 					*str++ = ' ';
550 					size--;
551 					pad--;
552 				}
553 				break;
554 			default:
555 				continue;
556 			}
557 			break;
558 		default:
559 			continue;
560 		}
561 		format++;
562 	}
563 	if (size > 0U)
564 		*str = '\0';
565 	return (count);
566 }
567