From bb201fecb0021e867192f1fecdebbdadc77847d3 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Tue, 4 Jul 2023 15:17:34 +0800 Subject: [PATCH] samples: posix: add sample for uname Add a simple sample to print everything in the utsname struct. Signed-off-by: Yong Cong Sin --- include/zephyr/posix/sys/utsname.h | 10 +++++----- lib/posix/utsname.c | 5 +++-- samples/posix/uname/CMakeLists.txt | 8 ++++++++ samples/posix/uname/prj.conf | 1 + samples/posix/uname/sample.yaml | 22 ++++++++++++++++++++++ samples/posix/uname/src/main.c | 28 ++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 samples/posix/uname/CMakeLists.txt create mode 100644 samples/posix/uname/prj.conf create mode 100644 samples/posix/uname/sample.yaml create mode 100644 samples/posix/uname/src/main.c diff --git a/include/zephyr/posix/sys/utsname.h b/include/zephyr/posix/sys/utsname.h index 11a3cbf8231f450..9c49cca7f947f4f 100644 --- a/include/zephyr/posix/sys/utsname.h +++ b/include/zephyr/posix/sys/utsname.h @@ -6,23 +6,23 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_ #define ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_ +#include "version.h" #include -#include #include -#include "version.h" +#include #ifdef __cplusplus extern "C" { #endif /* sysname */ -#define SYS_NAMELEN sizeof("Zephyr") +#define SYS_NAMELEN sizeof("Zephyr") /* nodename */ #ifdef CONFIG_NET_HOSTNAME_ENABLE -#define NODE_NAMELEN NET_HOSTNAME_MAX_LEN +#define NODE_NAMELEN NET_HOSTNAME_MAX_LEN #else -#define NODE_NAMELEN SYS_NAMELEN +#define NODE_NAMELEN SYS_NAMELEN #endif /* release */ diff --git a/lib/posix/utsname.c b/lib/posix/utsname.c index ece0717ef7327db..d1a28a4a8b225b1 100644 --- a/lib/posix/utsname.c +++ b/lib/posix/utsname.c @@ -4,9 +4,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include #include #include +#include int uname(struct utsname *name) { @@ -14,7 +14,8 @@ int uname(struct utsname *name) strncpy(name->nodename, net_hostname_get(), sizeof(name->nodename)); strncpy(name->release, KERNEL_VERSION_STRING, sizeof(name->release)); #if defined(__DATE__) && defined(__TIME__) - snprintf(name->version, sizeof(name->version), "%s %s %s", VERSION_BUILD, __DATE__, __TIME__); + snprintf(name->version, sizeof(name->version), "%s %s %s", VERSION_BUILD, __DATE__, + __TIME__); #else strncpy(name->version, VERSION_BUILD, sizeof(name->version)); #endif diff --git a/samples/posix/uname/CMakeLists.txt b/samples/posix/uname/CMakeLists.txt new file mode 100644 index 000000000000000..82257e7cc7d8e17 --- /dev/null +++ b/samples/posix/uname/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(posix_uname) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/posix/uname/prj.conf b/samples/posix/uname/prj.conf new file mode 100644 index 000000000000000..b1c2aa0433a336b --- /dev/null +++ b/samples/posix/uname/prj.conf @@ -0,0 +1 @@ +CONFIG_POSIX_API=y diff --git a/samples/posix/uname/sample.yaml b/samples/posix/uname/sample.yaml new file mode 100644 index 000000000000000..cfe990eb127d359 --- /dev/null +++ b/samples/posix/uname/sample.yaml @@ -0,0 +1,22 @@ +sample: + description: posix uname sample + name: posix uname +common: + tags: posix + filter: not CONFIG_ARCH_POSIX + integration_platforms: + - native_posix_64 + - qemu_riscv64 + harness: console + harness_config: + type: multi_line + ordered: true + regex: + - "sysname\\[7\\]: Zephyr" + - "nodename\\[\\d+\\]: .*" + - "release\\[\\d+\\]: \\d+\\.\\d+\\.\\d+" + - "version\\[\\d+\\]: .*" + - "machine\\[\\d+\\]: .*" +tests: + sample.posix.uname: + tags: uname diff --git a/samples/posix/uname/src/main.c b/samples/posix/uname/src/main.c new file mode 100644 index 000000000000000..f19487f185a11c6 --- /dev/null +++ b/samples/posix/uname/src/main.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +int main(void) +{ + struct utsname info; + + uname(&info); + + printk("\nPrinting everything in utsname...\n"); + printk("sysname[%ld]: %s\n", sizeof(info.sysname), info.sysname); + printk("nodename[%ld]: %s\n", sizeof(info.nodename), info.nodename); + printk("release[%ld]: %s\n", sizeof(info.release), info.release); + printk("version[%ld]: %s\n", sizeof(info.version), info.version); + printk("machine[%ld]: %s\n", sizeof(info.machine), info.machine); + + return 0; +} + + + return 0; +}