Skip to content

Commit

Permalink
samples: posix: add sample for uname
Browse files Browse the repository at this point in the history
Add a simple sample to print everything in the utsname struct.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jul 4, 2023
1 parent 1373944 commit bb201fe
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
10 changes: 5 additions & 5 deletions include/zephyr/posix/sys/utsname.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_
#define ZEPHYR_INCLUDE_POSIX_SYS_UTSNAME_H_

#include "version.h"
#include <errno.h>
#include <zephyr/toolchain/common.h>
#include <zephyr/net/hostname.h>
#include "version.h"
#include <zephyr/toolchain/common.h>

#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 */
Expand Down
5 changes: 3 additions & 2 deletions lib/posix/utsname.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/posix/sys/utsname.h>
#include <stdio.h>
#include <string.h>
#include <zephyr/posix/sys/utsname.h>

int uname(struct utsname *name)
{
strncpy(name->sysname, "Zephyr", sizeof(name->sysname));
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
Expand Down
8 changes: 8 additions & 0 deletions samples/posix/uname/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions samples/posix/uname/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_POSIX_API=y
22 changes: 22 additions & 0 deletions samples/posix/uname/sample.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions samples/posix/uname/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Meta
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <sys/utsname.h>

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

0 comments on commit bb201fe

Please sign in to comment.