Here's a copy program that I wrote at one point that I think worked:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fdin, fdout;
struct stat st;
char buf[BUFSIZ];
int n_read;
if(argc != 3)
{
puts("Usage: mycp <source> <destination>");
exit(1);
}
if((fdin = open(argv[1], O_RDONLY)) == -1)
{
printf("open(): %s\n", strerror(errno));
exit(1);
}
if(fstat(fdin, &st) == -1)
{
printf("stat(): %s\n", strerror(errno));
exit(1);
}
umask(0);
if((fdout = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, st.st_mode)) == -1)
{
printf("open(): %s\n", strerror(errno));
exit(1);
}
while((n_read = read(fdin, buf, sizeof(buf))) > 0)
{
if(write(fdout, buf, n_read) == -1)
{
printf("write(): %s\n", strerror(errno));
exit(1);
}
}
close(fdin);
close(fdout);
if(n_read == -1)
{
printf("read(): %s\n", strerror(errno));
exit(1);
}
return 0;
}