Ramesh
Ramesh

Reputation:

C Program argv does not contain double quotes

I want to write c program which calls another exe. This wrapper c program does nothing but set some environment variable before I call my original exec. For example, say I have an exe called test.exe and I wrote testwrapper.exe I want to call it as testwrapper.exe < parameter >, and internally it should call test.exe < parameter >

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

What is causing this problem, and what can I do to fix it?

Upvotes: 3

Views: 2524

Answers (6)

ramesh
ramesh

Reputation:

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);
}

Upvotes: 0

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);

Upvotes: 0

chmike
chmike

Reputation: 22134

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

Upvotes: 0

Sucuri
Sucuri

Reputation: 275

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&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html

Upvotes: 3

Paul
Paul

Reputation: 6439

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: \\ )

Upvotes: 2

hbw
hbw

Reputation: 15750

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.

Upvotes: 9

Related Questions