//This programme is writed by Vesuvius.
//It can be copy and motify under GNU General Public license.
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
//The result file for test output.
FILE *out;
typedef struct dirent Di;
//The time which to be compared.
time_t com;
//The tmp root dirent for each find function.
char *rot;
extern char *mkstring(char *a,char*b,char *c);
//We need function which can create dir and cp file properly.
void makedir(char *a)
{
int n,j;
char *b;
b=(char *)malloc(sizeof(char)*strlen(a));
for(n=0;n<=strlen(a);n++)
if(a[n]==’/'){
strncpy(b,a,n);b[n]=’\0′;mkdir(b,666);
//printf("The dir to be created is %s.\n",b);
}
mkdir(a,666);
//printf("The dir to be created is %s.\n",a);
free(b);
}
void copy(char *from,char *to)
{
char buffer[4096];
int in,out,n,m;
in=open(from,O_RDONLY,666);
if(in==-1) perror("open");
out=open(to,O_WRONLY|O_CREAT,666);
if(out==-1) perror("open");
while(1){
n=read(in,buffer,2048);
if(n==-1) {perror("read");break;}
if(n==0) break;
m=write(out,buffer,n);
if(m==-1) {perror("write");break;}
if(m<n) {printf("little:%s\n",from);break;}
}
n=close(in);m=close(out);
if(n==-1 || m==-1) printf("end\n");
printf("The file to be copyed is %s and %s .\n",from,to);
}
//The two functions below is for scandir()
//scandir() find dir and file which after com.
//then qsort them with dir first file below.
int select1(const Di *a)
{
struct stat buf;
char *fname;
if(strcmp(a->d_name,"..")!=0 && strcmp(a->d_name,".")!=0
&& strcmp(a->d_name,"System Volume Information")!=0) {
if (a->d_type==DT_DIR) return 1;
else{
fname=mkstring(rot,"/",a->d_name);
if(stat(fname,&buf)!=0) perror("stat");
free(fname);
if(buf.st_ctime>com) return 1;else return 0;
}
}
else return 0;
}
int compare(const Di **a,const Di **b)
{
if((*a)->d_type!=DT_DIR) return 1;
else return 0;
}
//This function link strings.
char *mkstring(char *a,char*b,char *c)
{
char *r;
r=(char *)malloc(sizeof(char)*(strlen(a)+strlen(b)+strlen(c)+1));
sprintf(r,"%s%s%s",a,b,c);
return r;
}
//The kernel function.
//find the file needed and write in result(like ls -R) or copy them.
void find(char *root,char *des)
{
Di **list;
int n;
char *name,*name2,*name3;
struct stat buf;