Need to pass parameters to system()

This is a discussion on Need to pass parameters to system() within the C Programming forums, part of the General Programming Boards category; Helo all, Please let me first state that I am not a c programmer!! What i need is a program ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    Need to pass parameters to system()

    Helo all,

    Please let me first state that I am not a c programmer!! What i need is a program that will take command line arguments and then run a program passing it those arguments.

    This is what I have so far
    #include <stdio.h>

    main( int argc, char *argv[] )
    {
    if ( argc == 5)
    {
    system("/usr/local/script/send_it.pl argv[1] argv[2] argv[3] argv[4]");
    }
    else if ( argc < 5)
    printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
    return ;
    }


    How do I get it to expand the argv values?

    Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    look into sprintf

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    So why not just invoke the perl script directly, rather than involving a C program which adds almost no value to your task.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    This runs under a vendor created subsytem that will not allow me to run a perl program directly. I have to call it with compiled code.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main( int argc, char *argv[] )
    {
        char String[255];
    
        
        if ( argc == 5)
        {
            sprintf(String, "/usr/local/script/send_pl %s %s %s %s", argv[1], argv[2], argv[3], argv[4]);
            system(String);
        }
        else if ( argc < 5)
        {
            printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
        }
    return ;
    }
    I can't test at the moment, but something like that. I am assuming your command and parameters will never be more then 254 characters.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
        if (argc == 5)
        {
            char cmdLine[4096];
            sprintf(cmdLine, "/usr/local/script/send_it.pl %s %s %s %s",
                               argv[1], argv[2], argv[3], argv[4]);
            return system(cmdLine);
        }
        else
        {
            printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
        }
    
        return EXIT_FAILURE;
    }

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    anonytmouse, I'd change your code into either:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
        if (argc == 5)
        {
            char cmdLine[4096];
            sprintf(cmdLine, "/usr/local/script/send_it.pl %s %s %s %s",
                               argv[1], argv[2], argv[3], argv[4]);
            return system(cmdLine);
        }
        printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
        return EXIT_FAILURE;
    }
    No need for the ELSE clause

    or

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
        int rtn;
        if (argc == 5)
        {
            char cmdLine[4096];
            sprintf(cmdLine, "/usr/local/script/send_it.pl %s %s %s %s",
                               argv[1], argv[2], argv[3], argv[4]);
            rtn = system(cmdLine);
        }
        else
        {
            printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
            rtn = EXIT_FAILURE;
        }
    
        return rtn;
    }
    Use the preferred one-return-per-function format
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    You might prefer to defer argument checking (other than exceeding the max string length) to send_it.pl. This potentially makes for easier maintenance.

    In other words, construct your cmdLine string with ALL the arguments and let send_it.pl decide how to handle them, whether to fail or not and what error message to kick out.
    Last edited by major_blagger; 04-22-2004 at 12:59 PM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    No line length problems at all - if you want to keep the same args
    Code:
    #include <unistd.h>
    int main ( int argc, char *argv[] ) {
      if ( argc == 5 ) {
        argv[0] = "/usr/local/script/send_it.pl";
        execv( argv[0], argv );
      }
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by nonpuz
    why can you assign to argv like that, but normally you would have to use strcpy?
    You can't use strcpy() on argv[0], because you have no idea where argv[0] is pointing at, nor how long that memory is. By changing the pointer to point to a new string, you guarantee not to overlay memory you're not supposed to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by Hammer
    You can't use strcpy() on argv[0], because you have no idea where argv[0] is pointing at, nor how long that memory is. By changing the pointer to point to a new string, you guarantee not to overlay memory you're not supposed to.
    laugh i thought i could delete that comment before anyone saw it

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Thanks for all your help. This worked great

  13. #13
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    if the problem is to take many parametres this is the solution
    you can make a new fuction that allocate new space memory name's free
    Code:
    #include <stdio.h>
    main( int argc, char *argv[] )
    {
    if (arg>=5){
     argc /* take any number */
    sauv=free(*arg[]); /* you have the @ of the first argument */
    for(i=1;i<arg;i++)
    {
    system("/usr/local/script/send_it.pl free (*argv[ ] ) "); /* the system will allocate a memory to him */
    }
    }
    else if ( argc < 5)
    printf("USAGE: sendit <keyname> <local file> <remote file> <speed>\n");
    return ;
    }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I give up. This is the last time I'm commenting on your code.
    Code:
    sauv=free(*arg[]);
    1) There is nothing called sauv in your code.
    2) You cannot free argv, and if you could, that would be wrong.
    2a) I'll have to assume you were trying to free argv, because there is no arg in your program.

    I'm not the boss here. I don't make the rules. However, please, for the sake of all other posters who are trying to learn, compile your code before posting it. Yes, I've typoed and even posted incorrect code before. But I don't do it every single post, all day long. So for the sake of everyone here, compile before you post.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Passing arguments to the system
    By ech0wave in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 11:15 AM
  3. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21