Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

xpprintf.c 32.3 KB
Newer Older
1
/* xpprintf.c */
2
// vi: tabstop=4
3 4 5

/* Deuce's vs[n]printf() replacement */

6
/* $Id: xpprintf.c,v 1.62 2018/04/07 07:21:46 rswindell Exp $ */
7 8 9 10 11

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
12
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

39 40 41
#if defined(__linux__) && !defined(_GNU_SOURCE)
	#define _GNU_SOURCE	// asprintf() on Linux
#endif
42 43 44
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
deuce's avatar
deuce committed
45
#include <stdlib.h>
46
#include "genwrap.h"    /* alloca() */
47 48

#include "xpprintf.h"
deuce's avatar
deuce committed
49
#include "gen_defs.h"
50

51
#if defined(_MSC_VER) || defined(__MSVCRT__)
Rob Swindell's avatar
Rob Swindell committed
52
int vasprintf(char **strptr, const char *format, va_list va)
53
{
54
	va_list	va2;
55 56 57 58
	int		ret;

	if (strptr == NULL)
		return -1;
59
	va_copy(va2, va);
60 61
	ret = _vscprintf(format, va);
	*strptr = (char *)malloc(ret+1);
deuce's avatar
deuce committed
62 63
	if (*strptr == NULL) {
		va_end(va2);
64
		return -1;
deuce's avatar
deuce committed
65
	}
66 67 68
	ret = vsprintf(*strptr, format, va2);
	va_end(va2);
	return ret;
69
}
70

Rob Swindell's avatar
Rob Swindell committed
71
int asprintf(char **strptr, const char *format, ...)
72 73 74 75 76 77 78 79 80 81 82
{
	va_list	va;
	int		ret;

	if (strptr == NULL)
		return -1;
	va_start(va, format);
	ret=vasprintf(strptr, format, va);
	va_end(va);
	return ret;
}
83 84
#endif

85 86 87
/* Maximum length of a format specifier including the % */
#define MAX_FORMAT_LEN	256

Rob Swindell's avatar
Rob Swindell committed
88
void xp_asprintf_free(char *format)
89 90 91 92
{
	free(format);
}

