openat: don’t close (-1)
[gnulib.git] / lib / execle.c
blob3a93a694eafd366be7ee669015d096de1fa49c61
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)
23 #include <config.h>
25 /* Specification. */
26 #include <unistd.h>
28 #include <errno.h>
29 #include <stdarg.h>
31 #include "malloca.h"
33 int
34 execle (const char *program, const char *arg0, ...)
36 va_list args;
38 /* The callee is not expecting a NULL argv[0]. */
39 if (arg0 == NULL)
41 errno = EINVAL;
42 return -1;
45 /* Count the number of arguments (including arg0 and the trailing NULL). */
46 size_t count = 1;
47 va_start (args, arg0);
48 for (;;)
50 count++;
51 if (va_arg (args, const char *) == NULL)
52 break;
54 va_end (args);
56 /* Allocate the argument vector. */
57 const char **argv = (const char **) malloca (count * sizeof (const char *));
58 if (argv == NULL)
60 errno = ENOMEM;
61 return -1;
64 /* Copy the arguments into the argument vector. */
66 size_t i = 0;
67 argv[i++] = arg0;
68 va_start (args, arg0);
69 for (; i < count;)
70 argv[i++] = va_arg (args, const char *);
72 char * const *env = va_arg (args, char * const *);
73 va_end (args);
75 /* Invoke execve. */
76 execve (program, argv, env);
78 /* If execve returned, it must have failed. */
79 int saved_errno = errno;
80 freea (argv);
81 errno = saved_errno;
82 return -1;