Reputation: 35
Creating my own shell program for a project. Currently trying to fork()
then perform execv()
by sending the location where ls is executed and then sending arguments for ls which is file path and options.
So far the ls
output is only showing me what's in the directory where my shell program is located.
// directoryPath[] is /home/pi/cs460
// option[] i use NULL or -al for the time being
void lsProcess(char directoryPath[], char option[])
{
string lsLocation = "/bin/ls";
char * lsPlace = new char[lsLocation.size() + 1];
strcpy(lsPlace, lsLocation.c_str());
char * args[] = {lsPlace, NULL, directoryPath};
execv(args[0], args);
exit(0);
}
Upvotes: 0
Views: 256
Reputation: 339
Shouldn't char * args[] = {lsPlace, NULL, directoryPath};
be char * args[] = {lsPlace, directoryPath, NULL};
? When ls
is parsing your argument array, it hits the null at args[1] and stops parsing. Also, you should probably verify that directoryPath
is null terminated...
EDIT
If you want a place holder for options, you need to include the one element array that only contains null to represent an empty string and then add another null at the end
char options[1];
options[0] = 0;
char * args[] = {lsPlace, options, directoryPath, NULL};
Upvotes: 2