Beispiel #1
0
void copy_dir_to_dir(char* from, char* to){
	DIR *dir;
	if((dir=opendir(from))==NULL)
		show_errno();

	struct dirent *dp;
	char next_from[FNAME] = {'\0'};
	char next_to[FNAME] = {'\0'};
	struct stat s;
	for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
		sprintf(next_from,"%s/%s",from,dp->d_name);
		if(dp->d_type==DT_REG){
			copy_file_to_dir(next_from,to);
		}
		if(dp->d_type==DT_DIR){
			if(strncmp(dp->d_name,"..",2)==0 || strncmp(dp->d_name,".",1)==0)
				continue;

			sprintf(next_to,"%s/%s",to,dp->d_name);
			if(stat(next_from,&s) == 0){
				mkdir(next_to,s.st_mode);
			}else{
				show_errno();
			}
			copy_dir_to_dir(next_from,next_to);
		}
	}
}
Beispiel #2
0
void copy_to_dir(int argc, char* argv[], char* backup){
	int i;
	char from[FNAME] = {'\0'};
	struct stat s;
	for(i=1;i<argc-1;i++){
		sprintf(from,"%s",argv[i]);
		if( stat(argv[i],&s) == 0 ){
			if( S_ISREG(s.st_mode) )
				copy_file_to_dir(argv[i],backup);

			if( S_ISDIR(s.st_mode) )
				copy_dir_to_dir(argv[i],backup);
		}else{
			show_errno();
		}
	}
}
Beispiel #3
0
/**
 * copy src to dst (recursively)
 * @param src and dst are file or dir
 * @return -1 if failed
 */
int copy_file(const char *src, const char *dst)
{
    struct stat st;

    if (stat(dst, &st) == 0 && S_ISDIR(st.st_mode)) {
	if (stat(src, &st) < 0)
	    return -1;
	
    	if (S_ISDIR(st.st_mode))
	    return copy_dir_to_dir(src, dst);
	else if (S_ISREG(st.st_mode))
	    return copy_file_to_dir(src, dst);
	return -1;
    }
    else if (stat(src, &st) == 0 && S_ISDIR(st.st_mode))
	return copy_dir_to_dir(src, dst);
    return copy_file_to_file(src, dst);
}