print.c 12.2 KB
Newer Older
1
/*
Tinderbox User's avatar
Tinderbox User committed
2
 * Copyright (C) 2004-2008, 2010, 2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6
7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9
10
11
12
13
14
15
 * 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.
16
17
 */

Automatic Updater's avatar
Automatic Updater committed
18
/* $Id: print.c,v 1.37 2010/10/18 23:47:08 tbox Exp $ */
19
20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

22
23
#include <config.h>

24
#include <ctype.h>
25
26
#include <stdio.h>		/* for sprintf() */
#include <string.h>		/* for strlen() */
27

28
29
#define	ISC__PRINT_SOURCE	/* Used to get the isc_print_* prototypes. */

30
#include <isc/assertions.h>
31
#include <isc/int.h>
32
#include <isc/msgs.h>
33
#include <isc/print.h>
34
#include <isc/stdlib.h>
Bob Halley's avatar
Bob Halley committed
35
#include <isc/util.h>
36

37
38
39
40
41
42
43
44
45
46
int
isc_print_sprintf(char *str, const char *format, ...) {
	va_list ap;

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

47
/*!
48
49
 * Return length of string that would have been written if not truncated.
 */
50

51
52
53
54
55
56
57
58
59
60
61
62
int
isc_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);

}

63
/*!
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 * Return length of string that would have been written if not truncated.
 */

int
isc_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;
78
	isc_int64_t tmpi;
79
	isc_uint64_t tmpui;
80
81
	unsigned long width;
	unsigned long precision;
82
	unsigned int length;
83
84
85
	char buf[1024];
	char c;
	void *v;
86
87
88
	char *save = str;
	const char *cp;
	const char *head;
89
90
91
92
93
	int count = 0;
	int pad;
	int zeropad;
	int dot;
	double dbl;
Mark Andrews's avatar
Mark Andrews committed
94
#ifdef HAVE_LONG_DOUBLE
95
	long double ldbl;
Mark Andrews's avatar
Mark Andrews committed
96
#endif
97
	char fmt[32];
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	INSIST(str != NULL);
	INSIST(format != NULL);

	while (*format != '\0') {
		if (*format != '%') {
			if (size > 1) {
				*str++ = *format;
				size--;
			}
			count++;
			format++;
			continue;
		}
		format++;

114
115
116
		/*
		 * Reset flags.
		 */
117
118
119
		dot = neg = space = plus = left = zero = alt = h = l = q = 0;
		width = precision = 0;
		head = "";
Bob Halley's avatar
Bob Halley committed
120
		length = pad = zeropad = 0;
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

		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);

146
147
148
		/*
		 * Width.
		 */
149
150
151
		if (*format == '*') {
			width = va_arg(ap, int);
			format++;
152
		} else if (isdigit((unsigned char)*format)) {
153
154
155
			char *e;
			width = strtoul(format, &e, 10);
			format = e;
156
		}
157

158
159
160
		/*
		 * Precision.
		 */
161
162
163
164
165
166
		if (*format == '.') {
			format++;
			dot = 1;
			if (*format == '*') {
				precision = va_arg(ap, int);
				format++;
167
			} else if (isdigit((unsigned char)*format)) {
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
				char *e;
				precision = strtoul(format, &e, 10);
				format = e;
			}
		}

		switch (*format) {
		case '\0':
			continue;
		case '%':
			if (size > 1) {
				*str++ = *format;
				size--;
			}
			count++;
			break;
		case 'q':
			q = 1;
			format++;
			goto doint;
		case 'h':
			h = 1;
			format++;
			goto doint;
		case 'l':
			l = 1;
			format++;
195
			if (*format == 'l') {
196
				q = 1;
197
198
				format++;
			}
199
200
201
202
203
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
			goto doint;
		case 'n':
		case 'i':
		case 'd':
		case 'o':
		case 'u':
		case 'x':
		case 'X':
		doint:
			if (precision != 0)
				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)
232
233
					tmpi = va_arg(ap, isc_int64_t);
				else if (l)
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
					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;
				}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
				if (tmpui <= 0xffffffffU)
					sprintf(buf, "%lu",
						(unsigned long)tmpui);
				else {
					unsigned long mid;
					unsigned long lo;
					unsigned long hi;
					lo = tmpui % 1000000000;
					tmpui /= 1000000000;
					mid = tmpui % 1000000000;
					hi = tmpui / 1000000000;
					if (hi != 0)
						sprintf(buf, "%lu", hi);
					else
263
						buf[0] = '\0';
264
265
266
					sprintf(buf + strlen(buf), "%lu", mid);
					sprintf(buf + strlen(buf), "%lu", lo);
				}
267
268
269
				goto printint;
			case 'o':
				if (q)
