Files
glibc/posix/execle.c
T

74 lines
2.1 KiB
C
Raw Normal View History

2005-01-20 20:10:02 +00:00
/* Copyright (C) 1991,97,98,99,2002,2005 Free Software Foundation, Inc.
1997-02-15 04:31:36 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
1995-02-18 01:27:10 +00:00
#include <unistd.h>
#include <stdarg.h>
#include <stddef.h>
2005-01-20 20:10:02 +00:00
#include <stdlib.h>
1997-02-15 04:31:36 +00:00
#include <string.h>
1995-02-18 01:27:10 +00:00
1999-04-28 23:13:52 +00:00
#include <stackinfo.h>
1995-02-18 01:27:10 +00:00
/* Execute PATH with all arguments after PATH until a NULL pointer,
and the argument after that for environment. */
int
1997-02-15 04:31:36 +00:00
execle (const char *path, const char *arg, ...)
1995-02-18 01:27:10 +00:00
{
2005-01-20 20:10:02 +00:00
#define INITIAL_ARGV_MAX 1024
size_t argv_max = INITIAL_ARGV_MAX;
const char *initial_argv[INITIAL_ARGV_MAX];
const char **argv = initial_argv;
1995-02-18 01:27:10 +00:00
va_list args;
1997-02-15 04:31:36 +00:00
argv[0] = arg;
1995-02-18 01:27:10 +00:00
1999-04-28 23:13:52 +00:00
va_start (args, arg);
2005-01-20 20:10:02 +00:00
unsigned int i = 0;
1998-02-27 10:19:10 +00:00
while (argv[i++] != NULL)
1995-02-18 01:27:10 +00:00
{
1997-02-15 04:31:36 +00:00
if (i == argv_max)
{
2005-01-20 20:10:02 +00:00
argv_max *= 2;
const char **nptr = realloc (argv == initial_argv ? NULL : argv,
argv_max * sizeof (const char *));
if (nptr == NULL)
{
2005-01-20 20:10:02 +00:00
if (argv != initial_argv)
free (argv);
return -1;
}
2005-01-20 20:10:02 +00:00
if (argv == initial_argv)
/* We have to copy the already filled-in data ourselves. */
memcpy (nptr, argv, i * sizeof (const char *));
argv = nptr;
1997-02-15 04:31:36 +00:00
}
argv[i] = va_arg (args, const char *);
}
2005-01-20 20:10:02 +00:00
const char *const *envp = va_arg (args, const char *const *);
1999-04-28 23:13:52 +00:00
va_end (args);
1995-02-18 01:27:10 +00:00
2005-01-20 20:10:02 +00:00
int ret = __execve (path, (char *const *) argv, (char *const *) envp);
if (argv != initial_argv)
free (argv);
return ret;
1995-02-18 01:27:10 +00:00
}
libc_hidden_def (execle)