lib

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

commit 7f1d6a01106eaad746d693126a17f8bd4dbc9237
parent 7b5bf5d9b91b7b3b3082d68ddd4b6d80b6da8242
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 22 May 2017 16:25:36 +0200

Add(Utils): Add a helper to set compilation flags in order to handle output of execution

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

diff --git a/inc/m_util.h b/inc/m_util.h @@ -23,11 +23,25 @@ # include <morphux.h> # include <sys/wait.h> +# define COMP_FLAGS_NONE 1 << 0x0 +# define COMP_FLAGS_FILE 1 << 0x1 +# define COMP_FLAGS_STDOUT 1 << 0x2 + # define STR_OR_EMPTY(str) (str ? str : "") # define STR_NULL_OR_EMPTY(str) (str == NULL || (str != NULL && *str == '\0')) /*! + * \brief Set flags for compilation output + * + * \param[in] flags Flags to set + * + * \note If COMP_FLAGS_FILE is used, the compilations will call m_log. + * Caller must init the logging first. + */ +void set_execute_flags(u8_t flags); + +/*! * \brief Delete a directory recursively * * \param[in] dir Path of the directory to delete diff --git a/src/m_util.c b/src/m_util.c @@ -17,6 +17,12 @@ #include <m_util.h> #include <fts.h> +static u8_t g_compile_flags = COMP_FLAGS_NONE; + +void set_execute_flags(u8_t flags) { + g_compile_flags = flags; +} + bool recursive_delete(const char *dir) { FTS *ftsp = NULL; FTSENT *curr = NULL; @@ -131,23 +137,33 @@ int exec_line(const char *str) { int exec_list(mlist_t *list) { char **tab = NULL; int pid, status = 1; + int fds[2]; tab = str_list_to_array(list); if (tab == NULL) goto end; + pipe(fds); if ((pid = fork()) == -1) goto end; if (pid == 0) { + if (g_compile_flags & COMP_FLAGS_NONE) + { + dup2(fds[1], 1); + dup2(fds[1], 2); + } + close(fds[1]); + execvp(tab[0], tab); exit(1); } else { waitpid(pid, &status, 0); + close(fds[1]); } end: