title: Including (linux) bash commands
body: <p>I'm currently working on creating a simple Linux shell. I have a Shell class, with a vector I am treating as a sort of queue. The user could input something like</p>
  
  <pre><code>$                ls        -l   /
  </code></pre>
  
  <p>and the program would put this into my vector through a private method, with "ls" being at the 0th position, "/" in the 2nd position, you get the idea.  Then I go into my "interpretation" stage:</p>
  
  <ul>
  <li>First I check to make sure the user has put something into the prompt.</li>
  <li>Then it checks to see if the user typed in the word "exit", and if so it exits.</li>
  <li>If neither of these checks go off, the program forks. It then checks to make sure the fork didn't fail.</li>
  <li><p><strong>This is where I'm stumbling.</strong> If the second to last character string in the queue is ">" we know that the last string is going to be a file that we need to create and/or open (and/or truncate?) and write the results of the command given by the user to said file.</p>
  
  <p>I need to do this using the bash command open and using flags, I cannot use c++ openers like ofstream. I then need to execute the input using the bash command exec.</p></li>
  </ul>
  
  <p>Below is how my code has this written as of the submission of this post.</p>
  
  <pre><code>if (commandQueue.size() &gt;= 3 &amp;&amp; commandQueue.at(commandQueue.size() - 2) == "&gt;") {
              //commandQueue.at(commandQueue.size() - 1) is fileName          open
  
  
              //commandQueue.at(0) is name of program to run                  exec
              //remaining substrings are args
  };          //commandQueue.at(0) is name of program to run                  exec
  // remaining substrings are args
  </code></pre>
  
  <p>How should I format these commands? fork was easy, it was just</p>
  
  <pre><code>pid_t pid = fork();
  </code></pre>
  
  <p>and a check. But I'm unsure as to how I'm supposed to open a file using flags, or how to format the exec command. I appreciate any help I receive, thank you.</p>
  
  <p>Edit: I should probably put the second part of the if statement I provided in an else statement, or else return from the if statement.</p>
  
  <p>Edit 2: I should probably mention I'm pretty new to bash commands, I've used a few of them before, but this is the first program I've written using them inside.</p>
  
  <p>Edit 3 (reply to Galik): </p>
  
  <p>Like, if you were using execve you'd have something like </p>
  
  <pre><code>    const char *path = "/bin/ls";
      char *const argv[] = { "/bin/ls", "/", NULL };
      execve(path, argv, environ);
  </code></pre>
  
  <p>(this is c code though, not c++) you see the "execve(path, argv, environ)"? That's what I'm curious about.</p>
  
  <p>The man page for open has</p>
  
  <pre><code>   int open(const char *pathname, int flags);
     int open(const char *pathname, int flags, mode_t mode);
  </code></pre>
  
  <p>but since I'm not using c, and I have my user's input stored as strings, I'm not sure how to deal with the const char*, or the flags or mode.</p>