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

my login script page is not working on remote computers
hi my login is working on my computer, but when i tried 2 computers from 2 different locations, they

Create multiple (n) arrays
Hi there,

I have the following need:

I have 2 arrays (coming from a databases)

Edit MySQL Row Using PHP and HTML Form
Hello,

Here's what I'm trying to do. Build a page where a user enters a MySQL row number in.

Slashes
Have a small problem and I'm not able to understand why I'm getting the results I'm getting... and i

Running External Scripts
I am very new to PHP and am not even sure what I'm asking is possible. I have just installed mediaW

form variables from database help.
Hi all,

I would like to have a form that gives you options based on the results of an mysql q

CODE NOT WORKING
Code: [Select]<?php
//include shared codes

include '../lib/common.php';
include

Help please - How to validate from 2 possible answers
Hi

I hope somebody can help me with what will probably be really simple, I'm pulling my hair

Error: SQL Syntax; Line 1
Code:

<?php require "global_settings.php"; ?>
<title&g

ECC6 - Single sign-on
We are in the process of upgrading to ECC 6 which will support single sign on with user passwords al

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