From 980afcce53fefd762f2a1c16eaa01e9ff3c004e0 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sat, 6 Aug 2016 12:41:56 +0300 Subject: [PATCH] cli: Share a common write_cmd() implementation wpa_cli and hostapd_cli had identical copies of this function. Signed-off-by: Jouni Malinen --- hostapd/hostapd_cli.c | 30 ------------------------------ src/common/cli.c | 29 +++++++++++++++++++++++++++++ src/common/cli.h | 2 ++ wpa_supplicant/wpa_cli.c | 30 ------------------------------ 4 files changed, 31 insertions(+), 60 deletions(-) diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c index 56f5fe251..196f512f1 100644 --- a/hostapd/hostapd_cli.c +++ b/hostapd/hostapd_cli.c @@ -187,36 +187,6 @@ static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd) } -static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, - char *argv[]) -{ - int i, res; - char *pos, *end; - - pos = buf; - end = buf + buflen; - - res = os_snprintf(pos, end - pos, "%s", cmd); - if (os_snprintf_error(end - pos, res)) - goto fail; - pos += res; - - for (i = 0; i < argc; i++) { - res = os_snprintf(pos, end - pos, " %s", argv[i]); - if (os_snprintf_error(end - pos, res)) - goto fail; - pos += res; - } - - buf[buflen - 1] = '\0'; - return 0; - -fail: - printf("Too long command\n"); - return -1; -} - - static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args, int argc, char *argv[]) { diff --git a/src/common/cli.c b/src/common/cli.c index 6e0018875..64c015916 100644 --- a/src/common/cli.c +++ b/src/common/cli.c @@ -206,3 +206,32 @@ int get_cmd_arg_num(const char *str, int pos) arg--; return arg; } + + +int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, char *argv[]) +{ + int i, res; + char *pos, *end; + + pos = buf; + end = buf + buflen; + + res = os_snprintf(pos, end - pos, "%s", cmd); + if (os_snprintf_error(end - pos, res)) + goto fail; + pos += res; + + for (i = 0; i < argc; i++) { + res = os_snprintf(pos, end - pos, " %s", argv[i]); + if (os_snprintf_error(end - pos, res)) + goto fail; + pos += res; + } + + buf[buflen - 1] = '\0'; + return 0; + +fail: + printf("Too long command\n"); + return -1; +} diff --git a/src/common/cli.h b/src/common/cli.h index 4caf321c4..9f9f85113 100644 --- a/src/common/cli.h +++ b/src/common/cli.h @@ -38,5 +38,7 @@ int cli_txt_list_add_word(struct dl_list *txt_list, const char *txt, char ** cli_txt_list_array(struct dl_list *txt_list); int get_cmd_arg_num(const char *str, int pos); +int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, + char *argv[]); #endif /* CLI_H */ diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c index 42be5b6f6..28fe76c1f 100644 --- a/wpa_supplicant/wpa_cli.c +++ b/wpa_supplicant/wpa_cli.c @@ -256,36 +256,6 @@ static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd) } -static int write_cmd(char *buf, size_t buflen, const char *cmd, int argc, - char *argv[]) -{ - int i, res; - char *pos, *end; - - pos = buf; - end = buf + buflen; - - res = os_snprintf(pos, end - pos, "%s", cmd); - if (os_snprintf_error(end - pos, res)) - goto fail; - pos += res; - - for (i = 0; i < argc; i++) { - res = os_snprintf(pos, end - pos, " %s", argv[i]); - if (os_snprintf_error(end - pos, res)) - goto fail; - pos += res; - } - - buf[buflen - 1] = '\0'; - return 0; - -fail: - printf("Too long command\n"); - return -1; -} - - static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args, int argc, char *argv[]) {