Back to index

glibc  2.9
execl.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,94,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 
00028 /* Execute PATH with all arguments after PATH until
00029    a NULL pointer and environment from `environ'.  */
00030 int
00031 execl (const char *path, const char *arg, ...)
00032 {
00033 #define INITIAL_ARGV_MAX 1024
00034   size_t argv_max = INITIAL_ARGV_MAX;
00035   const char *initial_argv[INITIAL_ARGV_MAX];
00036   const char **argv = initial_argv;
00037   va_list args;
00038 
00039   argv[0] = arg;
00040 
00041   va_start (args, arg);
00042   unsigned int i = 0;
00043   while (argv[i++] != NULL)
00044     {
00045       if (i == argv_max)
00046        {
00047          argv_max *= 2;
00048          const char **nptr = realloc (argv == initial_argv ? NULL : argv,
00049                                    argv_max * sizeof (const char *));
00050          if (nptr == NULL)
00051            {
00052              if (argv != initial_argv)
00053               free (argv);
00054              return -1;
00055            }
00056          if (argv == initial_argv)
00057            /* We have to copy the already filled-in data ourselves.  */
00058            memcpy (nptr, argv, i * sizeof (const char *));
00059 
00060          argv = nptr;
00061        }
00062 
00063       argv[i] = va_arg (args, const char *);
00064     }
00065   va_end (args);
00066 
00067   int ret = __execve (path, (char *const *) argv, __environ);
00068   if (argv != initial_argv)
00069     free (argv);
00070 
00071   return ret;
00072 }
00073 libc_hidden_def (execl)