tags:

views:

623

answers:

6

I want to write c program which calls another exe. This wrapper c program does nothing but sets some environment variable before I call my original exec. For example My original exec name is test.exe and I wrote testwrapper.exe I want to call it as testrapper.exe < parameter >, internally it will should call test.exe < parameter >

My program is when I call test.exe as test.exe "c:\program files\input" C escapes " passes as parameter

Any solution to this problem.

+8  A: 

The quotes are supposed to allow for arguments with spaces. For example:

test.exe "this is an argument with spaces"

In order to put quotes in the argument, escape them:

test.exe "\"c:\program files\input\""

If you were calling this from within a C program, you'd have to double-escape the quotes. For example:

system("test.exe \"\\\"c:\\program files\\input\\\"\"");

It would be helpful, though, to see your line of code that runs test.exe.

htw
+2  A: 

Escape your quotes by adding a backslash before them: \"

Also, you might want to use the forward slash as the directory deliminator: / That way, you don't have to escape your backslashes (you still can if you want to though: \\ )

Colin
+2  A: 

Well, if you want to pass the exact arguments, just use execv:

execv(argv[0], argv);

No need to escape anything...

the manual for all them execX functions should help:

http://www.openbsd.org/cgi-bin/man.cgi?query=execvp&amp;apropos=0&amp;sektion=0&amp;manpath=OpenBSD+Current&amp;arch=i386&amp;format=html

Sucuri
A: 

If you are on a unix system, then use execv as suggested by sucuri. On windows use _execv() defined in process.h.

chmike
A: 

int main (int argc, char *argv[])

{

char *install_dir = 0;
char perlbin[MAX_PATH];
char **new_argv;
int index = 0;

setlocale(LC_ALL, "");
install_dir = get_install_dir();

sprintf(perlbin,"%sbin%cperl.bin",install_dir,sep);
set_env_variable(install_dir);

new_argv = malloc(MAX_PATH);
new_argv[0] = malloc(sizeof(char)*(strlen("perl.bin")+1));
strcpy ( new_argv[0],"perl.bin");
for ( index = 1 ; index < argc ; index++)
{
 new_argv[index] = malloc( sizeof(char)*(strlen(argv[index])+1));
 strcpy ( new_argv[index], argv[index]);
}
new_argv[index] = NULL;

_execv( perlbin,new_argv);
exit (0);

}

A: 

You can also add the double quotes yourself.

(warning: quick-hacked, untested)

char ** quoted_argv = malloc((argc - 1) * sizeof(char *));
size_t i;
for(i = 0; i < argc - 1; i++)
{
    quoted_argv[i] = malloc(strlen(argv[i + 1]) + 3); /* allocate space for \0 and quotes */
    sprintf(quoted_argv[i], "\"%s\"", argv[i + 1]);
}

/* ... use quoted_argv ... */

for(i = 0; i < argc - 1; i++)
    free(quoted_argv[i]);
free(quoted_argv);
RaphaelSP