270
271
272
					tmpui = va_arg(ap, isc_uint64_t);
				else if (l)
					tmpui = va_arg(ap, long int);
273
				else
274
					tmpui = va_arg(ap, int);
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
				if (tmpui <= 0xffffffffU)
					sprintf(buf, alt ?  "%#lo" : "%lo",
						(unsigned long)tmpui);
				else {
					unsigned long mid;
					unsigned long lo;
					unsigned long hi;
					lo = tmpui % 010000000000;
					tmpui /= 010000000000;
					mid = tmpui % 010000000000;
					hi = tmpui / 010000000000;
					if (hi != 0) {
						sprintf(buf,
							alt ?  "%#lo" : "%lo",
							hi);
						sprintf(buf + strlen(buf),
							"%lo", mid);
					} else
						sprintf(buf,
							alt ?  "%#lo" : "%lo",
							mid);
					sprintf(buf + strlen(buf), "%lo", lo);
				}
298
299
300
				goto printint;
			case 'u':
				if (q)
301
302
					tmpui = va_arg(ap, isc_uint64_t);
				else if (l)
303
304
305
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
				if (tmpui <= 0xffffffffU)
					sprintf(buf, "%lu",
						(unsigned long)tmpui);
				else {
					unsigned long mid;
					unsigned long lo;
					unsigned long hi;
					lo = tmpui % 1000000000;
					tmpui /= 1000000000;
					mid = tmpui % 1000000000;
					hi = tmpui / 1000000000;
					if (hi != 0)
						sprintf(buf, "%lu", hi);
					else
320
						buf[0] = '\0';
321
322
323
					sprintf(buf + strlen(buf), "%lu", mid);
					sprintf(buf + strlen(buf), "%lu", lo);
				}
324
325
326
				goto printint;
			case 'x':
				if (q)
327
328
					tmpui = va_arg(ap, isc_uint64_t);
				else if (l)
329
330
331
332
333
334
335
336
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
				if (alt) {
					head = "0x";
					if (precision > 2)
						precision -= 2;
				}
337
338
339
340
341
342
343
344
345
				if (tmpui <= 0xffffffffU)
					sprintf(buf, "%lx",
						(unsigned long)tmpui);
				else {
					unsigned long hi = tmpui>>32;
					unsigned long lo = tmpui & 0xffffffff;
					sprintf(buf, "%lx", hi);
					sprintf(buf + strlen(buf), "%lx", lo);
				}
346
347
348
				goto printint;
			case 'X':
				if (q)
349
350
					tmpui = va_arg(ap, isc_uint64_t);
				else if (l)
351
352
353
354
355
356
357
358
					tmpui = va_arg(ap, unsigned long int);
				else
					tmpui = va_arg(ap, unsigned int);
				if (alt) {
					head = "0X";
					if (precision > 2)
						precision -= 2;
				}
359
360
361
362
363
364
365
366
367
				if (tmpui <= 0xffffffffU)
					sprintf(buf, "%lX",
						(unsigned long)tmpui);
				else  {
					unsigned long hi = tmpui>>32;
					unsigned long lo = tmpui & 0xffffffff;
					sprintf(buf, "%lX", hi);
					sprintf(buf + strlen(buf), "%lX", lo);
				}
368
369
370
371
372
373
				goto printint;
			printint:
				if (precision != 0 || width != 0) {
					length = strlen(buf);
					if (length < precision)
						zeropad = precision - length;
374
375
376
					else if (length < width && zero)
						zeropad = width - length;
					if (width != 0) {
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
						pad = width - length -
						      zeropad - strlen(head);
						if (pad < 0)
							pad = 0;
					}
				}
				count += strlen(head) + strlen(buf) + pad +
					 zeropad;
				if (!left) {
					while (pad > 0 && size > 1) {
						*str++ = ' ';
						size--;
						pad--;
					}
				}
				cp = head;
				while (*cp != '\0' && size > 1) {
					*str++ = *cp++;
					size--;
				}
				while (zeropad > 0 && size > 1) {
					*str++ = '0';
					size--;
					zeropad--;
				}
				cp = buf;
				while (*cp != '\0' && size > 1) {
					*str++ = *cp++;
					size--;
				}
				while (pad > 0 && size > 1) {
					*str++ = ' ';
					size--;
					pad--;
				}
				break;
			default:
				break;
			}
			break;
		case 's':
			cp = va_arg(ap, char *);
			REQUIRE(cp != NULL);

			if (precision != 0) {
422
423
424
				/*
				 * cp need not be NULL terminated.
				 */
425
				const char *tp;
426
427
428
429
				unsigned long n;

				n = precision;
				tp = cp;
430
				while (n != 0 && *tp != '\0')
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
					n--, tp++;
				length = precision - n;
			} else {
				length = strlen(cp);
			}
			if (width != 0) {
				pad = width - length;
				if (pad < 0)
					pad = 0;
			}
			count += pad + length;
			if (!left)
				while (pad > 0 && size > 1) {
					*str++ = ' ';
					size--;
					pad--;
				}
