title: "tee" like programming api in UNIX
body: <p>I want to have something like this </p>
  
  <pre><code>$&gt; ps -ax | tee -a processes.txt
  </code></pre>
  
  <p>In a UNIX C programming environment, meaning not via shell scripts.</p>
  
  <p>Basically is there a API so that I can tee on STDIN and/or STDOUT so that I can recording automatically anything appears on the CLI down to a file. Imagine there's a foreground process interact with user and response with some output to the terminal. I want to have everything shown in the terminal also in a file for later examination.</p>
  
  <p>I would imagine something as magic as this:</p>
  
  <pre><code>tee(STDIN, "append", logFile);
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Follow-up, this is the program I wrote according to Lars (see Answer section below), but not exactly what I desired:</p>
  
  <pre><code>int main(int argc, char** argv){
  
          int pfd[2];
          if (pipe(pfd) == -1) { perror("pipe"); }
  
  
          if (fork()==0) { // child reads from pipe
                  close(pfd[1]);
  
                  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
                  int ufd = open("user_study.log", O_CREAT | O_APPEND | O_WRONLY, mode);
                  if (ufd == -1) {
                          perror("Cannot open output file"); exit(1);
                  } // ++
  
                  char buf;
                  while (read(pfd[0], &amp;buf, 1) &gt; 0) {
                          write(STDOUT_FILENO, &amp;buf, 1); // write to stdout
                          write(ufd, &amp;buf,1);            //   and to log
                  }
  
                  close(pfd[0]);
  
          } else { // parent write to pipe
                  // dup STDOUT to the pipe
                  close(pfd[0]);
                  dup2(pfd[1], STDOUT_FILENO);
  
                  char msg[200];
                  msg[0] = "";
                  do {
                          scanf("%s", msg);
                          printf("program response..");
  
                  } while (strcmp(msg, "exit")!=0);
  
                  close(pfd[1]);
  
          }
  
          return 1;
  }
  </code></pre>
  
  <p>Actual running:</p>
  
  <pre><code>[feih@machine ~/mytest]$ ./a.out
  abc
  haha
  exit 
  program response..   &lt;---- the output is delayed by the chld process, not desired
  program response..
  program response..
  
  [feih@machine ~/mytest]$ less user_study.log
  program response..   &lt;---- the log doesn't contain input
  program response..
  program response..
  </code></pre>
  
  <p>Desired running and log(simulated):</p>
  
  <pre><code>[feih@machine ~/mytest]$ ./a.out
  abc
  program response..
  haha
  program response..
  exit 
  program response..
  
  
  [feih@machine ~/mytest]$ less user_study.log 
  abc
  program response..      &lt;--- the log should be the same as the running
  haha
  program response..
  exit 
  program response..
  </code></pre>
  
  <p>So up till now, this problem hasn't been fully resolved.</p>