>From 29d55bf8c7d0c5a4a3b4aa2d93d897ff8463eb4c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 14 Dec 2020 19:22:14 +0100 Subject: [PATCH 3/4] posix_spawn-internal: Make better use of 'const'. * lib/spawn_int.h (__spawni): Does not need write access to the elements of argv and envp. * lib/spawni.c (__spawni, script_execute): Likewise. * lib/spawn.c (posix_spawn): Update caller. * lib/spawnp.c (posix_spawnp): Likewise. --- ChangeLog | 9 +++++++++ lib/spawn.c | 3 ++- lib/spawn_int.h | 4 ++-- lib/spawni.c | 20 +++++++++++--------- lib/spawnp.c | 3 ++- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 21cc216..f909f61 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2020-12-14 Bruno Haible + posix_spawn-internal: Make better use of 'const'. + * lib/spawn_int.h (__spawni): Does not need write access to the elements + of argv and envp. + * lib/spawni.c (__spawni, script_execute): Likewise. + * lib/spawn.c (posix_spawn): Update caller. + * lib/spawnp.c (posix_spawnp): Likewise. + +2020-12-14 Bruno Haible + spawn: Make it compile on native Windows. * modules/spawn (Depends-on): Add signal-h. diff --git a/lib/spawn.c b/lib/spawn.c index c139b71..b658453 100644 --- a/lib/spawn.c +++ b/lib/spawn.c @@ -29,5 +29,6 @@ posix_spawn (pid_t *pid, const char *path, const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) { - return __spawni (pid, path, file_actions, attrp, argv, envp, 0); + return __spawni (pid, path, file_actions, attrp, + (const char * const *) argv, (const char * const *) envp, 0); } diff --git a/lib/spawn_int.h b/lib/spawn_int.h index 7c063b6..60a94dd 100644 --- a/lib/spawn_int.h +++ b/lib/spawn_int.h @@ -68,5 +68,5 @@ extern int __posix_spawn_file_actions_realloc (posix_spawn_file_actions_t * #endif extern int __spawni (pid_t *pid, const char *path, const posix_spawn_file_actions_t *file_actions, - const posix_spawnattr_t *attrp, char *const argv[], - char *const envp[], int use_path); + const posix_spawnattr_t *attrp, const char *const argv[], + const char *const envp[], int use_path); diff --git a/lib/spawni.c b/lib/spawni.c index 07bbec7..1e20a97 100644 --- a/lib/spawni.c +++ b/lib/spawni.c @@ -109,7 +109,8 @@ __spawni (pid_t *pid, const char *file, the shell to interpret it as a script. */ static void internal_function -script_execute (const char *file, char *const argv[], char *const envp[]) +script_execute (const char *file, const char *const argv[], + const char *const envp[]) { /* Count the arguments. */ int argc = 0; @@ -118,9 +119,10 @@ script_execute (const char *file, char *const argv[], char *const envp[]) /* Construct an argument list for the shell. */ { - char **new_argv = (char **) alloca ((argc + 1) * sizeof (char *)); - new_argv[0] = (char *) _PATH_BSHELL; - new_argv[1] = (char *) file; + const char **new_argv = + (const char **) alloca ((argc + 1) * sizeof (const char *)); + new_argv[0] = _PATH_BSHELL; + new_argv[1] = file; while (argc > 1) { new_argv[argc] = argv[argc - 1]; @@ -128,7 +130,7 @@ script_execute (const char *file, char *const argv[], char *const envp[]) } /* Execute the shell. */ - execve (new_argv[0], new_argv, envp); + execve (new_argv[0], (char * const *) new_argv, (char * const *) envp); } } @@ -138,8 +140,8 @@ script_execute (const char *file, char *const argv[], char *const envp[]) int __spawni (pid_t *pid, const char *file, const posix_spawn_file_actions_t *file_actions, - const posix_spawnattr_t *attrp, char *const argv[], - char *const envp[], int use_path) + const posix_spawnattr_t *attrp, const char *const argv[], + const char *const envp[], int use_path) { pid_t new_pid; char *path, *p, *name; @@ -303,7 +305,7 @@ __spawni (pid_t *pid, const char *file, if (! use_path || strchr (file, '/') != NULL) { /* The FILE parameter is actually a path. */ - execve (file, argv, envp); + execve (file, (char * const *) argv, (char * const *) envp); if (errno == ENOEXEC) script_execute (file, argv, envp); @@ -354,7 +356,7 @@ __spawni (pid_t *pid, const char *file, startp = (char *) memcpy (name - (p - path), path, p - path); /* Try to execute this name. If it works, execv will not return. */ - execve (startp, argv, envp); + execve (startp, (char * const *) argv, (char * const *) envp); if (errno == ENOEXEC) script_execute (startp, argv, envp); diff --git a/lib/spawnp.c b/lib/spawnp.c index 0f03e79..91c54a1 100644 --- a/lib/spawnp.c +++ b/lib/spawnp.c @@ -29,5 +29,6 @@ posix_spawnp (pid_t *pid, const char *file, const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) { - return __spawni (pid, file, file_actions, attrp, argv, envp, 1); + return __spawni (pid, file, file_actions, attrp, + (const char * const *) argv, (const char * const *) envp, 1); } -- 2.7.4