Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving issues with execvp()
    primarykey
    data
    text
    <p>So here is the bit of my code that's giving me problems:</p> <pre><code>void childProcessHandler(string command){ int argCounter = 0; for(int i=0; i!=command.size(); i++) argCounter+=( command.at(i) == ' '); char * temp, *token; char *childArgs[argCounter]; argCounter = 1; temp = new char [command.size()+1]; strcpy (temp, command.c_str()); token = strtok (temp," "); childArgs[0] = token; while (token!=NULL) { token = strtok(NULL," "); childArgs[argCounter] = token; argCounter++; } //delete[] temp; //Should remove token as well? execvp(childArgs[0], childArgs); cout&lt;&lt;"PROBLEM!"&lt;&lt;endl; exit(-1); } </code></pre> <p>In the main() method my code gets to a point where it forks() (the parent process then waits for the child to exit.) then the child process (process ID == 0 yes?) calls the method childProcessHandler with the user input (command to run + args) as it's argument. Then I tokenize the user input and call execvp on it.</p> <p>Everything compiles and executes. The line after execvp is never reached because execvp only returns when there is an error yes?</p> <p>The project is to simulate a unix terminal however when I give it the command "date" nothing gets printed like it should... The child exits and the parent process resumes just fine however nothing is sent back up to the terminal window...</p> <p>What am I doing wrong?</p> <p>(Also we were "recommended" to use strtok to tokenize it but if anyone has anything simpler i'm open to opinions.)</p> <p>THANKS! </p> <h1><strong>EDIT</strong></h1> <p>The above code works, for example, if I type in "date " instead of "date". I think there might be something fishy with the "tokenizer" not putting a null character at the end of the childArgs[] array. I'll play around with that and thanks for the quick responses!</p> <p>(Ninja edit, also commented out delete[] temp for the time being)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload