lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 937f638ad514ecc65b88226266b9396e8003d0d6
parent 2486ec289bc34a602155311bb33d289bc2d05911
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 19 May 2017 12:19:03 +0200

Add(Utils): New functions for execution

Diffstat:
Minc/m_util.h | 2++
Msrc/m_util.c | 26++++++++++++++++++++++++--
2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/inc/m_util.h b/inc/m_util.h @@ -21,6 +21,8 @@ # include <sys/types.h> # include <sys/stat.h> # include <morphux.h> +# include <sys/wait.h> + # define STR_OR_EMPTY(str) (str ? str : "") # define STR_NULL_OR_EMPTY(str) (str == NULL || (str != NULL && *str == '\0')) diff --git a/src/m_util.c b/src/m_util.c @@ -80,6 +80,7 @@ MPX_STATIC char **str_list_to_array(mlist_t *list) { arr[i + 1] = NULL; if (arr[i] == NULL) goto end; + i++; } return arr; @@ -128,8 +129,29 @@ int exec_line(const char *str) { int exec_list(mlist_t *list) { char **tab = NULL; + int pid, status = 1; tab = str_list_to_array(list); - // exec - return 0; + + if (tab == NULL) + goto end; + + if ((pid = fork()) == -1) + goto end; + + if (pid == 0) + { + execvp(tab[0], tab); + exit(1); + } + else + { + waitpid(pid, &status, 0); + } + +end: + for (size_t i = 0; tab[i]; i++) + free(tab[i]); + free(tab); + return status; }