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.

09-30-2008

tabstop

Have you read man execl? What did you try, and how did it not work?

09-30-2008

imtiaz3

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("&#37;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 :(

09-30-2008

MK27

The "exec" commands are slightly confusing due to the (inexplicable?) need to repeat the filename. Also, you cannot pass it an integer directly:

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??

09-30-2008

tabstop

Do you need any further information from shooter? Why isn't system() what you need?

09-30-2008

MK27

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.

09-30-2008

imtiaz3

Quote:

Originally Posted by MK27

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

09-30-2008

tabstop

Quote:

Originally Posted by imtiaz3

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).

09-30-2008

dwks

Quote:

The "exec" commands are slightly confusing due to the (inexplicable?) need to repeat the filename.

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."

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 :p 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!!

09-30-2008

tabstop

Quote:

Originally Posted by MK27

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.

09-30-2008

MK27

Quote:

Originally Posted by imtiaz3

thanks!!! you are my GOD!

just another small question :p 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.