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

Two fgetcsv related questions ;D
First of im trying to get a file from a merchants server but the directory is protected, I know the

Auto-populating dropdowns and multiple forms.
Here's what I have so far:

First drop down = select a state (works)
This populates the sec

Not Inserting into DB
I can't get my Add new Division form to submit to the database. Anyone see my issue?

Code: &a

search query in mysql in php problem
my search query wont work, i know, that my codes are correct.please help

Code: [Select]&l

Is there a more efficient way to code this than what I have?
I have three associative arrays.

$combinedSettings
$userSettings
$defaultSettings

mysql_query returning boolean instead of mysql ressource
Hi,

I seem to have a weird issue with the php command mysql_query. I pass 2 arguments the que

add text and number image
hi guys ..

ok see i want the user to add their name and pick a number then click get it and i

rookie looking for help coding a CSS form with PHP
I'm trying to figure out how to add PHP code to my xhtml form so that it is a working form embedded

Socket programming
Hello all,

I have a PHP socket script. GPS trackers connect to this socket. IMEI verification

extract a file from zip file
hi,
i know how to unzip a zip file in php, but is there a way to just extract a certain file only

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