Listing 2 Copies files via handles

/* filecopy.c: Low-level file copy */

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFSIZ 512
#define INPUT_MODE  (O_RDONLY | O_BINARY)
#define OUTPUT_MODE (O_WRONLY | O_BINARY | O_CREAT)

int filecopy(char *from, char *to)
{
   int nbytes;
   int status = -1;
   int fd1 = open(from,INPUT_MODE);
   int fd2 = open(to,OUTPUT_MODE,S_IWRITE);
   static char buffer[BUFSIZ];

   if (fd1 >= 0 && fd2 >= 0)
   {
      status = 0;
      while ((nbytes = read(fd1,buffer,BUFSIZ)) > 0)
         if (write(fd2,buffer,nbytes) != nbytes)
         {
            /* Write error */
            status = -1;
            break;       /* Write error */
         }

      /* Was there a read error? */
      if (nbytes == -1)
         status = -1;
   }

   if (fd1 >= 0)
      close(fd1);
   if (fd2 >= 0)
      close(fd2);
   return status;
}

/* End of File */