lib

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

commit f6e8bc33b372c6797875357ded0345c5156cab25
parent 5de16ecefa802b8dade488d2ee8e7b12f1b6848e
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 17 May 2017 19:15:04 +0200

Add(Utils): Beginning of the work on the exec_* commands

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

diff --git a/inc/m_util.h b/inc/m_util.h @@ -50,4 +50,13 @@ static inline bool file_exist(const char *str) { return result; } +/*! + * \brief Execute a command from a string + * + * \param[in] str Command to execute + * + * \return 0 success, error on failure + */ +int exec_line(const char *str); + #endif /* M_UTIL */ diff --git a/src/m_util.c b/src/m_util.c @@ -65,3 +65,38 @@ error: fts_close(ftsp); return false; } + +int exec_line(const char *str) { + mlist_t *list = NULL; + char *cmd; + size_t j, i, len; + + if (str == NULL) + return 0; + + cmd = strdup(str); + if (cmd == NULL) + return 0; + + len = strlen(cmd); + for (i = 0, j = 0; i < len; i++) + { + if (cmd[i] == ' ') + { + while (i < len && cmd[i] == ' ') + i++; + + i--; + cmd[i] = '\0'; + list_add(list, cmd + j, i - j + 1); + cmd[i] = ' '; + j = i + 1; + } + } + list_add(list, cmd + j, i - j + 1); + + /* TODO: exec */ + list_free(list, NULL); + free(cmd); + return 0; +}