promote argv handling functions to fko_util (avoids duplication across client and server)

This commit is contained in:
Michael Rash
2015-12-16 18:59:10 -08:00
parent f230c32371
commit 998fb96f0b
10 changed files with 95 additions and 200 deletions
+2 -2
View File
@@ -995,7 +995,7 @@ run_last_args(fko_cli_options_t *options, const char * const args_save_file)
{
FILE *args_file_ptr = NULL;
int argc_new = 0, args_broken = 0;
char args_str[MAX_LINE_LEN] = {0};
char args_str[MAX_ARGS_LINE_LEN] = {0};
char *argv_new[MAX_CMDLINE_ARGS]; /* should be way more than enough */
memset(argv_new, 0x0, sizeof(argv_new));
@@ -1014,7 +1014,7 @@ run_last_args(fko_cli_options_t *options, const char * const args_save_file)
args_str[MAX_LINE_LEN-1] = '\0';
if (options->verbose)
log_msg(LOG_VERBOSITY_NORMAL, "Executing: %s", args_str);
if(strtoargv(args_str, argv_new, &argc_new, options) != 1)
if(strtoargv(args_str, argv_new, &argc_new) != 1)
{
args_broken = 1;
}
+1 -1
View File
@@ -412,7 +412,7 @@ resolve_ip_https(fko_cli_options_t *options)
#endif
#if HAVE_EXECVPE
if(strtoargv(wget_ssl_cmd, wget_argv, &wget_argc, options) != 1)
if(strtoargv(wget_ssl_cmd, wget_argv, &wget_argc) != 1)
{
log_msg(LOG_VERBOSITY_ERROR, "Error converting wget cmd str to argv");
return(-1);
-93
View File
@@ -288,97 +288,4 @@ proto_strtoint(const char *pr_str)
return proto_int;
}
static int
add_argv(char **argv_new, int *argc_new,
const char *new_arg, fko_cli_options_t *opts)
{
int buf_size = 0;
if(opts->verbose > 2)
log_msg(LOG_VERBOSITY_NORMAL, "[+] add_argv() + arg: %s", new_arg);
buf_size = strlen(new_arg) + 1;
argv_new[*argc_new] = calloc(1, buf_size);
if(argv_new[*argc_new] == NULL)
{
log_msg(LOG_VERBOSITY_ERROR, "[*] Memory allocation error.");
return 0;
}
strlcpy(argv_new[*argc_new], new_arg, buf_size);
*argc_new += 1;
if(*argc_new >= MAX_CMDLINE_ARGS-1)
{
log_msg(LOG_VERBOSITY_ERROR, "[*] max command line args exceeded.");
return 0;
}
argv_new[*argc_new] = NULL;
return 1;
}
int
strtoargv(char *args_str, char **argv_new, int *argc_new,
fko_cli_options_t *opts)
{
int current_arg_ctr = 0, i;
char arg_tmp[MAX_LINE_LEN] = {0};
for (i=0; i < (int)strlen(args_str); i++)
{
if (!isspace(args_str[i]))
{
arg_tmp[current_arg_ctr] = args_str[i];
current_arg_ctr++;
}
else
{
if(current_arg_ctr > 0)
{
arg_tmp[current_arg_ctr] = '\0';
if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1)
{
free_argv(argv_new, argc_new);
return 0;
}
current_arg_ctr = 0;
}
}
}
/* pick up the last argument in the string
*/
if(current_arg_ctr > 0)
{
arg_tmp[current_arg_ctr] = '\0';
if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1)
{
free_argv(argv_new, argc_new);
return 0;
}
}
return 1;
}
void
free_argv(char **argv_new, int *argc_new)
{
int i;
if(argv_new == NULL || *argv_new == NULL)
return;
for (i=0; i < *argc_new; i++)
{
if(argv_new[i] == NULL)
break;
else
free(argv_new[i]);
}
return;
}
/***EOF***/
-2
View File
@@ -60,7 +60,5 @@ int resolve_dst_addr(const char *dns_str, struct addrinfo *hints,
char *ip_str, size_t ip_bufsize, fko_cli_options_t *opts);
short proto_inttostr(int proto, char *proto_str, size_t proto_size);
short proto_strtoint(const char *pr_str);
int strtoargv(char *args_str, char **argv_new, int *argc_new, fko_cli_options_t *opts);
void free_argv(char **argv_new, int *argc_new);
#endif /* UTILS_H */