Rob Swindell's avatar
Rob Swindell committed
93
int xp_printf_get_type(const char *format)
94 95 96 97
{
	const char	*p;
	int		modifier=0;
	int		j;
98
	int		correct_type=0;
99 100 101 102 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

	if(!*(size_t *)format)
		return(0);
	p=format+*(size_t *)format;
	if(*p!='%')
		return(0);
	p++;

	/*
	 * Skip flags (zero or more)
	 */
	j=1;
	while(j) {
		switch(*p) {
			case '#':
				p++;
				break;
			case '-':
				p++;
				break;
			case '+':
				p++;
				break;
			case ' ':
				p++;
				break;
			case '0':
				p++;
				break;
			case '\'':
				p++;
				break;
			default:
				j=0;
				break;
		}
	}
	if(*p=='*')
		return(XP_PRINTF_TYPE_INT);
	while(*p>= '0' && *p <= '9')
		p++;
	if(*p=='.') {
		p++;
		if(*p=='*')
			return(XP_PRINTF_TYPE_INT);
	}
	while(*p>= '0' && *p <= '9')
		p++;
	switch(*p) {
		case 'h':
			modifier='h';
			p++;
			if(*p=='h') {
				p++;
				modifier+='h'<<8;
			}
			break;
		case 'l':
157
			modifier='l';
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
			p++;
			if(*p=='l') {
				p++;
				modifier+='l'<<8;
			}
			break;
		case 'j':
			modifier='j';
			p++;
			break;
		case 't':
			modifier='t';
			p++;
			break;
		case 'z':
			modifier='z';
			p++;
			break;
		case 'L':
			modifier='L';
			p++;
			break;
	}
	/*
	 * The next char is now the type... if type is auto, 
	 * set type to what it SHOULD be
	 */
	switch(*p) {
		/* INT types */
		case 'd':
		case 'i':
			switch(modifier) {
				case 'h'|'h'<<8:
					correct_type=XP_PRINTF_TYPE_SCHAR;
					break;
				case 'h':
					correct_type=XP_PRINTF_TYPE_SHORT;
					break;
				case 'l':
					correct_type=XP_PRINTF_TYPE_LONG;
					break;
199
#if defined(XP_PRINTF_TYPE_ULONGLONG)
200 201 202
				case 'l'|'l'<<8:
					correct_type=XP_PRINTF_TYPE_LONGLONG;
					break;
203
#endif
204 205 206 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
				case 'j':
					correct_type=XP_PRINTF_TYPE_INTMAX;
					break;
				case 't':
					correct_type=XP_PRINTF_TYPE_PTRDIFF;
					break;
				case 'z':
					/*
					 * ToDo this is a signed type of same size
					 * as size_t
					 */
					correct_type=XP_PRINTF_TYPE_LONG;
					break;
				default:
					correct_type=XP_PRINTF_TYPE_INT;
					break;
			}
			break;
		case 'o':
		case 'u':
		case 'x':
		case 'X':
			switch(modifier) {
				case 'h'|'h'<<8:
					correct_type=XP_PRINTF_TYPE_UCHAR;
					break;
				case 'h':
					correct_type=XP_PRINTF_TYPE_USHORT;
					break;
				case 'l':
					correct_type=XP_PRINTF_TYPE_ULONG;
					break;
236
#if defined(XP_PRINTF_TYPE_ULONGLONG)
237 238 239
				case 'l'|'l'<<8:
					correct_type=XP_PRINTF_TYPE_ULONGLONG;
					break;
240
#endif
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
				case 'j':
					correct_type=XP_PRINTF_TYPE_UINTMAX;
					break;
				case 't':
					/*
					 * ToDo this is an unsigned type of same size
					 * as ptrdiff_t
					 */
					correct_type=XP_PRINTF_TYPE_ULONG;
					break;
				case 'z':
					correct_type=XP_PRINTF_TYPE_SIZET;
					break;
				default:
					correct_type=XP_PRINTF_TYPE_UINT;
					break;
			}
			break;
		case 'a':
		case 'A':
		case 'e':
		case 'E':
		case 'f':
		case 'F':
		case 'g':
		case 'G':
			switch(modifier) {
				case 'L':
					correct_type=XP_PRINTF_TYPE_LONGDOUBLE;
					break;
				case 'l':
				default:
					correct_type=XP_PRINTF_TYPE_DOUBLE;
					break;
			}
			break;
		case 'C':
			/* ToDo wide chars... not yet supported */
			correct_type=XP_PRINTF_TYPE_CHAR;
			break;
		case 'c':
			switch(modifier) {
				case 'l':
					/* ToDo wide chars... not yet supported */
				default:
					correct_type=XP_PRINTF_TYPE_CHAR;
			}
			break;
		case 'S':
			/* ToDo wide chars... not yet supported */
			correct_type=XP_PRINTF_TYPE_CHARP;
			break;
		case 's':
			switch(modifier) {
				case 'l':
					/* ToDo wide chars... not yet supported */
				default:
					correct_type=XP_PRINTF_TYPE_CHARP;
			}
			break;
		case 'p':
			correct_type=XP_PRINTF_TYPE_VOIDP;
			break;
	}
	return(correct_type);
}

308 309 310 311 312 313 314
/*
 * Performs the next replacement in format using the variable
 * specified as the only vararg which is currently the type
 * specified in type (defined in xpprintf.h).
 *
 * Does not currently support the $ argument selector.
 *
deuce's avatar
deuce committed
315
 * Currently, the type is not overly useful, but this could be used for
316 317
 * automatic type conversions (ie: int to char *).  Right now it just assures
 * that the type passed to sprintf() is the type passed to
318
 * xp_asprintf_next().
319
 */
