diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index bb4e201295..ace70ee1b0 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -143,6 +143,7 @@ NXSYMBOLS(strlen) NXSYMBOLS(strtol) NXSYMBOLS(sysconf) NXSYMBOLS(syslog) +NXSYMBOLS(system) NXSYMBOLS(tcgetattr) NXSYMBOLS(tcsetattr) NXSYMBOLS(unlink) diff --git a/arch/sim/src/sim/posix/sim_hostmisc.c b/arch/sim/src/sim/posix/sim_hostmisc.c index 8be18a05d7..b23403c24c 100644 --- a/arch/sim/src/sim/posix/sim_hostmisc.c +++ b/arch/sim/src/sim/posix/sim_hostmisc.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,61 @@ int host_backtrace(void** array, int size) #endif } +/**************************************************************************** + * Name: host_system + * + * Description: + * Execute the command and get the result. + * + * Input Parameters: + * buf - return massage, which return info will be stored + * len - buf length + * fmt - the format of parameters + * ... - variable parameters. + * + * Returned Value: + * A nonnegative integer is returned on success. Otherwise, + * a negated errno value is returned to indicate the nature of the failure. + ****************************************************************************/ + +int host_system(char *buf, size_t len, const char *fmt, ...) +{ + FILE *fp; + int ret; + uint64_t flags; + char cmd[512]; + va_list vars; + + va_start(vars, fmt); + ret = vsnprintf(cmd, sizeof(cmd), fmt, vars); + va_end(vars); + if (ret <= 0 || ret > sizeof(cmd)) + { + return ret < 0 ? -errno : -EINVAL; + } + + if (buf == NULL) + { + ret = system(cmd); + } + else + { + flags = up_irq_save(); + fp = popen(cmd, "r"); + if (fp == NULL) + { + up_irq_restore(flags); + return -errno; + } + + ret = fread(buf, sizeof(char), len, fp); + pclose(fp); + up_irq_restore(flags); + } + + return ret < 0 ? -errno : ret; +} + /**************************************************************************** * Name: host_init_cwd ****************************************************************************/ diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index ef78bb891b..47b0084a22 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -163,6 +163,7 @@ void *sim_doirq(int irq, void *regs); void host_abort(int status); int host_backtrace(void** array, int size); +int host_system(char *buf, size_t len, const char *fmt, ...); #ifdef CONFIG_SIM_IMAGEPATH_AS_CWD void host_init_cwd(void);