/* * Create a process description */ ni_shellcmd_t * ni_shellcmd_new(const ni_string_array_t *argv) { ni_shellcmd_t *cmd; unsigned int i; cmd = xcalloc(1, sizeof(*cmd)); cmd->refcount = 1; if (!argv) return cmd; for (i = 0; i < argv->count; ++i) { const char *arg = argv->data[i]; if (ni_string_empty(arg)) { __ni_shellcmd_free(cmd); return NULL; } if (ni_string_array_append(&cmd->argv, arg) < 0) { __ni_shellcmd_free(cmd); return NULL; } } if (__ni_shellcmd_format(&cmd->command, &cmd->argv) == NULL) { __ni_shellcmd_free(cmd); return NULL; } if (ni_string_array_copy(&cmd->environ, __ni_default_environment()) < 0) { __ni_shellcmd_free(cmd); return NULL; } return cmd; }
ni_bool_t ni_shellcmd_add_arg(ni_shellcmd_t *cmd, const char *arg) { if (!cmd || ni_string_empty(arg)) return FALSE; if (ni_string_array_append(&cmd->argv, arg) < 0) return FALSE; if (__ni_shellcmd_format(&cmd->command, &cmd->argv) == NULL) return FALSE; return TRUE; }
static ni_bool_t __ni_shellcmd_set_argv(ni_shellcmd_t *cmd, const ni_string_array_t *argv) { unsigned int i; for (i = 0; i < argv->count; ++i) { const char *arg = argv->data[i]; if (ni_string_empty(arg)) return FALSE; if (ni_string_array_append(&cmd->argv, arg) < 0) return FALSE; } return __ni_shellcmd_format(&cmd->command, &cmd->argv) != NULL; }