www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - system vs. execvp ?

reply "Peter Sommerfeld" <noreply rubrica.at> writes:
  Hi!

This works as expected:

   string cmd = "dmd src/xyz.d";
   int i = system(cmd);

But this not:

   string[] cmd;
   cmd ~= "src/xyz.d";
   int i = execvp("dmd",cmd);

Of course, dmd is in PATH (Win7).

What is wrong here?

tia Peter
Sep 22 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, September 23, 2012 00:53:48 Peter Sommerfeld wrote:
   Hi!
 
 This works as expected:
 
    string cmd = "dmd src/xyz.d";
    int i = system(cmd);
 
 But this not:
 
    string[] cmd;
    cmd ~= "src/xyz.d";
    int i = execvp("dmd",cmd);
 
 Of course, dmd is in PATH (Win7).
 
 What is wrong here?

Please elaborate on what doesn't work as expected. We can't help you if you don't tell us what's wrong. system should run your command in a new process and return, whereas execvp will run it and never return, because the new process replaces your current one. Now, looking at the docs for std.process.execvp, they seem to think that the exec functions are going to return, but that's not what the man pages for the C functions (which they're calling) say, nor is it how they behave. Maybe that's your problem? http://linux.die.net/man/3/exec http://msdn.microsoft.com/en-us/library/3xw6zy53.aspx - Jonathan M Davis
Sep 22 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/23/12, Peter Sommerfeld <noreply rubrica.at> wrote:
 What is wrong here?

string[] cmd; cmd ~= "dmd"; cmd ~= "src/xyz.d"; int i = execvp("dmd",cmd); 1st arg should always be the app name, even though apps typically ignore/skip the first arg.
Sep 22 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, September 23, 2012 01:12:34 Andrej Mitrovic wrote:
 On 9/23/12, Peter Sommerfeld <noreply rubrica.at> wrote:
 What is wrong here?

string[] cmd; cmd ~= "dmd"; cmd ~= "src/xyz.d"; int i = execvp("dmd",cmd); 1st arg should always be the app name, even though apps typically ignore/skip the first arg.

Are you sure about that? That seems pretty messed up if that's the case. Yes, the first element in the argument list that main gets is the name of the program, but it's pretty messed up if any of the exec* functions require that you give the program name as the first argument rather than it being appropriately added by that program before its main is called. I'd be very surprised if you were correct about this. - Jonathan M Davis
Sep 22 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, September 22, 2012 16:10:11 Jonathan M Davis wrote:
 Now, looking at the docs for std.process.execvp, they seem to think that the
 exec functions are going to return, but that's not what the man pages for
 the C functions (which they're calling) say, nor is it how they behave.

The problem with the documentation has been reported: http://d.puremagic.com/issues/show_bug.cgi?id=8708 - Jonathan M Davis
Sep 22 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/23/12, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 I'd be very surprised if you were correct about this.

I was wrong, it's for a different reason: http://stackoverflow.com/questions/3027320/why-first-arg-to-execve-must-be-path-to-executable
Sep 22 2012
prev sibling parent "Peter Sommerfeld" <noreply rubrica.at> writes:
Jonathan M Davis wrote:
 Peter Sommerfeld wrote:
 This works as expected:

    string cmd = "dmd src/xyz.d";
    int i = system(cmd);

 But this not:

    string[] cmd;
    cmd ~= "src/xyz.d";
    int i = execvp("dmd",cmd);

 Of course, dmd is in PATH (Win7).

 What is wrong here?

Please elaborate on what doesn't work as expected. We can'thelp you if you don't tell us what's wrong.

Well, system(cmd) compiles xyz.d and creates an executable. execvp(cmd) does call dmd but that behaves as if no arguments where given (Usage msg etc). No executables are created.
 system should run your command in a new process and return,whereas   
 execvp will run it and never return, because the
 new process replaces your  current one.

I did not know that it does not return. Anyway, it should compile the args IMHO but it does not. Peter
Sep 22 2012