#include #include #include #include #include #include #include #include "fcp.h" int nusxe_fayl(char *, char *); int is_reg_file(char *); void set_src_list_elem(struct source_list_elem *, char *, char *); void copy_all(struct source_list *); void new_src_lst(struct source_list *); int is_same_file(char *,char *); void add_src_list_elem(struct source_list *,struct source_list_elem *); int is_folder(char *); void prn_src_lst(struct source_list *); void set_src_lst(struct source_list *, char *, char *); char *get_name(char *); void copy_file(char *,char *); char *get_curr_fold(); int main(int argc, char *argv[]){ /*program icra olunmaqa bu yerden bashlayir*/ struct source_list *src_lst; src_lst=(struct source_list *) malloc(sizeof(struct source_list)); new_src_lst(src_lst); int i,k; struct source_list_elem *src_lst_el; char dest_buf[100]; memset(dest_buf,0,100); if (argc<3){ printf("istifade : gcp kocurulmeli olan fayl ve ya qovluqlarin siyahisi son_qovluq\n"); exit(0); } if (!is_folder(argv[argc-1])){ printf("istifade : gcp kocurulmeli olan fayl ve ya qovluqlarin siyahisi son_qovluq\n"); exit(0); } strcpy(dest_buf,argv[argc-1]); *(dest_buf+strlen(argv[argc-1]))='/'; strcat(dest_buf,argv[1]); src_lst_el=(struct source_list_elem *)malloc(sizeof(struct source_list_elem)); set_src_list_elem(src_lst_el,argv[1],dest_buf); add_src_list_elem(src_lst,src_lst_el); copy_all(src_lst); return 0; } /*=========================================================================*/ /*------------------------------------------------------------------------------------------------*/ int is_reg_file(char *fn){ struct stat *info = (struct stat *)malloc(sizeof(struct stat)); memset(info,0,sizeof(struct stat)); if (stat(fn,info)==-1){ printf("cannot stat file %s\n",fn); exit(0);} if (((info->st_mode)&(S_IFMT))==(S_IFREG)) return 1; return 0; } /*=================================================*/ int is_folder(char *fn){ struct stat *info = (struct stat *)malloc(sizeof(struct stat)); memset(info,0,sizeof(struct stat)); if (stat(fn,info)==-1){ printf("cannot stat %s\n",fn); exit(0);} if (((info->st_mode)&(S_IFMT))==(S_IFDIR)) return 1; return 0; } /*=================================================*/ void set_src_list_elem(struct source_list_elem *src_lst_el, char *src_f, char *dest_f){ int k; memset(src_lst_el,0,sizeof(struct source_list_elem)); k=strlen(src_f); src_lst_el->src_file = (char *)malloc(k+1); memset(src_lst_el->src_file,0,k+1); strcpy(src_lst_el->src_file,src_f); k=strlen(dest_f); src_lst_el->dest_file = (char *)malloc(k+1); memset(src_lst_el->dest_file,0,k+1); strcpy(src_lst_el->dest_file,dest_f); src_lst_el->copied = 0; src_lst_el->next_el=NULL; } /*=================================================*/ void new_src_lst(struct source_list *src_lst){ memset(src_lst,0,sizeof(struct source_list)); src_lst->first_el=src_lst->last_el=NULL; src_lst->count=0; } /*======================================================*/ int is_same_file(char *curr_fold,char *dest_fold){ char *tmp=(char *)malloc(BUF_LEN); memset(tmp,0,BUF_LEN); if((*dest_fold)=='/') return strcmp(curr_fold,dest_fold); strcpy(tmp,curr_fold); *(tmp+sizeof(curr_fold))='/'; strcat(tmp,dest_fold); if((*dest_fold)=='/') return strcmp(curr_fold,dest_fold); } /*======================================================*/ void add_src_list_elem(struct source_list *src_lst, struct source_list_elem *sle){ if (src_lst->first_el==NULL) { src_lst->first_el=src_lst->last_el=sle; sle=NULL; } else { src_lst->last_el->next_el=sle; src_lst->last_el=sle; sle=NULL; } } /*======================================================*/ void prn_src_lst(struct source_list * src_lst){ struct source_list_elem *tmp=src_lst->first_el; while(tmp!=NULL){ printf("source file %s\n",tmp->src_file); printf("dest file %s\n",tmp->dest_file); tmp=tmp->next_el; } } /*======================================================*/ void set_src_lst(struct source_list *src_lp, char *src_fold, char *dest_fold){ DIR *dir_ptr; struct source_list_elem *src_lst_el; char *src_buf,*dest_buf; src_buf = (char *)malloc(100); dest_buf = (char *)malloc(100); memset(src_buf,0,100); memset(dest_buf,0,100); struct dirent *direntp; if ((dir_ptr=opendir(src_fold))==NULL){ printf("cannot open %s exiting\n",src_fold); exit(0); } while((direntp=readdir(dir_ptr))!=NULL){ memset(dest_buf,0,100); strcpy(dest_buf,dest_fold); *(dest_buf+strlen(dest_buf))='/'; strcat(dest_buf,direntp->d_name); memset(src_buf,0,100); strcpy(src_buf,src_fold); *(src_buf+strlen(src_buf))='/'; strcat(src_buf,direntp->d_name); src_lst_el=(struct source_list_elem *) malloc(sizeof(struct source_list_elem)); set_src_list_elem(src_lst_el,src_buf,dest_buf); add_src_list_elem(src_lp,src_lst_el);} closedir(dir_ptr); } /*======================================================*/ char *get_name(char *fn){ int k=strlen(fn); char *p=fn+k; while((*(p)!='/')&&(k--)) p--; if (*p=='/') return p+1; else return p; } /*======================================================*/ void copy_file(char *src_file,char *dest_file){ int i,k; int fd1,fd2; char buf[BUF_LEN]; fd1=open(src_file,O_RDONLY); if(fd1==-1){ printf("copy_fayl: %s fayli achilmir\n",src_file); exit(0); } /* BUG yeni faylin modu oroginal deyil 644 goturulur!*/ if ((fd2=open(dest_file, O_WRONLY | O_CREAT | O_TRUNC, 0644))==-1) printf("nuxse_fayl: %s faylini yaratmaq mumkun olmadi\n",dest_file); /*hisse - hisse (1024 bayt) köçürürük*/ while((i=read(fd1,buf,BUF_LEN)) >0) write(fd2,buf,i); close(fd1); close(fd2); } /*======================================================*/ void copy_all(struct source_list *src_lst){ struct source_list *src_lp; struct source_list_elem *tmp=src_lst->first_el; while (tmp!=NULL) { if (is_reg_file(tmp->src_file)) { copy_file(tmp->src_file,tmp->dest_file); } if (is_folder(tmp->src_file)&&((strcmp(get_name(tmp->src_file),"..")!=0)&&(strcmp(get_name(tmp->src_file),".")!=0))) { if (mkdir(tmp->dest_file, 0755)==-1) { printf("copy_all: %s qovlugu yaradilmir\n",tmp->dest_file); exit(0); } src_lp=(struct source_list *) malloc(sizeof(struct source_list)); new_src_lst(src_lp); set_src_lst(src_lp,tmp->src_file,tmp->dest_file); copy_all(src_lp); free(src_lp); } tmp=tmp->next_el; } } /*===================================================*/