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

Take info from one coloum and move to another
Hi all,

I have this:
Code: [Select]$array = "SELECT stock_id FROM stocks WHERE stock_

need help with mail()
hi i want to send an email with attaching pdf file using php. i have the following script but the pr

Binding 2 UDP sockets on same port, connected to different destinations, 1 receives
Hello,
My application wants to send/recv data to 2 different UDP ports on a remote computer, us

Comparing MySql data and arrays.
I have a mysql table set up like this:
idsubjectbodyuseriddatetimetags1blog subjectblog body111|2

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 &

Gantt Chart Tool
Does anyone know of a good free gantt chart creator tool

Thanks

Linked Keywords
I am trying to get a script that makes my predefined keyword converted to links and / or converted t

finding key position of specified value in multi-dimensional array
Code: $openedfile = fopen($tfc_file4, "r");
$rc = 1;
while(!feof($openedfile))
{<

Array disappearing in foreach loop
Okay, I have this problem where an array will become undefined in a foreach loop.

Here is the

matching numbers inside ( )
I know I can match numbers by just [0-9]+, so I thought matching numbers inside ( ) would be somethi

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