summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/libntp/xsbprintf.c
blob: 4586758bc88aa1f704136aaa9a7b4da8383dad69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
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
/*
 * xsbprintf.c - string buffer formatting helpers
 *
 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
 * The contents of 'html/copyright.html' apply.
 */

#include <config.h>
#include <sys/types.h>

#include "ntp_stdlib.h"

/* eXtended Varlist String Buffer printf
 *
 * Formats via 'vsnprintf' into a string buffer, with some semantic
 * specialties:
 *
 * - The start of the buffer pointer is updated according to the number
 *   of characters written.
 * - If the buffer is insufficient to format the number of charactes,
 *   the partial result will be be discarded, and zero is returned to
 *   indicate nothing was written to the buffer.
 * - On successful formatting, the return code is the return value of
 *   the inner call to 'vsnprintf()'.
 * - If there is any error, the state of the buffer will not be
 *   changed. (Bytes in the buffer might be smashed, but the buffer
 *   position does not change, and the NUL marker stays in place at the
 *   current buffer position.)
 * - If '(*ppbuf - pend) <= 0' (or ppbuf is NULL), fail with EINVAL.
 */
int
xvsbprintf(
	char       **ppbuf,	/* pointer to buffer pointer (I/O) */
	char * const pend,	/* buffer end (I)		   */
	char const  *pfmt,	/* printf-like format string       */
	va_list      va		/* formatting args for above       */
	)
{
	char *pbuf = (ppbuf) ? *ppbuf : NULL;
	int   rc   = -1;
	if (pbuf && (pend - pbuf > 0)) {
		size_t blen = (size_t)(pend - pbuf);
		rc = vsnprintf(pbuf, blen, pfmt, va);
		if (rc > 0) {
		    if ((size_t)rc >= blen)
			rc = 0;
		    pbuf += rc;
		}
		*pbuf = '\0'; /* fear of bad vsnprintf */
		*ppbuf = pbuf;
	} else {
		errno = EINVAL;
	}
	return rc;
}

/* variadic wrapper around the buffer string formatter */
int
xsbprintf(
	char       **ppbuf,	/* pointer to buffer pointer (I/O) */
	char * const pend,	/* buffer end (I)		   */
	char const  *pfmt,	/* printf-like format string       */
	...			/* formatting args for above       */
	)
{
	va_list va;
	int     rc;
	
	va_start(va, pfmt);
	rc = xvsbprintf(ppbuf, pend, pfmt, va);
	va_end(va);
	return rc;
}

/* that's all folks! */
OpenPOWER on IntegriCloud