From eea014efb7f19eb160581314e74a899a2efca5c6 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 18 Mar 2022 15:10:04 +0800 Subject: [PATCH] Fix error: conflicting types for built-in function 'execl'; expected 'int(const char *, const char *, ...) Signed-off-by: Xiang Xiao --- include/unistd.h | 2 +- libs/libc/unistd/lib_execl.c | 82 ++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/include/unistd.h b/include/unistd.h index d158955a98..b383f20553 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -367,7 +367,7 @@ int lchown(FAR const char *path, uid_t owner, gid_t group); /* Execution of programs from files */ #ifdef CONFIG_LIBC_EXECFUNCS -int execl(FAR const char *path, ...); +int execl(FAR const char *path, FAR const char *arg0, ...); int execv(FAR const char *path, FAR char * const argv[]); #endif diff --git a/libs/libc/unistd/lib_execl.c b/libs/libc/unistd/lib_execl.c index 5caba340cb..54ea688798 100644 --- a/libs/libc/unistd/lib_execl.c +++ b/libs/libc/unistd/lib_execl.c @@ -113,10 +113,10 @@ * ****************************************************************************/ -int execl(FAR const char *path, ...) +int execl(FAR const char *path, FAR const char *arg0, ...) { - FAR char **argv = (FAR char **)NULL; - FAR char *arg; + FAR char **argv = NULL; + FAR char *arg = (FAR char *)arg0; size_t nargs; va_list ap; int argc; @@ -124,67 +124,57 @@ int execl(FAR const char *path, ...) /* Count the number of arguments */ - va_start(ap, path); + va_start(ap, arg0); nargs = 0; - do + + while (arg != NULL) { - /* Check if the next argument is present */ + /* Yes.. increment the number of arguments. Here is a sanity + * check to prevent running away with an unterminated argv[] list. + * MAX_EXECL_ARGS should be sufficiently large that this never + * happens in normal usage. + */ + + if (++nargs > MAX_EXECL_ARGS) + { + set_errno(E2BIG); + va_end(ap); + return ERROR; + } arg = va_arg(ap, FAR char *); - if (arg) - { - /* Yes.. increment the number of arguments. Here is a sanity - * check to prevent running away with an unterminated argv[] list. - * MAX_EXECL_ARGS should be sufficiently large that this never - * happens in normal usage. - */ - - if (++nargs > MAX_EXECL_ARGS) - { - set_errno(E2BIG); - va_end(ap); - return ERROR; - } - } } - while (arg); va_end(ap); /* Allocate a temporary argv[] array */ - if (nargs > 0) + argv = (FAR char **)lib_malloc((nargs + 1) * sizeof(FAR char *)); + if (argv == NULL) { - argv = (FAR char **)lib_malloc((nargs + 1) * sizeof(FAR char *)); - if (argv == (FAR char **)NULL) - { - set_errno(ENOMEM); - return ERROR; - } - - /* Collect the arguments into the argv[] array */ - - va_start(ap, path); - for (argc = 0; argc < nargs; argc++) - { - argv[argc] = va_arg(ap, FAR char *); - } - - argv[nargs] = NULL; - va_end(ap); + set_errno(ENOMEM); + return ERROR; } + argv[0] = (FAR char *)arg0; + + /* Collect the arguments into the argv[] array */ + + va_start(ap, arg0); + for (argc = 1; argc <= nargs; argc++) + { + argv[argc] = va_arg(ap, FAR char *); + } + + va_end(ap); + /* Then let execv() do the real work */ - ret = execv(path, (FAR char * const *)argv); + ret = execv(path, argv); /* Free the allocated argv[] list */ - if (argv) - { - lib_free(argv); - } - + lib_free(argv); return ret; }