Thread: execl and command line arguments

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    execl and command line arguments

    Hi everyone

    Having a bit of a problem

    I'm trying to call a program within another program using execl. I want to be able to pass an integer as a command line argument through execl. I am having trouble doing this. Any tips or advice would be very helpful indeed. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you read man execl? What did you try, and how did it not work?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    thanks for the quick reply

    right now in our calling program, we have a variable called child2, it is an integer

    we then tried this

    Code:
    execl("sigshooter1", (char *) child2);
    the program we are calling is sigshooter1, as seen below

    Code:
    main(char *argv)
    {
    	printf("%d\n", argv);
    }
    now i think there is a problem in both programs, what am i doing wrong???

    and we tried looking at the man pages, but we haven't been able to make sense of how this is supposed to look like

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The "exec" commands are slightly confusing due to the (inexplicable?) need to repeat the filename. Also, you cannot pass it an integer directly:

    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
            short int mode=777;
            char string[4];
            sprintf(string,"&#37;d", mode);
            execl("/bin/chmod","chmod","-v",string,argv[0],NULL);
    }
    The output:

    mode of `./a.out' changed to 0777 (rwxrwxrwx)

    ps. casting an integer into a char pointer won't help you here
    Last edited by MK27; 09-30-2008 at 10:50 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    okay, after further trial an error, i now realize the exec commands end the current running process, which is what i DON'T want

    so basically, I don't have an idea of what I need to do to solve my problem

    I'll re-phrase it and see if it makes more sense

    I have two programs, perf and shooter. perf has an integer variable, and perf needs to call shooter and pass the integer as a command line argument. How should I do this??

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you need any further information from shooter? Why isn't system() what you need?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What's wrong with:

    system("shooter 11694");

    If you need the output from shooter back, use popen(). If you wanted to check the exit status of shooter, fork exec.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Quote Originally Posted by MK27 View Post
    What's wrong with:

    system("shooter 11694");

    If you need the output from shooter back, use popen(). If you wanted to check the exit status of shooter, fork exec.
    ok yea, this makes more sense, so thats kinda working

    only problem now, because I have teh int I want in a variable, which we'll just call x for now, when i try

    system("shooter x");

    it passed just x, not the value it stores

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by imtiaz3 View Post
    ok yea, this makes more sense, so thats kinda working

    only problem now, because I have teh int I want in a variable, which we'll just call x for now, when i try

    system("shooter x");

    it passed just x, not the value it stores
    Well, yes. That's the string you typed in, "shooter x", that's what's going to get passed to the shell. If you need to create a string, you should use sprintf (an example of which Mk gave you earlier, or you could look it up I suppose).

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The "exec" commands are slightly confusing due to the (inexplicable?) need to repeat the filename.
    I'm sure you enjoy having argv[0], don't you?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "it passed just x, not the value it stores"

    An awkward thing about c. You will have to do something like this:

    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
            short int mode=777;
            char cmmd[12];
            sprintf(cmmd,"shooter %d", mode);
            system(cmmd);
    }
    ps (dwks): Here's a great example of opacity in documentation -- the "explanation" according to GNU:
    "By convention, the first element of this array is the file name of the program sans directory names."
    Last edited by MK27; 09-30-2008 at 11:57 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Quote Originally Posted by MK27 View Post
    "it passed just x, not the value it stores"

    An awkward thing about c. You will have to do something like this:

    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
            short int mode=777;
            char cmmd[12];
            sprintf(cmmd,"shooter %d", mode);
            system(cmmd);
    }
    ps (dwks): Here's a great example of opacity in documentation -- the "explanation" according to GNU:
    "By convention, the first element of this array is the file name of the program sans directory names."
    thanks!!! you are my GOD!

    just another small question is there a way I can have cmmd as an arbitray length, since i don't know how long that string will be, it works, but for now i just have it declared as

    char cmmd[50];

    wonder if there is a way with points or something, thanks again!!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    ps (dwks): Here's a great example of opacity in documentation -- the "explanation" according to GNU:
    "By convention, the first element of this array is the file name of the program sans directory names."
    I'm not sure what's wrong with the explanation, given the context in which it appears -- i.e., up above in the prototype you should see that the first argument is the command, the second becomes argv[0], the next argv[1], and so on.

    I mean, it's the same convention (for obvious reasons) as how command-line arguments are handled normally -- argv[0] is the name of the program as it was typed, argv[1] is the first command-line argument, and so on. (This is what allows you to do things like have vi and vim share an executable but do different things based on how it was called.) You can make argv[0] anything you like in your exec call, but you shouldn't expect most programs to check it.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by imtiaz3 View Post
    thanks!!! you are my GOD!

    just another small question is there a way I can have cmmd as an arbitray length, since i don't know how long that string will be
    There's not really an easy way out of that -- you could use three or four char variables, strlen them, malloc and realloc -- but in this case cmmd[50] (if you are sure that number is big enough) might as well do. It's only 50 bytes of memory, anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed