diff --git a/apps/cli/cli_pipe.c b/apps/cli/cli_pipe.c index a1e30009..f593877f 100644 --- a/apps/cli/cli_pipe.c +++ b/apps/cli/cli_pipe.c @@ -31,7 +31,7 @@ ***** END LICENSE BLOCK ***** * - * Note, here assume all binaries are in /bin + * @note Paths to bins, such as GREP_BIN, are detected in configure.ac */ #ifdef HAVE_CONFIG_H @@ -80,33 +80,40 @@ */ int pipe_arg_fn(clicon_handle h, - const char *cmd, - const char *option, - const char *value) + char *cmd, + char *option, + char *value) { - int retval = -1; - - /* XXX rewrite using execv */ - if (option){ - if (value){ - fprintf(stderr, "%s (1): %s %s %s %s NULL\n", __FUNCTION__, cmd, cmd, option, value); - retval = execl(cmd, cmd, option, value, NULL); - } - else{ - fprintf(stderr, "%s (2): %s %s %s NULL\n", __FUNCTION__, cmd, cmd, option); - retval = execl(cmd, cmd, option, NULL); - } + int retval = -1; + struct stat fstat; + char **argv = NULL; + int i; + + if (cmd == NULL || strlen(cmd) == 0){ + clicon_err(OE_PLUGIN, EINVAL, "cmd '%s' NULL or empty", cmd); + goto done; } - else{ - if (value){ - fprintf(stderr, "%s (3): %s %s %s NULL\n", __FUNCTION__, cmd, cmd, value); - retval = execl(cmd, cmd, value, NULL); - } - else{ - fprintf(stderr, "%s (4): %s %s NULL\n", __FUNCTION__, cmd, cmd); - retval = execl(cmd, cmd, NULL); - } + if (stat(cmd, &fstat) < 0) { + clicon_err(OE_UNIX, errno, "stat(%s)", cmd); + goto done; } + if (!S_ISREG(fstat.st_mode)){ + clicon_err(OE_UNIX, errno, "%s is not a regular file", cmd); + goto done; + } + if ((argv = calloc(4, sizeof(char *))) == NULL){ + clicon_err(OE_UNIX, errno, "calloc"); + goto done; + } + i = 0; + argv[i++] = cmd; + argv[i++] = option; + argv[i++] = value; + argv[i++] = NULL; + retval = execv(cmd, argv); + done: + if (argv) + free(argv); return retval; } @@ -146,12 +153,41 @@ pipe_grep_fn(clicon_handle h, strlen(str)) value = str; } - retval = pipe_arg_fn(h, "/bin/grep", option, value); + retval = pipe_arg_fn(h, GREP_BIN, option, value); done: return retval; } -/* Grep pipe output function +/*! wc pipe output function + * + * @param[in] h Clicon handle + * @param[in] cvv Vector of cli string and instantiated variables + * @param[in] argv String vector of options. Format: