1 /* execle() function: Execute a program, replacing the current process.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 may optimize away the arg0 == NULL test below. */
21 #define _GL_ARG_NONNULL(params)
34 execle (const char *program
, const char *arg0
, ...)
38 /* The callee is not expecting a NULL argv[0]. */
45 /* Count the number of arguments (including arg0 and the trailing NULL). */
47 va_start (args
, arg0
);
51 if (va_arg (args
, const char *) == NULL
)
56 /* Allocate the argument vector. */
57 const char **argv
= (const char **) malloca (count
* sizeof (const char *));
64 /* Copy the arguments into the argument vector. */
68 va_start (args
, arg0
);
70 argv
[i
++] = va_arg (args
, const char *);
72 char * const *env
= va_arg (args
, char * const *);
76 execve (program
, argv
, env
);
78 /* If execve returned, it must have failed. */
79 int saved_errno
= errno
;