title: Creating a cat like program
body: <p>I am trying a write a program that implements a bare bones POSIX <code>cat</code> command. Although I have written the code and it behaves normally for the default (without paramaters) <code>STDIN</code>, It doesn't work well for others. Can anyone help me with this. Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #define BUFFSIZE 10240
  #define MAX_FILES 127
  
  int main(int argc, char *argv[])
  {
      int n,t,i;
      char buf[BUFFSIZE];
      char *myargv[MAX_FILES];
      //aiparser(argv,myargv);
      if (argc == 1) {
          while(( n = read(STDIN_FILENO,buf,BUFFSIZE)) &gt; 0) {
              if (write(STDOUT_FILENO, buf ,n ) != n) {
                  perror("Write Error");
              }
          }
          if ( n &lt; 0 ) {
              perror("Read Error");
          }
      }
      else {
          for ( i = 1; i &lt; argc-1 ; i++) {
              if (strcmp(argv[i],"-") != 0 ) {
                  t = open(argv[i],O_RDONLY);
              }
              else {
                  t = STDIN_FILENO;
              }
              while(( n = read(t,buf,BUFFSIZE)) &gt; 0) {
                  if (write(STDOUT_FILENO, buf ,n ) != n) {
                      perror("Write Error");
                  }
              }
              if ( n &lt; 0 ) {
                  perror("Read Error");
              }
              if (close(t) == -1) {
                  perror("Closing Error");
              }
          }
          }   
      exit(0);
  }
  </code></pre>