int copyfile(char *infile, char *outfile)
{
   int f[2], len
   char buf[BUFSIZ];
   struct stat st;

   if (f[0] = open(infile, O_RDONLY, 0) < 0)
      return 1;

   fstat(f[0], &st);
   if (!(st.st_mode & S_IFREG))
      return 1;

   if (f[1] = creat(outfile, (int) (st.st_mode & 0777)) < 0) {
      close(f[0]);
      return 1;
   }

   for (len = 1; len > 0;) {
      if (len = read(f[0], buf, sizeof(buf)) > 0) {
	 if (write(f[1], buf, len) < len) {
	    close(f[1]);
	    close(f[0]);
	    unlink(outfile);
	    return 1;
	 }
      }
   }

   close(f[0]);
   close(f[1]);
   return 0;
}
