Skip to content

Commit

Permalink
samples: posix: uname: add toy uname cmd
Browse files Browse the repository at this point in the history
Added `uname` shell cmd, basically copied from nuttx.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jul 4, 2023
1 parent bcbc1b6 commit 58c7e3e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions samples/posix/uname/prj.conf
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
CONFIG_POSIX_API=y
CONFIG_SHELL=y
CONFIG_SHELL_GETOPT=y
147 changes: 147 additions & 0 deletions samples/posix/uname/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/

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

int main(void)
{
Expand All @@ -23,6 +25,151 @@ int main(void)
return 0;
}

#define UNAME_KERNEL (1 << 0)
#define UNAME_NODE (1 << 1)
#define UNAME_RELEASE (1 << 2)
#define UNAME_VERSION (1 << 3)
#define UNAME_MACHINE (1 << 4)
#define UNAME_PLATFORM (1 << 5)
#define UNAME_UNKNOWN (1 << 6)
#define UNAME_ALL \
(UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM)
void getopt_init(void);
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
{
struct utsname info;
unsigned int set;
int option;
bool badarg;
bool first;
int ret;
int i;

set = 0;
badarg = false;

/* Get the uname options */

optind = 1;
while ((option = getopt(argc, argv, "asonrvmpi")) != -1) {
switch (option) {
case 'a':
set = UNAME_ALL;
break;

case 'o':
case 's':
set |= UNAME_KERNEL;
break;

case 'n':
set |= UNAME_NODE;
break;

case 'r':
set |= UNAME_RELEASE;
break;

case 'v':
set |= UNAME_VERSION;
break;

case 'm':
set |= UNAME_MACHINE;
break;

case 'p':
if (set != UNAME_ALL) {
set |= UNAME_UNKNOWN;
}
break;

case 'i':
set |= UNAME_PLATFORM;
break;

case '?':
default:
badarg = true;
break;
}
}

/* If a bad argument was encountered, then return without processing the
* command
*/

if (badarg) {
struct getopt_state *state = getopt_state_get();

shell_error(sh, "uname: illegal option -- %c", state->optopt);
shell_print(sh, "usage: uname [-asonrvmpi]");
return -1;
}

/* If nothing is provided on the command line, the default is -s */

if (set == 0) {
set = UNAME_KERNEL;
}

/* Get uname data */

ret = uname(&info);
if (ret < 0) {
shell_error(sh, "uname: returned %d", ret);
return -1;
}

/* Process each option */

first = true;
for (i = 0; set != 0; i++) {
unsigned int mask = (1 << i);

if ((set & mask) != 0) {
set &= ~mask;
switch (i) {
case 0: /* print the kernel/operating system name */
shell_fprintf(sh, SHELL_NORMAL, "%s", info.sysname);
break;

case 1: /* Print nodename */
shell_fprintf(sh, SHELL_NORMAL, "%s", info.nodename);
break;

case 2: /* Print the kernel release */
shell_fprintf(sh, SHELL_NORMAL, "%s", info.release);
break;

case 3: /* Print the kernel version */
shell_fprintf(sh, SHELL_NORMAL, "%s", info.version);
break;

case 4: /* Print the machine hardware name */
shell_fprintf(sh, SHELL_NORMAL, "%s", info.machine);
break;

case 5: /* Print the machine platform name */
shell_fprintf(sh, SHELL_NORMAL, "%s", CONFIG_BOARD);
break;

case 6: /* Print "unknown" */
shell_fprintf(sh, SHELL_NORMAL, "%s", "unknown");
break;

default:
shell_error(sh, "uname: illegal option -- %d", i);
return -1;
}

shell_fprintf(sh, SHELL_NORMAL, " ");
}
}

shell_fprintf(sh, SHELL_NORMAL, "\n");

return 0;
}

SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler);

0 comments on commit 58c7e3e

Please sign in to comment.