logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 91568cf63c4a3a8774ab1e63aae905db04d2608b
parent ed9f79677cc7cda1cf857b59bb4559ea1d4907a9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 18 Dec 2022 08:48:26 +0100

cmd/sname: Use tab-separation

Default in awk, look, ...

Diffstat:

Mcmd/sname.13++-
Mcmd/sname.c27++++++++++++++++-----------
Mtest-cmd/sname10+++++-----
3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/cmd/sname.1 b/cmd/sname.1 @@ -13,10 +13,11 @@ .Nm is equivalent to .Xr uname 1 -except it's output is structed, which allows to reuse it more easily in other programs. +except the output is a sorted tab-separated key-value list, which allows to be extracted and extended. .Sh EXIT STATUS .Ex -std .Sh SEE ALSO +.Xr look 1 .Xr uname 3 .Sh STANDARDS No applicable one known. diff --git a/cmd/sname.c b/cmd/sname.c @@ -3,8 +3,19 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #include <stdio.h> // printf(), perror() +#include <stdlib.h> // exit() #include <sys/utsname.h> // utsname, uname() +void +print_kv(char *key, char *value) +{ + if(printf("%s %s\n", key, value) < 0) + { + perror("sname: printf"); + exit(1); + } +} + int main() { @@ -15,17 +26,11 @@ main() return 1; } - int ret = printf("sysname=%s\nnodename=%s\nrelease=%s\nversion=%s\nmachine=%s\n", - name.sysname, - name.nodename, - name.release, - name.version, - name.machine); - if(ret < 0) - { - perror("sname"); - return 1; - } + print_kv("machine", name.machine); + print_kv("nodename", name.nodename); + print_kv("release", name.release); + print_kv("sysname", name.sysname); + print_kv("version", name.version); return 0; } diff --git a/test-cmd/sname b/test-cmd/sname @@ -6,11 +6,11 @@ atf_test_case generic generic_body() { # sed is because of FreeBSD atf_check -o "inline:\ -sysname=$(uname -s) -nodename=$(uname -n) -release=$(uname -r) -version=$(uname -v | sed 's; *$;;') -machine=$(uname -m) +machine $(uname -m) +nodename $(uname -n) +release $(uname -r) +sysname $(uname -s) +version $(uname -v | sed 's; *$;;') " ../cmd/sname }