print.c 10.7 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

18
/* $Id: print.c,v 1.7 2005/06/08 02:07:02 marka Exp $ */
19 20 21 22 23

#include <config.h>

#include <ctype.h>
#include <stdio.h>		/* for sprintf */
Mark Andrews's avatar
Mark Andrews committed
24
#include <string.h>
25 26 27

#define	LWRES__PRINT_SOURCE	/* Used to get the lwres_print_* prototypes. */

28
#include <lwres/stdlib.h>
29 30 31 32

#include "assert_p.h"
#include "print_p.h"

33 34
#define LWRES_PRINT_QUADFORMAT LWRES_PLATFORM_QUADFORMAT

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
int
lwres__print_sprintf(char *str, const char *format, ...) {
	va_list ap;

	va_start(ap, format);
	vsprintf(str, format, ap);
	va_end(ap);
	return (strlen(str));
}

/*
 * Return length of string that would have been written if not truncated.
 */

int
lwres__print_snprintf(char *str, size_t size, const char *format, ...) {
	va_list ap;
	int ret;

	va_start(ap, format);
	ret = vsnprintf(str, size, format, ap);
	va_end(ap);
	return (ret);

}

/*
 * Return length of string that would have been written if not truncated.
 */

int
lwres__print_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
	int h;
	int l;
	int q;
	int alt;
	int zero;
	int left;
	int plus;
	int space;
	int neg;
	long long tmpi;
	unsigned long long tmpui;
	unsigned long width;
	unsigned long precision;
	unsigned int length;
	char buf[1024];
	char c;
	void *v;
	char *save = str;
	const char *cp;
	const char *head;
	int count = 0;
	int pad;
	int zeropad;
	int dot;
	double dbl;
#ifdef HAVE_LONG_DOUBLE
	long double ldbl;
#endif
	char fmt[32];

	INSIST(str != NULL);
	INSIST(format != NULL);

	while (*format != '\0') {
		if (*format != '%') {
Mark Andrews's avatar
Mark Andrews committed
102
			if (size > 1U) {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				*str++ = *format;
				size--;
			}
			count++;
			format++;
			continue;
		}
		format++;

		/*
		 * Reset flags.
		 */
		dot = neg = space = plus = left = zero = alt = h = l = q = 0;
		width = precision = 0;
		head = "";
		length = pad = zeropad = 0;

		do {
			if (*format == '#') {
				alt = 1;
				format++;
			} else if (*format == '-') {
				left = 1;
				zero = 0;
				format++;
			} else if (*format == ' ') {
				if (!plus)
					space = 1;
				format++;
			} else if (*format == '+') {
				plus = 1;
				space = 0;
				format++;
			} else if (*format == '0') {
				if (!left)
					zero = 1;
				format++;
			} else
				break;
		} while (1);

		/*
		 * Width.
		 */
		if (*format == '*') {
			width = va_arg(ap, int);
			format++;
		} else if (isdigit((unsigned char)*format)) {
			char *e;
			width = strtoul(format, &e, 10);
			format = e;
		}

		/*
		 * Precision.
		 */
		if (*format == '.') {
			format++;
			dot = 1;
			if (*format == '*') {
				precision = va_arg(ap, int);
				format++;
			} else if (isdigit((unsigned char)*format)) {
				char *e;
				precision = strtoul(format, &e, 10);
				format = e;
			}
		}

		switch (*format) {
		case '\0':
			continue;
		case '%':
Mark Andrews's avatar
Mark Andrews committed
176
			if (size > 1U) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
				*str++ = *format;
				size--;
			}
			count++;
			break;
		case 'q':
			q = 1;
			format++;
			goto doint;
		case 'h':
			h = 1;
			format++;
			goto doint;
		case 'l':
			l = 1;
			format++;
			if (*format == 'l') {
				q = 1;
				format++;
			}
			goto doint;
		case 'n':
		case 'i':
		case 'd':
		case 'o':
		case 'u':
		case 'x':
		case 'X':
		doint:
Mark Andrews's avatar
Mark Andrews committed
206
			if (precision != 0U)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
				zero = 0;
			switch (*format) {
			case 'n':
				if (h) {
					short int *p;
					p = va_arg(ap, short *);
					REQUIRE(p != NULL);
					*p = str - save;
				} else if (l) {
					long int *p;
					p = va_arg(ap, long *);
					REQUIRE(p != NULL);
					*p = str - save;
				} else {
					int *p;
					p = va_arg(ap, int *);
					REQUIRE(p != NULL);
					*p = str - save;
				}
				break;
			case 'i':
			case 'd':
				if (q)
					tmpi = va_arg(ap, long long int);
				else if (l)
					tmpi = va_arg(ap, long int);
				else
					tmpi = va_arg(ap, int);
				if (tmpi < 0) {
					head = "-";
					tmpui = -tmpi;
				} else {
					if (plus)
						head = "+";
					else if (space)
						head = " ";
					else
						head = "";
					tmpui = tmpi;
				}
247
				sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
248 249 250 251 252 253 254 255 256 257 258
					tmpui);
				goto printint;
			case 'o':
				if (q)
					tmpui = va_arg(ap,
						       unsigned long long int);
				else if (l)
					tmpui = va_arg(ap, long int);
				else
					tmpui = va_arg(ap, int);
				sprintf(buf,
259 260 261
					alt ? "%#" LWRES_PRINT_QUADFORMAT "o"
					    : "%" LWRES_PRINT_QUADFORMAT "o",
					tmpui);
262 263 264 265 266 267 268 269 270
				goto printint;
			case 'u':
				if (q)
					tmpui = va_arg(ap,
						       unsigned long long int);
				else if (l)
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
271 272
				sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "u",
					tmpui);
