Thread: _spawnl() - Bug or sadistic "feature"?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    _spawnl() - Bug or sadistic "feature"?

    I just had a very unpleasant experience when trying to use the _spawnl() function...

    The MSDN says the syntax is:
    Code:
    intptr_t _spawnl(
       int mode,
          const char *cmdname,
       const char *arg0,
       const char *arg1,
       ... const char *argn,
       NULL 
    );
    So I ran it as:
    Code:
    _spawnl( _P_NOWAIT, "UserExit.bat", "ChangePassword", "User1", "Password", NULL );
    My UserExit.bat file calls ChangePassword.bat (depending on the parameters passed)... but I noticed that UserExit.bat was never getting called, but somehow ChangePassword.bat was called. For almost a day I thought I was going crazy. Then after looking at some examples that use _spawnl() I noticed that they always pass the same value for the 2nd & 3rd parameters. That's when I realized that the arg0 in the MSDN syntax relates to the argv[0] that gets passed to programs (i.e. the program name), not the first real parameter.
    Obviously when I passed "UserExit.bat" twice everything was working fine, but what I found so strange is that it was calling "ChangePassword.bat" (i.e. using the arg0 parameter as the program to run) instead of "UserExit.bat" which is listed as cmdname. Is this a bug in _spawnl()? Why would it run the arg0 parameter, and if that's what it actually does, then what's the point of even specifying a cmdname parameter?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When I looked it up at MSDN, it said that cmdname was "Path of the file to be executed." I agree that that is an extremely misleading name.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But it is consistent with the exec() series of functions which originate from Unix-land.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Salem View Post
    But it is consistent with the exec() series of functions which originate from Unix-land.
    Yeah, I noticed that. Except I believe the spawn functions do a fork() + exec().
    I've never used spawn or exec before (at least not from scratch), so this problem really surprised the hell out of me.

    So if arg0 is actually the program that _spawnl() will run, then what does it use cmdname for?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The program run is cmd
    The first param is argv0

    An old hack was to do
    exec( "/usr/games/rogue", "/bin/ed", "prog.c", NULL );
    so you could play in peace whilst others thought you were working

    If you use the 'v' variations, it becomes simply
    execv( argv[0], argv );
    if you've set things up properly.

    I suspect when the system call was first created, argv[0] was indeed the first actual parameter, but over time it became convention for argv[0] to be the program name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  5. Some help with switch case bug...
    By hykyit in forum C Programming
    Replies: 5
    Last Post: 05-18-2005, 03:04 AM