Thread: spawnvp: bad parameter

  1. #1
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    spawnvp: bad parameter

    Somehow the spawnvp function doesn't like the second parameter (>). Any ideas how to solve this?

    Thanks in advance,
    Monster
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <process.h>
    #include <errno.h>
    
    int main(void)
    {
      char *my_args[5];
      
      my_args[0] = "ping";
      my_args[1] = "125.125.125.125";
      my_args[2] = ">";
      my_args[3] = "c:\\output.txt";
      my_args[4] = NULL;
       
      if(spawnvp(P_WAIT, my_args[0], my_args) == -1)
      {		
        puts(strerror(errno));
      }
    
      getchar();
      return 0;
    }
    Output: Bad parameter >

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    The reason for the problem:

    ping is a binary (executable). spawnvp and execvp call a shell when a shell script is present. ping is not a script.

    You should wrap the command in a small shell script then call that script. Or a better call,
    system("/usr/sbin/ping somenode > somefile.txt").

    Also, it's a very good idea to put the path to any file or script:
    /usr/sbin/ping

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > ping is a binary (executable). spawnvp and execvp call a shell when a shell script is present. ping is not a script.
    Nope - nothing to do with that at all

    exec functions run executables
    If you want to run a script, you run the executable with the name of a script as a parameter

    system() allows you to run scripts directly, where it can figure out which script interpreter to run for a given script.

    > my_args[2] = ">";
    This is the real problem
    I/O redirection is a function of whatever command shell you are running.
    At this low level, it's just another parameter for the ping command (which it complains about)

    > Any ideas how to solve this?
    The short answer is use system()

    The long answer is to get involved in opening and closing streams to make stdout goto a file within the process you exec()
    This is reasonably easy to do in unix/linux, but I've no idea what you need to do to make it work in DOS.
    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. error: storage class specified for parameter
    By aim4sky in forum C Programming
    Replies: 20
    Last Post: 03-16-2009, 02:37 AM
  2. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  3. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM