#include #include #include #include #include #include #include #define BUF_SIZE 1024 /* Error and die. */ void croak(char *msg, int syserr) { fprintf(stderr, "%s", msg); if(syserr) fprintf(stderr, ": %s", strerror(errno)); fprintf(stderr, "\n"); exit(1); } int main(int argc, char **argv) { int infile, outfile; /* Input and output file descrs. */ if(argc != 3) croak("Need three args.", 0); /* Open the input file to read. */ infile = open(argv[1], O_RDONLY); if(infile < 0) croak("Input file open", 1); /* Open output file, writable, truncate existing content, or create if not. */ outfile = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0777); if(infile < 0) croak("Output file open", 1); /* Copy the file */ while(1) { char buf[BUF_SIZE]; /* Read, return number of bytes read. */ int nread = read(infile, buf, BUF_SIZE); /* Negative return indicates an error. */ if(nread < 0) croak("Read input", 1); /* Zero return indicates EOF. */ if(nread == 0) break; /* Write the amout of data read. */ if(write(outfile, buf, nread) < 0) croak("Write output", 1); } /* All done. */ close(infile); close(outfile); }