273 274 275 276 277 278 279 280 281 282 283
				goto printint;
			case 'x':
				if (q)
					tmpui = va_arg(ap,
						       unsigned long long int);
				else if (l)
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
				if (alt) {
					head = "0x";
Mark Andrews's avatar
Mark Andrews committed
284
					if (precision > 2U)
285 286
						precision -= 2;
				}
287 288
				sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "x",
					tmpui);
289 290 291 292 293 294 295 296 297 298 299
				goto printint;
			case 'X':
				if (q)
					tmpui = va_arg(ap,
						       unsigned long long int);
				else if (l)
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
				if (alt) {
					head = "0X";
Mark Andrews's avatar
Mark Andrews committed
300
					if (precision > 2U)
301 302
						precision -= 2;
				}
303 304
				sprintf(buf, "%" LWRES_PRINT_QUADFORMAT "X",
					tmpui);
305 306
				goto printint;
			printint:
Mark Andrews's avatar
Mark Andrews committed
307
				if (precision != 0U || width != 0U) {
308 309 310 311 312
					length = strlen(buf);
					if (length < precision)
						zeropad = precision - length;
					else if (length < width && zero)
						zeropad = width - length;
Mark Andrews's avatar
Mark Andrews committed
313
					if (width != 0U) {
314 315 316 317 318 319 320 321 322
						pad = width - length -
						      zeropad - strlen(head);
						if (pad < 0)
							pad = 0;
					}
				}
				count += strlen(head) + strlen(buf) + pad +
					 zeropad;
				if (!left) {
Mark Andrews's avatar
Mark Andrews committed
323
					while (pad > 0 && size > 1U) {
324 325 326 327 328 329
						*str++ = ' ';
						size--;
						pad--;
					}
				}
				cp = head;
