title: Having issues trying to redirect file to stdin using dup2() and open() and execv()
body: <p>Edit: The issue is that echo does not read from stdin apparently. Thanks @Jonathan Leffler</p>
  <p>I have a programming exercise, and my code is below:</p>
  <pre><code>int processProgram(char **tokens, int requireWait) {
      char *cmd = tokens[0];
      if (access(cmd, R_OK) == -1) {
          printf(&quot;%s not found\n&quot;, cmd);
          return -1;
      }
  
      int index = 1;
      char *iterator = tokens[index];
      int inputTokenIndex = 0;
      // Look for a &lt; to figure out where the redirection is
      while (iterator != NULL) {
          if (strcmp(iterator, &quot;&lt;&quot;) == 0) {                      // &lt;---- THIS IS WHERE I ATTEMPT TO FIND THE INDEX OF &quot;&lt;&quot;
              if (access(tokens[index + 1], R_OK) == -1) {
                  printf(&quot;%s does not exist\n&quot;, tokens[index + 1]);
                  return -1;
              }
              // set null at &lt;
              tokens[index] = NULL;
              inputTokenIndex = index + 1;
              break;
          }
          index++;
          iterator = tokens[index];
      }
      // basically create a struct to store pids and stuff....
      processList *process = addNewProcess();
      int childPid = fork();
      if (childPid == 0) {
          // is the child
          if (inputTokenIndex &gt; 0) {
              // if previously strcmp &quot;&lt;&quot; is true
              // set redirection for input....
              int fd = open(tokens[inputTokenIndex], O_RDONLY);   // &lt;----- THIS IS WHERE I DO THE REDIRECTING FOR STDIN!
              dup2(fd, STDIN_FILENO);
              close(fd);
          }
          // start from where inputTokenIndex could have left off
          // since &gt; is always at least after &lt; in our test cases
          index = inputTokenIndex + 1;
          iterator = tokens[index];
          while (iterator != NULL) {                            // &lt;--- LOOKING FOR &quot;&gt;&quot; and &quot;2&gt;&quot;, CURRENTLY THIS WORKS FINE
              // look for output redirection FROM previous while loop onwards
              int out = 0;
              if (strcmp(iterator, &quot;&gt;&quot;) == 0) {
                  // stdout
                  out = STDOUT_FILENO;
              } else if (strcmp(iterator, &quot;2&gt;&quot;) == 0) {
                  // stderr
                  out = STDERR_FILENO;
              }
              if (out != 0) {
                  int fd = open(tokens[index + 1], O_WRONLY | O_TRUNC | O_CREAT, 0777);
                  dup2(fd, out);
                  tokens[index] = NULL;
                  close(fd);
              }
              index++;
              iterator = tokens[index];
          }
          
          int value = execv(cmd, tokens);                  // &lt;----- EXEC IS HERE!!
          if (value == -1) {
              exit(errno);
          }
          exit(0);
      } else {
          int status;
          if (requireWait == 1) {
              // wait for process to finish
              process-&gt;pid = childPid;
              wait(&amp;status);
              process-&gt;running = 0;
              process-&gt;status = status;
              if (status != 0) {
                  printf(&quot;%s failed&quot;, cmd);
              }
          } else {
              process-&gt;pid = childPid;
              printf(&quot;Child[%d] in background\n&quot;, childPid);
              // no return should be expected
              status = 0;
          }
          return status;
      }
  }
  </code></pre>
  <p>The idea is to simulate linux redirection using &lt; and &gt;</p>
  <p>tokens is the array of strings provided into the shell.</p>
  <p>Example call to write hello to &quot;a.txt&quot;: /bin/echo hello &gt; ./a.txt</p>
  <p><strong>The problem</strong></p>
  <p>It does not work for redirecting in, i.e &quot;/bin/echo &lt; ./a.txt&quot; would echo as if there was no arguments passed in, which is an empty line. &quot;/bin/sleep &lt; ./a.txt&quot; would say no arguments passed in when the file is not empty.</p>
  <p>The file a.txt is created using this program. So full example would be</p>
  <p>/bin/echo hellooooo &gt; ./a.txt</p>
  <p>/bin/echo &lt; ./a.txt</p>
  <p>in which the first command successfully creates and writes a.txt, but the second command produces only an empty line</p>
  <p><strong>How can you help</strong></p>
  <p>Assume that tokens is correct, and that the filename exists, why doesn't the execv() call take in input from the filename given that I redirected it????</p>
  <p>Thank you for reading this!!</p>