/*
 * Copyright (c) 2018-2021 mcumgr authors
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <string.h>

#include "img_mgmt/image.h"
#include "img_mgmt/img_mgmt.h"

int
img_mgmt_ver_str(const struct image_version *ver, char *dst)
{
	int rc = 0;
	int rc1 = 0;

	rc = snprintf(dst, IMG_MGMT_VER_MAX_STR_LEN, "%hhu.%hhu.%hu",
		ver->iv_major, ver->iv_minor, ver->iv_revision);

	if (rc >= 0 && ver->iv_build_num != 0) {
		rc1 = snprintf(&dst[rc], IMG_MGMT_VER_MAX_STR_LEN - rc, ".%u",
			ver->iv_build_num);
	}

	if (rc1 >= 0 && rc >= 0) {
		rc = rc + rc1;
	} else {
		/* If any failed then all failed */
		rc = -1;
	}

	return rc;
}

/*
 * Copyright (c) 2018-2021 mcumgr authors
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <string.h>

#include "img_mgmt/image.h"
#include "img_mgmt/img_mgmt.h"

int
img_mgmt_ver_str(const struct image_version *ver, char *dst)
{
	int rc = 0;
	int rc1 = 0;

	rc = snprintf(dst, IMG_MGMT_VER_MAX_STR_LEN, "%hu.%hu.%hu",
		(uint16_t)ver->iv_major, (uint16_t)ver->iv_minor,
		ver->iv_revision);

	if (rc >= 0 && ver->iv_build_num != 0) {
		rc1 = snprintf(&dst[rc], IMG_MGMT_VER_MAX_STR_LEN - rc, ".%u",
			ver->iv_build_num);
	}

	if (rc1 >= 0 && rc >= 0) {
		rc = rc + rc1;
	} else {
		/* If any failed then all failed */
		rc = -1;
	}

	return rc;
}

Make version string work with CONFIG_NEWLIB_LIBC
mgmt/mcumgr/lib: Make version string work with CONFIG_NEWLIB_LIBC

By default the newlib does not compile with %hhu processing,
to avoid turning on additional options for newlib compilation
this commit changes formatting string to use %hu instead.
The commit fixes problem with malformed version string being
returned, by smp_svr, when CONFIG_NEWLIB_LIBC=y is set together
with CONFIG_NEWLIB_LIBC_NANO=y.

Fixes #45261

Signed-off-by: Dominik Ermel <1a1d45a9cc0c98a37f8d0a0d2dbe3cacc0b2344f@nordicsemi.no>
