From edc20a7505f61002117042ffeb19f328fe519277 Mon Sep 17 00:00:00 2001 From: axel Date: Tue, 12 Dec 2006 05:03:08 +1000 Subject: [PATCH] Update to the ':' patch by Nicholas Burlett. This patch makes sure that ':' style executing of a command is only performed if a regular call to execve fails. This should improve performance a mall bit in the common case. darcs-hash:20061211190308-ac50b-d284fa30c8c633816898141b644bd3039f80cc62.gz --- exec.c | 55 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/exec.c b/exec.c index da0c5b75a..e4511394f 100644 --- a/exec.c +++ b/exec.c @@ -446,42 +446,57 @@ static int setup_child_process( job_t *j, process_t *p ) */ static void launch_process( process_t *p ) { + FILE* f; + // debug( 1, L"exec '%ls'", p->argv[0] ); - /* check for a ":\n", and run system() if so */ - - FILE* f = wfopen(p->actual_cmd, "r"); - if (f != NULL) + execve ( wcs2str(p->actual_cmd), + wcsv2strv( (const wchar_t **) p->argv), + env_export_arr( 0 ) ); + + /* + Something went wrong with execve, check for a ":", and run + /bin/sh if encountered. This is a weird predecessor to the shebang + that is still sometimes used since it is supported on Windows. + */ + f = wfopen(p->actual_cmd, "r"); + if( f ) { char begin[1] = {0}; - fread(begin, 1, 1, f); - if (begin[0] == ':') + size_t read; + + read = fread(begin, 1, 1, f); + fclose( f ); + + if( (read==1) && (begin[0] == ':') ) { int count = 0; int i = 1; int j = 2; + wchar_t **res; + while( p->argv[count] != 0 ) - count++; - wchar_t **res = malloc( sizeof(wchar_t*)*(count+2)); - res[0] = L"/bin/sh"; + count++; + + res = malloc( sizeof(wchar_t*)*(count+2)); + + res[0] = L"/bin/sh"; res[1] = p->actual_cmd; - while( p->argv[i] != 0 ) - { - res[j] = p->argv[i]; - i++; - j++; + + for( i=1; p->argv[i]; i++ ){ + res[i+1] = p->argv[i]; } - res[j] = NULL; - free(p->argv); + + res[i+1] = 0; p->argv = res; p->actual_cmd = L"/bin/sh"; + + execve ( wcs2str(p->actual_cmd), + wcsv2strv( (const wchar_t **) p->argv), + env_export_arr( 0 ) ); } - } - execve ( wcs2str(p->actual_cmd), - wcsv2strv( (const wchar_t **) p->argv), - env_export_arr( 0 ) ); debug( 0, _( L"Failed to execute process '%ls'" ), p->actual_cmd );