1 /* execl() function: Execute a program, replacing the current process.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
18
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)
22
23 #include <config.h>
24
25 /* Specification. */
26 #include <unistd.h>
27
28 #include <errno.h>
29 #include <stdarg.h>
30
31 #include "malloca.h"
32
33 int
34 execl (const char *program, const char *arg0, ...)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
35 {
36 va_list args;
37
38 /* The callee is not expecting a NULL argv[0]. */
39 if (arg0 == NULL)
40 {
41 errno = EINVAL;
42 return -1;
43 }
44
45 /* Count the number of arguments (including arg0 and the trailing NULL). */
46 size_t count = 1;
47 va_start (args, arg0);
48 for (;;)
49 {
50 count++;
51 if (va_arg (args, const char *) == NULL)
52 break;
53 }
54 va_end (args);
55
56 /* Allocate the argument vector. */
57 const char **argv = (const char **) malloca (count * sizeof (const char *));
58 if (argv == NULL)
59 {
60 errno = ENOMEM;
61 return -1;
62 }
63
64 /* Copy the arguments into the argument vector. */
65 {
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 *);
71 va_end (args);
72 }
73
74 /* Invoke execv. */
75 execv (program, argv);
76
77 /* If execv returned, it must have failed. */
78 int saved_errno = errno;
79 freea (argv);
80 errno = saved_errno;
81 return -1;
82 }