I'm real lost here. Building a Linux shell, working on processing external commands. I'm trying to create a new process then execute the commands. Very new to exec(), fork(), pipe() and dup(), but I'm sure I need them somewhere.
Basically my question is: what's the best way to pass arguments into exec? I know there's many options, but if I have a string vector that contains "ls -l" how do I pass that into execute? Should I re-parse it into "ls" and "-l"?
I already have fork() creating a child process, but I don't know how to run exec().
On a somewhat related note, what should I put in the waitpid part of fork()
pid_t pid;
int status;
pid = fork();
if (pid < 0)
{
cout << "Fork failed." << endl;
}
else if (pid == 0)
{
execv("/bin/sh", (VECTOR OF COMMANDS?));
_exit (EXIT_FAILURE);
}
else
{
if (waitpid (pid, &status, 0) == pid)
{
cout << "huh?" << endl;
}
else
{
cout << "Error." << endl;
}
}
The next hurdle is piping, but I'll cross that bridge when I get there.
EDIT:
For what it's worth, here's the parsing and calling that I'm having trouble with. The lines with "**" after seem to be the ones giving me problems
const char *args [1024];
string::iterator it5;
size_t pos5;
for (it5=origCstr.begin(); it5 < origCstr.end(); it5++)
{
string::iterator it2;
pos5 = origCstr.find(' ');
if (pos5 == string::npos)
{
tmpChar = origCstr.c_str();
args[argCount] = tmpChar;
argCount++;
break;
}
it2 = it5 + pos5;
tmpCstr = origCstr.substr(0, pos5);
tmpChar = tmpCstr.c_str();
args[argCount] = tmpChar;
origCstr.erase(it5, it2+1);
argCount++;
}
tmpChar = origCstr.c_str();
args[argCount] = tmpChar;
argCount++;
pid_t pid;
int status;
pid = fork();
if (pid < 0)
{
cout << "Fork failed." << endl;
}
else if (pid == 0)
{
execv("/bin/", args); ****
_exit (EXIT_FAILURE);
}
else
{
if (waitpid (pid, &status, 0) == pid)
{
cout << "huh?" << endl;
}
else
{
cout << "Error." << endl;
}
}