Commit 98b45309 by Aaron W. LaFramboise Committed by Aaron W. LaFramboise

pex-win32.c (argv_to_argc): New function.

2008-08-07  Aaron W. LaFramboise  <aaronavay62@aaronwl.com>

	* pex-win32.c (argv_to_argc): New function.
	(spawn_script): Duplicate argv before calling win32_spawn.

From-SVN: r138849
parent 056dd1af
2008-08-07 Aaron W. LaFramboise <aaronavay62@aaronwl.com>
* pex-win32.c (argv_to_argc): New function.
(spawn_script): Duplicate argv before calling win32_spawn.
2008-07-31 Jakub Jelinek <jakub@redhat.com> 2008-07-31 Jakub Jelinek <jakub@redhat.com>
* mkstemps.c (mkstemps): Keep looping even for EISDIR. * mkstemps.c (mkstemps): Keep looping even for EISDIR.
......
...@@ -321,6 +321,18 @@ msys_rootify (const char *executable) ...@@ -321,6 +321,18 @@ msys_rootify (const char *executable)
} }
#endif #endif
/* Return the number of arguments in an argv array, not including the null
terminating argument. */
static int
argv_to_argc (char *const *argv)
{
char *const *i = argv;
while (*i)
i++;
return i - argv;
}
/* Return a Windows command-line from ARGV. It is the caller's /* Return a Windows command-line from ARGV. It is the caller's
responsibility to free the string returned. */ responsibility to free the string returned. */
...@@ -522,6 +534,9 @@ env_compare (const void *a_ptr, const void *b_ptr) ...@@ -522,6 +534,9 @@ env_compare (const void *a_ptr, const void *b_ptr)
return c1 - c2; return c1 - c2;
} }
/* Execute a Windows executable as a child process. This will fail if the
* target is not actually an executable, such as if it is a shell script. */
static pid_t static pid_t
win32_spawn (const char *executable, win32_spawn (const char *executable,
BOOL search, BOOL search,
...@@ -619,6 +634,9 @@ win32_spawn (const char *executable, ...@@ -619,6 +634,9 @@ win32_spawn (const char *executable,
return (pid_t) -1; return (pid_t) -1;
} }
/* Spawn a script. This simulates the Unix script execution mechanism.
This function is called as a fallback if win32_spawn fails. */
static pid_t static pid_t
spawn_script (const char *executable, char *const *argv, spawn_script (const char *executable, char *const *argv,
char* const *env, char* const *env,
...@@ -630,6 +648,8 @@ spawn_script (const char *executable, char *const *argv, ...@@ -630,6 +648,8 @@ spawn_script (const char *executable, char *const *argv,
int save_errno = errno; int save_errno = errno;
int fd = _open (executable, _O_RDONLY); int fd = _open (executable, _O_RDONLY);
/* Try to open script, check header format, extract interpreter path,
and spawn script using that interpretter. */
if (fd >= 0) if (fd >= 0)
{ {
char buf[MAX_PATH + 5]; char buf[MAX_PATH + 5];
...@@ -642,16 +662,28 @@ spawn_script (const char *executable, char *const *argv, ...@@ -642,16 +662,28 @@ spawn_script (const char *executable, char *const *argv,
eol = strchr (buf, '\n'); eol = strchr (buf, '\n');
if (eol && strncmp (buf, "#!", 2) == 0) if (eol && strncmp (buf, "#!", 2) == 0)
{ {
/* Header format is OK. */
char *executable1; char *executable1;
const char ** avhere = (const char **) --argv; int new_argc;
const char **avhere;
/* Extract interpreter path. */
do do
*eol = '\0'; *eol = '\0';
while (*--eol == '\r' || *eol == ' ' || *eol == '\t'); while (*--eol == '\r' || *eol == ' ' || *eol == '\t');
for (executable1 = buf + 2; *executable1 == ' ' || *executable1 == '\t'; executable1++) for (executable1 = buf + 2; *executable1 == ' ' || *executable1 == '\t'; executable1++)
continue; continue;
backslashify (executable1); backslashify (executable1);
/* Duplicate argv, prepending the interpreter path. */
new_argc = argv_to_argc (argv) + 1;
avhere = XNEWVEC (const char *, new_argc + 1);
*avhere = executable1; *avhere = executable1;
memcpy (avhere + 1, argv, new_argc * sizeof(*argv));
argv = (char *const *)avhere;
/* Spawn the child. */
#ifndef USE_MINGW_MSYS #ifndef USE_MINGW_MSYS
executable = strrchr (executable1, '\\') + 1; executable = strrchr (executable1, '\\') + 1;
if (!executable) if (!executable)
...@@ -686,6 +718,7 @@ spawn_script (const char *executable, char *const *argv, ...@@ -686,6 +718,7 @@ spawn_script (const char *executable, char *const *argv,
} }
} }
#endif #endif
free (avhere);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment