#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>
char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];
void copy(char *spathname,char *tpathname)        //文件复制
{
int sfp,tfp;
  char buf[1024];
  int count=0;
  struct stat s;
  stat(spathname,&s);
  sfp=open(spathname,O_RDONLY,s.st_mode);
  while(1){
   if(sfp<0){printf("don‘t open the file1\n");exit(1);}
  count=read(sfp,buf,1023);
  if(count==0)break;
  buf[count]=‘\0‘;
  tfp=open(tpathname,O_CREAT|O_RDWR,s.st_mode);
  if(tfp<0){printf("don‘t open the file2\n");exit(1);}
  write(tfp,buf,strlen(buf));
}
close(sfp);
close(tfp);
}
void d_copy(char *spathname,char *tpathname)    //目录复制
{
	struct stat s,t,temp_s,temp_t;
	struct dirent *s_p;
	DIR *dirs,*dirt;
	stat(spathname,&s);
	mkdir(tpathname,s.st_mode);
	chown(tpathname,s.st_uid,s.st_gid);
	dirt=opendir(tpathname);
	dirs=opendir(spathname);
	strcpy(temp_paths,spathname);
	strcpy(temp_patht,tpathname);
	while((s_p=readdir(dirs))!=NULL)
	{
		if(strcmp(s_p->d_name,".")!=0&&strcmp(s_p->d_name,"..")!=0)
		{
			strcat(temp_paths,"/");
			strcat(temp_paths,s_p->d_name);
			strcat(temp_patht,"/");
			strcat(temp_patht,s_p->d_name);
			lstat(temp_paths,&s);
			temp_s.st_mode=s.st_mode;
			if(S_ISLNK(temp_s.st_mode))//判断是否为链接文件
			{
				printf("%s is a symbol link file\n",temp_paths);
			}
			else if(S_ISDIR(temp_s.st_mode))//判断是否为目录
			{
				printf("copy directory %s....\n",temp_paths);
				d_copy(temp_paths,temp_patht);
				strcpy(temp_paths,spathname);
				strcpy(temp_patht,tpathname);
			}
			else if(S_ISREG(temp_s.st_mode))//判断是否是文件
			{
				printf("copy file %s....\n",temp_paths);
				copy(temp_paths,temp_patht);
				strcpy(temp_paths,spathname);
				strcpy(temp_patht,tpathname);
                        }
		}
	}
}
int main(int argc,char *argv[])//传参主函数;
{
	struct dirent *sp,*tp;
	char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000];
	struct stat sbuf,tbuf,temp_sbuf,temp_tbuf;
	DIR *dir_s,*dir_t;
	dir_s=opendir(argv[1]);
	if(dir_s==NULL)
	{
		printf("目录不存在");
		return 0;
	}
	if(stat(argv[1],&sbuf)!=0)
	{
		printf("get satus error!\n");
		return 0;
	}
	dir_t=opendir(argv[2]);
	if(dir_t==NULL)
	{
		mkdir(argv[2],sbuf.st_mode);
		chown(argv[2],sbuf.st_uid,sbuf.st_gid);
	}
	strcpy(spath,argv[1]);
	strcpy(tpath,argv[2]);
	strcpy(temp_spath,argv[1]);
	strcpy(temp_tpath,argv[2]);
	printf("being copy...");
	while((sp=readdir(dir_s))!=NULL)
	{
		if(strcmp(sp->d_name,".")!=0&&strcmp(sp->d_name,"..")!=0)
		{
			strcat(temp_spath,"/");
			strcat(temp_spath,sp->d_name);
			strcat(temp_tpath,"/");
			strcat(temp_tpath,sp->d_name);
			lstat(temp_spath,&sbuf);
			temp_sbuf.st_mode=sbuf.st_mode;
			if(S_ISLNK(temp_sbuf.st_mode))
			{
				printf("%s is a symbolic link file\n",temp_spath);
			}
			else if ((S_IFMT&temp_sbuf.st_mode)==S_IFREG)//判断是否是文件;
			{
				printf("copy file %s.....\n",temp_spath);
				copy(temp_spath,temp_tpath);
				strcpy(temp_tpath,tpath);
				strcpy(temp_spath,spath);
			}
			else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR)//判断是否是目录;
			{
				printf("copy directory %s....\n",temp_spath);
				d_copy(temp_spath,temp_tpath);
				strcpy(temp_tpath,tpath);
				strcpy(temp_spath,spath);
			}
		}
	}
	printf("copy end!\n");//copy结束;
	closedir(dir_t);
	closedir(dir_s);	return 0;
}
原文:http://www.cnblogs.com/victorlingfeng/p/7368036.html