cli: Share a common write_cmd() implementation

wpa_cli and hostapd_cli had identical copies of this function.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2016-08-06 12:41:56 +03:00
parent fcc84b48b2
commit 980afcce53
4 changed files with 31 additions and 60 deletions

View file

@ -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, static int hostapd_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd,
int min_args, int argc, char *argv[]) int min_args, int argc, char *argv[])
{ {

View file

@ -206,3 +206,32 @@ int get_cmd_arg_num(const char *str, int pos)
arg--; arg--;
return 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;
}

View file

@ -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); char ** cli_txt_list_array(struct dl_list *txt_list);
int get_cmd_arg_num(const char *str, int pos); 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 */ #endif /* CLI_H */

View file

@ -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, static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
int argc, char *argv[]) int argc, char *argv[])
{ {