Rob Swindell's avatar
Rob Swindell committed
320
char* xp_asprintf_next(char *format, int type, ...)
321 322 323 324
{
	va_list vars;
	char			*p;
	char			*newbuf;
325
	int				i=0,j;
326 327 328
	unsigned int	ui=0;
	long int		l=0;
	unsigned long int	ul=0;
329
#if defined(XP_PRINTF_TYPE_LONGLONG)
330 331
	long long int	ll=0;
	unsigned long long int	ull=0;
332
#endif
333 334 335 336 337
	double			d=0;
	long double		ld=0;
	char*			cp=NULL;
	void*			pntr=NULL;
	size_t			s=0;
338
	unsigned long	offset=0;
339
	unsigned long	offset2=0;
340 341
	size_t			format_len;
	size_t			this_format_len;
deuce's avatar
deuce committed
342
	char			int_buf[MAX_FORMAT_LEN];
343
	char			*entry;
344 345
	char			this_format[MAX_FORMAT_LEN];
	char			*fmt;
346 347
	int				modifier=0;
	int				correct_type=0;
348
	char			num_str[128];		/* More than enough room for a 256-bit int */
349

350 351 352 353 354
	/*
	 * Check if we're already done...
	 */
	if(!*(size_t *) format)
		return(format);
355

356
	p=format+*(size_t *)format;
357
	offset=p-format;
358
	format_len=*(size_t *)(format+sizeof(size_t))+sizeof(size_t)*2+1;
359 360 361 362 363 364 365
	this_format[0]=0;
	fmt=this_format;
	*(fmt++)=*(p++);

	/*
	 * Skip flags (zero or more)
	 */
366 367
	j=1;
	while(j) {
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		switch(*p) {
			case '#':
				*(fmt++)=*(p++);
				break;
			case '-':
				*(fmt++)=*(p++);
				break;
			case '+':
				*(fmt++)=*(p++);
				break;
			case ' ':
				*(fmt++)=*(p++);
				break;
			case '0':
				*(fmt++)=*(p++);
				break;
			case '\'':
				*(fmt++)=*(p++);
				break;
			default:
388
				j=0;
389 390 391 392 393
				break;
		}
	}

	/*
394
	 * If width is '*' then the argument is an int
395 396 397 398
	 * which specifies the width.
	 */
	if(*p=='*') {		/* The argument is this width */
		va_start(vars, type);
deuce's avatar
deuce committed
399
		i=sprintf(int_buf,"%d", va_arg(vars, int));
400 401
		va_end(vars);
		if(i > 1) {
deuce's avatar
deuce committed
402 403 404 405
			/*
			 * We must calculate this before we go mucking about
			 * with format and p
			 */
406
			offset2=p-format;
407
			newbuf=(char *)realloc(format, format_len+i-1 /* -1 for the '*' that's already there */);
408 409
			if(newbuf==NULL)
				return(NULL);
deuce's avatar
deuce committed
410
			format=newbuf;
411
			p=format+offset2;
412 413 414 415
			/*
			 * Move trailing end to make space... leaving the * where it
			 * is so it can be overwritten
			 */
416
			memmove(p+i, p+1, format-p+format_len-1);
deuce's avatar
deuce committed
417
			memcpy(p, int_buf, i);
418
			*(size_t *)(format+sizeof(size_t))+=i-1;
419 420
		}
		else
deuce's avatar
deuce committed
421
			*p=int_buf[0];
422 423
		p=format+offset;
		*(size_t *)format=p-format;
424 425 426 427 428 429 430
		return(format);
	}
	/* Skip width */
	while(*p >= '0' && *p <= '9')
		*(fmt++)=*(p++);
	/* Check for precision */
	if(*p=='.') {
431
		*(fmt++)=*(p++);
432
		/*
433
		 * If the precision is '*' then the argument is an int which
434 435 436 437
		 * specifies the precision.
		 */
		if(*p=='*') {
			va_start(vars, type);
deuce's avatar
deuce committed
438
			i=sprintf(int_buf,"%d", va_arg(vars, int));
439 440
			va_end(vars);
			if(i > 1) {
deuce's avatar
deuce committed
441 442 443 444
				/*
				 * We must calculate this before we go mucking about
				 * with format and p
				 */
445
				offset2=p-format;
446
				newbuf=(char *)realloc(format, format_len+i-1 /* -1 for the '*' that's already there */);
447 448
				if(newbuf==NULL)
					return(NULL);
deuce's avatar
deuce committed
449
				format=newbuf;
450
				p=format+offset2;
451 452 453 454
				/*
				 * Move trailing end to make space... leaving the * where it
				 * is so it can be overwritten
				 */
455
				memmove(p+i, p+1, format-p+format_len-1);
deuce's avatar
deuce committed
456
				memcpy(p, int_buf, i);
457
				*(size_t *)(format+sizeof(size_t))+=i-1;
458 459
			}
			else
deuce's avatar
deuce committed
460
				*p=int_buf[0];
461 462
			p=format+offset;
			*(size_t *)format=p-format;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
			return(format);
		}
		/* Skip precision */
		while(*p >= '0' && *p <= '9')
			*(fmt++)=*(p++);
	}

	/* Skip/Translate length modifiers */
	/*
	 * ToDo: This could (should?) convert the standard ll modifier
	 * to the MSVC equivilant (I64 or something?)
	 * if you do this, the calculations using this_format_len will need
	 * rewriting.
	 */
	switch(*p) {
		case 'h':
			modifier='h';
			*(fmt++)=*(p++);
			if(*p=='h') {
				*(fmt++)=*(p++);
				modifier+='h'<<8;
			}
			break;
		case 'l':
deuce's avatar
deuce committed
487
			modifier='l';
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
			*(fmt++)=*(p++);
			if(*p=='l') {
				*(fmt++)=*(p++);
				modifier+='l'<<8;
			}
			break;
		case 'j':
			modifier='j';
			*(fmt++)=*(p++);
			break;
		case 't':
			modifier='t';
			*(fmt++)=*(p++);
			break;
		case 'z':
			modifier='z';
			*(fmt++)=*(p++);
			break;
		case 'L':
			modifier='L';
			*(fmt++)=*(p++);
			break;
	}
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

	/*
	 * The next char is now the type... if type is auto, 
	 * set type to what it SHOULD be
	 */
	if(type==XP_PRINTF_TYPE_AUTO || type & XP_PRINTF_CONVERT) {
		switch(*p) {
			/* INT types */
			case 'd':
			case 'i':
				switch(modifier) {
					case 'h'|'h'<<8:
						correct_type=XP_PRINTF_TYPE_SCHAR;
						break;
					case 'h':
						correct_type=XP_PRINTF_TYPE_SHORT;
						break;
					case 'l':
						correct_type=XP_PRINTF_TYPE_LONG;
						break;
531
#if defined(XP_PRINTF_TYPE_LONGLONG)
532 533 534
					case 'l'|'l'<<8:
						correct_type=XP_PRINTF_TYPE_LONGLONG;
						break;
535
#endif
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
					case 'j':
						correct_type=XP_PRINTF_TYPE_INTMAX;
						break;
					case 't':
						correct_type=XP_PRINTF_TYPE_PTRDIFF;
						break;
					case 'z':
						/*
						 * ToDo this is a signed type of same size
						 * as size_t
						 */
						correct_type=XP_PRINTF_TYPE_LONG;
						break;
					default:
						correct_type=XP_PRINTF_TYPE_INT;
						break;
				}
				break;
			case 'o':
			case 'u':
			case 'x':
			case 'X':
				switch(modifier) {
					case 'h'|'h'<<8:
						correct_type=XP_PRINTF_TYPE_UCHAR;
						break;
					case 'h':
						correct_type=XP_PRINTF_TYPE_USHORT;
						break;
					case 'l':
						correct_type=XP_PRINTF_TYPE_ULONG;
						break;
568
#if defined(XP_PRINTF_TYPE_LONGLONG)
569 570 571
					case 'l'|'l'<<8:
						correct_type=XP_PRINTF_TYPE_ULONGLONG;
						break;
572
#endif
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
					case 'j':
						correct_type=XP_PRINTF_TYPE_UINTMAX;
						break;
					case 't':
						/*
						 * ToDo this is an unsigned type of same size
						 * as ptrdiff_t
						 */
						correct_type=XP_PRINTF_TYPE_ULONG;
						break;
					case 'z':
						correct_type=XP_PRINTF_TYPE_SIZET;
						break;
					default:
						correct_type=XP_PRINTF_TYPE_UINT;
						break;
				}
				break;
			case 'a':
			case 'A':
			case 'e':
			case 'E':
			case 'f':
			case 'F':
			case 'g':
			case 'G':
				switch(modifier) {
					case 'L':
						correct_type=XP_PRINTF_TYPE_LONGDOUBLE;
						break;
					case 'l':
					default:
						correct_type=XP_PRINTF_TYPE_DOUBLE;
						break;
				}
				break;
609 610 611 612
			case 'C':
				/* ToDo wide chars... not yet supported */
				correct_type=XP_PRINTF_TYPE_CHAR;
				break;
613 614 615 616 617 618 619 620
			case 'c':
				switch(modifier) {
					case 'l':
						/* ToDo wide chars... not yet supported */
					default:
						correct_type=XP_PRINTF_TYPE_CHAR;
				}
				break;
621 622 623 624
			case 'S':
				/* ToDo wide chars... not yet supported */
				correct_type=XP_PRINTF_TYPE_CHARP;
				break;
625 626 627 628 629 630 631 632
			case 's':
				switch(modifier) {
					case 'l':
						/* ToDo wide chars... not yet supported */
					default:
						correct_type=XP_PRINTF_TYPE_CHARP;
				}
				break;
633 634 635
			case 'p':
				correct_type=XP_PRINTF_TYPE_VOIDP;
				break;
636 637 638 639 640 641 642 643 644
		}
	}
	if(type==XP_PRINTF_TYPE_AUTO)
		type=correct_type;

	/*
	 * Copy the arg to the passed type.
	 */
	va_start(vars, type);
645
	switch(type & ~XP_PRINTF_CONVERT) {
646
		case XP_PRINTF_TYPE_CHAR:
647 648 649 650
		case XP_PRINTF_TYPE_INT:	/* Also includes char and short */
			i=va_arg(vars, int);
			break;
		case XP_PRINTF_TYPE_UINT:	/* Also includes char and short */
651 652 653 654
			/*
			 * ToDo: If it's a %c, and the value is 0, should it output [null]
			 * or should it terminate the string?
			 */
655 656 657 658 659 660 661 662
			ui=va_arg(vars, unsigned int);
			break;
		case XP_PRINTF_TYPE_LONG:
			l=va_arg(vars, long);
			break;
		case XP_PRINTF_TYPE_ULONG:
			ul=va_arg(vars, unsigned long int);
			break;
663
#if defined(XP_PRINTF_TYPE_LONGLONG)
664 665 666 667 668 669
		case XP_PRINTF_TYPE_LONGLONG:
			ll=va_arg(vars, long long int);
			break;
		case XP_PRINTF_TYPE_ULONGLONG:
			ull=va_arg(vars, unsigned long long int);
			break;
670
#endif
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
		case XP_PRINTF_TYPE_CHARP:
			cp=va_arg(vars, char*);
			break;
		case XP_PRINTF_TYPE_DOUBLE:
			d=va_arg(vars, double);
			break;
		case XP_PRINTF_TYPE_LONGDOUBLE:
			ld=va_arg(vars, long double);
			break;
		case XP_PRINTF_TYPE_VOIDP:
			pntr=va_arg(vars, void*);
			break;
		case XP_PRINTF_TYPE_SIZET:
			s=va_arg(vars, size_t);
			break;
	}
	va_end(vars);

689 690 691 692
	if(type & XP_PRINTF_CONVERT) {
		type=type & ~XP_PRINTF_CONVERT;
		if(type != correct_type) {
			switch(correct_type) {
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
				case XP_PRINTF_TYPE_CHAR:
					switch(type) {
						case XP_PRINTF_TYPE_CHAR:
						case XP_PRINTF_TYPE_INT:
							break;
						case XP_PRINTF_TYPE_UINT:
							i=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							i=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							i=ul;
							break;
#if defined(XP_PRINTF_TYPE_LONGLONG)
						case XP_PRINTF_TYPE_LONGLONG:
							i=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							i=ull;
							break;
#endif
						case XP_PRINTF_TYPE_CHARP:
716 717 718 719
							if(cp)
								i=*cp;
							else
								i=0;
720 721 722 723 724 725 726 727
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							i=(int)d;
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
							i=(int)ld;
							break;
						case XP_PRINTF_TYPE_VOIDP:
rswindell's avatar
rswindell committed
728
							i=(long)pntr;
729 730 731 732 733 734
							break;
						case XP_PRINTF_TYPE_SIZET:
							i=s;
							break;
					}
					break;
735 736
				case XP_PRINTF_TYPE_INT:
					switch(type) {
737
						case XP_PRINTF_TYPE_CHAR:
738 739 740 741 742 743 744 745 746 747 748
						case XP_PRINTF_TYPE_INT:
							break;
						case XP_PRINTF_TYPE_UINT:
							i=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							i=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							i=ul;
							break;
749
#if defined(XP_PRINTF_TYPE_LONGLONG)
750 751 752 753 754 755
						case XP_PRINTF_TYPE_LONGLONG:
							i=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							i=ull;
							break;
756
#endif
757 758 759 760
						case XP_PRINTF_TYPE_CHARP:
							i=strtol(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
761
							i=(int)d;
762 763
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
764
							i=(int)ld;
765 766
							break;
						case XP_PRINTF_TYPE_VOIDP:
rswindell's avatar
rswindell committed
767
							i=(long)pntr;
768 769 770 771 772 773 774 775
							break;
						case XP_PRINTF_TYPE_SIZET:
							i=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_UINT:
					switch(type) {
776
						case XP_PRINTF_TYPE_CHAR:
777 778 779 780 781 782 783 784 785 786 787
						case XP_PRINTF_TYPE_INT:
							ui=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							break;
						case XP_PRINTF_TYPE_LONG:
							ui=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							ui=ul;
							break;
788
#if defined(XP_PRINTF_TYPE_LONGLONG)
789 790 791 792 793 794
						case XP_PRINTF_TYPE_LONGLONG:
							ui=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							ui=ull;
							break;
795
#endif
796 797 798 799
						case XP_PRINTF_TYPE_CHARP:
							ui=strtoul(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
800
							ui=(unsigned)d;
801 802
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
803
							ui=(unsigned)ld;
804 805
							break;
						case XP_PRINTF_TYPE_VOIDP:
rswindell's avatar
rswindell committed
806
							ui=(unsigned long)pntr;
807 808 809 810 811 812 813 814
							break;
						case XP_PRINTF_TYPE_SIZET:
							ui=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_LONG:
					switch(type) {
815
						case XP_PRINTF_TYPE_CHAR:
816 817 818 819 820 821 822 823 824 825 826
						case XP_PRINTF_TYPE_INT:
							l=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							l=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							break;
						case XP_PRINTF_TYPE_ULONG:
							l=ul;
							break;
827
#if defined(XP_PRINTF_TYPE_LONGLONG)
828 829 830 831 832 833
						case XP_PRINTF_TYPE_LONGLONG:
							l=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							l=ull;
							break;
834
#endif
835 836 837 838
						case XP_PRINTF_TYPE_CHARP:
							l=strtol(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
839
							l=(long)d;
840 841
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
842
							l=(long)ld;
843 844 845 846 847 848 849 850 851 852 853
							break;
						case XP_PRINTF_TYPE_VOIDP:
							l=(long)pntr;
							break;
						case XP_PRINTF_TYPE_SIZET:
							l=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_ULONG:
					switch(type) {
854
						case XP_PRINTF_TYPE_CHAR:
855 856 857 858 859 860 861 862 863 864 865
						case XP_PRINTF_TYPE_INT:
							ul=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							ul=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							ul=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							break;
866
#if defined(XP_PRINTF_TYPE_LONGLONG)
867 868 869 870 871 872
						case XP_PRINTF_TYPE_LONGLONG:
							ul=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							ul=ull;
							break;
873
#endif
874 875 876 877
						case XP_PRINTF_TYPE_CHARP:
							ul=strtoul(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
878
							ul=(unsigned long)d;
879 880
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
881
							ul=(unsigned long)ld;
882 883 884 885 886 887 888 889 890
							break;
						case XP_PRINTF_TYPE_VOIDP:
							ul=(unsigned long)pntr;
							break;
						case XP_PRINTF_TYPE_SIZET:
							ul=s;
							break;
					}
					break;
891
#if defined(XP_PRINTF_TYPE_LONGLONG)
892 893
				case XP_PRINTF_TYPE_LONGLONG:
					switch(type) {
894
						case XP_PRINTF_TYPE_CHAR:
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
						case XP_PRINTF_TYPE_INT:
							ll=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							ll=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							ll=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							ll=ul;
							break;
						case XP_PRINTF_TYPE_LONGLONG:
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							ll=ull;
							break;
						case XP_PRINTF_TYPE_CHARP:
							ll=strtoll(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							ll=d;
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
							ll=ld;
							break;
						case XP_PRINTF_TYPE_VOIDP:
deuce's avatar
deuce committed
922
							ll=(long long)((intptr_t)pntr);
923 924 925 926 927 928 929 930
							break;
						case XP_PRINTF_TYPE_SIZET:
							ll=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_ULONGLONG:
					switch(type) {
931
						case XP_PRINTF_TYPE_CHAR:
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
						case XP_PRINTF_TYPE_INT:
							ull=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							ull=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							ull=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							ull=ul;
							break;
						case XP_PRINTF_TYPE_LONGLONG:
							ull=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							break;
						case XP_PRINTF_TYPE_CHARP:
							ull=strtoull(cp, NULL, 0);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							ull=d;
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
							ull=ld;
							break;
						case XP_PRINTF_TYPE_VOIDP:
deuce's avatar
deuce committed
959
							ull=(unsigned long long int)((uintptr_t)pntr);
960 961 962 963 964 965
							break;
						case XP_PRINTF_TYPE_SIZET:
							ull=s;
							break;
					}
					break;
966
#endif
967 968 969
				case XP_PRINTF_TYPE_CHARP:
					num_str[0]=0;
					switch(type) {
970
						case XP_PRINTF_TYPE_CHAR:
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
						case XP_PRINTF_TYPE_INT:
							sprintf(num_str, "%d", i);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_UINT:
							sprintf(num_str, "%u", i);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_LONG:
							sprintf(num_str, "%ld", l);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_ULONG:
							sprintf(num_str, "%lu", ul);
							cp=num_str;
							break;
987
#if defined(XP_PRINTF_TYPE_LONGLONG)
988 989 990 991 992 993 994 995 996 997
						case XP_PRINTF_TYPE_LONGLONG:
							/* ToDo MSVC doesn't like this */
							sprintf(num_str, "%lld", ll);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							/* ToDo MSVC doesn't like this */
							sprintf(num_str, "%llu", ull);
							cp=num_str;
							break;
998
#endif
999 1000 1001 1002 1003 1004 1005
						case XP_PRINTF_TYPE_CHARP:
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							sprintf(num_str, "%f", d);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
1006
							sprintf(num_str, "%Lf", ld);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_VOIDP:
							/* ToDo: Or should this pretend it's a char *? */
							sprintf(num_str, "%p", pntr);
							cp=num_str;
							break;
						case XP_PRINTF_TYPE_SIZET:
							sprintf(num_str, "%zu", s);
							cp=num_str;
							break;
					}
					break;
				case XP_PRINTF_TYPE_DOUBLE:
					switch(type) {
1022
						case XP_PRINTF_TYPE_CHAR:
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
						case XP_PRINTF_TYPE_INT:
							d=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							d=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							d=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							d=ul;
							break;
1035
#if defined(XP_PRINTF_TYPE_LONGLONG)
1036 1037 1038 1039 1040 1041
						case XP_PRINTF_TYPE_LONGLONG:
							d=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							d=ull;
							break;
1042
#endif
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
						case XP_PRINTF_TYPE_CHARP:
							d=strtod(cp, NULL);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
							d=ld;
							break;
						case XP_PRINTF_TYPE_VOIDP:
							d=(double)((long int)pntr);
							break;
						case XP_PRINTF_TYPE_SIZET:
							d=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_LONGDOUBLE:
					switch(type) {
1061
						case XP_PRINTF_TYPE_CHAR:
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
						case XP_PRINTF_TYPE_INT:
							ld=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							ld=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							ld=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							ld=ul;
							break;
1074
#if defined(XP_PRINTF_TYPE_LONGLONG)
1075 1076 1077 1078 1079 1080
						case XP_PRINTF_TYPE_LONGLONG:
							ld=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							ld=ull;
							break;
1081
#endif
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
						case XP_PRINTF_TYPE_CHARP:
							/* strtold() isn't ubiquitous yet */
							ld=strtod(cp, NULL);
							break;
						case XP_PRINTF_TYPE_DOUBLE:
							ld=d;
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
							break;
						case XP_PRINTF_TYPE_VOIDP:
							ld=(long double)((long int)pntr);
							break;
						case XP_PRINTF_TYPE_SIZET:
							ld=s;
							break;
					}
					break;
				case XP_PRINTF_TYPE_VOIDP:
					/* ToDo: this is nasty... */
					switch(type) {
1102
						case XP_PRINTF_TYPE_CHAR:
1103
						case XP_PRINTF_TYPE_INT:
1104
							pntr=(void *)((intptr_t)i);
1105 1106
							break;
						case XP_PRINTF_TYPE_UINT:
deuce's avatar
deuce committed
1107
							pntr=(void *)((uintptr_t)ui);
1108 1109
							break;
						case XP_PRINTF_TYPE_LONG:
1110
							pntr=(void *)((intptr_t)l);
1111 1112
							break;
						case XP_PRINTF_TYPE_ULONG:
deuce's avatar
deuce committed
1113
							pntr=(void *)((uintptr_t)ul);
1114
							break;
1115
#if defined(XP_PRINTF_TYPE_LONGLONG)
1116
						case XP_PRINTF_TYPE_LONGLONG:
deuce's avatar
deuce committed
1117
							pntr=(void *)((intptr_t)ll);
1118 1119
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
deuce's avatar
deuce committed
1120
							pntr=(void *)((uintptr_t)ull);
1121
							break;
1122
#endif
1123
						case XP_PRINTF_TYPE_CHARP:
deuce's avatar
deuce committed
1124
							pntr=(void *)(cp);
1125 1126
							break;
						case XP_PRINTF_TYPE_DOUBLE:
1127
							pntr=(void *)((intptr_t)d);
1128 1129
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
1130
							pntr=(void *)((intptr_t)ld);
1131 1132 1133 1134
							break;
						case XP_PRINTF_TYPE_VOIDP:
							break;
						case XP_PRINTF_TYPE_SIZET:
1135
							pntr=(void *)((intptr_t)s);
1136 1137 1138 1139 1140
							break;
					}
					break;
				case XP_PRINTF_TYPE_SIZET:
					switch(type) {
1141
						case XP_PRINTF_TYPE_CHAR:
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
						case XP_PRINTF_TYPE_INT:
							s=i;
							break;
						case XP_PRINTF_TYPE_UINT:
							s=ui;
							break;
						case XP_PRINTF_TYPE_LONG:
							s=l;
							break;
						case XP_PRINTF_TYPE_ULONG:
							s=ul;
							break;
1154
#if defined(XP_PRINTF_TYPE_LONGLONG)
1155 1156 1157 1158 1159 1160
						case XP_PRINTF_TYPE_LONGLONG:
							s=ll;
							break;
						case XP_PRINTF_TYPE_ULONGLONG:
							s=ull;
							break;
1161
#endif
1162
						case XP_PRINTF_TYPE_CHARP:
1163
							s=strtol(cp, NULL, 0);	/* was strtoll */
1164 1165
							break;
						case XP_PRINTF_TYPE_DOUBLE:
1166
							s=(size_t)d;
1167 1168
							break;
						case XP_PRINTF_TYPE_LONGDOUBLE:
1169
							s=(size_t)ld;
1170 1171 1172 1173 1174 1175 1176 1177
							break;
						case XP_PRINTF_TYPE_VOIDP:
							s=(size_t)pntr;
							break;
						case XP_PRINTF_TYPE_SIZET:
							break;
					}
					break;
1178
			}
1179
			type=correct_type;
1180 1181 1182
		}
	}

1183
	/* The next char is now the type... check the length required to spore the printf()ed string */
1184 1185
	*(fmt++)=*p;
	*fmt=0;
1186
	entry=NULL;
1187
	switch(type) {
1188
		case XP_PRINTF_TYPE_CHAR:	/* Also includes char and short */
1189
		case XP_PRINTF_TYPE_INT:	/* Also includes char and short */
1190
			j=asprintf(&entry, this_format, i);
1191 1192
			break;
		case XP_PRINTF_TYPE_UINT:	/* Also includes char and short */
1193
			j=asprintf(&entry, this_format, ui);
1194 1195
			break;
		case XP_PRINTF_TYPE_LONG:
1196
			j=asprintf(&entry, this_format, l);
1197 1198
			break;
		case XP_PRINTF_TYPE_ULONG:
1199
			j=asprintf(&entry, this_format, ul);
1200
			break;
1201
#if defined(XP_PRINTF_TYPE_LONGLONG)
1202
		case XP_PRINTF_TYPE_LONGLONG:
1203
			j=asprintf(&entry, this_format, ll);
1204 1205
			break;
		case XP_PRINTF_TYPE_ULONGLONG:
1206
			j=asprintf(&entry, this_format, ull);
1207
			break;
1208
#endif
1209
		case XP_PRINTF_TYPE_CHARP:
1210
			if(cp==NULL)
1211
				j=asprintf(&entry, this_format, "<null>");
deuce's avatar
deuce committed
1212
			else
1213
				j=asprintf(&entry, this_format, cp);
1214 1215
			break;
		case XP_PRINTF_TYPE_DOUBLE:
1216
			j=asprintf(&entry, this_format, d);
1217 1218
			break;
		case XP_PRINTF_TYPE_LONGDOUBLE:
1219
			j=asprintf(&entry, this_format, ld);
1220 1221
			break;
		case XP_PRINTF_TYPE_VOIDP:
1222
			j=asprintf(&entry, this_format, pntr);
1223 1224
			break;
		case XP_PRINTF_TYPE_SIZET:
1225 1226 1227 1228 1229
			j=asprintf(&entry, this_format, s);
			break;
		default:
			j = -1;
			entry = NULL;
1230 1231 1232
			break;
	}

1233
	if (j<0) {
1234 1235
		FREE_AND_NULL(entry);
		entry = strdup("<error>");
1236