Back to index

glibc  2.9
execle.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,97,98,99,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <unistd.h>
00020 #include <stdarg.h>
00021 #include <stddef.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 
00025 #include <stackinfo.h>
00026 
00027 /* Execute PATH with all arguments after PATH until a NULL pointer,
00028    and the argument after that for environment.  */
00029 int
00030 execle (const char *path, const char *arg, ...)
00031 {
00032 #define INITIAL_ARGV_MAX 1024
00033   size_t argv_max = INITIAL_ARGV_MAX;
00034   const char *initial_argv[INITIAL_ARGV_MAX];
00035   const char **argv = initial_argv;
00036   va_list args;
00037   argv[0] = arg;
00038 
00039   va_start (args, arg);
00040   unsigned int i = 0;
00041   while (argv[i++] != NULL)
00042     {
00043       if (i == argv_max)
00044        {
00045          argv_max *= 2;
00046          const char **nptr = realloc (argv == initial_argv ? NULL : argv,
00047                                    argv_max * sizeof (const char *));
00048          if (nptr == NULL)
00049            {
00050              if (argv != initial_argv)
00051               free (argv);
00052              return -1;
00053            }
00054          if (argv == initial_argv)
00055            /* We have to copy the already filled-in data ourselves.  */
00056            memcpy (nptr, argv, i * sizeof (const char *));
00057 
00058          argv = nptr;
00059        }
00060 
00061       argv[i] = va_arg (args, const char *);
00062     }
00063 
00064   const char *const *envp = va_arg (args, const char *const *);
00065   va_end (args);
00066 
00067   int ret = __execve (path, (char *const *) argv, (char *const *) envp);
00068   if (argv != initial_argv)
00069     free (argv);
00070 
00071   return ret;
00072 }
00073 libc_hidden_def (execle)