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

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 Like 5 Dislike
Previous forums Next forums
Other forums

When file included, REMOTE_ADDR is server's?
I have an index file that handles my site, basically index.php?page=ip will activate "require('

Referential Integrity problem when inserting into 2 tables at same time
Hello Everyone,

Im relativity new to PHP and MySQL and i have come up against a problem. i h

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

selection tool on raster image
Hi!
I have to implement in my app a selection tool which lets users to select region of any shape

socket communication between c++/java and sending image
hi,

i have a class in c++ called win32_sockserver which creates socket to java. i am trying

urldecode question
How would I format this line of code properly?

<?php echo urldecode($_GET['Title']); ?

phpmailer class & pop.gmail.com?
Code: <?php
$mail->IsSMTP();
$mail->Host = "pop.gmail.com";

Rss feed question
Hi

My blog and my website are different. Am i able to take my rss feed
from my blog and p

Trying to add functions to table
I have this list of characters and I'm trying to figure out what I have to do to put the results of

SAP Management Console is blank
Dear All ,

I am facing problem in my des sever suddely in SAP Management Console is blank and

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash