twsmale
twsmale

Reputation: 138

c++ fork exec from command vector

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;
    }
}

Upvotes: 1

Views: 4013

Answers (1)

bmargulies
bmargulies

Reputation: 100013

You will want to call 'execv' so that you can make a char*[] containing the options. "ls" and "-l" each get their own slot in the array.

You'll have to cast away const, or use a char `char const*[]' array and then cast away the const on that to pass it to execv. In general, the declarations for these system calls are mildly unfriendly to C++.

See a stack overflow question on this subject.

There's a reasonable tutorial at http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html.

Roughly speaking:

char * exec_args[1024];
int arg_count = 0;
std::vector<std::string> theArgs;

exec_args[arg_count++] = "/bin/whatever"; // leave command in argv[0]
for (int x = 0; x < theArgs.size(); x++) {
   exec_args[arg_count++] = strdup(theArgs[x].c_str());
}
exec_args[arg_count++] = 0; // tell it when to stop!

execv("/bin/whatever", exec_args);

Upvotes: 1

Related Questions