[color=blue:058a6f686c]
在UNIX系统上,我们有时会遇到这种情况:在某台UNIX系统机上安装好一个几百M的应用系统(如一个数据库系统)后,若在一个局域网内安装另一台机器,只需使用RCP把相应目录及文件拷贝即可,而毋需再安装一次;但当另一台机器不在一个局域网内,只是一个低速的广域网,怎么利用已安装好的机器来快速安装呢?显然使用RCP已不现实,这时我们可以把已安装好的UNIX系统上的相应目录合并成一个文件,然后拷贝到另一硬盘上;携带该硬盘至需待装机处,把合并后的文件拷贝到待装机上,再把文件还原成目录,这样便完成了安装。有兴趣的可修改该程序,编写一个UNIX上的arj实用工具。
笔者在维护下属单位的系统中常使用这种方法,速度快,方便。
该程序不仅适应于安装,而且也适应于用DOS系统备份UNIX系统上的应用。
附源程序:(已调试通过,并多次实现使用)
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<ftw.h>
#define AR″ ″
FILE farj;intlist();
void arx();
main(argc,argv)
int argc;char argv[]
{charfname1[100],opt[1];
if(argc<4){printf(″\nusage:myar a|x arj—filepathdestpath\n″);exit(1);}
strcpy(opt,argv[1]);
if(opt[0]==″ a ″){strcpy(fname1,argv[2]);
if((farj=fopen(fname1,″ wb ″))==NULL)
{printf(″\nerrorin open file%s\n″,fname1);exit(1);}
else ftw(argv[3],list,1);}
else if(opt[0]==″ x ″arx(argv[2],argv[3]);
else{printf(″\nusage:myar a|x arj-filepathdestpath\n″);exit(1);}
fclose(farj);}
intlist(name,status,type)
char name;struct stat status;inttype;
{int size,left,bb=80;char buf[80],buf1[80];FILE ft;
charfname[100];if(type==FTW-NS)return(0);
if(type==FTW-F){
sprintf(buf,″%s%30%d%d\n″,name,status->st—mode&0777,status->st—reserved1,status->st-reserved2,status->st-size);
/若UNIX是SCO-UNIX可把″st-reserved1″改成″st-uid″把″st-reserved2″改成″st-gid″/
fwrite(buf,strlen(buf),1,farj);printf(″\nbeing add%s″,name);
strcpy(fname,name);ft=fopen(fname,″rb″);size=status->st-size;
if(ft==NULL){printf(“\nerrorin open%s",fname);
exit(1);}
while(fread(buf1,bb,1,ft)>0)fwrite(buf1,bb,1,farj);left=size%bb;
if(left>0){fread(buf1,left,1,ft);fwrite(buf1,left,1,farj);}
fclose(ft);}
else
fprintf(farj,″%s%30%d\n″,name,status->st—mode&0777,status->st—reserved1,status->st—reserved2);
return(0);
}
/ ==================== /
/ the follow function for extract /
void arx(filepath,destpath)
charfilepath[100],destpath[100];
{FILE ft;
int acc,owner,grp,size,s1=80,s2,i,len,p,sp,sl;
charfname[70],fnamet[70],fname[70],path[70],str[80],buf[80];
strcpy(fname1,filepath);
if((farj=fopen(fnam1,″rb″)=NULL){perror(″farj″;exit(1);}
while(!feof(farj)){
fgets(buf,s1,farj);
acc=0;owner=0;grp=0;size=0;
sscanf(buf,″%s%30%d%d%d″,fnamet,&acc,&owner,&grp,&size);
if(fnamet[0]==″ ″){len=strlen(fanmet);
for(i=1;i<len;i++)path[i-1]=fnamet[i];
path[len-1]=″\0″;
strcpy(str,″ mkdir ″);
strcat(str,destpath);
if(path[0]==″/″);
else strcat(str,″/″);
strcat(str,path);
printf(″\nbeing%s″,str);
system(str);
continue;}
else{printf(″\nbeing extracted from\n%s″,fnamet);
if(fnamet[0]==″/″)
sprintf(fname,″%s/%s″,destpath,fnamet);
else
sprintf(fname,″%s/%s,destpath,fnamet);
if((ft=fopen(fname,″ab″))==NULL){
perror(″\nopen ft=″);
printf(″%s\n″,fname);
continue;}
sp=0;
while(sp<size){
if(size-sp >=s1){fread(buf,s1,1,farj);fwrite(buf,s1,1,ft);
sp=sp+s1;}
else{fread(buf,size-sp,1,farj);
fwrite(buf,size-sp,1,ft);
sp=size;
break;}}
fclose(ft);
if(chmod(fname,acc)< 0) printf(″\nerrorin chmod:%s″,fname);
if(chown(fname,owner,grp)==-1)printf(″\nerror inchown:%s″,fname);
}
}
}
作者单位:王丽君(农业银行广西全州县支行)[/color:058a6f686c]
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/