1 #include <stdio.h> 2 3 #include <unistd.h> 4 5 #include <fcntl.h> 6 7 #include <stdlib.h> 8 9 10 11 #define BUFFERSIZE 4096 12 13 #define COPYMODE 0644 14 15 16 17 void oops(char * , char * ) ; 18 19 20 21 main(int ac , char * av[]){ 22 23 int in_fd , out_fd , n_chars ; 24 25 char buf[BUFFERSIZE]; 26 27 28 29 if( ac != 3 ){ 30 31 fprintf(stderr, "usage:%s source destination ", *av ); 32 33 exit(1); 34 35 } 36 37 38 39 if( (in_fd = open(av[1] , O_RDONLY)) == -1 ){ 40 41 oops("Can not open " , av[1] ); 42 43 } 44 45 46 47 if(( out_fd = creat(av[2] , COPYMODE)) == -1 ){ 48 49 oops("Can not open " , av[2] ); 50 51 } 52 53 54 55 while( (n_chars = read(in_fd , buf , BUFFERSIZE ) ) > 0 ){ 56 57 58 59 if( write(out_fd , buf , n_chars) != n_chars ) 60 61 { 62 63 oops("Write error to " , av[2]); 64 65 } 66 67 } 68 69 70 71 if(n_chars == -1) 72 73 { 74 75 oops("Read error from " , av[1]); 76 77 } 78 79 80 81 if( close(in_fd ) == -1 || close( out_fd ) == -1 ){ 82 83 oops("Error closing files." , ""); 84 85 } 86 87 88 89 } 90 91 92 93 void oops(char * s1 , char * s2 ){ 94 95 fprintf(stderr, "Error:%s ", s1 ); 96 97 perror(s2); 98 99 exit(1); 100 101 }