#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#define BUFFER_SIZE 1024
int main (int argc,char** argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
Char Buffer[buffer_size];
char* ptr;
if (argc!= 3)
{
fprintf (stderr, "Usage:%s fromfile tofile\n\a", argv[0]);
Exit (1);
}
/* Open source file * *
if ((from_fd = open (argv[1],o_rdonly)) = = 1)
{
fprintf (stderr, "Open%s Error:%s\n", Argv[1],strerror (errno));
Exit (1);
}
/* Create destination file * *
if (to_fd = open (argv[2],o_wronly| o_creat,s_irusr| S_IWUSR)) = =-1)
{
fprintf (stderr, "Open%s Error:%s\n", Argv[2],strerror (errno));
Exit (1);
}
/* The following code is a classic copy of the file code * * *
while (Bytes_read = Read (from_fd,buffer,buffer_size))
{
* * A fatal error has occurred.
if ((Bytes_read = = 1) && (errno!= eintr)) break;
else if (Bytes_read > 0)
{
ptr = buffer;
while (Bytes_write = Write (To_fd,ptr,bytes_read))
{
* * A fatal error has occurred.
if ((Bytes_write = = 1) && (errno!= eintr)) break;
/* All read bytes have been written/*
else if (Bytes_write = = bytes_read) break;
/* Only write a part, continue to write * *
else if (Bytes_write > 0)
{
PTR + = Bytes_write;
Bytes_read-= Bytes_write;
}
}
/* A fatal error occurred when writing * *
if (Bytes_write = = 1) break;
}
}
Close (FROM_FD);
Close (TO_FD);
Exit (0);
}