aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_print.c
blob: 30a06c2f44eac44d53e062abaf277e6c99b82ccc (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
/* Copyright (c) 2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/api/hints.h>
#include <odp_print_internal.h>
#include <stdarg.h>
#include <stdio.h>

/* Helps with snprintf() return value checking
 *
 * Otherwise like snprintf(), but returns always the number of characters
 * printed (without the end mark) or zero on error. Terminates the string
 * always with the end mark. */
ODP_PRINTF_FORMAT(3, 4)
int _odp_snprint(char *str, size_t size, const char *format, ...)
{
	va_list args;
	int len;

	/* No space to print new characters */
	if (size < 1)
		return 0;

	if (size < 2) {
		str[0] = 0;
		return 0;
	}

	va_start(args, format);
	len = vsnprintf(str, size, format, args);
	va_end(args);

	/* Error. Ensure that string has the end mark */
	if (len < 0) {
		str[0] = 0;
		return 0;
	}

	/* Print would have been longer. Return the number of characters printed. */
	if (len >= (int)size)
		return (int)size - 1;

	return len;
}