lib

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

commit 2486ec289bc34a602155311bb33d289bc2d05911
parent f6e8bc33b372c6797875357ded0345c5156cab25
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 18 May 2017 19:44:15 +0200

Add(Utils): exec_list, and string list to array

Diffstat:
Minc/m_util.h | 2++
Msrc/m_util.c | 35++++++++++++++++++++++++++++++++++-
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/inc/m_util.h b/inc/m_util.h @@ -59,4 +59,6 @@ static inline bool file_exist(const char *str) { */ int exec_line(const char *str); +int exec_list(mlist_t *list); + #endif /* M_UTIL */ diff --git a/src/m_util.c b/src/m_util.c @@ -66,6 +66,31 @@ error: return false; } +MPX_STATIC char **str_list_to_array(mlist_t *list) { + char **arr = NULL, *ptr = NULL; + mlist_t *tmp; + size_t i = 0; + + arr = malloc(sizeof(char *) * (list_size(list) + 1)); + if (arr == NULL) + return NULL; + + list_for_each(list, tmp, ptr) { + arr[i] = strdup(ptr); + arr[i + 1] = NULL; + if (arr[i] == NULL) + goto end; + } + + return arr; + +end: + for (i = 0; arr[i] != NULL; i++) + free(arr[i]); + free(arr); + return NULL; +} + int exec_line(const char *str) { mlist_t *list = NULL; char *cmd; @@ -95,8 +120,16 @@ int exec_line(const char *str) { } list_add(list, cmd + j, i - j + 1); - /* TODO: exec */ + exec_list(list); list_free(list, NULL); free(cmd); return 0; } + +int exec_list(mlist_t *list) { + char **tab = NULL; + + tab = str_list_to_array(list); + // exec + return 0; +}