448
			if (precision != 0)
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
				while (precision > 0 && *cp != '\0' &&
				       size > 1) {
					*str++ = *cp++;
					size--;
					precision--;
				}
			else
				while (*cp != '\0' && size > 1) {
					*str++ = *cp++;
					size--;
				}
			while (pad > 0 && size > 1) {
				*str++ = ' ';
				size--;
				pad--;
			}
			break;
		case 'c':
			c = va_arg(ap, int);
			if (width > 0) {
				count += width;
				width--;
471
				if (left && size > 1) {
472
473
					*str++ = c;
					size--;
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
				while (width-- > 0 && size > 1) {
					*str++ = ' ';
					size--;
				}
				if (!left && size > 1) {
					*str++ = c;
					size--;
				}
			} else {
				count++;
				if (size > 1) {
					*str++ = c;
					size--;
				}
			}
			break;
		case 'p':
			v = va_arg(ap, void *);
			sprintf(buf, "%p", v);
			length = strlen(buf);
			if (precision > length)
				zeropad = precision - length;
			if (width > 0) {
				pad = width - length - zeropad;
				if (pad < 0)
					pad = 0;
			}
			count += length + pad + zeropad;
			if (!left)
				while (pad > 0 && size > 1) {
					*str++ = ' ';
					size--;
					pad--;
				}
			cp = buf;
			if (zeropad > 0 && buf[0] == '0' &&
			    (buf[1] == 'x' || buf[1] == 'X')) {
				if (size > 1) {
					*str++ = *cp++;
					size--;
				}
				if (size > 1) {
					*str++ = *cp++;
					size--;
				}
				while (zeropad > 0 && size > 1) {
					*str++ = '0';
					size--;
					zeropad--;
				}
			}
			while (*cp != '\0' && size > 1) {
				*str++ = *cp++;
				size--;
			}
			while (pad > 0 && size > 1) {
				*str++ = ' ';
				size--;
				pad--;
			}
			break;
		case 'D':	/*deprecated*/
537
			INSIST("use %ld instead of %D" == NULL);
538
		case 'O':	/*deprecated*/
539
			INSIST("use %lo instead of %O" == NULL);
540
		case 'U':	/*deprecated*/
541
			INSIST("use %lu instead of %U" == NULL);
542
543

		case 'L':
Mark Andrews's avatar
Mark Andrews committed
544
#ifdef HAVE_LONG_DOUBLE
545
			l = 1;
Mark Andrews's avatar
Mark Andrews committed
546
#else
547
			INSIST("long doubles are not supported" == NULL);
Mark Andrews's avatar
Mark Andrews committed
548
#endif
549
550
551
552
553
554
555
556
557
558
559
560
561
562
			/*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.
			 *
563
			 * precisions > 324 don't make much sense.
564
			 * if we cap the precision at 512 we will not
565
			 * overflow buf.
566
567
568
			 */
			if (precision > 512)
				precision = 512;
569
			sprintf(fmt, "%%%s%s.%lu%s%c", alt ? "#" : "",
570
571
572
573
574
575
576
577
				plus ? "+" : space ? " " : "",
				precision, l ? "L" : "", *format);
			switch (*format) {
			case 'e':
			case 'E':
			case 'f':
			case 'g':
			case 'G':
Mark Andrews's avatar
Mark Andrews committed
578
#ifdef HAVE_LONG_DOUBLE
579
580
581
				if (l) {
					ldbl = va_arg(ap, long double);
					sprintf(buf, fmt, ldbl);
Mark Andrews's avatar
Mark Andrews committed
582
583
584
				} else
#endif
				{
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
					dbl = va_arg(ap, double);
					sprintf(buf, fmt, dbl);
				}
				length = strlen(buf);
				if (width > 0) {
					pad = width - length;
					if (pad < 0)
						pad = 0;
				}
				count += length + pad;
				if (!left)
					while (pad > 0 && size > 1) {
						*str++ = ' ';
						size--;
						pad--;
					}
				cp = buf;
				while (*cp != ' ' && size > 1) {
					*str++ = *cp++;
					size--;
				}
				while (pad > 0 && size > 1) {
					*str++ = ' ';
					size--;
					pad--;
				}
				break;
			default:
				continue;
			}
			break;
		default:
			continue;
		}
		format++;
	}
	if (size > 0)
		*str = '\0';
	return (count);
}