title: In C on linux how would you implement cp
body: <p>I've found on google code that was over 50 lines long and that's completely unnecessary for what I'm trying to do. </p>
  
  <p>I want to make a very simple <code>cp</code> implementation in C. </p>
  
  <p>Just so I can play with the buffer sizes and see how it affects performance.</p>
  
  <p>I want to use only Linux API calls like <code>read()</code> and <code>write()</code> but I'm having no luck. </p>
  
  <p>I want a buffer that is defined as a certain size so data from file1 can be read into buffer and then written to file2 and that continues until file1 has reached EOF.</p>
  
  <p>Here is what I tried but it doesn't do anything</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  
  #define BUFSIZE 1024
  
  int main(int argc, char* argv[]){
  
      FILE fp1, fp2;
      char buf[1024];
      int pos;
  
  
      fp1 = open(argv[1], "r");
      fp2 = open(argv[2], "w");
  
      while((pos=read(fp1, &amp;buf, 1024)) != 0)
      {
          write(fp2, &amp;buf, 1024);
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>The way it would work is <code>./mycopy file1.txt file2.txt</code></p>