Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <sys/fcntl.h>
- #include <pthread.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <stdtool.c>
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE PIPE_BUF
- #define PERM (S_IRUSR |S_IWUSR |S_IRGRP|S_IROTH )
- #define O_RWC (O_RDONLY | O_CREAT | O_WRONLY | O_TRUNC )
- #define READ O_RDONLY
- void *fcopy( void *arg )
- {
- int fd_to = *((int*)arg) ;
- int fd_from = *((int*)arg+1) ;
- char buf[SIZE] ;
- int bytes_reades = 0 ;
- int bytes_writen = 0 ;
- *((int*)arg+2) = 0 ;
- for(;;)
- {
- if((bytes_reades = read(fd_from , buf , SIZE) ) <0&&errno == EINTR )
- {
- continue;
- }
- if(bytes_reades == 0 )
- {
- break ;
- }
- if(bytes_reades == -1 )
- {
- perror("unable to read file " ) ;
- *((int*)arg+2) = -1 ;
- pthread_exit(NULL) ;
- }
- if( ((bytes_writen = write(fd_to , buf , SIZE ) )<0) && errno==EINTR )
- {
- continue ;
- }
- if(bytes_writen == -1){
- perror("unable to read file " ) ;
- *((int*)arg+2) = -1 ;
- pthread_exit(NULL) ;
- }
- if(bytes_writen == 0) break;
- *((int*)arg+2) = *((int*)arg+2) + bytes_writen ;
- }
- pthread_exit(NULL) ;
- }
- int file_copy(char *to ,char *from)
- {
- pthread_t thread1 ;
- pthread_attr_t attr1 ;
- int *arg ;
- int fd1_to ;
- int fd2_from ;
- if( ( fd1_to = open(to, O_RWC ,PERM) ) < 0 )
- {
- fprintf(stderr,"%s[at line (%d) ] Error unable to open file [%s]",__FILE__,__LINE__,to) ;
- exit(-1) ;
- }
- if((fd2_from = open(from,READ ))<0)
- {
- fprintf(stderr,"%s[at line (%d) ] Error unable to open file [%s]",__FILE__,__LINE__,from) ;
- close(fd1_to);
- exit(-1) ;
- }
- if( (arg = (int*)malloc( sizeof( int )*(3)) ) == NULL )
- {
- fprintf(stderr,"\n%s[at line (%d) ] Error unable to allocat memory \n ",__FILE__,__LINE__) ;
- close(fd1_to) ;
- close(fd2_from) ;
- exit(-1) ;
- }
- *arg = fd1_to ;
- *(arg + 1 ) = fd2_from ;
- if(pthread_attr_init(&attr1))
- {
- perror("Unable to initiallize thread atrribute object");
- close(fd1_to) ;
- close(fd2_from) ;
- safe_free(arg) ;
- exit(-1) ;
- }
- if(pthread_attr_setdetachstate( &attr1 , PTHREAD_CREATE_JOINABLE ))
- {
- perror("unable to getdetachstate");
- }
- if(pthread_attr_setscope(&attr1,PTHREAD_SCOPE_SYSTEM))
- {
- fprintf(stderr, "Error failed to get system scope for thread") ;
- }
- if(pthread_create(&thread1, &attr1 ,(void * (*)( void *) )fcopy, (void *)arg))
- {
- fprintf(stderr,"%s[at line (%d) ] Error unable to create thread ",__FILE__,__LINE__) ;
- close(fd1_to) ;
- close(fd2_from) ;
- safe_free( arg ) ;
- exit(-1) ;
- }
- if(pthread_join(thread1 , NULL ) )
- {
- perror("Unable to join thread") ;
- close(fd1_to) ;
- close(fd2_from) ;
- safe_free(arg) ;
- exit(-1) ;
- }
- if(*(arg+2) == -1)
- {
- return -1 ;
- }
- printf("\nFile Copied : Bytes copied = [%u]bytes\n",*(arg+2)) ;
- close(fd1_to) ;
- close(fd2_from) ;
- safe_free(arg) ;
- return 0 ;
- }
- int main(int argc ,char **argv )
- {
- if(argc < 2)
- {
- fprintf(stderr,"%s Error : usage file to file ",__FILE__) ;
- exit(-1) ;
- }
- if(file_copy(*(argv+1) , *(argv+2)))
- {
- perror("unable to copy data" ) ;
- exit(-1) ;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment