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

Undefined variable when using $_SERVER['PHP_SELF']
Hi guyz, please suggest me something...
On first.php I have one input field NAME, and on posting

mysql UNION | warning mysql_fetch_array !!
Code: <?php
$i = 0;
$query1="SELECT * FROM `products` WHERE `div

Cloud Computing?
Hello,

I was wondering if anyone could help explain this term to me. I'm starting to hear it

If a form submits back to same page do you need to use $get
IM trying to pass page number and year that was selected back to the same page.

i have this c

PHP switch function help
Hi, I am trying to create a search function for a directory website. On the search function on html

Effort Estimation Template for Steady State Support
Hi Everyone,

Is there any SAP provided template/format for system change and enhancements

JQuery, Masterpage, and Control issue
Hey everyone, I am using JQuery in several sites so I thought it would be best to place the library

Which PHP-Editor do you think is the best?
Hi everyone,
Myself Brian Anderson. For the last 2 years i have been working a

switch not getting value
what am i doing wrong?
i want to populate the country list according to the category
i want to

Implementing Single Sign-On using SAML 1.1, x.509, LDAP in C#.net
Hi, I got a requirement from the client i.e implementing single sing on using SAML 1.1( LDAP &

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