- 帖子
- 6
- 积分
- 10
- 威望
- 14
- 金钱
- 10
- 在线时间
- 0 小时
|
文件备份c程序
//前两天购买刻录机供备份之用,于是编了一个程序以找出某一日
//期之后的文件,并复制到指定位置及建立相应的目录结构。
//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;
rot=root;
n=scandir(root,&list,select1,0);
if(n>0 && list[n-1]->d_type!=DT_DIR) {
fprintf(out,"%s\n",root);
name2=mkstring(des,"/",root+1);
makedir(name2);
free(name2);
}
if(n<=0) {printf("%s is %d.\n",root,n);perror("scandir");}
n–;
while(n>=0){
name=mkstring(root,"/",list[n]->d_name);
stat(name,&buf);
if(list[n]->d_type==DT_DIR) {
find(name,des);
free(name);
}else{
fprintf(out," %s\n",name);
name3=mkstring(des,"/",name+1);
copy(name,name3);
free(name);
free(name3);
}
free(list[n]);
n–;
}
}
//change the string refer to time.
//xxxxxxxx is 20040102
//to time_t
time_t gettime(char *a)
{
struct tm t;
int i;
for(i=0;i<=7;i++) a-=48;
t.tm_year=a[0]*1000+a[1]*100+a[2]*10+a[3]-1900;
t.tm_mon=a[4]*10+a[5]-1;
t.tm_mday=a[6]*10+a[7];
t.tm_sec=t.tm_hour=t.tm_min=t.tm_isdst=0;
return mktime(&t);
}
int main(int a,char **li)
{
out=fopen("result","w");
com=gettime(li[2]);
find(li[1],li[3]);
fclose(out);
return 0; |
|