Passing Arguments to execlp()
Posted on
16th Feb 2014 07:03 pm by
admin
I'm writing a program that mimics a unix shell. It's supposed to take commands with arguments and execute them. I'm having trouble passing in the arguments into the execlp call to correctly execute the program. Here's the code I have:
Code:
// Construct the command argument array
char* args[argsEndIndex]; // Array to hold command arguments
int i = 0; // Loop counter to loop through command
int j = 0; // Loop counter for argument array
while (i <= argsEndIndex)
{
if (i != 0)
{
args[j] = cmdParts[i];
j++;
}
i++;
}
execlp("/bin/ls", "ls", args, NULL);
Did you know?Explore Trending and Topic pages for more stories like this.
cmdParts is a char* array that contains the formatted command inputted. I parsed through the command above this code to insert characters so the pointers point to null terminated strings. That's also how I got argsEndIndex. For example I input this command:
Code:
ls -a -l
argsEndIndex gets correctly set to 2 because that's the index of the last argument in the command. 0 is obviously the command itself. Now when I make the execlp call I get:
Code:
ls: cannot access sH*?vH*?y: No such file or directory
Which I'm assuming it's accessing garbage data somewhere through the args array but I can't figure out why it's doing it because I'm correctly setting each element of the array.
I can post the whole code if you need it but it's long so I tried to keep this short. Any help is appreciated. Thanks!
No comments posted yet
Your Answer:
Login to answer
265
5
Other forums
single page with referrer to show correct page
I have a simple website with a couple of different pages. I would like to be able to in a div tag w
Help on Order Entry Form/System Where is best to begin.
I have a dilema and a very short amount of time at this point and I'm looking for some help on decid
asking for direction
I have 2 tables in my data, which are "post" and "event"
I won't make a summa
Query issues
I probably have this setup wrong well I know I do because nothing is showing up now. What I want is
preg_replace question
Hello!!
I hope someone can help me with preg_replace.
I load two tables from an extern
Why does my php page download images over and over
Can anybody please explain to me why my php page keeps downloading the images and other items every
Converting from one format to another
"I have got a date in DD/MM/YYYY but I need it in MM-DD-YYYY. Help!"
adding 0 to numbers
Hey guys i need to create a 00001 number in a loop. Is there a better way of forcing a 5 digit numbe
Alternate messaging
I have 4 strings in MySQL db1
$string1 : Hello
$string2 : Hi
$string3 : Great
$strin
Material xxx does not exist in plant xxx
Dear All,
I am working for a steel project which is repetitive manufacturing.