Example #1
0
ErrorCodes Command::run(){
    Shell *shell = Shell::sharedShell();
    childPID = fork();
    status = GOOD;
    if(childPID > 0){
        status = this->_runParentProcess(childPID);
        shell->addToHistory(this);
    }
    else if(childPID == 0){
        // first convert the stuff arg vector to an char ** array then call _runChildProcess
        size_t size = args.size()+2;
        char *argv[size];
        std::strcpy (argv[0], commandName.c_str());
        size_t i;
        for(i = 0; i < args.size(); i ++){
            argv[i+1] = new char[args[i].size()];
            std::strcpy (argv[i+1], args[i].c_str());
        }
        i++;
        argv[i] = (char *)NULL;

        //I want the process to kill itself when done. so call exit on the ret value of the child
        exit(_runChildProcess(argv));
    }
    else{
        //handle fork error
        cout << "Error On Fork" << endl;
    }
    return status;
}