#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#define BUFFER_SIZE 4096
char input_buffer[BUFFER_SIZE];
char output_buffer[BUFFER_SIZE];
int main(int argc, char ** argv)
{
int input_fd=0;
if(argc>1)
{
input_fd=open(argv[1],O_RDONLY);
if(input_fd==-1)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
}
int output_fd=1;
if(argc>2)
{
output_fd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
if(output_fd==-1)
{
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
int read_bytes=0;
int written_bytes=0;
while((read_bytes=read(input_fd,input_buffer,BUFFER_SIZE))>0)
{
int i;
for(i=0;i<read_bytes;i++)
{
output_buffer[written_bytes++]=input_buffer[i];
if(written_bytes==BUFFER_SIZE)
{
write(output_fd,output_buffer,written_bytes);
written_bytes=0;
}
output_buffer[written_bytes++]='\0';
if(written_bytes==BUFFER_SIZE)
{
write(output_fd,output_buffer,written_bytes);
written_bytes=0;
}
}
}
write(output_fd,output_buffer,written_bytes);
exit(EXIT_SUCCESS);
}