Mark Andrews's avatar
Mark Andrews committed
330
				while (*cp != '\0' && size > 1U) {
331 332 333
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
334
				while (zeropad > 0 && size > 1U) {
335 336 337 338 339
					*str++ = '0';
					size--;
					zeropad--;
				}
				cp = buf;
Mark Andrews's avatar
Mark Andrews committed
340
				while (*cp != '\0' && size > 1U) {
341 342 343
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
344
				while (pad > 0 && size > 1U) {
345 346 347 348 349 350 351 352 353 354 355 356 357
					*str++ = ' ';
					size--;
					pad--;
				}
				break;
			default:
				break;
			}
			break;
		case 's':
			cp = va_arg(ap, char *);
			REQUIRE(cp != NULL);

Mark Andrews's avatar
Mark Andrews committed
358
			if (precision != 0U) {
359 360 361 362 363 364 365 366
				/*
				 * cp need not be NULL terminated.
				 */
				const char *tp;
				unsigned long n;

				n = precision;
				tp = cp;
Mark Andrews's avatar
Mark Andrews committed
367
				while (n != 0U && *tp != '\0')
368 369 370 371 372
					n--, tp++;
				length = precision - n;
			} else {
				length = strlen(cp);
			}
Mark Andrews's avatar
Mark Andrews committed
373
			if (width != 0U) {
374 375 376 377 378 379
				pad = width - length;
				if (pad < 0)
					pad = 0;
			}
			count += pad + length;
			if (!left)
Mark Andrews's avatar
Mark Andrews committed
380
				while (pad > 0 && size > 1U) {
381 382 383 384
					*str++ = ' ';
					size--;
					pad--;
				}
Mark Andrews's avatar
Mark Andrews committed
385 386
			if (precision != 0U)
				while (precision > 0U && *cp != '\0' &&
Mark Andrews's avatar
Mark Andrews committed
387
				       size > 1U) {
388 389 390 391 392
					*str++ = *cp++;
					size--;
					precision--;
				}
			else
Mark Andrews's avatar
Mark Andrews committed
393
				while (*cp != '\0' && size > 1U) {
394 395 396
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
397
			while (pad > 0 && size > 1U) {
398 399 400 401 402 403 404
				*str++ = ' ';
				size--;
				pad--;
			}
			break;
		case 'c':
			c = va_arg(ap, int);
Mark Andrews's avatar
Mark Andrews committed
405
			if (width > 0U) {
406 407 408 409 410 411
				count += width;
				width--;
				if (left) {
					*str++ = c;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
412
				while (width-- > 0U && size > 1U) {
413 414 415
					*str++ = ' ';
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
416
				if (!left && size > 1U) {
417 418 419 420 421
					*str++ = c;
					size--;
				}
			} else {
				count++;
Mark Andrews's avatar
Mark Andrews committed
422
				if (size > 1U) {
423 424 425 426 427 428 429 430 431 432 433
					*str++ = c;
					size--;
				}
			}
			break;
		case 'p':
			v = va_arg(ap, void *);
			sprintf(buf, "%p", v);
			length = strlen(buf);
			if (precision > length)
				zeropad = precision - length;
Mark Andrews's avatar
Mark Andrews committed
434
			if (width > 0U) {
435 436 437 438 439 440
				pad = width - length - zeropad;
				if (pad < 0)
					pad = 0;
			}
			count += length + pad + zeropad;
			if (!left)
Mark Andrews's avatar
Mark Andrews committed
441
				while (pad > 0 && size > 1U) {
442 443 444 445 446 447 448
					*str++ = ' ';
					size--;
					pad--;
				}
			cp = buf;
			if (zeropad > 0 && buf[0] == '0' &&
			    (buf[1] == 'x' || buf[1] == 'X')) {
Mark Andrews's avatar
Mark Andrews committed
449
				if (size > 1U) {
450 451 452
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
453
				if (size > 1U) {
454 455 456
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
457
				while (zeropad > 0 && size > 1U) {
458 459 460 461 462
					*str++ = '0';
					size--;
					zeropad--;
				}
			}
Mark Andrews's avatar
Mark Andrews committed
463
			while (*cp != '\0' && size > 1U) {
464 465 466
				*str++ = *cp++;
				size--;
			}
Mark Andrews's avatar
Mark Andrews committed
467
			while (pad > 0 && size > 1U) {
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
				*str++ = ' ';
				size--;
				pad--;
			}
			break;
		case 'D':	/*deprecated*/
			INSIST("use %ld instead of %D" == NULL);
		case 'O':	/*deprecated*/
			INSIST("use %lo instead of %O" == NULL);
		case 'U':	/*deprecated*/
			INSIST("use %lu instead of %U" == NULL);

		case 'L':
#ifdef HAVE_LONG_DOUBLE
			l = 1;
#else
			INSIST("long doubles are not supported" == NULL);
#endif
			/*FALLTHROUGH*/
		case 'e':
		case 'E':
		case 'f':
		case 'g':
		case 'G':
			if (!dot)
				precision = 6;
			/*
			 * IEEE floating point.
			 * MIN 2.2250738585072014E-308
			 * MAX 1.7976931348623157E+308
			 * VAX floating point has a smaller range than IEEE.
			 *
			 * precisions > 324 don't make much sense.
			 * if we cap the precision at 512 we will not
			 * overflow buf.
			 */
Mark Andrews's avatar
Mark Andrews committed
504
			if (precision > 512U)
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
				precision = 512;
			sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
				plus ? "+" : space ? " " : "",
				precision, l ? "L" : "", *format);
			switch (*format) {
			case 'e':
			case 'E':
			case 'f':
			case 'g':
			case 'G':
#ifdef HAVE_LONG_DOUBLE
				if (l) {
					ldbl = va_arg(ap, long double);
					sprintf(buf, fmt, ldbl);
				} else
#endif
				{
					dbl = va_arg(ap, double);
					sprintf(buf, fmt, dbl);
				}
				length = strlen(buf);
Mark Andrews's avatar
Mark Andrews committed
526
				if (width > 0U) {
527 528 529 530 531 532
					pad = width - length;
					if (pad < 0)
						pad = 0;
				}
				count += length + pad;
				if (!left)
Mark Andrews's avatar
Mark Andrews committed
533
					while (pad > 0 && size > 1U) {
534 535 536 537 538
						*str++ = ' ';
						size--;
						pad--;
					}
				cp = buf;
Mark Andrews's avatar
Mark Andrews committed
539
				while (*cp != ' ' && size > 1U) {
540 541 542
					*str++ = *cp++;
					size--;
				}
Mark Andrews's avatar
Mark Andrews committed
543
				while (pad > 0 && size > 1U) {
544 545 546 547 548 549 550 551 552 553 554 555 556 557
					*str++ = ' ';
					size--;
					pad--;
				}
				break;
			default:
				continue;
			}
			break;
		default:
			continue;
		}
		format++;
	}
Mark Andrews's avatar
Mark Andrews committed
558
	if (size > 0U)
559 560 561
		*str = '\0';
	return (count);
}