Debian Codegraph @

src/execlp.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #include <stdarg.h>
24 #include <stddef.h>
25 #ifdef HAVE_ALLOCA_H
26 # include <alloca.h>
27 #endif
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include "libfakechroot.h"
31
32
33 /*
34 execlp function taken from GNU C Library.
35 Copyright (C) 1991,93,96,97,98,99,2002,2005 Free Software Foundation, Inc.
36 */
37 wrapper(execlp, int, (const char * file, const char * arg, ...))
38 {
39 size_t argv_max = 1024;
40 const char **argv = alloca(argv_max * sizeof(const char *));
41 unsigned int i;
42
43 va_list args;
44 va_start(args, arg);
45
46 debug("execlp(\"%s\", \"%s\", ...)", file, arg);
47 argv[0] = arg;
48
49 i = 0;
50 while (argv[i++] != NULL) {
51 if (i == argv_max) {
52 const char **nptr = alloca((argv_max *= 2) * sizeof(const char *));
53
54 if ((char *) argv + i == (char *) nptr)
55 /* Stack grows up. */
56 argv_max += i;
57 else
58 /* We have a hole in the stack. */
59 argv = (const char **) memcpy(nptr, argv, i
60 * sizeof(const char *));
61 }
62
63 argv[i] = va_arg(args, const char *);
64 }
65 va_end(args);
66
67 return execvp(file, (char * const *) argv);
68 }